config_test.go 20 KB

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