config_test.go 43 KB

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