syncthing_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package syncthing
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "testing"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/config"
  14. "github.com/syncthing/syncthing/lib/events"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/tlsutil"
  17. )
  18. func tempCfgFilename(t *testing.T) string {
  19. t.Helper()
  20. f, err := ioutil.TempFile("", "syncthing-testConfig-")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. defer f.Close()
  25. return f.Name()
  26. }
  27. func TestShortIDCheck(t *testing.T) {
  28. cfg := config.Wrap(tempCfgFilename(t), config.Configuration{
  29. Devices: []config.DeviceConfiguration{
  30. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 0, 0}},
  31. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 1, 1}}, // first 56 bits same, differ in the first 64 bits
  32. },
  33. }, events.NoopLogger)
  34. defer os.Remove(cfg.ConfigPath())
  35. if err := checkShortIDs(cfg); err != nil {
  36. t.Error("Unexpected error:", err)
  37. }
  38. cfg = config.Wrap("/tmp/test", config.Configuration{
  39. Devices: []config.DeviceConfiguration{
  40. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 64, 0}},
  41. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 64, 1}}, // first 64 bits same
  42. },
  43. }, events.NoopLogger)
  44. if err := checkShortIDs(cfg); err == nil {
  45. t.Error("Should have gotten an error")
  46. }
  47. }
  48. func TestStartupFail(t *testing.T) {
  49. tmpDir, err := ioutil.TempDir("", "syncthing-TestStartupFail-")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer os.RemoveAll(tmpDir)
  54. cert, err := tlsutil.NewCertificate(filepath.Join(tmpDir, "cert"), filepath.Join(tmpDir, "key"), "syncthing")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. id := protocol.NewDeviceID(cert.Certificate[0])
  59. conflID := protocol.DeviceID{}
  60. copy(conflID[:8], id[:8])
  61. cfg := config.Wrap(tempCfgFilename(t), config.Configuration{
  62. Devices: []config.DeviceConfiguration{
  63. {DeviceID: id},
  64. {DeviceID: conflID},
  65. },
  66. }, events.NoopLogger)
  67. defer os.Remove(cfg.ConfigPath())
  68. app := New(cfg, nil, events.NoopLogger, cert, Options{})
  69. startErr := app.Start()
  70. if startErr == nil {
  71. t.Fatal("Expected an error from Start, got nil")
  72. }
  73. done := make(chan struct{})
  74. var waitE ExitStatus
  75. go func() {
  76. waitE = app.Wait()
  77. close(done)
  78. }()
  79. select {
  80. case <-time.After(time.Second):
  81. t.Fatal("Wait did not return within 1s")
  82. case <-done:
  83. }
  84. if waitE != ExitError {
  85. t.Errorf("Got exit status %v, expected %v", waitE, ExitError)
  86. }
  87. if err = app.Error(); err != startErr {
  88. t.Errorf(`Got different errors "%v" from Start and "%v" from Error`, startErr, err)
  89. }
  90. }