syncthing_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/db/backend"
  15. "github.com/syncthing/syncthing/lib/events"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. "github.com/syncthing/syncthing/lib/tlsutil"
  18. "github.com/syncthing/syncthing/lib/util"
  19. )
  20. func tempCfgFilename(t *testing.T) string {
  21. t.Helper()
  22. f, err := ioutil.TempFile("", "syncthing-testConfig-")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. defer f.Close()
  27. return f.Name()
  28. }
  29. func TestShortIDCheck(t *testing.T) {
  30. cfg := config.Wrap(tempCfgFilename(t), config.Configuration{
  31. Devices: []config.DeviceConfiguration{
  32. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 0, 0}},
  33. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 1, 1}}, // first 56 bits same, differ in the first 64 bits
  34. },
  35. }, protocol.LocalDeviceID, events.NoopLogger)
  36. defer os.Remove(cfg.ConfigPath())
  37. if err := checkShortIDs(cfg); err != nil {
  38. t.Error("Unexpected error:", err)
  39. }
  40. cfg = config.Wrap("/tmp/test", config.Configuration{
  41. Devices: []config.DeviceConfiguration{
  42. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 64, 0}},
  43. {DeviceID: protocol.DeviceID{8, 16, 24, 32, 40, 48, 56, 64, 1}}, // first 64 bits same
  44. },
  45. }, protocol.LocalDeviceID, events.NoopLogger)
  46. if err := checkShortIDs(cfg); err == nil {
  47. t.Error("Should have gotten an error")
  48. }
  49. }
  50. func TestStartupFail(t *testing.T) {
  51. tmpDir, err := ioutil.TempDir("", "syncthing-TestStartupFail-")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer os.RemoveAll(tmpDir)
  56. cert, err := tlsutil.NewCertificate(filepath.Join(tmpDir, "cert"), filepath.Join(tmpDir, "key"), "syncthing", 365)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. id := protocol.NewDeviceID(cert.Certificate[0])
  61. conflID := protocol.DeviceID{}
  62. copy(conflID[:8], id[:8])
  63. cfg := config.Wrap(tempCfgFilename(t), config.Configuration{
  64. Devices: []config.DeviceConfiguration{
  65. {DeviceID: id},
  66. {DeviceID: conflID},
  67. },
  68. }, protocol.LocalDeviceID, events.NoopLogger)
  69. defer os.Remove(cfg.ConfigPath())
  70. db := backend.OpenMemory()
  71. app, err := New(cfg, db, events.NoopLogger, cert, Options{})
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. startErr := app.Start()
  76. if startErr == nil {
  77. t.Fatal("Expected an error from Start, got nil")
  78. }
  79. done := make(chan struct{})
  80. var waitE util.ExitStatus
  81. go func() {
  82. waitE = app.Wait()
  83. close(done)
  84. }()
  85. select {
  86. case <-time.After(time.Second):
  87. t.Fatal("Wait did not return within 1s")
  88. case <-done:
  89. }
  90. if waitE != util.ExitError {
  91. t.Errorf("Got exit status %v, expected %v", waitE, util.ExitError)
  92. }
  93. if err = app.Error(); err != startErr {
  94. t.Errorf(`Got different errors "%v" from Start and "%v" from Error`, startErr, err)
  95. }
  96. if trans, err := db.NewReadTransaction(); err == nil {
  97. t.Error("Expected error due to db being closed, got nil")
  98. trans.Release()
  99. } else if !backend.IsClosed(err) {
  100. t.Error("Expected error due to db being closed, got", err)
  101. }
  102. }