config_test.go 20 KB

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