config_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 config
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "os"
  12. "path/filepath"
  13. "reflect"
  14. "runtime"
  15. "sort"
  16. "strings"
  17. "testing"
  18. "github.com/d4l3k/messagediff"
  19. "github.com/syncthing/syncthing/lib/fs"
  20. "github.com/syncthing/syncthing/lib/protocol"
  21. )
  22. var device1, device2, device3, device4 protocol.DeviceID
  23. func init() {
  24. device1, _ = protocol.DeviceIDFromString("AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ")
  25. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  26. device3, _ = protocol.DeviceIDFromString("LGFPDIT-7SKNNJL-VJZA4FC-7QNCRKA-CE753K7-2BW5QDK-2FOZ7FR-FEP57QJ")
  27. device4, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
  28. }
  29. func TestDefaultValues(t *testing.T) {
  30. expected := OptionsConfiguration{
  31. ListenAddresses: []string{"default"},
  32. GlobalAnnServers: []string{"default"},
  33. GlobalAnnEnabled: true,
  34. LocalAnnEnabled: true,
  35. LocalAnnPort: 21027,
  36. LocalAnnMCAddr: "[ff12::8384]:21027",
  37. MaxSendKbps: 0,
  38. MaxRecvKbps: 0,
  39. ReconnectIntervalS: 60,
  40. RelaysEnabled: true,
  41. RelayReconnectIntervalM: 10,
  42. StartBrowser: true,
  43. NATEnabled: true,
  44. NATLeaseM: 60,
  45. NATRenewalM: 30,
  46. NATTimeoutS: 10,
  47. RestartOnWakeup: true,
  48. AutoUpgradeIntervalH: 12,
  49. KeepTemporariesH: 24,
  50. CacheIgnoredFiles: false,
  51. ProgressUpdateIntervalS: 5,
  52. LimitBandwidthInLan: false,
  53. MinHomeDiskFree: Size{1, "%"},
  54. URURL: "https://data.syncthing.net/newdata",
  55. URInitialDelayS: 1800,
  56. URPostInsecurely: false,
  57. ReleasesURL: "https://upgrades.syncthing.net/meta.json",
  58. AlwaysLocalNets: []string{},
  59. OverwriteRemoteDevNames: false,
  60. TempIndexMinBlocks: 10,
  61. UnackedNotificationIDs: []string{},
  62. WeakHashSelectionMethod: WeakHashAuto,
  63. StunKeepaliveS: 24,
  64. StunServers: []string{"default"},
  65. DefaultKCPEnabled: false,
  66. KCPCongestionControl: true,
  67. KCPReceiveWindowSize: 128,
  68. KCPSendWindowSize: 128,
  69. KCPUpdateIntervalMs: 25,
  70. KCPFastResend: false,
  71. DefaultFolderPath: "~",
  72. }
  73. cfg := New(device1)
  74. if diff, equal := messagediff.PrettyDiff(expected, cfg.Options); !equal {
  75. t.Errorf("Default config differs. Diff:\n%s", diff)
  76. }
  77. }
  78. func TestDeviceConfig(t *testing.T) {
  79. for i := OldestHandledVersion; i <= CurrentVersion; i++ {
  80. os.RemoveAll("testdata/.stfolder")
  81. wr, err := Load(fmt.Sprintf("testdata/v%d.xml", i), device1)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. _, err = os.Stat("testdata/.stfolder")
  86. if i < 6 && err != nil {
  87. t.Fatal(err)
  88. } else if i >= 6 && err == nil {
  89. t.Fatal("Unexpected file")
  90. }
  91. cfg := wr.cfg
  92. expectedFolders := []FolderConfiguration{
  93. {
  94. ID: "test",
  95. FilesystemType: fs.FilesystemTypeBasic,
  96. Path: "testdata",
  97. Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
  98. Type: FolderTypeSendOnly,
  99. RescanIntervalS: 600,
  100. Copiers: 0,
  101. Pullers: 0,
  102. Hashers: 0,
  103. AutoNormalize: true,
  104. MinDiskFree: Size{1, "%"},
  105. MaxConflicts: -1,
  106. Versioning: VersioningConfiguration{
  107. Params: map[string]string{},
  108. },
  109. WeakHashThresholdPct: 25,
  110. },
  111. }
  112. // The cachedFilesystem will have been resolved to an absolute path,
  113. // depending on where the tests are running. Zero it out so we don't
  114. // fail based on that.
  115. for i := range cfg.Folders {
  116. cfg.Folders[i].cachedFilesystem = nil
  117. }
  118. expectedDevices := []DeviceConfiguration{
  119. {
  120. DeviceID: device1,
  121. Name: "node one",
  122. Addresses: []string{"tcp://a"},
  123. Compression: protocol.CompressMetadata,
  124. AllowedNetworks: []string{},
  125. },
  126. {
  127. DeviceID: device4,
  128. Name: "node two",
  129. Addresses: []string{"tcp://b"},
  130. Compression: protocol.CompressMetadata,
  131. AllowedNetworks: []string{},
  132. },
  133. }
  134. expectedDeviceIDs := []protocol.DeviceID{device1, device4}
  135. if cfg.Version != CurrentVersion {
  136. t.Errorf("%d: Incorrect version %d != %d", i, cfg.Version, CurrentVersion)
  137. }
  138. if diff, equal := messagediff.PrettyDiff(expectedFolders, cfg.Folders); !equal {
  139. t.Errorf("%d: Incorrect Folders. Diff:\n%s", i, diff)
  140. }
  141. if diff, equal := messagediff.PrettyDiff(expectedDevices, cfg.Devices); !equal {
  142. t.Errorf("%d: Incorrect Devices. Diff:\n%s", i, diff)
  143. }
  144. if diff, equal := messagediff.PrettyDiff(expectedDeviceIDs, cfg.Folders[0].DeviceIDs()); !equal {
  145. t.Errorf("%d: Incorrect DeviceIDs. Diff:\n%s", i, diff)
  146. }
  147. }
  148. }
  149. func TestNoListenAddresses(t *testing.T) {
  150. cfg, err := Load("testdata/nolistenaddress.xml", device1)
  151. if err != nil {
  152. t.Error(err)
  153. }
  154. expected := []string{""}
  155. actual := cfg.Options().ListenAddresses
  156. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  157. t.Errorf("Unexpected ListenAddresses. Diff:\n%s", diff)
  158. }
  159. }
  160. func TestOverriddenValues(t *testing.T) {
  161. expected := OptionsConfiguration{
  162. ListenAddresses: []string{"tcp://:23000"},
  163. GlobalAnnServers: []string{"udp4://syncthing.nym.se:22026"},
  164. GlobalAnnEnabled: false,
  165. LocalAnnEnabled: false,
  166. LocalAnnPort: 42123,
  167. LocalAnnMCAddr: "quux:3232",
  168. MaxSendKbps: 1234,
  169. MaxRecvKbps: 2341,
  170. ReconnectIntervalS: 6000,
  171. RelaysEnabled: false,
  172. RelayReconnectIntervalM: 20,
  173. StartBrowser: false,
  174. NATEnabled: false,
  175. NATLeaseM: 90,
  176. NATRenewalM: 15,
  177. NATTimeoutS: 15,
  178. RestartOnWakeup: false,
  179. AutoUpgradeIntervalH: 24,
  180. KeepTemporariesH: 48,
  181. CacheIgnoredFiles: true,
  182. ProgressUpdateIntervalS: 10,
  183. LimitBandwidthInLan: true,
  184. MinHomeDiskFree: Size{5.2, "%"},
  185. URSeen: 2,
  186. URURL: "https://localhost/newdata",
  187. URInitialDelayS: 800,
  188. URPostInsecurely: true,
  189. ReleasesURL: "https://localhost/releases",
  190. AlwaysLocalNets: []string{},
  191. OverwriteRemoteDevNames: true,
  192. TempIndexMinBlocks: 100,
  193. UnackedNotificationIDs: []string{
  194. "channelNotification", // added in 17->18 migration
  195. },
  196. WeakHashSelectionMethod: WeakHashNever,
  197. StunKeepaliveS: 10,
  198. StunServers: []string{"a.stun.com", "b.stun.com"},
  199. DefaultKCPEnabled: true,
  200. KCPCongestionControl: false,
  201. KCPReceiveWindowSize: 1280,
  202. KCPSendWindowSize: 1280,
  203. KCPUpdateIntervalMs: 1000,
  204. KCPFastResend: true,
  205. DefaultFolderPath: "/media/syncthing",
  206. }
  207. os.Unsetenv("STNOUPGRADE")
  208. cfg, err := Load("testdata/overridenvalues.xml", device1)
  209. if err != nil {
  210. t.Error(err)
  211. }
  212. if diff, equal := messagediff.PrettyDiff(expected, cfg.Options()); !equal {
  213. t.Errorf("Overridden config differs. Diff:\n%s", diff)
  214. }
  215. }
  216. func TestDeviceAddressesDynamic(t *testing.T) {
  217. name, _ := os.Hostname()
  218. expected := map[protocol.DeviceID]DeviceConfiguration{
  219. device1: {
  220. DeviceID: device1,
  221. Addresses: []string{"dynamic"},
  222. AllowedNetworks: []string{},
  223. },
  224. device2: {
  225. DeviceID: device2,
  226. Addresses: []string{"dynamic"},
  227. AllowedNetworks: []string{},
  228. },
  229. device3: {
  230. DeviceID: device3,
  231. Addresses: []string{"dynamic"},
  232. AllowedNetworks: []string{},
  233. },
  234. device4: {
  235. DeviceID: device4,
  236. Name: name, // Set when auto created
  237. Addresses: []string{"dynamic"},
  238. Compression: protocol.CompressMetadata,
  239. AllowedNetworks: []string{},
  240. },
  241. }
  242. cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
  243. if err != nil {
  244. t.Error(err)
  245. }
  246. actual := cfg.Devices()
  247. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  248. t.Errorf("Devices differ. Diff:\n%s", diff)
  249. }
  250. }
  251. func TestDeviceCompression(t *testing.T) {
  252. name, _ := os.Hostname()
  253. expected := map[protocol.DeviceID]DeviceConfiguration{
  254. device1: {
  255. DeviceID: device1,
  256. Addresses: []string{"dynamic"},
  257. Compression: protocol.CompressMetadata,
  258. AllowedNetworks: []string{},
  259. },
  260. device2: {
  261. DeviceID: device2,
  262. Addresses: []string{"dynamic"},
  263. Compression: protocol.CompressMetadata,
  264. AllowedNetworks: []string{},
  265. },
  266. device3: {
  267. DeviceID: device3,
  268. Addresses: []string{"dynamic"},
  269. Compression: protocol.CompressNever,
  270. AllowedNetworks: []string{},
  271. },
  272. device4: {
  273. DeviceID: device4,
  274. Name: name, // Set when auto created
  275. Addresses: []string{"dynamic"},
  276. Compression: protocol.CompressMetadata,
  277. AllowedNetworks: []string{},
  278. },
  279. }
  280. cfg, err := Load("testdata/devicecompression.xml", device4)
  281. if err != nil {
  282. t.Error(err)
  283. }
  284. actual := cfg.Devices()
  285. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  286. t.Errorf("Devices differ. Diff:\n%s", diff)
  287. }
  288. }
  289. func TestDeviceAddressesStatic(t *testing.T) {
  290. name, _ := os.Hostname()
  291. expected := map[protocol.DeviceID]DeviceConfiguration{
  292. device1: {
  293. DeviceID: device1,
  294. Addresses: []string{"tcp://192.0.2.1", "tcp://192.0.2.2"},
  295. AllowedNetworks: []string{},
  296. },
  297. device2: {
  298. DeviceID: device2,
  299. Addresses: []string{"tcp://192.0.2.3:6070", "tcp://[2001:db8::42]:4242"},
  300. AllowedNetworks: []string{},
  301. },
  302. device3: {
  303. DeviceID: device3,
  304. Addresses: []string{"tcp://[2001:db8::44]:4444", "tcp://192.0.2.4:6090"},
  305. AllowedNetworks: []string{},
  306. },
  307. device4: {
  308. DeviceID: device4,
  309. Name: name, // Set when auto created
  310. Addresses: []string{"dynamic"},
  311. Compression: protocol.CompressMetadata,
  312. AllowedNetworks: []string{},
  313. },
  314. }
  315. cfg, err := Load("testdata/deviceaddressesstatic.xml", device4)
  316. if err != nil {
  317. t.Error(err)
  318. }
  319. actual := cfg.Devices()
  320. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  321. t.Errorf("Devices differ. Diff:\n%s", diff)
  322. }
  323. }
  324. func TestVersioningConfig(t *testing.T) {
  325. cfg, err := Load("testdata/versioningconfig.xml", device4)
  326. if err != nil {
  327. t.Error(err)
  328. }
  329. vc := cfg.Folders()["test"].Versioning
  330. if vc.Type != "simple" {
  331. t.Errorf(`vc.Type %q != "simple"`, vc.Type)
  332. }
  333. if l := len(vc.Params); l != 2 {
  334. t.Errorf("len(vc.Params) %d != 2", l)
  335. }
  336. expected := map[string]string{
  337. "foo": "bar",
  338. "baz": "quux",
  339. }
  340. if diff, equal := messagediff.PrettyDiff(expected, vc.Params); !equal {
  341. t.Errorf("vc.Params differ. Diff:\n%s", diff)
  342. }
  343. }
  344. func TestIssue1262(t *testing.T) {
  345. if runtime.GOOS != "windows" {
  346. t.Skipf("path gets converted to absolute as part of the filesystem initialization on linux")
  347. }
  348. cfg, err := Load("testdata/issue-1262.xml", device4)
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. actual := cfg.Folders()["test"].Filesystem().URI()
  353. expected := `e:\`
  354. if actual != expected {
  355. t.Errorf("%q != %q", actual, expected)
  356. }
  357. }
  358. func TestIssue1750(t *testing.T) {
  359. cfg, err := Load("testdata/issue-1750.xml", device4)
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. if cfg.Options().ListenAddresses[0] != "tcp://:23000" {
  364. t.Errorf("%q != %q", cfg.Options().ListenAddresses[0], "tcp://:23000")
  365. }
  366. if cfg.Options().ListenAddresses[1] != "tcp://:23001" {
  367. t.Errorf("%q != %q", cfg.Options().ListenAddresses[1], "tcp://:23001")
  368. }
  369. if cfg.Options().GlobalAnnServers[0] != "udp4://syncthing.nym.se:22026" {
  370. t.Errorf("%q != %q", cfg.Options().GlobalAnnServers[0], "udp4://syncthing.nym.se:22026")
  371. }
  372. if cfg.Options().GlobalAnnServers[1] != "udp4://syncthing.nym.se:22027" {
  373. t.Errorf("%q != %q", cfg.Options().GlobalAnnServers[1], "udp4://syncthing.nym.se:22027")
  374. }
  375. }
  376. func TestFolderPath(t *testing.T) {
  377. folder := FolderConfiguration{
  378. Path: "~/tmp",
  379. }
  380. realPath := folder.Filesystem().URI()
  381. if !filepath.IsAbs(realPath) {
  382. t.Error(realPath, "should be absolute")
  383. }
  384. if strings.Contains(realPath, "~") {
  385. t.Error(realPath, "should not contain ~")
  386. }
  387. }
  388. func TestNewSaveLoad(t *testing.T) {
  389. path := "testdata/temp.xml"
  390. os.Remove(path)
  391. exists := func(path string) bool {
  392. _, err := os.Stat(path)
  393. return err == nil
  394. }
  395. intCfg := New(device1)
  396. cfg := Wrap(path, intCfg)
  397. // To make the equality pass later
  398. cfg.cfg.XMLName.Local = "configuration"
  399. if exists(path) {
  400. t.Error(path, "exists")
  401. }
  402. err := cfg.Save()
  403. if err != nil {
  404. t.Error(err)
  405. }
  406. if !exists(path) {
  407. t.Error(path, "does not exist")
  408. }
  409. cfg2, err := Load(path, device1)
  410. if err != nil {
  411. t.Error(err)
  412. }
  413. if diff, equal := messagediff.PrettyDiff(cfg.RawCopy(), cfg2.RawCopy()); !equal {
  414. t.Errorf("Configs are not equal. Diff:\n%s", diff)
  415. }
  416. os.Remove(path)
  417. }
  418. func TestPrepare(t *testing.T) {
  419. var cfg Configuration
  420. if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddresses != nil {
  421. t.Error("Expected nil")
  422. }
  423. cfg.prepare(device1)
  424. if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddresses == nil {
  425. t.Error("Unexpected nil")
  426. }
  427. }
  428. func TestCopy(t *testing.T) {
  429. wrapper, err := Load("testdata/example.xml", device1)
  430. if err != nil {
  431. t.Fatal(err)
  432. }
  433. cfg := wrapper.RawCopy()
  434. bsOrig, err := json.MarshalIndent(cfg, "", " ")
  435. if err != nil {
  436. t.Fatal(err)
  437. }
  438. copy := cfg.Copy()
  439. cfg.Devices[0].Addresses[0] = "wrong"
  440. cfg.Folders[0].Devices[0].DeviceID = protocol.DeviceID{0, 1, 2, 3}
  441. cfg.Options.ListenAddresses[0] = "wrong"
  442. cfg.GUI.APIKey = "wrong"
  443. bsChanged, err := json.MarshalIndent(cfg, "", " ")
  444. if err != nil {
  445. t.Fatal(err)
  446. }
  447. bsCopy, err := json.MarshalIndent(copy, "", " ")
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. if bytes.Equal(bsOrig, bsChanged) {
  452. t.Error("Config should have changed")
  453. }
  454. if !bytes.Equal(bsOrig, bsCopy) {
  455. //ioutil.WriteFile("a", bsOrig, 0644)
  456. //ioutil.WriteFile("b", bsCopy, 0644)
  457. t.Error("Copy should be unchanged")
  458. }
  459. }
  460. func TestPullOrder(t *testing.T) {
  461. wrapper, err := Load("testdata/pullorder.xml", device1)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. folders := wrapper.Folders()
  466. expected := []struct {
  467. name string
  468. order PullOrder
  469. }{
  470. {"f1", OrderRandom}, // empty value, default
  471. {"f2", OrderRandom}, // explicit
  472. {"f3", OrderAlphabetic}, // explicit
  473. {"f4", OrderRandom}, // unknown value, default
  474. {"f5", OrderSmallestFirst}, // explicit
  475. {"f6", OrderLargestFirst}, // explicit
  476. {"f7", OrderOldestFirst}, // explicit
  477. {"f8", OrderNewestFirst}, // explicit
  478. }
  479. // Verify values are deserialized correctly
  480. for _, tc := range expected {
  481. if actual := folders[tc.name].Order; actual != tc.order {
  482. t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
  483. }
  484. }
  485. // Serialize and deserialize again to verify it survives the transformation
  486. buf := new(bytes.Buffer)
  487. cfg := wrapper.RawCopy()
  488. cfg.WriteXML(buf)
  489. t.Logf("%s", buf.Bytes())
  490. cfg, err = ReadXML(buf, device1)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. wrapper = Wrap("testdata/pullorder.xml", cfg)
  495. folders = wrapper.Folders()
  496. for _, tc := range expected {
  497. if actual := folders[tc.name].Order; actual != tc.order {
  498. t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
  499. }
  500. }
  501. }
  502. func TestLargeRescanInterval(t *testing.T) {
  503. wrapper, err := Load("testdata/largeinterval.xml", device1)
  504. if err != nil {
  505. t.Fatal(err)
  506. }
  507. if wrapper.Folders()["l1"].RescanIntervalS != MaxRescanIntervalS {
  508. t.Error("too large rescan interval should be maxed out")
  509. }
  510. if wrapper.Folders()["l2"].RescanIntervalS != 0 {
  511. t.Error("negative rescan interval should become zero")
  512. }
  513. }
  514. func TestGUIConfigURL(t *testing.T) {
  515. testcases := [][2]string{
  516. {"192.0.2.42:8080", "http://192.0.2.42:8080/"},
  517. {":8080", "http://127.0.0.1:8080/"},
  518. {"0.0.0.0:8080", "http://127.0.0.1:8080/"},
  519. {"127.0.0.1:8080", "http://127.0.0.1:8080/"},
  520. {"127.0.0.2:8080", "http://127.0.0.2:8080/"},
  521. {"[::]:8080", "http://[::1]:8080/"},
  522. {"[2001::42]:8080", "http://[2001::42]:8080/"},
  523. }
  524. for _, tc := range testcases {
  525. c := GUIConfiguration{
  526. RawAddress: tc[0],
  527. }
  528. u := c.URL()
  529. if u != tc[1] {
  530. t.Errorf("Incorrect URL %s != %s for addr %s", u, tc[1], tc[0])
  531. }
  532. }
  533. }
  534. func TestDuplicateDevices(t *testing.T) {
  535. // Duplicate devices should be removed
  536. wrapper, err := Load("testdata/dupdevices.xml", device1)
  537. if err != nil {
  538. t.Fatal(err)
  539. }
  540. if l := len(wrapper.RawCopy().Devices); l != 3 {
  541. t.Errorf("Incorrect number of devices, %d != 3", l)
  542. }
  543. f := wrapper.Folders()["f2"]
  544. if l := len(f.Devices); l != 2 {
  545. t.Errorf("Incorrect number of folder devices, %d != 2", l)
  546. }
  547. }
  548. func TestDuplicateFolders(t *testing.T) {
  549. // Duplicate folders are a loading error
  550. _, err := Load("testdata/dupfolders.xml", device1)
  551. if err == nil || !strings.HasPrefix(err.Error(), "duplicate folder ID") {
  552. t.Fatal(`Expected error to mention "duplicate folder ID":`, err)
  553. }
  554. }
  555. func TestEmptyFolderPaths(t *testing.T) {
  556. // Empty folder paths are allowed at the loading stage, and should not
  557. // get messed up by the prepare steps (e.g., become the current dir or
  558. // get a slash added so that it becomes the root directory or similar).
  559. wrapper, err := Load("testdata/nopath.xml", device1)
  560. if err != nil {
  561. t.Fatal(err)
  562. }
  563. folder := wrapper.Folders()["f1"]
  564. if folder.cachedFilesystem != nil {
  565. t.Errorf("Expected %q to be empty", folder.cachedFilesystem)
  566. }
  567. }
  568. func TestV14ListenAddressesMigration(t *testing.T) {
  569. tcs := [][3][]string{
  570. // Default listen plus default relays is now "default"
  571. {
  572. {"tcp://0.0.0.0:22000"},
  573. {"dynamic+https://relays.syncthing.net/endpoint"},
  574. {"default"},
  575. },
  576. // Default listen address without any relay addresses gets converted
  577. // to just the listen address. It's easier this way, and frankly the
  578. // user has gone to some trouble to get the empty string in the
  579. // config to start with...
  580. {
  581. {"tcp://0.0.0.0:22000"}, // old listen addrs
  582. {""}, // old relay addrs
  583. {"tcp://0.0.0.0:22000"}, // new listen addrs
  584. },
  585. // Default listen plus non-default relays gets copied verbatim
  586. {
  587. {"tcp://0.0.0.0:22000"},
  588. {"dynamic+https://other.example.com"},
  589. {"tcp://0.0.0.0:22000", "dynamic+https://other.example.com"},
  590. },
  591. // Non-default listen plus default relays gets copied verbatim
  592. {
  593. {"tcp://1.2.3.4:22000"},
  594. {"dynamic+https://relays.syncthing.net/endpoint"},
  595. {"tcp://1.2.3.4:22000", "dynamic+https://relays.syncthing.net/endpoint"},
  596. },
  597. // Default stuff gets sucked into "default", the rest gets copied
  598. {
  599. {"tcp://0.0.0.0:22000", "tcp://1.2.3.4:22000"},
  600. {"dynamic+https://relays.syncthing.net/endpoint", "relay://other.example.com"},
  601. {"default", "tcp://1.2.3.4:22000", "relay://other.example.com"},
  602. },
  603. }
  604. for _, tc := range tcs {
  605. cfg := Configuration{
  606. Version: 13,
  607. Options: OptionsConfiguration{
  608. ListenAddresses: tc[0],
  609. DeprecatedRelayServers: tc[1],
  610. },
  611. }
  612. convertV13V14(&cfg)
  613. if cfg.Version != 14 {
  614. t.Error("Configuration was not converted")
  615. }
  616. sort.Strings(tc[2])
  617. if !reflect.DeepEqual(cfg.Options.ListenAddresses, tc[2]) {
  618. t.Errorf("Migration error; actual %#v != expected %#v", cfg.Options.ListenAddresses, tc[2])
  619. }
  620. }
  621. }
  622. func TestIgnoredDevices(t *testing.T) {
  623. // Verify that ignored devices that are also present in the
  624. // configuration are not in fact ignored.
  625. wrapper, err := Load("testdata/ignoreddevices.xml", device1)
  626. if err != nil {
  627. t.Fatal(err)
  628. }
  629. if wrapper.IgnoredDevice(device1) {
  630. t.Errorf("Device %v should not be ignored", device1)
  631. }
  632. if !wrapper.IgnoredDevice(device3) {
  633. t.Errorf("Device %v should be ignored", device3)
  634. }
  635. }
  636. func TestGetDevice(t *testing.T) {
  637. // Verify that the Device() call does the right thing
  638. wrapper, err := Load("testdata/ignoreddevices.xml", device1)
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. // device1 is mentioned in the config
  643. device, ok := wrapper.Device(device1)
  644. if !ok {
  645. t.Error(device1, "should exist")
  646. }
  647. if device.DeviceID != device1 {
  648. t.Error("Should have returned", device1, "not", device.DeviceID)
  649. }
  650. // device3 is not
  651. device, ok = wrapper.Device(device3)
  652. if ok {
  653. t.Error(device3, "should not exist")
  654. }
  655. if device.DeviceID == device3 {
  656. t.Error("Should not returned ID", device3)
  657. }
  658. }
  659. func TestSharesRemovedOnDeviceRemoval(t *testing.T) {
  660. wrapper, err := Load("testdata/example.xml", device1)
  661. if err != nil {
  662. t.Errorf("Failed: %s", err)
  663. }
  664. raw := wrapper.RawCopy()
  665. raw.Devices = raw.Devices[:len(raw.Devices)-1]
  666. if len(raw.Folders[0].Devices) <= len(raw.Devices) {
  667. t.Error("Should have less devices")
  668. }
  669. err = wrapper.Replace(raw)
  670. if err != nil {
  671. t.Errorf("Failed: %s", err)
  672. }
  673. raw = wrapper.RawCopy()
  674. if len(raw.Folders[0].Devices) > len(raw.Devices) {
  675. t.Error("Unexpected extra device")
  676. }
  677. }
  678. func TestIssue4219(t *testing.T) {
  679. // Adding a folder that was previously ignored should make it unignored.
  680. r := bytes.NewReader([]byte(`{
  681. "folders": [
  682. {"id": "abcd123"}
  683. ],
  684. "ignoredFolders": ["t1", "abcd123", "t2"]
  685. }`))
  686. cfg, err := ReadJSON(r, protocol.LocalDeviceID)
  687. if err != nil {
  688. t.Fatal(err)
  689. }
  690. if len(cfg.IgnoredFolders) != 2 {
  691. t.Errorf("There should be two ignored folders, not %d", len(cfg.IgnoredFolders))
  692. }
  693. w := Wrap("/tmp/cfg", cfg)
  694. if !w.IgnoredFolder("t1") {
  695. t.Error("Folder t1 should be ignored")
  696. }
  697. if !w.IgnoredFolder("t2") {
  698. t.Error("Folder t2 should be ignored")
  699. }
  700. if w.IgnoredFolder("abcd123") {
  701. t.Error("Folder abcd123 should not be ignored")
  702. }
  703. }