config_test.go 20 KB

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