config_test.go 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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. "context"
  10. "encoding/json"
  11. "encoding/xml"
  12. "fmt"
  13. "io"
  14. "os"
  15. "path/filepath"
  16. "reflect"
  17. "runtime"
  18. "sort"
  19. "strings"
  20. "testing"
  21. "github.com/d4l3k/messagediff"
  22. "golang.org/x/crypto/bcrypt"
  23. "github.com/syncthing/syncthing/lib/build"
  24. "github.com/syncthing/syncthing/lib/events"
  25. "github.com/syncthing/syncthing/lib/fs"
  26. "github.com/syncthing/syncthing/lib/protocol"
  27. "github.com/syncthing/syncthing/lib/rand"
  28. )
  29. var device1, device2, device3, device4 protocol.DeviceID
  30. var testFs fs.Filesystem
  31. func init() {
  32. device1, _ = protocol.DeviceIDFromString("AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ")
  33. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  34. device3, _ = protocol.DeviceIDFromString("LGFPDIT-7SKNNJL-VJZA4FC-7QNCRKA-CE753K7-2BW5QDK-2FOZ7FR-FEP57QJ")
  35. device4, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
  36. testFs = fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
  37. loadTestFiles()
  38. }
  39. func TestDefaultValues(t *testing.T) {
  40. size, err := ParseSize("1%")
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. expected := Configuration{
  45. Version: CurrentVersion,
  46. Folders: []FolderConfiguration{},
  47. Options: OptionsConfiguration{
  48. RawListenAddresses: []string{"default"},
  49. RawGlobalAnnServers: []string{"default"},
  50. GlobalAnnEnabled: true,
  51. LocalAnnEnabled: true,
  52. LocalAnnPort: 21027,
  53. LocalAnnMCAddr: "[ff12::8384]:21027",
  54. MaxSendKbps: 0,
  55. MaxRecvKbps: 0,
  56. ReconnectIntervalS: 60,
  57. RelaysEnabled: true,
  58. RelayReconnectIntervalM: 10,
  59. StartBrowser: true,
  60. NATEnabled: true,
  61. NATLeaseM: 60,
  62. NATRenewalM: 30,
  63. NATTimeoutS: 10,
  64. AutoUpgradeIntervalH: 12,
  65. KeepTemporariesH: 24,
  66. CacheIgnoredFiles: false,
  67. ProgressUpdateIntervalS: 5,
  68. LimitBandwidthInLan: false,
  69. MinHomeDiskFree: Size{1, "%"},
  70. URURL: "https://data.syncthing.net/newdata",
  71. URInitialDelayS: 1800,
  72. URPostInsecurely: false,
  73. ReleasesURL: "https://upgrades.syncthing.net/meta.json",
  74. AlwaysLocalNets: []string{},
  75. OverwriteRemoteDevNames: false,
  76. TempIndexMinBlocks: 10,
  77. UnackedNotificationIDs: []string{"authenticationUserAndPassword"},
  78. SetLowPriority: true,
  79. CRURL: "https://crash.syncthing.net/newcrash",
  80. CREnabled: true,
  81. StunKeepaliveStartS: 180,
  82. StunKeepaliveMinS: 20,
  83. RawStunServers: []string{"default"},
  84. AnnounceLANAddresses: true,
  85. FeatureFlags: []string{},
  86. ConnectionPriorityTCPLAN: 10,
  87. ConnectionPriorityQUICLAN: 20,
  88. ConnectionPriorityTCPWAN: 30,
  89. ConnectionPriorityQUICWAN: 40,
  90. ConnectionPriorityRelay: 50,
  91. },
  92. Defaults: Defaults{
  93. Folder: FolderConfiguration{
  94. FilesystemType: fs.FilesystemTypeBasic,
  95. Path: "~",
  96. Type: FolderTypeSendReceive,
  97. Devices: []FolderDeviceConfiguration{{DeviceID: device1}},
  98. RescanIntervalS: 3600,
  99. FSWatcherEnabled: true,
  100. FSWatcherDelayS: 10,
  101. IgnorePerms: false,
  102. AutoNormalize: true,
  103. MinDiskFree: size,
  104. Versioning: VersioningConfiguration{
  105. CleanupIntervalS: 3600,
  106. Params: map[string]string{},
  107. },
  108. MaxConflicts: 10,
  109. WeakHashThresholdPct: 25,
  110. MarkerName: ".stfolder",
  111. MaxConcurrentWrites: 2,
  112. XattrFilter: XattrFilter{
  113. Entries: []XattrFilterEntry{},
  114. MaxSingleEntrySize: 1024,
  115. MaxTotalSize: 4096,
  116. },
  117. },
  118. Device: DeviceConfiguration{
  119. Addresses: []string{"dynamic"},
  120. AllowedNetworks: []string{},
  121. Compression: protocol.CompressionMetadata,
  122. IgnoredFolders: []ObservedFolder{},
  123. },
  124. Ignores: Ignores{
  125. Lines: []string{},
  126. },
  127. },
  128. IgnoredDevices: []ObservedDevice{},
  129. }
  130. expected.Devices = []DeviceConfiguration{expected.Defaults.Device.Copy()}
  131. expected.Devices[0].DeviceID = device1
  132. expected.Devices[0].Name, _ = os.Hostname()
  133. cfg := New(device1)
  134. cfg.GUI = GUIConfiguration{}
  135. cfg.LDAP = LDAPConfiguration{}
  136. if diff, equal := messagediff.PrettyDiff(expected, cfg); !equal {
  137. t.Errorf("Default config differs. Diff:\n%s", diff)
  138. }
  139. }
  140. func TestDeviceConfig(t *testing.T) {
  141. for i := OldestHandledVersion; i <= CurrentVersion; i++ {
  142. cfgFile := fmt.Sprintf("v%d.xml", i)
  143. if _, err := testFs.Stat(cfgFile); os.IsNotExist(err) {
  144. continue
  145. }
  146. testFs.RemoveAll(DefaultMarkerName)
  147. wr, wrCancel, err := copyAndLoad(testFs, cfgFile, device1)
  148. defer wrCancel()
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. _, err = testFs.Stat(DefaultMarkerName)
  153. if i < 6 && err != nil {
  154. t.Fatal(err)
  155. } else if i >= 6 && err == nil {
  156. t.Fatal("Unexpected file")
  157. }
  158. cfg := wr.Wrapper.(*wrapper).cfg
  159. expectedFolders := []FolderConfiguration{
  160. {
  161. ID: "test",
  162. FilesystemType: fs.FilesystemTypeBasic,
  163. Path: "testdata",
  164. Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
  165. Type: FolderTypeSendOnly,
  166. RescanIntervalS: 600,
  167. FSWatcherEnabled: false,
  168. FSWatcherDelayS: 10,
  169. Copiers: 0,
  170. Hashers: 0,
  171. AutoNormalize: true,
  172. MinDiskFree: Size{1, "%"},
  173. MaxConflicts: -1,
  174. Versioning: VersioningConfiguration{
  175. Params: map[string]string{},
  176. },
  177. WeakHashThresholdPct: 25,
  178. MarkerName: DefaultMarkerName,
  179. JunctionsAsDirs: true,
  180. MaxConcurrentWrites: maxConcurrentWritesDefault,
  181. XattrFilter: XattrFilter{
  182. Entries: []XattrFilterEntry{},
  183. },
  184. },
  185. }
  186. expectedDevices := []DeviceConfiguration{
  187. {
  188. DeviceID: device1,
  189. Name: "node one",
  190. Addresses: []string{"tcp://a"},
  191. Compression: protocol.CompressionMetadata,
  192. AllowedNetworks: []string{},
  193. IgnoredFolders: []ObservedFolder{},
  194. },
  195. {
  196. DeviceID: device4,
  197. Name: "node two",
  198. Addresses: []string{"tcp://b"},
  199. Compression: protocol.CompressionMetadata,
  200. AllowedNetworks: []string{},
  201. IgnoredFolders: []ObservedFolder{},
  202. },
  203. }
  204. expectedDeviceIDs := []protocol.DeviceID{device1, device4}
  205. if cfg.Version != CurrentVersion {
  206. t.Errorf("%d: Incorrect version %d != %d", i, cfg.Version, CurrentVersion)
  207. }
  208. if diff, equal := messagediff.PrettyDiff(expectedFolders, cfg.Folders); !equal {
  209. t.Errorf("%d: Incorrect Folders. Diff:\n%s\n%v", i, diff, cfg)
  210. }
  211. if diff, equal := messagediff.PrettyDiff(expectedDevices, cfg.Devices); !equal {
  212. t.Errorf("%d: Incorrect Devices. Diff:\n%s", i, diff)
  213. }
  214. if diff, equal := messagediff.PrettyDiff(expectedDeviceIDs, cfg.Folders[0].DeviceIDs()); !equal {
  215. t.Errorf("%d: Incorrect DeviceIDs. Diff:\n%s", i, diff)
  216. }
  217. }
  218. }
  219. func TestNoListenAddresses(t *testing.T) {
  220. cfg, cfgCancel, err := copyAndLoad(testFs, "nolistenaddress.xml", device1)
  221. defer cfgCancel()
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. expected := []string{""}
  226. actual := cfg.Options().RawListenAddresses
  227. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  228. t.Errorf("Unexpected RawListenAddresses. Diff:\n%s", diff)
  229. }
  230. }
  231. func TestOverriddenValues(t *testing.T) {
  232. expected := OptionsConfiguration{
  233. RawListenAddresses: []string{"tcp://:23000"},
  234. RawGlobalAnnServers: []string{"udp4://syncthing.nym.se:22026"},
  235. GlobalAnnEnabled: false,
  236. LocalAnnEnabled: false,
  237. LocalAnnPort: 42123,
  238. LocalAnnMCAddr: "quux:3232",
  239. MaxSendKbps: 1234,
  240. MaxRecvKbps: 2341,
  241. ReconnectIntervalS: 6000,
  242. RelaysEnabled: false,
  243. RelayReconnectIntervalM: 20,
  244. StartBrowser: false,
  245. NATEnabled: false,
  246. NATLeaseM: 90,
  247. NATRenewalM: 15,
  248. NATTimeoutS: 15,
  249. AutoUpgradeIntervalH: 24,
  250. KeepTemporariesH: 48,
  251. CacheIgnoredFiles: true,
  252. ProgressUpdateIntervalS: 10,
  253. LimitBandwidthInLan: true,
  254. MinHomeDiskFree: Size{5.2, "%"},
  255. URSeen: 8,
  256. URAccepted: 4,
  257. URURL: "https://localhost/newdata",
  258. URInitialDelayS: 800,
  259. URPostInsecurely: true,
  260. ReleasesURL: "https://localhost/releases",
  261. AlwaysLocalNets: []string{},
  262. OverwriteRemoteDevNames: true,
  263. TempIndexMinBlocks: 100,
  264. UnackedNotificationIDs: []string{"asdfasdf"},
  265. SetLowPriority: false,
  266. CRURL: "https://localhost/newcrash",
  267. CREnabled: false,
  268. StunKeepaliveStartS: 9000,
  269. StunKeepaliveMinS: 900,
  270. RawStunServers: []string{"foo"},
  271. FeatureFlags: []string{"feature"},
  272. ConnectionPriorityTCPLAN: 40,
  273. ConnectionPriorityQUICLAN: 45,
  274. ConnectionPriorityTCPWAN: 50,
  275. ConnectionPriorityQUICWAN: 55,
  276. ConnectionPriorityRelay: 9000,
  277. }
  278. expectedPath := "/media/syncthing"
  279. os.Unsetenv("STNOUPGRADE")
  280. cfg, cfgCancel, err := copyAndLoad(testFs, "overridenvalues.xml", device1)
  281. defer cfgCancel()
  282. if err != nil {
  283. t.Error(err)
  284. }
  285. if diff, equal := messagediff.PrettyDiff(expected, cfg.Options()); !equal {
  286. t.Errorf("Overridden config differs. Diff:\n%s", diff)
  287. }
  288. if path := cfg.DefaultFolder().Path; path != expectedPath {
  289. t.Errorf("Default folder path is %v, expected %v", path, expectedPath)
  290. }
  291. }
  292. func TestDeviceAddressesDynamic(t *testing.T) {
  293. name, _ := os.Hostname()
  294. expected := map[protocol.DeviceID]DeviceConfiguration{
  295. device1: {
  296. DeviceID: device1,
  297. Addresses: []string{"dynamic"},
  298. AllowedNetworks: []string{},
  299. IgnoredFolders: []ObservedFolder{},
  300. },
  301. device2: {
  302. DeviceID: device2,
  303. Addresses: []string{"dynamic"},
  304. AllowedNetworks: []string{},
  305. IgnoredFolders: []ObservedFolder{},
  306. },
  307. device3: {
  308. DeviceID: device3,
  309. Addresses: []string{"dynamic"},
  310. AllowedNetworks: []string{},
  311. IgnoredFolders: []ObservedFolder{},
  312. },
  313. device4: {
  314. DeviceID: device4,
  315. Name: name, // Set when auto created
  316. Addresses: []string{"dynamic"},
  317. Compression: protocol.CompressionMetadata,
  318. AllowedNetworks: []string{},
  319. IgnoredFolders: []ObservedFolder{},
  320. },
  321. }
  322. cfg, cfgCancel, err := copyAndLoad(testFs, "deviceaddressesdynamic.xml", device4)
  323. defer cfgCancel()
  324. if err != nil {
  325. t.Error(err)
  326. }
  327. actual := cfg.Devices()
  328. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  329. t.Errorf("Devices differ. Diff:\n%s", diff)
  330. }
  331. }
  332. func TestDeviceCompression(t *testing.T) {
  333. name, _ := os.Hostname()
  334. expected := map[protocol.DeviceID]DeviceConfiguration{
  335. device1: {
  336. DeviceID: device1,
  337. Addresses: []string{"dynamic"},
  338. Compression: protocol.CompressionMetadata,
  339. AllowedNetworks: []string{},
  340. IgnoredFolders: []ObservedFolder{},
  341. },
  342. device2: {
  343. DeviceID: device2,
  344. Addresses: []string{"dynamic"},
  345. Compression: protocol.CompressionMetadata,
  346. AllowedNetworks: []string{},
  347. IgnoredFolders: []ObservedFolder{},
  348. },
  349. device3: {
  350. DeviceID: device3,
  351. Addresses: []string{"dynamic"},
  352. Compression: protocol.CompressionNever,
  353. AllowedNetworks: []string{},
  354. IgnoredFolders: []ObservedFolder{},
  355. },
  356. device4: {
  357. DeviceID: device4,
  358. Name: name, // Set when auto created
  359. Addresses: []string{"dynamic"},
  360. Compression: protocol.CompressionMetadata,
  361. AllowedNetworks: []string{},
  362. IgnoredFolders: []ObservedFolder{},
  363. },
  364. }
  365. cfg, cfgCancel, err := copyAndLoad(testFs, "devicecompression.xml", device4)
  366. defer cfgCancel()
  367. if err != nil {
  368. t.Error(err)
  369. }
  370. actual := cfg.Devices()
  371. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  372. t.Errorf("Devices differ. Diff:\n%s", diff)
  373. }
  374. }
  375. func TestDeviceAddressesStatic(t *testing.T) {
  376. name, _ := os.Hostname()
  377. expected := map[protocol.DeviceID]DeviceConfiguration{
  378. device1: {
  379. DeviceID: device1,
  380. Addresses: []string{"tcp://192.0.2.1", "tcp://192.0.2.2"},
  381. AllowedNetworks: []string{},
  382. IgnoredFolders: []ObservedFolder{},
  383. },
  384. device2: {
  385. DeviceID: device2,
  386. Addresses: []string{"tcp://192.0.2.3:6070", "tcp://[2001:db8::42]:4242"},
  387. AllowedNetworks: []string{},
  388. IgnoredFolders: []ObservedFolder{},
  389. },
  390. device3: {
  391. DeviceID: device3,
  392. Addresses: []string{"tcp://[2001:db8::44]:4444", "tcp://192.0.2.4:6090"},
  393. AllowedNetworks: []string{},
  394. IgnoredFolders: []ObservedFolder{},
  395. },
  396. device4: {
  397. DeviceID: device4,
  398. Name: name, // Set when auto created
  399. Addresses: []string{"dynamic"},
  400. Compression: protocol.CompressionMetadata,
  401. AllowedNetworks: []string{},
  402. IgnoredFolders: []ObservedFolder{},
  403. },
  404. }
  405. cfg, cfgCancel, err := copyAndLoad(testFs, "deviceaddressesstatic.xml", device4)
  406. defer cfgCancel()
  407. if err != nil {
  408. t.Error(err)
  409. }
  410. actual := cfg.Devices()
  411. if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
  412. t.Errorf("Devices differ. Diff:\n%s", diff)
  413. }
  414. }
  415. func TestVersioningConfig(t *testing.T) {
  416. cfg, cfgCancel, err := copyAndLoad(testFs, "versioningconfig.xml", device4)
  417. defer cfgCancel()
  418. if err != nil {
  419. t.Error(err)
  420. }
  421. vc := cfg.Folders()["test"].Versioning
  422. if vc.Type != "simple" {
  423. t.Errorf(`vc.Type %q != "simple"`, vc.Type)
  424. }
  425. if l := len(vc.Params); l != 2 {
  426. t.Errorf("len(vc.Params) %d != 2", l)
  427. }
  428. expected := map[string]string{
  429. "foo": "bar",
  430. "baz": "quux",
  431. }
  432. if diff, equal := messagediff.PrettyDiff(expected, vc.Params); !equal {
  433. t.Errorf("vc.Params differ. Diff:\n%s", diff)
  434. }
  435. }
  436. func TestIssue1262(t *testing.T) {
  437. if !build.IsWindows {
  438. t.Skipf("path gets converted to absolute as part of the filesystem initialization on linux")
  439. }
  440. cfg, cfgCancel, err := copyAndLoad(testFs, "issue-1262.xml", device4)
  441. defer cfgCancel()
  442. if err != nil {
  443. t.Fatal(err)
  444. }
  445. actual := cfg.Folders()["test"].Filesystem(nil).URI()
  446. expected := `e:\`
  447. if actual != expected {
  448. t.Errorf("%q != %q", actual, expected)
  449. }
  450. }
  451. func TestIssue1750(t *testing.T) {
  452. cfg, cfgCancel, err := copyAndLoad(testFs, "issue-1750.xml", device4)
  453. defer cfgCancel()
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. if cfg.Options().RawListenAddresses[0] != "tcp://:23000" {
  458. t.Errorf("%q != %q", cfg.Options().RawListenAddresses[0], "tcp://:23000")
  459. }
  460. if cfg.Options().RawListenAddresses[1] != "tcp://:23001" {
  461. t.Errorf("%q != %q", cfg.Options().RawListenAddresses[1], "tcp://:23001")
  462. }
  463. if cfg.Options().RawGlobalAnnServers[0] != "udp4://syncthing.nym.se:22026" {
  464. t.Errorf("%q != %q", cfg.Options().RawGlobalAnnServers[0], "udp4://syncthing.nym.se:22026")
  465. }
  466. if cfg.Options().RawGlobalAnnServers[1] != "udp4://syncthing.nym.se:22027" {
  467. t.Errorf("%q != %q", cfg.Options().RawGlobalAnnServers[1], "udp4://syncthing.nym.se:22027")
  468. }
  469. }
  470. func TestFolderPath(t *testing.T) {
  471. folder := FolderConfiguration{
  472. Path: "~/tmp",
  473. }
  474. realPath := folder.Filesystem(nil).URI()
  475. if !filepath.IsAbs(realPath) {
  476. t.Error(realPath, "should be absolute")
  477. }
  478. if strings.Contains(realPath, "~") {
  479. t.Error(realPath, "should not contain ~")
  480. }
  481. }
  482. func TestFolderCheckPath(t *testing.T) {
  483. tmpFs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(16)+"?nostfolder=true")
  484. _ = tmpFs.MkdirAll(filepath.Join("dir", ".stfolder"), 0o777)
  485. testcases := []struct {
  486. path string
  487. err error
  488. }{
  489. {
  490. path: ".",
  491. err: ErrMarkerMissing,
  492. },
  493. {
  494. path: "does not exist",
  495. err: ErrPathMissing,
  496. },
  497. {
  498. path: "dir",
  499. err: nil,
  500. },
  501. }
  502. for _, testcase := range testcases {
  503. cfg := FolderConfiguration{
  504. FilesystemType: fs.FilesystemTypeFake,
  505. MarkerName: DefaultMarkerName,
  506. }
  507. if err := cfg.checkFilesystemPath(tmpFs, testcase.path); testcase.err != err {
  508. t.Errorf("unexpected error in case; path: [%s] err [%s] expected err [%v]", testcase.path, err, testcase.err)
  509. }
  510. }
  511. }
  512. func TestNewSaveLoad(t *testing.T) {
  513. path := "temp.xml"
  514. os.Remove(path)
  515. defer os.Remove(path)
  516. exists := func(path string) bool {
  517. _, err := os.Stat(path)
  518. return err == nil
  519. }
  520. intCfg := New(device1)
  521. cfg := wrap(path, intCfg, device1)
  522. defer cfg.stop()
  523. if exists(path) {
  524. t.Error(path, "exists")
  525. }
  526. err := cfg.Save()
  527. if err != nil {
  528. t.Error(err)
  529. }
  530. if !exists(path) {
  531. t.Error(path, "does not exist")
  532. }
  533. cfg2, err := load(path, device1)
  534. defer cfg2.stop()
  535. if err != nil {
  536. t.Error(err)
  537. }
  538. if diff, equal := messagediff.PrettyDiff(cfg.RawCopy(), cfg2.RawCopy()); !equal {
  539. t.Errorf("Configs are not equal. Diff:\n%s", diff)
  540. }
  541. }
  542. func TestWindowsLineEndings(t *testing.T) {
  543. if !build.IsWindows {
  544. t.Skip("Windows specific")
  545. }
  546. dir := t.TempDir()
  547. path := filepath.Join(dir, "config.xml")
  548. os.Remove(path)
  549. defer os.Remove(path)
  550. intCfg := New(device1)
  551. cfg := wrap(path, intCfg, device1)
  552. defer cfg.stop()
  553. if err := cfg.Save(); err != nil {
  554. t.Error(err)
  555. }
  556. bs, err := os.ReadFile(path)
  557. if err != nil {
  558. t.Error(err)
  559. }
  560. unixLineEndings := bytes.Count(bs, []byte("\n"))
  561. windowsLineEndings := bytes.Count(bs, []byte("\r\n"))
  562. if unixLineEndings == 0 || windowsLineEndings != unixLineEndings {
  563. t.Error("expected there to be a non-zero number of Windows line endings")
  564. }
  565. }
  566. func TestPrepare(t *testing.T) {
  567. var cfg Configuration
  568. if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.RawListenAddresses != nil {
  569. t.Error("Expected nil")
  570. }
  571. cfg.prepare(device1)
  572. if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.RawListenAddresses == nil {
  573. t.Error("Unexpected nil")
  574. }
  575. }
  576. func TestCopy(t *testing.T) {
  577. wrapper, wrapperCancel, err := copyAndLoad(testFs, "example.xml", device1)
  578. defer wrapperCancel()
  579. if err != nil {
  580. t.Fatal(err)
  581. }
  582. cfg := wrapper.RawCopy()
  583. bsOrig, err := json.MarshalIndent(cfg, "", " ")
  584. if err != nil {
  585. t.Fatal(err)
  586. }
  587. copy := cfg.Copy()
  588. cfg.Devices[0].Addresses[0] = "wrong"
  589. cfg.Folders[0].Devices[0].DeviceID = protocol.DeviceID{0, 1, 2, 3}
  590. cfg.Options.RawListenAddresses[0] = "wrong"
  591. cfg.GUI.APIKey = "wrong"
  592. bsChanged, err := json.MarshalIndent(cfg, "", " ")
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. bsCopy, err := json.MarshalIndent(copy, "", " ")
  597. if err != nil {
  598. t.Fatal(err)
  599. }
  600. if bytes.Equal(bsOrig, bsChanged) {
  601. t.Error("Config should have changed")
  602. }
  603. if !bytes.Equal(bsOrig, bsCopy) {
  604. t.Error("Copy should be unchanged")
  605. }
  606. }
  607. func TestPullOrder(t *testing.T) {
  608. wrapper, wrapperCleanup, err := copyAndLoad(testFs, "pullorder.xml", device1)
  609. defer wrapperCleanup()
  610. if err != nil {
  611. t.Fatal(err)
  612. }
  613. folders := wrapper.Folders()
  614. expected := []struct {
  615. name string
  616. order PullOrder
  617. }{
  618. {"f1", PullOrderRandom}, // empty value, default
  619. {"f2", PullOrderRandom}, // explicit
  620. {"f3", PullOrderAlphabetic}, // explicit
  621. {"f4", PullOrderRandom}, // unknown value, default
  622. {"f5", PullOrderSmallestFirst}, // explicit
  623. {"f6", PullOrderLargestFirst}, // explicit
  624. {"f7", PullOrderOldestFirst}, // explicit
  625. {"f8", PullOrderNewestFirst}, // explicit
  626. }
  627. // Verify values are deserialized correctly
  628. for _, tc := range expected {
  629. if actual := folders[tc.name].Order; actual != tc.order {
  630. t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
  631. }
  632. }
  633. // Serialize and deserialize again to verify it survives the transformation
  634. buf := new(bytes.Buffer)
  635. cfg := wrapper.RawCopy()
  636. cfg.WriteXML(buf)
  637. t.Logf("%s", buf.Bytes())
  638. cfg, _, err = ReadXML(buf, device1)
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. wrapper2 := wrap(wrapper.ConfigPath(), cfg, device1)
  643. defer wrapper2.stop()
  644. folders = wrapper2.Folders()
  645. for _, tc := range expected {
  646. if actual := folders[tc.name].Order; actual != tc.order {
  647. t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
  648. }
  649. }
  650. }
  651. func TestLargeRescanInterval(t *testing.T) {
  652. wrapper, wrapperCancel, err := copyAndLoad(testFs, "largeinterval.xml", device1)
  653. defer wrapperCancel()
  654. if err != nil {
  655. t.Fatal(err)
  656. }
  657. if wrapper.Folders()["l1"].RescanIntervalS != MaxRescanIntervalS {
  658. t.Error("too large rescan interval should be maxed out")
  659. }
  660. if wrapper.Folders()["l2"].RescanIntervalS != 0 {
  661. t.Error("negative rescan interval should become zero")
  662. }
  663. }
  664. func TestGUIConfigURL(t *testing.T) {
  665. testcases := [][2]string{
  666. {"192.0.2.42:8080", "http://192.0.2.42:8080/"},
  667. {":8080", "http://127.0.0.1:8080/"},
  668. {"0.0.0.0:8080", "http://127.0.0.1:8080/"},
  669. {"127.0.0.1:8080", "http://127.0.0.1:8080/"},
  670. {"127.0.0.2:8080", "http://127.0.0.2:8080/"},
  671. {"[::]:8080", "http://[::1]:8080/"},
  672. {"[2001::42]:8080", "http://[2001::42]:8080/"},
  673. }
  674. for _, tc := range testcases {
  675. c := GUIConfiguration{
  676. RawAddress: tc[0],
  677. }
  678. u := c.URL()
  679. if u != tc[1] {
  680. t.Errorf("Incorrect URL %s != %s for addr %s", u, tc[1], tc[0])
  681. }
  682. }
  683. }
  684. func TestGUIPasswordHash(t *testing.T) {
  685. var c GUIConfiguration
  686. // Setting a plaintext password should work
  687. testPass := "pass"
  688. if err := c.SetPassword(testPass); err != nil {
  689. t.Fatal(err)
  690. }
  691. if c.Password == testPass {
  692. t.Error("Password hashing resulted in plaintext")
  693. }
  694. if err := c.CompareHashedPassword(testPass); err != nil {
  695. t.Errorf("No match on same password: %v", err)
  696. }
  697. failPass := "different"
  698. if err := c.CompareHashedPassword(failPass); err == nil {
  699. t.Errorf("Match on different password: %v", err)
  700. }
  701. // Setting a bcrypt hash directly should also work
  702. hash, err := bcrypt.GenerateFromPassword([]byte("test"), bcrypt.MinCost)
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. c.SetPassword(string(hash))
  707. if err := c.CompareHashedPassword("test"); err != nil {
  708. t.Errorf("No match on hashed password: %v", err)
  709. }
  710. }
  711. func TestDuplicateDevices(t *testing.T) {
  712. // Duplicate devices should be removed
  713. wrapper, wrapperCancel, err := copyAndLoad(testFs, "dupdevices.xml", device1)
  714. defer wrapperCancel()
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. if l := len(wrapper.RawCopy().Devices); l != 3 {
  719. t.Errorf("Incorrect number of devices, %d != 3", l)
  720. }
  721. f := wrapper.Folders()["f2"]
  722. if l := len(f.Devices); l != 2 {
  723. t.Errorf("Incorrect number of folder devices, %d != 2", l)
  724. }
  725. }
  726. func TestDuplicateFolders(t *testing.T) {
  727. // Duplicate folders are a loading error
  728. _, _Cancel, err := copyAndLoad(testFs, "dupfolders.xml", device1)
  729. defer _Cancel()
  730. if err == nil || !strings.Contains(err.Error(), errFolderIDDuplicate.Error()) {
  731. t.Fatal(`Expected error to mention "duplicate folder ID":`, err)
  732. }
  733. }
  734. func TestEmptyFolderPaths(t *testing.T) {
  735. // Empty folder paths are not allowed at the loading stage, and should not
  736. // get messed up by the prepare steps (e.g., become the current dir or
  737. // get a slash added so that it becomes the root directory or similar).
  738. _, _Cancel, err := copyAndLoad(testFs, "nopath.xml", device1)
  739. defer _Cancel()
  740. if err == nil || !strings.Contains(err.Error(), errFolderPathEmpty.Error()) {
  741. t.Fatal("Expected error due to empty folder path, got", err)
  742. }
  743. }
  744. func TestV14ListenAddressesMigration(t *testing.T) {
  745. tcs := [][3][]string{
  746. // Default listen plus default relays is now "default"
  747. {
  748. {"tcp://0.0.0.0:22000"},
  749. {"dynamic+https://relays.syncthing.net/endpoint"},
  750. {"default"},
  751. },
  752. // Default listen address without any relay addresses gets converted
  753. // to just the listen address. It's easier this way, and frankly the
  754. // user has gone to some trouble to get the empty string in the
  755. // config to start with...
  756. {
  757. {"tcp://0.0.0.0:22000"}, // old listen addrs
  758. {""}, // old relay addrs
  759. {"tcp://0.0.0.0:22000"}, // new listen addrs
  760. },
  761. // Default listen plus non-default relays gets copied verbatim
  762. {
  763. {"tcp://0.0.0.0:22000"},
  764. {"dynamic+https://other.example.com"},
  765. {"tcp://0.0.0.0:22000", "dynamic+https://other.example.com"},
  766. },
  767. // Non-default listen plus default relays gets copied verbatim
  768. {
  769. {"tcp://1.2.3.4:22000"},
  770. {"dynamic+https://relays.syncthing.net/endpoint"},
  771. {"tcp://1.2.3.4:22000", "dynamic+https://relays.syncthing.net/endpoint"},
  772. },
  773. // Default stuff gets sucked into "default", the rest gets copied
  774. {
  775. {"tcp://0.0.0.0:22000", "tcp://1.2.3.4:22000"},
  776. {"dynamic+https://relays.syncthing.net/endpoint", "relay://other.example.com"},
  777. {"default", "tcp://1.2.3.4:22000", "relay://other.example.com"},
  778. },
  779. }
  780. m := migration{14, migrateToConfigV14}
  781. for _, tc := range tcs {
  782. cfg := Configuration{
  783. Version: 13,
  784. Options: OptionsConfiguration{
  785. RawListenAddresses: tc[0],
  786. DeprecatedRelayServers: tc[1],
  787. },
  788. }
  789. m.apply(&cfg)
  790. if cfg.Version != 14 {
  791. t.Error("Configuration was not converted")
  792. }
  793. sort.Strings(tc[2])
  794. if !reflect.DeepEqual(cfg.Options.RawListenAddresses, tc[2]) {
  795. t.Errorf("Migration error; actual %#v != expected %#v", cfg.Options.RawListenAddresses, tc[2])
  796. }
  797. }
  798. }
  799. func TestIgnoredDevices(t *testing.T) {
  800. // Verify that ignored devices that are also present in the
  801. // configuration are not in fact ignored.
  802. wrapper, wrapperCancel, err := copyAndLoad(testFs, "ignoreddevices.xml", device1)
  803. defer wrapperCancel()
  804. if err != nil {
  805. t.Fatal(err)
  806. }
  807. if wrapper.IgnoredDevice(device1) {
  808. t.Errorf("Device %v should not be ignored", device1)
  809. }
  810. if !wrapper.IgnoredDevice(device3) {
  811. t.Errorf("Device %v should be ignored", device3)
  812. }
  813. }
  814. func TestIgnoredFolders(t *testing.T) {
  815. // Verify that ignored folder that are also present in the
  816. // configuration are not in fact ignored.
  817. // Also, verify that folders that are shared with a device are not ignored.
  818. wrapper, wrapperCancel, err := copyAndLoad(testFs, "ignoredfolders.xml", device1)
  819. defer wrapperCancel()
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. if wrapper.IgnoredFolder(device2, "folder1") {
  824. t.Errorf("Device %v should not be ignored", device2)
  825. }
  826. if !wrapper.IgnoredFolder(device3, "folder1") {
  827. t.Errorf("Device %v should be ignored", device3)
  828. }
  829. // Should be removed, hence not ignored.
  830. if wrapper.IgnoredFolder(device4, "folder1") {
  831. t.Errorf("Device %v should not be ignored", device4)
  832. }
  833. if !wrapper.IgnoredFolder(device2, "folder2") {
  834. t.Errorf("Device %v should not be ignored", device2)
  835. }
  836. if !wrapper.IgnoredFolder(device3, "folder2") {
  837. t.Errorf("Device %v should be ignored", device3)
  838. }
  839. // 2 for folder2, 1 for folder1, as non-existing device and device the folder is shared with is removed.
  840. expectedIgnoredFolders := 3
  841. for _, dev := range wrapper.Devices() {
  842. expectedIgnoredFolders -= len(dev.IgnoredFolders)
  843. }
  844. if expectedIgnoredFolders != 0 {
  845. t.Errorf("Left with %d ignored folders", expectedIgnoredFolders)
  846. }
  847. }
  848. func TestGetDevice(t *testing.T) {
  849. // Verify that the Device() call does the right thing
  850. wrapper, wrapperCancel, err := copyAndLoad(testFs, "ignoreddevices.xml", device1)
  851. defer wrapperCancel()
  852. if err != nil {
  853. t.Fatal(err)
  854. }
  855. // device1 is mentioned in the config
  856. device, ok := wrapper.Device(device1)
  857. if !ok {
  858. t.Error(device1, "should exist")
  859. }
  860. if device.DeviceID != device1 {
  861. t.Error("Should have returned", device1, "not", device.DeviceID)
  862. }
  863. // device3 is not
  864. device, ok = wrapper.Device(device3)
  865. if ok {
  866. t.Error(device3, "should not exist")
  867. }
  868. if device.DeviceID == device3 {
  869. t.Error("Should not returned ID", device3)
  870. }
  871. }
  872. func TestSharesRemovedOnDeviceRemoval(t *testing.T) {
  873. t.Skip("to fix: test hangs")
  874. wrapper, wrapperCancel, err := copyAndLoad(testFs, "example.xml", device1)
  875. defer wrapperCancel()
  876. if err != nil {
  877. t.Errorf("Failed: %s", err)
  878. }
  879. raw := wrapper.RawCopy()
  880. raw.Devices = raw.Devices[:len(raw.Devices)-1]
  881. if len(raw.Folders[0].Devices) <= len(raw.Devices) {
  882. t.Error("Should have less devices")
  883. }
  884. replace(t, wrapper, raw)
  885. raw = wrapper.RawCopy()
  886. if len(raw.Folders[0].Devices) > len(raw.Devices) {
  887. t.Error("Unexpected extra device")
  888. }
  889. }
  890. func TestIssue4219(t *testing.T) {
  891. // Adding a folder that was previously ignored should make it unignored.
  892. r := bytes.NewReader([]byte(`{
  893. "devices": [
  894. {
  895. "deviceID": "GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY",
  896. "ignoredFolders": [
  897. {
  898. "id": "t1"
  899. },
  900. {
  901. "id": "abcd123"
  902. }
  903. ]
  904. },
  905. {
  906. "deviceID": "LGFPDIT-7SKNNJL-VJZA4FC-7QNCRKA-CE753K7-2BW5QDK-2FOZ7FR-FEP57QJ",
  907. "ignoredFolders": [
  908. {
  909. "id": "t1"
  910. },
  911. {
  912. "id": "abcd123"
  913. }
  914. ]
  915. }
  916. ],
  917. "folders": [
  918. {
  919. "id": "abcd123",
  920. "path": "testdata",
  921. "devices":[
  922. {"deviceID": "GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY"}
  923. ]
  924. }
  925. ],
  926. "remoteIgnoredDevices": [
  927. {
  928. "deviceID": "GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY"
  929. }
  930. ]
  931. }`))
  932. myID := protocol.LocalDeviceID
  933. cfg, err := ReadJSON(r, myID)
  934. if err != nil {
  935. t.Fatal(err)
  936. }
  937. if len(cfg.IgnoredDevices) != 0 { // 1 gets removed
  938. t.Errorf("There should be zero ignored devices, not %d", len(cfg.IgnoredDevices))
  939. }
  940. ignoredFolders := 0
  941. for _, dev := range cfg.Devices {
  942. ignoredFolders += len(dev.IgnoredFolders)
  943. }
  944. if ignoredFolders != 3 { // 1 gets removed
  945. t.Errorf("There should be three ignored folders, not %d", ignoredFolders)
  946. }
  947. w := wrap("/tmp/cfg", cfg, myID)
  948. defer w.stop()
  949. if !w.IgnoredFolder(device2, "t1") {
  950. t.Error("Folder device2 t1 should be ignored")
  951. }
  952. if !w.IgnoredFolder(device3, "t1") {
  953. t.Error("Folder device3 t1 should be ignored")
  954. }
  955. if w.IgnoredFolder(device2, "abcd123") {
  956. t.Error("Folder device2 abcd123 should not be ignored")
  957. }
  958. if !w.IgnoredFolder(device3, "abcd123") {
  959. t.Error("Folder device3 abcd123 should be ignored")
  960. }
  961. }
  962. func TestInvalidDeviceIDRejected(t *testing.T) {
  963. // This test verifies that we properly reject invalid device IDs when
  964. // deserializing a JSON config.
  965. cases := []struct {
  966. id string
  967. ok bool
  968. }{
  969. // a genuine device ID
  970. {"GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY", true},
  971. // incorrect check digit
  972. {"GYRZZQB-IRNPV4A-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY", false},
  973. // missing digit
  974. {"GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VA", false},
  975. // clearly broken
  976. {"invalid", false},
  977. // accepted as the empty device ID for historical reasons...
  978. {"", true},
  979. }
  980. for _, tc := range cases {
  981. cfg := defaultConfigAsMap()
  982. // Change the device ID of the first device to "invalid". Fast and loose
  983. // with the type assertions as we know what the JSON decoder returns.
  984. devs := cfg["devices"].([]interface{})
  985. dev0 := devs[0].(map[string]interface{})
  986. dev0["deviceID"] = tc.id
  987. devs[0] = dev0
  988. invalidJSON, err := json.Marshal(cfg)
  989. if err != nil {
  990. t.Fatal(err)
  991. }
  992. _, err = ReadJSON(bytes.NewReader(invalidJSON), device1)
  993. if tc.ok && err != nil {
  994. t.Errorf("unexpected error for device ID %q: %v", tc.id, err)
  995. } else if !tc.ok && err == nil {
  996. t.Errorf("device ID %q, expected error but got nil", tc.id)
  997. }
  998. }
  999. }
  1000. func TestInvalidFolderIDRejected(t *testing.T) {
  1001. // This test verifies that we properly reject invalid folder IDs when
  1002. // deserializing a JSON config.
  1003. cases := []struct {
  1004. id string
  1005. ok bool
  1006. }{
  1007. // a reasonable folder ID
  1008. {"foo", true},
  1009. // empty is not OK
  1010. {"", false},
  1011. }
  1012. for _, tc := range cases {
  1013. cfg := defaultConfigAsMap()
  1014. // Change the folder ID of the first folder to the empty string.
  1015. // Fast and loose with the type assertions as we know what the JSON
  1016. // decoder returns.
  1017. devs := cfg["folders"].([]interface{})
  1018. dev0 := devs[0].(map[string]interface{})
  1019. dev0["id"] = tc.id
  1020. devs[0] = dev0
  1021. invalidJSON, err := json.Marshal(cfg)
  1022. if err != nil {
  1023. t.Fatal(err)
  1024. }
  1025. _, err = ReadJSON(bytes.NewReader(invalidJSON), device1)
  1026. if tc.ok && err != nil {
  1027. t.Errorf("unexpected error for folder ID %q: %v", tc.id, err)
  1028. } else if !tc.ok && err == nil {
  1029. t.Errorf("folder ID %q, expected error but got nil", tc.id)
  1030. }
  1031. }
  1032. }
  1033. func TestFilterURLSchemePrefix(t *testing.T) {
  1034. cases := []struct {
  1035. before []string
  1036. prefix string
  1037. after []string
  1038. }{
  1039. {[]string{}, "kcp", []string{}},
  1040. {[]string{"tcp://foo"}, "kcp", []string{"tcp://foo"}},
  1041. {[]string{"kcp://foo"}, "kcp", []string{}},
  1042. {[]string{"tcp6://foo", "kcp6://foo"}, "kcp", []string{"tcp6://foo"}},
  1043. {[]string{"kcp6://foo", "tcp6://foo"}, "kcp", []string{"tcp6://foo"}},
  1044. {
  1045. []string{"tcp://foo", "tcp4://foo", "kcp://foo", "kcp4://foo", "banana://foo", "banana4://foo", "banananas!"},
  1046. "kcp",
  1047. []string{"tcp://foo", "tcp4://foo", "banana://foo", "banana4://foo", "banananas!"},
  1048. },
  1049. }
  1050. for _, tc := range cases {
  1051. res := filterURLSchemePrefix(tc.before, tc.prefix)
  1052. if !reflect.DeepEqual(res, tc.after) {
  1053. t.Errorf("filterURLSchemePrefix => %q, expected %q", res, tc.after)
  1054. }
  1055. }
  1056. }
  1057. func TestDeviceConfigObservedNotNil(t *testing.T) {
  1058. cfg := Configuration{
  1059. Devices: []DeviceConfiguration{
  1060. {},
  1061. },
  1062. }
  1063. cfg.prepare(device1)
  1064. for _, dev := range cfg.Devices {
  1065. if dev.IgnoredFolders == nil {
  1066. t.Errorf("Ignored folders nil")
  1067. }
  1068. }
  1069. }
  1070. func TestRemoveDeviceWithEmptyID(t *testing.T) {
  1071. cfg := Configuration{
  1072. Devices: []DeviceConfiguration{
  1073. {
  1074. Name: "foo",
  1075. },
  1076. },
  1077. Folders: []FolderConfiguration{
  1078. {
  1079. ID: "foo",
  1080. Path: "testdata",
  1081. Devices: []FolderDeviceConfiguration{{}},
  1082. },
  1083. },
  1084. }
  1085. cfg.prepare(device1)
  1086. if len(cfg.Devices) != 1 {
  1087. t.Error("Expected one device")
  1088. } else if cfg.Devices[0].DeviceID != device1 {
  1089. t.Error("Expected device with empty ID to be removed from config:", cfg.Devices)
  1090. }
  1091. if len(cfg.Folders[0].Devices) != 1 {
  1092. t.Error("Expected one device in folder")
  1093. } else if cfg.Folders[0].Devices[0].DeviceID != device1 {
  1094. t.Error("Expected device with empty ID to be removed from folder")
  1095. }
  1096. }
  1097. func TestMaxConcurrentFolders(t *testing.T) {
  1098. cases := []struct {
  1099. input int
  1100. output int
  1101. }{
  1102. {input: -42, output: 0},
  1103. {input: -1, output: 0},
  1104. {input: 0, output: runtime.GOMAXPROCS(-1)},
  1105. {input: 1, output: 1},
  1106. {input: 42, output: 42},
  1107. }
  1108. for _, tc := range cases {
  1109. opts := OptionsConfiguration{RawMaxFolderConcurrency: tc.input}
  1110. res := opts.MaxFolderConcurrency()
  1111. if res != tc.output {
  1112. t.Errorf("Wrong MaxFolderConcurrency, %d => %d, expected %d", tc.input, res, tc.output)
  1113. }
  1114. }
  1115. }
  1116. func adjustDeviceConfiguration(cfg *DeviceConfiguration, id protocol.DeviceID, name string) {
  1117. cfg.DeviceID = id
  1118. cfg.Name = name
  1119. }
  1120. func adjustFolderConfiguration(cfg *FolderConfiguration, id, label string, fsType fs.FilesystemType, path string) {
  1121. cfg.ID = id
  1122. cfg.Label = label
  1123. cfg.FilesystemType = fsType
  1124. cfg.Path = path
  1125. }
  1126. // defaultConfigAsMap returns a valid default config as a JSON-decoded
  1127. // map[string]interface{}. This is useful to override random elements and
  1128. // re-encode into JSON.
  1129. func defaultConfigAsMap() map[string]interface{} {
  1130. cfg := New(device1)
  1131. dev := cfg.Defaults.Device.Copy()
  1132. adjustDeviceConfiguration(&dev, device2, "name")
  1133. cfg.Devices = append(cfg.Devices, dev)
  1134. folder := cfg.Defaults.Folder.Copy()
  1135. adjustFolderConfiguration(&folder, "default", "default", fs.FilesystemTypeBasic, "/tmp")
  1136. cfg.Folders = append(cfg.Folders, folder)
  1137. bs, err := json.Marshal(cfg)
  1138. if err != nil {
  1139. // can't happen
  1140. panic(err)
  1141. }
  1142. var tmp map[string]interface{}
  1143. if err := json.Unmarshal(bs, &tmp); err != nil {
  1144. // can't happen
  1145. panic(err)
  1146. }
  1147. return tmp
  1148. }
  1149. func copyToTmp(fs fs.Filesystem, path string) (string, error) {
  1150. orig, err := fs.Open(path)
  1151. if err != nil {
  1152. return "", err
  1153. }
  1154. defer orig.Close()
  1155. temp, err := fs.Create("syncthing-configTest-" + rand.String(6))
  1156. if err != nil {
  1157. return "", err
  1158. }
  1159. defer temp.Close()
  1160. if _, err := io.Copy(temp, orig); err != nil {
  1161. return "", err
  1162. }
  1163. return temp.Name(), nil
  1164. }
  1165. func copyAndLoad(fs fs.Filesystem, path string, myID protocol.DeviceID) (*testWrapper, func(), error) {
  1166. temp, err := copyToTmp(fs, path)
  1167. if err != nil {
  1168. return nil, func() {}, err
  1169. }
  1170. wrapper, err := loadTest(fs, temp, myID)
  1171. if err != nil {
  1172. return nil, func() {}, err
  1173. }
  1174. return wrapper, func() {
  1175. fs.Remove(temp)
  1176. wrapper.stop()
  1177. }, nil
  1178. }
  1179. func loadTest(fs fs.Filesystem, path string, myID protocol.DeviceID) (*testWrapper, error) {
  1180. cfg, _, err := loadWrapTest(fs, path, myID, events.NoopLogger)
  1181. if err != nil {
  1182. return nil, err
  1183. }
  1184. return startWrapper(cfg), nil
  1185. }
  1186. func loadWrapTest(fs fs.Filesystem, path string, myID protocol.DeviceID, evLogger events.Logger) (Wrapper, int, error) {
  1187. fd, err := fs.Open(path)
  1188. if err != nil {
  1189. return nil, 0, err
  1190. }
  1191. defer fd.Close()
  1192. cfg, originalVersion, err := ReadXML(fd, myID)
  1193. if err != nil {
  1194. return nil, 0, err
  1195. }
  1196. return Wrap(filepath.Join(testFs.URI(), path), cfg, myID, evLogger), originalVersion, nil
  1197. }
  1198. func load(path string, myID protocol.DeviceID) (*testWrapper, error) {
  1199. cfg, _, err := Load(path, myID, events.NoopLogger)
  1200. if err != nil {
  1201. return nil, err
  1202. }
  1203. return startWrapper(cfg), nil
  1204. }
  1205. func wrap(path string, cfg Configuration, myID protocol.DeviceID) *testWrapper {
  1206. wrapper := Wrap(path, cfg, myID, events.NoopLogger)
  1207. return startWrapper(wrapper)
  1208. }
  1209. type testWrapper struct {
  1210. Wrapper
  1211. cancel context.CancelFunc
  1212. done chan struct{}
  1213. }
  1214. func (w *testWrapper) stop() {
  1215. w.cancel()
  1216. <-w.done
  1217. }
  1218. func startWrapper(wrapper Wrapper) *testWrapper {
  1219. tw := &testWrapper{
  1220. Wrapper: wrapper,
  1221. done: make(chan struct{}),
  1222. }
  1223. ctx, cancel := context.WithCancel(context.Background())
  1224. tw.cancel = cancel
  1225. go func() {
  1226. wrapper.Serve(ctx)
  1227. close(tw.done)
  1228. }()
  1229. return tw
  1230. }
  1231. func TestInternalVersioningConfiguration(t *testing.T) {
  1232. // Verify that the versioning configuration XML serializes to something
  1233. // reasonable.
  1234. cfg := New(device1)
  1235. folder := cfg.Defaults.Folder.Copy()
  1236. adjustFolderConfiguration(&folder, "default", "default", fs.FilesystemTypeBasic, "/tmp")
  1237. cfg.Folders = append(cfg.Folders, folder)
  1238. cfg.Folders[0].Versioning = VersioningConfiguration{
  1239. Type: "foo",
  1240. Params: map[string]string{"bar": "baz"},
  1241. CleanupIntervalS: 42,
  1242. }
  1243. // These things should all be present in the serialized version.
  1244. expected := []string{
  1245. `<versioning type="foo">`,
  1246. `<param key="bar" val="baz"`,
  1247. `<cleanupIntervalS>42<`,
  1248. `</versioning>`,
  1249. }
  1250. bs, err := xml.MarshalIndent(cfg, "", " ")
  1251. if err != nil {
  1252. t.Fatal(err)
  1253. }
  1254. for _, exp := range expected {
  1255. if !strings.Contains(string(bs), exp) {
  1256. t.Logf("%s", bs)
  1257. t.Fatal("bad serializion of versioning parameters")
  1258. }
  1259. }
  1260. }
  1261. func TestReceiveEncryptedFolderFixed(t *testing.T) {
  1262. cfg := Configuration{
  1263. Folders: []FolderConfiguration{
  1264. {
  1265. ID: "foo",
  1266. Path: "testdata",
  1267. Type: FolderTypeReceiveEncrypted,
  1268. DisableTempIndexes: false,
  1269. IgnorePerms: false,
  1270. },
  1271. },
  1272. }
  1273. cfg.prepare(device1)
  1274. if len(cfg.Folders) != 1 {
  1275. t.Fatal("Expected one folder")
  1276. }
  1277. f := cfg.Folders[0]
  1278. if !f.DisableTempIndexes {
  1279. t.Error("DisableTempIndexes should be true")
  1280. }
  1281. if !f.IgnorePerms {
  1282. t.Error("IgnorePerms should be true")
  1283. }
  1284. }
  1285. func TestXattrFilter(t *testing.T) {
  1286. cases := []struct {
  1287. in []string
  1288. filter []XattrFilterEntry
  1289. out []string
  1290. }{
  1291. {in: nil, filter: nil, out: nil},
  1292. {in: []string{"foo", "bar", "baz"}, filter: nil, out: []string{"foo", "bar", "baz"}},
  1293. {
  1294. in: []string{"foo", "bar", "baz"},
  1295. filter: []XattrFilterEntry{{Match: "b*", Permit: true}},
  1296. out: []string{"bar", "baz"},
  1297. },
  1298. {
  1299. in: []string{"foo", "bar", "baz"},
  1300. filter: []XattrFilterEntry{{Match: "b*", Permit: false}, {Match: "*", Permit: true}},
  1301. out: []string{"foo"},
  1302. },
  1303. {
  1304. in: []string{"foo", "bar", "baz"},
  1305. filter: []XattrFilterEntry{{Match: "yoink", Permit: true}},
  1306. out: []string{},
  1307. },
  1308. }
  1309. for _, tc := range cases {
  1310. f := XattrFilter{Entries: tc.filter}
  1311. var out []string
  1312. for _, s := range tc.in {
  1313. if f.Permit(s) {
  1314. out = append(out, s)
  1315. }
  1316. }
  1317. if fmt.Sprint(out) != fmt.Sprint(tc.out) {
  1318. t.Errorf("Filter.Apply(%v, %v) == %v, expected %v", tc.in, tc.filter, out, tc.out)
  1319. }
  1320. }
  1321. }
  1322. func TestUntrustedIntroducer(t *testing.T) {
  1323. fd, err := os.Open("testdata/untrustedintroducer.xml")
  1324. if err != nil {
  1325. t.Fatal(err)
  1326. }
  1327. cfg, _, err := ReadXML(fd, device1)
  1328. if err != nil {
  1329. t.Fatal(err)
  1330. }
  1331. if len(cfg.Devices) != 2 {
  1332. // ourselves and the remote in the config
  1333. t.Fatal("Expected two devices")
  1334. }
  1335. // Check that the introducer and auto-accept flags were negated
  1336. var foundUntrusted protocol.DeviceID
  1337. for _, d := range cfg.Devices {
  1338. if d.Name == "untrusted" {
  1339. foundUntrusted = d.DeviceID
  1340. if !d.Untrusted {
  1341. t.Error("untrusted device should be untrusted")
  1342. }
  1343. if d.Introducer {
  1344. t.Error("untrusted device should not be an introducer")
  1345. }
  1346. if d.AutoAcceptFolders {
  1347. t.Error("untrusted device should not auto-accept folders")
  1348. }
  1349. }
  1350. }
  1351. if foundUntrusted.Equals(protocol.EmptyDeviceID) {
  1352. t.Error("untrusted device not found")
  1353. }
  1354. // Folder A has the device added without a password, which is not permitted
  1355. folderA := cfg.FolderMap()["a"]
  1356. for _, dev := range folderA.Devices {
  1357. if dev.DeviceID == foundUntrusted {
  1358. t.Error("untrusted device should not be in folder A")
  1359. }
  1360. }
  1361. // Folder B has the device added with a password, this is just a sanity check
  1362. folderB := cfg.FolderMap()["b"]
  1363. found := false
  1364. for _, dev := range folderB.Devices {
  1365. if dev.DeviceID == foundUntrusted {
  1366. found = true
  1367. if dev.EncryptionPassword == "" {
  1368. t.Error("untrusted device should have a password in folder B")
  1369. }
  1370. }
  1371. }
  1372. if !found {
  1373. t.Error("untrusted device not found in folder B")
  1374. }
  1375. }
  1376. // Verify that opening a config with myID == protocol.EmptyDeviceID doesn't add that ID to the config.
  1377. // Done in various places where config is needed, but the device ID isn't known.
  1378. func TestLoadEmptyDeviceID(t *testing.T) {
  1379. temp, err := copyToTmp(testFs, "example.xml")
  1380. if err != nil {
  1381. t.Fatal(err)
  1382. }
  1383. fd, err := testFs.Open(temp)
  1384. if err != nil {
  1385. t.Fatal(err)
  1386. }
  1387. defer fd.Close()
  1388. cfg, _, err := ReadXML(fd, protocol.EmptyDeviceID)
  1389. if err != nil {
  1390. t.Fatal(err)
  1391. }
  1392. for _, devCfg := range cfg.Devices {
  1393. if devCfg.DeviceID == protocol.EmptyDeviceID {
  1394. t.Fatal("Device with empty ID")
  1395. }
  1396. }
  1397. for _, folderCfg := range cfg.Folders {
  1398. for _, devCfg := range folderCfg.Devices {
  1399. if devCfg.DeviceID == protocol.EmptyDeviceID {
  1400. t.Fatalf("Device with empty ID in folder %v", folderCfg.Description())
  1401. }
  1402. }
  1403. }
  1404. }
  1405. func loadTestFiles() {
  1406. entries, err := os.ReadDir("testdata")
  1407. if err != nil {
  1408. return
  1409. }
  1410. for _, e := range entries {
  1411. handleFile(e.Name())
  1412. }
  1413. }
  1414. func handleFile(name string) {
  1415. fd, err := testFs.Create(name)
  1416. if err != nil {
  1417. return
  1418. }
  1419. origin, _ := os.ReadFile(filepath.Join("testdata", name))
  1420. fd.Write(origin)
  1421. fd.Close()
  1422. }
  1423. func TestCopyMatching(t *testing.T) {
  1424. type Nested struct {
  1425. A int
  1426. }
  1427. type Test struct {
  1428. CopyA int
  1429. CopyB []string
  1430. CopyC Nested
  1431. CopyD *Nested
  1432. NoCopy int `restart:"true"`
  1433. }
  1434. from := Test{
  1435. CopyA: 1,
  1436. CopyB: []string{"friend", "foe"},
  1437. CopyC: Nested{
  1438. A: 2,
  1439. },
  1440. CopyD: &Nested{
  1441. A: 3,
  1442. },
  1443. NoCopy: 4,
  1444. }
  1445. to := Test{
  1446. CopyA: 11,
  1447. CopyB: []string{"foot", "toe"},
  1448. CopyC: Nested{
  1449. A: 22,
  1450. },
  1451. CopyD: &Nested{
  1452. A: 33,
  1453. },
  1454. NoCopy: 44,
  1455. }
  1456. // Copy empty fields
  1457. copyMatchingTag(&from, &to, "restart", func(v string) bool {
  1458. return v != "true"
  1459. })
  1460. if to.CopyA != 1 {
  1461. t.Error("CopyA")
  1462. }
  1463. if len(to.CopyB) != 2 || to.CopyB[0] != "friend" || to.CopyB[1] != "foe" {
  1464. t.Error("CopyB")
  1465. }
  1466. if to.CopyC.A != 2 {
  1467. t.Error("CopyC")
  1468. }
  1469. if to.CopyD.A != 3 {
  1470. t.Error("CopyC")
  1471. }
  1472. if to.NoCopy != 44 {
  1473. t.Error("NoCopy")
  1474. }
  1475. }