model_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "io/ioutil"
  12. "math/rand"
  13. "net"
  14. "os"
  15. "path/filepath"
  16. "runtime"
  17. "strconv"
  18. "testing"
  19. "time"
  20. "github.com/d4l3k/messagediff"
  21. "github.com/syncthing/syncthing/lib/config"
  22. "github.com/syncthing/syncthing/lib/db"
  23. "github.com/syncthing/syncthing/lib/protocol"
  24. )
  25. var device1, device2 protocol.DeviceID
  26. var defaultConfig *config.Wrapper
  27. var defaultFolderConfig config.FolderConfiguration
  28. func init() {
  29. device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  30. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  31. defaultFolderConfig = config.NewFolderConfiguration("default", "testdata")
  32. defaultFolderConfig.Devices = []config.FolderDeviceConfiguration{{DeviceID: device1}}
  33. _defaultConfig := config.Configuration{
  34. Folders: []config.FolderConfiguration{defaultFolderConfig},
  35. Devices: []config.DeviceConfiguration{config.NewDeviceConfiguration(device1, "device1")},
  36. Options: config.OptionsConfiguration{
  37. // Don't remove temporaries directly on startup
  38. KeepTemporariesH: 1,
  39. },
  40. }
  41. defaultConfig = config.Wrap("/tmp/test", _defaultConfig)
  42. }
  43. var testDataExpected = map[string]protocol.FileInfo{
  44. "foo": {
  45. Name: "foo",
  46. Flags: 0,
  47. Modified: 0,
  48. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0x7, Hash: []uint8{0xae, 0xc0, 0x70, 0x64, 0x5f, 0xe5, 0x3e, 0xe3, 0xb3, 0x76, 0x30, 0x59, 0x37, 0x61, 0x34, 0xf0, 0x58, 0xcc, 0x33, 0x72, 0x47, 0xc9, 0x78, 0xad, 0xd1, 0x78, 0xb6, 0xcc, 0xdf, 0xb0, 0x1, 0x9f}}},
  49. },
  50. "empty": {
  51. Name: "empty",
  52. Flags: 0,
  53. Modified: 0,
  54. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0x0, Hash: []uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}}},
  55. },
  56. "bar": {
  57. Name: "bar",
  58. Flags: 0,
  59. Modified: 0,
  60. Blocks: []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}},
  61. },
  62. }
  63. func init() {
  64. // Fix expected test data to match reality
  65. for n, f := range testDataExpected {
  66. fi, _ := os.Stat("testdata/" + n)
  67. f.Flags = uint32(fi.Mode())
  68. f.Modified = fi.ModTime().Unix()
  69. testDataExpected[n] = f
  70. }
  71. }
  72. func TestRequest(t *testing.T) {
  73. db := db.OpenMemory()
  74. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  75. // device1 shares default, but device2 doesn't
  76. m.AddFolder(defaultFolderConfig)
  77. m.StartFolderRO("default")
  78. m.ServeBackground()
  79. m.ScanFolder("default")
  80. bs := make([]byte, protocol.BlockSize)
  81. // Existing, shared file
  82. bs = bs[:6]
  83. err := m.Request(device1, "default", "foo", 0, nil, 0, nil, bs)
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. if !bytes.Equal(bs, []byte("foobar")) {
  88. t.Errorf("Incorrect data from request: %q", string(bs))
  89. }
  90. // Existing, nonshared file
  91. err = m.Request(device2, "default", "foo", 0, nil, 0, nil, bs)
  92. if err == nil {
  93. t.Error("Unexpected nil error on insecure file read")
  94. }
  95. // Nonexistent file
  96. err = m.Request(device1, "default", "nonexistent", 0, nil, 0, nil, bs)
  97. if err == nil {
  98. t.Error("Unexpected nil error on insecure file read")
  99. }
  100. // Shared folder, but disallowed file name
  101. err = m.Request(device1, "default", "../walk.go", 0, nil, 0, nil, bs)
  102. if err == nil {
  103. t.Error("Unexpected nil error on insecure file read")
  104. }
  105. // Negative offset
  106. err = m.Request(device1, "default", "foo", -4, nil, 0, nil, bs[:0])
  107. if err == nil {
  108. t.Error("Unexpected nil error on insecure file read")
  109. }
  110. // Larger block than available
  111. bs = bs[:42]
  112. err = m.Request(device1, "default", "foo", 0, nil, 0, nil, bs)
  113. if err == nil {
  114. t.Error("Unexpected nil error on insecure file read")
  115. }
  116. }
  117. func genFiles(n int) []protocol.FileInfo {
  118. files := make([]protocol.FileInfo, n)
  119. t := time.Now().Unix()
  120. for i := 0; i < n; i++ {
  121. files[i] = protocol.FileInfo{
  122. Name: fmt.Sprintf("file%d", i),
  123. Modified: t,
  124. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  125. }
  126. }
  127. return files
  128. }
  129. func BenchmarkIndex_10000(b *testing.B) {
  130. benchmarkIndex(b, 10000)
  131. }
  132. func BenchmarkIndex_100(b *testing.B) {
  133. benchmarkIndex(b, 100)
  134. }
  135. func benchmarkIndex(b *testing.B, nfiles int) {
  136. db := db.OpenMemory()
  137. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  138. m.AddFolder(defaultFolderConfig)
  139. m.StartFolderRO("default")
  140. m.ServeBackground()
  141. files := genFiles(nfiles)
  142. m.Index(device1, "default", files, 0, nil)
  143. b.ResetTimer()
  144. for i := 0; i < b.N; i++ {
  145. m.Index(device1, "default", files, 0, nil)
  146. }
  147. b.ReportAllocs()
  148. }
  149. func BenchmarkIndexUpdate_10000_10000(b *testing.B) {
  150. benchmarkIndexUpdate(b, 10000, 10000)
  151. }
  152. func BenchmarkIndexUpdate_10000_100(b *testing.B) {
  153. benchmarkIndexUpdate(b, 10000, 100)
  154. }
  155. func BenchmarkIndexUpdate_10000_1(b *testing.B) {
  156. benchmarkIndexUpdate(b, 10000, 1)
  157. }
  158. func benchmarkIndexUpdate(b *testing.B, nfiles, nufiles int) {
  159. db := db.OpenMemory()
  160. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  161. m.AddFolder(defaultFolderConfig)
  162. m.StartFolderRO("default")
  163. m.ServeBackground()
  164. files := genFiles(nfiles)
  165. ufiles := genFiles(nufiles)
  166. m.Index(device1, "default", files, 0, nil)
  167. b.ResetTimer()
  168. for i := 0; i < b.N; i++ {
  169. m.IndexUpdate(device1, "default", ufiles, 0, nil)
  170. }
  171. b.ReportAllocs()
  172. }
  173. type downloadProgressMessage struct {
  174. folder string
  175. updates []protocol.FileDownloadProgressUpdate
  176. flags uint32
  177. options []protocol.Option
  178. }
  179. type FakeConnection struct {
  180. id protocol.DeviceID
  181. requestData []byte
  182. downloadProgressMessages []downloadProgressMessage
  183. }
  184. func (FakeConnection) Close() error {
  185. return nil
  186. }
  187. func (f FakeConnection) Start() {
  188. }
  189. func (f FakeConnection) ID() protocol.DeviceID {
  190. return f.id
  191. }
  192. func (f FakeConnection) Name() string {
  193. return ""
  194. }
  195. func (f FakeConnection) Option(string) string {
  196. return ""
  197. }
  198. func (FakeConnection) Index(string, []protocol.FileInfo, uint32, []protocol.Option) error {
  199. return nil
  200. }
  201. func (FakeConnection) IndexUpdate(string, []protocol.FileInfo, uint32, []protocol.Option) error {
  202. return nil
  203. }
  204. func (f FakeConnection) Request(folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
  205. return f.requestData, nil
  206. }
  207. func (FakeConnection) ClusterConfig(protocol.ClusterConfigMessage) {}
  208. func (FakeConnection) Ping() bool {
  209. return true
  210. }
  211. func (FakeConnection) Closed() bool {
  212. return false
  213. }
  214. func (FakeConnection) Statistics() protocol.Statistics {
  215. return protocol.Statistics{}
  216. }
  217. func (f *FakeConnection) DownloadProgress(folder string, updates []protocol.FileDownloadProgressUpdate, flags uint32, options []protocol.Option) {
  218. f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
  219. folder: folder,
  220. updates: updates,
  221. flags: flags,
  222. options: options,
  223. })
  224. }
  225. func BenchmarkRequest(b *testing.B) {
  226. db := db.OpenMemory()
  227. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  228. m.AddFolder(defaultFolderConfig)
  229. m.ServeBackground()
  230. m.ScanFolder("default")
  231. const n = 1000
  232. files := make([]protocol.FileInfo, n)
  233. t := time.Now().Unix()
  234. for i := 0; i < n; i++ {
  235. files[i] = protocol.FileInfo{
  236. Name: fmt.Sprintf("file%d", i),
  237. Modified: t,
  238. Blocks: []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}},
  239. }
  240. }
  241. fc := &FakeConnection{
  242. id: device1,
  243. requestData: []byte("some data to return"),
  244. }
  245. m.AddConnection(Connection{
  246. &net.TCPConn{},
  247. fc,
  248. ConnectionTypeDirectAccept,
  249. }, protocol.HelloMessage{})
  250. m.Index(device1, "default", files, 0, nil)
  251. b.ResetTimer()
  252. for i := 0; i < b.N; i++ {
  253. data, err := m.requestGlobal(device1, "default", files[i%n].Name, 0, 32, nil, false)
  254. if err != nil {
  255. b.Error(err)
  256. }
  257. if data == nil {
  258. b.Error("nil data")
  259. }
  260. }
  261. }
  262. func TestDeviceRename(t *testing.T) {
  263. hello := protocol.HelloMessage{
  264. ClientName: "syncthing",
  265. ClientVersion: "v0.9.4",
  266. }
  267. defer os.Remove("tmpconfig.xml")
  268. rawCfg := config.New(device1)
  269. rawCfg.Devices = []config.DeviceConfiguration{
  270. {
  271. DeviceID: device1,
  272. },
  273. }
  274. cfg := config.Wrap("tmpconfig.xml", rawCfg)
  275. db := db.OpenMemory()
  276. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  277. if cfg.Devices()[device1].Name != "" {
  278. t.Errorf("Device already has a name")
  279. }
  280. conn := Connection{
  281. &net.TCPConn{},
  282. &FakeConnection{
  283. id: device1,
  284. requestData: []byte("some data to return"),
  285. },
  286. ConnectionTypeDirectAccept,
  287. }
  288. m.AddConnection(conn, hello)
  289. m.ServeBackground()
  290. if cfg.Devices()[device1].Name != "" {
  291. t.Errorf("Device already has a name")
  292. }
  293. m.Close(device1, protocol.ErrTimeout)
  294. hello.DeviceName = "tester"
  295. m.AddConnection(conn, hello)
  296. if cfg.Devices()[device1].Name != "tester" {
  297. t.Errorf("Device did not get a name")
  298. }
  299. m.Close(device1, protocol.ErrTimeout)
  300. hello.DeviceName = "tester2"
  301. m.AddConnection(conn, hello)
  302. if cfg.Devices()[device1].Name != "tester" {
  303. t.Errorf("Device name got overwritten")
  304. }
  305. cfgw, err := config.Load("tmpconfig.xml", protocol.LocalDeviceID)
  306. if err != nil {
  307. t.Error(err)
  308. return
  309. }
  310. if cfgw.Devices()[device1].Name != "tester" {
  311. t.Errorf("Device name not saved in config")
  312. }
  313. m.Close(device1, protocol.ErrTimeout)
  314. opts := cfg.Options()
  315. opts.OverwriteNames = true
  316. cfg.SetOptions(opts)
  317. hello.DeviceName = "tester2"
  318. m.AddConnection(conn, hello)
  319. if cfg.Devices()[device1].Name != "tester2" {
  320. t.Errorf("Device name not overwritten")
  321. }
  322. }
  323. func TestClusterConfig(t *testing.T) {
  324. cfg := config.New(device1)
  325. cfg.Devices = []config.DeviceConfiguration{
  326. {
  327. DeviceID: device1,
  328. Introducer: true,
  329. },
  330. {
  331. DeviceID: device2,
  332. },
  333. }
  334. cfg.Folders = []config.FolderConfiguration{
  335. {
  336. ID: "folder1",
  337. Devices: []config.FolderDeviceConfiguration{
  338. {DeviceID: device1},
  339. {DeviceID: device2},
  340. },
  341. },
  342. {
  343. ID: "folder2",
  344. Devices: []config.FolderDeviceConfiguration{
  345. {DeviceID: device1},
  346. {DeviceID: device2},
  347. },
  348. },
  349. }
  350. db := db.OpenMemory()
  351. m := NewModel(config.Wrap("/tmp/test", cfg), protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  352. m.AddFolder(cfg.Folders[0])
  353. m.AddFolder(cfg.Folders[1])
  354. m.ServeBackground()
  355. cm := m.generateClusterConfig(device2)
  356. if l := len(cm.Folders); l != 2 {
  357. t.Fatalf("Incorrect number of folders %d != 2", l)
  358. }
  359. r := cm.Folders[0]
  360. if r.ID != "folder1" {
  361. t.Errorf("Incorrect folder %q != folder1", r.ID)
  362. }
  363. if l := len(r.Devices); l != 2 {
  364. t.Errorf("Incorrect number of devices %d != 2", l)
  365. }
  366. if id := r.Devices[0].ID; !bytes.Equal(id, device1[:]) {
  367. t.Errorf("Incorrect device ID %x != %x", id, device1)
  368. }
  369. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  370. t.Error("Device1 should be flagged as Introducer")
  371. }
  372. if id := r.Devices[1].ID; !bytes.Equal(id, device2[:]) {
  373. t.Errorf("Incorrect device ID %x != %x", id, device2)
  374. }
  375. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  376. t.Error("Device2 should not be flagged as Introducer")
  377. }
  378. r = cm.Folders[1]
  379. if r.ID != "folder2" {
  380. t.Errorf("Incorrect folder %q != folder2", r.ID)
  381. }
  382. if l := len(r.Devices); l != 2 {
  383. t.Errorf("Incorrect number of devices %d != 2", l)
  384. }
  385. if id := r.Devices[0].ID; !bytes.Equal(id, device1[:]) {
  386. t.Errorf("Incorrect device ID %x != %x", id, device1)
  387. }
  388. if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
  389. t.Error("Device1 should be flagged as Introducer")
  390. }
  391. if id := r.Devices[1].ID; !bytes.Equal(id, device2[:]) {
  392. t.Errorf("Incorrect device ID %x != %x", id, device2)
  393. }
  394. if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
  395. t.Error("Device2 should not be flagged as Introducer")
  396. }
  397. }
  398. func TestIgnores(t *testing.T) {
  399. arrEqual := func(a, b []string) bool {
  400. if len(a) != len(b) {
  401. return false
  402. }
  403. for i := range a {
  404. if a[i] != b[i] {
  405. return false
  406. }
  407. }
  408. return true
  409. }
  410. // Assure a clean start state
  411. ioutil.WriteFile("testdata/.stfolder", nil, 0644)
  412. ioutil.WriteFile("testdata/.stignore", []byte(".*\nquux\n"), 0644)
  413. db := db.OpenMemory()
  414. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  415. m.AddFolder(defaultFolderConfig)
  416. m.StartFolderRO("default")
  417. m.ServeBackground()
  418. expected := []string{
  419. ".*",
  420. "quux",
  421. }
  422. ignores, _, err := m.GetIgnores("default")
  423. if err != nil {
  424. t.Error(err)
  425. }
  426. if !arrEqual(ignores, expected) {
  427. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  428. }
  429. ignores = append(ignores, "pox")
  430. err = m.SetIgnores("default", ignores)
  431. if err != nil {
  432. t.Error(err)
  433. }
  434. ignores2, _, err := m.GetIgnores("default")
  435. if err != nil {
  436. t.Error(err)
  437. }
  438. if arrEqual(expected, ignores2) {
  439. t.Errorf("Incorrect ignores: %v == %v", ignores2, expected)
  440. }
  441. if !arrEqual(ignores, ignores2) {
  442. t.Errorf("Incorrect ignores: %v != %v", ignores2, ignores)
  443. }
  444. err = m.SetIgnores("default", expected)
  445. if err != nil {
  446. t.Error(err)
  447. }
  448. ignores, _, err = m.GetIgnores("default")
  449. if err != nil {
  450. t.Error(err)
  451. }
  452. if !arrEqual(ignores, expected) {
  453. t.Errorf("Incorrect ignores: %v != %v", ignores, expected)
  454. }
  455. ignores, _, err = m.GetIgnores("doesnotexist")
  456. if err == nil {
  457. t.Error("No error")
  458. }
  459. err = m.SetIgnores("doesnotexist", expected)
  460. if err == nil {
  461. t.Error("No error")
  462. }
  463. // Invalid path, marker should be missing, hence returns an error.
  464. m.AddFolder(config.FolderConfiguration{ID: "fresh", RawPath: "XXX"})
  465. ignores, _, err = m.GetIgnores("fresh")
  466. if err == nil {
  467. t.Error("No error")
  468. }
  469. }
  470. func TestRefuseUnknownBits(t *testing.T) {
  471. db := db.OpenMemory()
  472. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  473. m.AddFolder(defaultFolderConfig)
  474. m.ServeBackground()
  475. m.ScanFolder("default")
  476. m.Index(device1, "default", []protocol.FileInfo{
  477. {
  478. Name: "invalid1",
  479. Flags: (protocol.FlagsAll + 1) &^ protocol.FlagInvalid,
  480. },
  481. {
  482. Name: "invalid2",
  483. Flags: (protocol.FlagsAll + 2) &^ protocol.FlagInvalid,
  484. },
  485. {
  486. Name: "invalid3",
  487. Flags: (1 << 31) &^ protocol.FlagInvalid,
  488. },
  489. {
  490. Name: "valid",
  491. Flags: protocol.FlagsAll &^ (protocol.FlagInvalid | protocol.FlagSymlink),
  492. },
  493. }, 0, nil)
  494. for _, name := range []string{"invalid1", "invalid2", "invalid3"} {
  495. f, ok := m.CurrentGlobalFile("default", name)
  496. if ok || f.Name == name {
  497. t.Error("Invalid file found or name match")
  498. }
  499. }
  500. f, ok := m.CurrentGlobalFile("default", "valid")
  501. if !ok || f.Name != "valid" {
  502. t.Error("Valid file not found or name mismatch", ok, f)
  503. }
  504. }
  505. func TestROScanRecovery(t *testing.T) {
  506. ldb := db.OpenMemory()
  507. set := db.NewFileSet("default", ldb)
  508. set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  509. {Name: "dummyfile"},
  510. })
  511. fcfg := config.FolderConfiguration{
  512. ID: "default",
  513. RawPath: "testdata/rotestfolder",
  514. RescanIntervalS: 1,
  515. }
  516. cfg := config.Wrap("/tmp/test", config.Configuration{
  517. Folders: []config.FolderConfiguration{fcfg},
  518. Devices: []config.DeviceConfiguration{
  519. {
  520. DeviceID: device1,
  521. },
  522. },
  523. })
  524. os.RemoveAll(fcfg.RawPath)
  525. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil)
  526. m.AddFolder(fcfg)
  527. m.StartFolderRO("default")
  528. m.ServeBackground()
  529. waitFor := func(status string) error {
  530. timeout := time.Now().Add(2 * time.Second)
  531. for {
  532. if time.Now().After(timeout) {
  533. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  534. }
  535. _, _, err := m.State("default")
  536. if err == nil && status == "" {
  537. return nil
  538. }
  539. if err != nil && err.Error() == status {
  540. return nil
  541. }
  542. time.Sleep(10 * time.Millisecond)
  543. }
  544. }
  545. if err := waitFor("folder path missing"); err != nil {
  546. t.Error(err)
  547. return
  548. }
  549. os.Mkdir(fcfg.RawPath, 0700)
  550. if err := waitFor("folder marker missing"); err != nil {
  551. t.Error(err)
  552. return
  553. }
  554. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  555. if err != nil {
  556. t.Error(err)
  557. return
  558. }
  559. fd.Close()
  560. if err := waitFor(""); err != nil {
  561. t.Error(err)
  562. return
  563. }
  564. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  565. if err := waitFor("folder marker missing"); err != nil {
  566. t.Error(err)
  567. return
  568. }
  569. os.Remove(fcfg.RawPath)
  570. if err := waitFor("folder path missing"); err != nil {
  571. t.Error(err)
  572. return
  573. }
  574. }
  575. func TestRWScanRecovery(t *testing.T) {
  576. ldb := db.OpenMemory()
  577. set := db.NewFileSet("default", ldb)
  578. set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  579. {Name: "dummyfile"},
  580. })
  581. fcfg := config.FolderConfiguration{
  582. ID: "default",
  583. RawPath: "testdata/rwtestfolder",
  584. RescanIntervalS: 1,
  585. }
  586. cfg := config.Wrap("/tmp/test", config.Configuration{
  587. Folders: []config.FolderConfiguration{fcfg},
  588. Devices: []config.DeviceConfiguration{
  589. {
  590. DeviceID: device1,
  591. },
  592. },
  593. })
  594. os.RemoveAll(fcfg.RawPath)
  595. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil)
  596. m.AddFolder(fcfg)
  597. m.StartFolderRW("default")
  598. m.ServeBackground()
  599. waitFor := func(status string) error {
  600. timeout := time.Now().Add(2 * time.Second)
  601. for {
  602. if time.Now().After(timeout) {
  603. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  604. }
  605. _, _, err := m.State("default")
  606. if err == nil && status == "" {
  607. return nil
  608. }
  609. if err != nil && err.Error() == status {
  610. return nil
  611. }
  612. time.Sleep(10 * time.Millisecond)
  613. }
  614. }
  615. if err := waitFor("folder path missing"); err != nil {
  616. t.Error(err)
  617. return
  618. }
  619. os.Mkdir(fcfg.RawPath, 0700)
  620. if err := waitFor("folder marker missing"); err != nil {
  621. t.Error(err)
  622. return
  623. }
  624. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  625. if err != nil {
  626. t.Error(err)
  627. return
  628. }
  629. fd.Close()
  630. if err := waitFor(""); err != nil {
  631. t.Error(err)
  632. return
  633. }
  634. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  635. if err := waitFor("folder marker missing"); err != nil {
  636. t.Error(err)
  637. return
  638. }
  639. os.Remove(fcfg.RawPath)
  640. if err := waitFor("folder path missing"); err != nil {
  641. t.Error(err)
  642. return
  643. }
  644. }
  645. func TestGlobalDirectoryTree(t *testing.T) {
  646. db := db.OpenMemory()
  647. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  648. m.AddFolder(defaultFolderConfig)
  649. m.ServeBackground()
  650. b := func(isfile bool, path ...string) protocol.FileInfo {
  651. flags := uint32(protocol.FlagDirectory)
  652. blocks := []protocol.BlockInfo{}
  653. if isfile {
  654. flags = 0
  655. blocks = []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}}
  656. }
  657. return protocol.FileInfo{
  658. Name: filepath.Join(path...),
  659. Flags: flags,
  660. Modified: 0x666,
  661. Blocks: blocks,
  662. }
  663. }
  664. filedata := []interface{}{time.Unix(0x666, 0), 0xa}
  665. testdata := []protocol.FileInfo{
  666. b(false, "another"),
  667. b(false, "another", "directory"),
  668. b(true, "another", "directory", "afile"),
  669. b(false, "another", "directory", "with"),
  670. b(false, "another", "directory", "with", "a"),
  671. b(true, "another", "directory", "with", "a", "file"),
  672. b(true, "another", "directory", "with", "file"),
  673. b(true, "another", "file"),
  674. b(false, "other"),
  675. b(false, "other", "rand"),
  676. b(false, "other", "random"),
  677. b(false, "other", "random", "dir"),
  678. b(false, "other", "random", "dirx"),
  679. b(false, "other", "randomx"),
  680. b(false, "some"),
  681. b(false, "some", "directory"),
  682. b(false, "some", "directory", "with"),
  683. b(false, "some", "directory", "with", "a"),
  684. b(true, "some", "directory", "with", "a", "file"),
  685. b(true, "rootfile"),
  686. }
  687. expectedResult := map[string]interface{}{
  688. "another": map[string]interface{}{
  689. "directory": map[string]interface{}{
  690. "afile": filedata,
  691. "with": map[string]interface{}{
  692. "a": map[string]interface{}{
  693. "file": filedata,
  694. },
  695. "file": filedata,
  696. },
  697. },
  698. "file": filedata,
  699. },
  700. "other": map[string]interface{}{
  701. "rand": map[string]interface{}{},
  702. "random": map[string]interface{}{
  703. "dir": map[string]interface{}{},
  704. "dirx": map[string]interface{}{},
  705. },
  706. "randomx": map[string]interface{}{},
  707. },
  708. "some": map[string]interface{}{
  709. "directory": map[string]interface{}{
  710. "with": map[string]interface{}{
  711. "a": map[string]interface{}{
  712. "file": filedata,
  713. },
  714. },
  715. },
  716. },
  717. "rootfile": filedata,
  718. }
  719. mm := func(data interface{}) string {
  720. bytes, err := json.Marshal(data)
  721. if err != nil {
  722. panic(err)
  723. }
  724. return string(bytes)
  725. }
  726. m.Index(device1, "default", testdata, 0, nil)
  727. result := m.GlobalDirectoryTree("default", "", -1, false)
  728. if mm(result) != mm(expectedResult) {
  729. t.Errorf("Does not match:\n%#v\n%#v", result, expectedResult)
  730. }
  731. result = m.GlobalDirectoryTree("default", "another", -1, false)
  732. if mm(result) != mm(expectedResult["another"]) {
  733. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult["another"]))
  734. }
  735. result = m.GlobalDirectoryTree("default", "", 0, false)
  736. currentResult := map[string]interface{}{
  737. "another": map[string]interface{}{},
  738. "other": map[string]interface{}{},
  739. "some": map[string]interface{}{},
  740. "rootfile": filedata,
  741. }
  742. if mm(result) != mm(currentResult) {
  743. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  744. }
  745. result = m.GlobalDirectoryTree("default", "", 1, false)
  746. currentResult = map[string]interface{}{
  747. "another": map[string]interface{}{
  748. "directory": map[string]interface{}{},
  749. "file": filedata,
  750. },
  751. "other": map[string]interface{}{
  752. "rand": map[string]interface{}{},
  753. "random": map[string]interface{}{},
  754. "randomx": map[string]interface{}{},
  755. },
  756. "some": map[string]interface{}{
  757. "directory": map[string]interface{}{},
  758. },
  759. "rootfile": filedata,
  760. }
  761. if mm(result) != mm(currentResult) {
  762. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  763. }
  764. result = m.GlobalDirectoryTree("default", "", -1, true)
  765. currentResult = map[string]interface{}{
  766. "another": map[string]interface{}{
  767. "directory": map[string]interface{}{
  768. "with": map[string]interface{}{
  769. "a": map[string]interface{}{},
  770. },
  771. },
  772. },
  773. "other": map[string]interface{}{
  774. "rand": map[string]interface{}{},
  775. "random": map[string]interface{}{
  776. "dir": map[string]interface{}{},
  777. "dirx": map[string]interface{}{},
  778. },
  779. "randomx": map[string]interface{}{},
  780. },
  781. "some": map[string]interface{}{
  782. "directory": map[string]interface{}{
  783. "with": map[string]interface{}{
  784. "a": map[string]interface{}{},
  785. },
  786. },
  787. },
  788. }
  789. if mm(result) != mm(currentResult) {
  790. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  791. }
  792. result = m.GlobalDirectoryTree("default", "", 1, true)
  793. currentResult = map[string]interface{}{
  794. "another": map[string]interface{}{
  795. "directory": map[string]interface{}{},
  796. },
  797. "other": map[string]interface{}{
  798. "rand": map[string]interface{}{},
  799. "random": map[string]interface{}{},
  800. "randomx": map[string]interface{}{},
  801. },
  802. "some": map[string]interface{}{
  803. "directory": map[string]interface{}{},
  804. },
  805. }
  806. if mm(result) != mm(currentResult) {
  807. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  808. }
  809. result = m.GlobalDirectoryTree("default", "another", 0, false)
  810. currentResult = map[string]interface{}{
  811. "directory": map[string]interface{}{},
  812. "file": filedata,
  813. }
  814. if mm(result) != mm(currentResult) {
  815. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  816. }
  817. result = m.GlobalDirectoryTree("default", "some/directory", 0, false)
  818. currentResult = map[string]interface{}{
  819. "with": map[string]interface{}{},
  820. }
  821. if mm(result) != mm(currentResult) {
  822. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  823. }
  824. result = m.GlobalDirectoryTree("default", "some/directory", 1, false)
  825. currentResult = map[string]interface{}{
  826. "with": map[string]interface{}{
  827. "a": map[string]interface{}{},
  828. },
  829. }
  830. if mm(result) != mm(currentResult) {
  831. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  832. }
  833. result = m.GlobalDirectoryTree("default", "some/directory", 2, false)
  834. currentResult = map[string]interface{}{
  835. "with": map[string]interface{}{
  836. "a": map[string]interface{}{
  837. "file": filedata,
  838. },
  839. },
  840. }
  841. if mm(result) != mm(currentResult) {
  842. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  843. }
  844. result = m.GlobalDirectoryTree("default", "another", -1, true)
  845. currentResult = map[string]interface{}{
  846. "directory": map[string]interface{}{
  847. "with": map[string]interface{}{
  848. "a": map[string]interface{}{},
  849. },
  850. },
  851. }
  852. if mm(result) != mm(currentResult) {
  853. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  854. }
  855. // No prefix matching!
  856. result = m.GlobalDirectoryTree("default", "som", -1, false)
  857. currentResult = map[string]interface{}{}
  858. if mm(result) != mm(currentResult) {
  859. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  860. }
  861. }
  862. func TestGlobalDirectorySelfFixing(t *testing.T) {
  863. db := db.OpenMemory()
  864. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  865. m.AddFolder(defaultFolderConfig)
  866. m.ServeBackground()
  867. b := func(isfile bool, path ...string) protocol.FileInfo {
  868. flags := uint32(protocol.FlagDirectory)
  869. blocks := []protocol.BlockInfo{}
  870. if isfile {
  871. flags = 0
  872. blocks = []protocol.BlockInfo{{Offset: 0x0, Size: 0xa, Hash: []uint8{0x2f, 0x72, 0xcc, 0x11, 0xa6, 0xfc, 0xd0, 0x27, 0x1e, 0xce, 0xf8, 0xc6, 0x10, 0x56, 0xee, 0x1e, 0xb1, 0x24, 0x3b, 0xe3, 0x80, 0x5b, 0xf9, 0xa9, 0xdf, 0x98, 0xf9, 0x2f, 0x76, 0x36, 0xb0, 0x5c}}}
  873. }
  874. return protocol.FileInfo{
  875. Name: filepath.Join(path...),
  876. Flags: flags,
  877. Modified: 0x666,
  878. Blocks: blocks,
  879. }
  880. }
  881. filedata := []interface{}{time.Unix(0x666, 0).Format(time.RFC3339), 0xa}
  882. testdata := []protocol.FileInfo{
  883. b(true, "another", "directory", "afile"),
  884. b(true, "another", "directory", "with", "a", "file"),
  885. b(true, "another", "directory", "with", "file"),
  886. b(false, "other", "random", "dirx"),
  887. b(false, "other", "randomx"),
  888. b(false, "some", "directory", "with", "x"),
  889. b(true, "some", "directory", "with", "a", "file"),
  890. b(false, "this", "is", "a", "deep", "invalid", "directory"),
  891. b(true, "xthis", "is", "a", "deep", "invalid", "file"),
  892. }
  893. expectedResult := map[string]interface{}{
  894. "another": map[string]interface{}{
  895. "directory": map[string]interface{}{
  896. "afile": filedata,
  897. "with": map[string]interface{}{
  898. "a": map[string]interface{}{
  899. "file": filedata,
  900. },
  901. "file": filedata,
  902. },
  903. },
  904. },
  905. "other": map[string]interface{}{
  906. "random": map[string]interface{}{
  907. "dirx": map[string]interface{}{},
  908. },
  909. "randomx": map[string]interface{}{},
  910. },
  911. "some": map[string]interface{}{
  912. "directory": map[string]interface{}{
  913. "with": map[string]interface{}{
  914. "a": map[string]interface{}{
  915. "file": filedata,
  916. },
  917. "x": map[string]interface{}{},
  918. },
  919. },
  920. },
  921. "this": map[string]interface{}{
  922. "is": map[string]interface{}{
  923. "a": map[string]interface{}{
  924. "deep": map[string]interface{}{
  925. "invalid": map[string]interface{}{
  926. "directory": map[string]interface{}{},
  927. },
  928. },
  929. },
  930. },
  931. },
  932. "xthis": map[string]interface{}{
  933. "is": map[string]interface{}{
  934. "a": map[string]interface{}{
  935. "deep": map[string]interface{}{
  936. "invalid": map[string]interface{}{
  937. "file": filedata,
  938. },
  939. },
  940. },
  941. },
  942. },
  943. }
  944. mm := func(data interface{}) string {
  945. bytes, err := json.Marshal(data)
  946. if err != nil {
  947. panic(err)
  948. }
  949. return string(bytes)
  950. }
  951. m.Index(device1, "default", testdata, 0, nil)
  952. result := m.GlobalDirectoryTree("default", "", -1, false)
  953. if mm(result) != mm(expectedResult) {
  954. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult))
  955. }
  956. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, false)
  957. currentResult := map[string]interface{}{
  958. "invalid": map[string]interface{}{
  959. "file": filedata,
  960. },
  961. }
  962. if mm(result) != mm(currentResult) {
  963. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  964. }
  965. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, true)
  966. currentResult = map[string]interface{}{
  967. "invalid": map[string]interface{}{},
  968. }
  969. if mm(result) != mm(currentResult) {
  970. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  971. }
  972. // !!! This is actually BAD, because we don't have enough level allowance
  973. // to accept this file, hence the tree is left unbuilt !!!
  974. result = m.GlobalDirectoryTree("default", "xthis", 1, false)
  975. currentResult = map[string]interface{}{}
  976. if mm(result) != mm(currentResult) {
  977. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  978. }
  979. }
  980. func genDeepFiles(n, d int) []protocol.FileInfo {
  981. rand.Seed(int64(n))
  982. files := make([]protocol.FileInfo, n)
  983. t := time.Now().Unix()
  984. for i := 0; i < n; i++ {
  985. path := ""
  986. for i := 0; i <= d; i++ {
  987. path = filepath.Join(path, strconv.Itoa(rand.Int()))
  988. }
  989. sofar := ""
  990. for _, path := range filepath.SplitList(path) {
  991. sofar = filepath.Join(sofar, path)
  992. files[i] = protocol.FileInfo{
  993. Name: sofar,
  994. }
  995. i++
  996. }
  997. files[i].Modified = t
  998. files[i].Blocks = []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}}
  999. }
  1000. return files
  1001. }
  1002. func BenchmarkTree_10000_50(b *testing.B) {
  1003. benchmarkTree(b, 10000, 50)
  1004. }
  1005. func BenchmarkTree_100_50(b *testing.B) {
  1006. benchmarkTree(b, 100, 50)
  1007. }
  1008. func BenchmarkTree_100_10(b *testing.B) {
  1009. benchmarkTree(b, 100, 10)
  1010. }
  1011. func benchmarkTree(b *testing.B, n1, n2 int) {
  1012. db := db.OpenMemory()
  1013. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  1014. m.AddFolder(defaultFolderConfig)
  1015. m.ServeBackground()
  1016. m.ScanFolder("default")
  1017. files := genDeepFiles(n1, n2)
  1018. m.Index(device1, "default", files, 0, nil)
  1019. b.ResetTimer()
  1020. for i := 0; i < b.N; i++ {
  1021. m.GlobalDirectoryTree("default", "", -1, false)
  1022. }
  1023. b.ReportAllocs()
  1024. }
  1025. func TestIgnoreDelete(t *testing.T) {
  1026. db := db.OpenMemory()
  1027. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  1028. // This folder should ignore external deletes
  1029. cfg := defaultFolderConfig
  1030. cfg.IgnoreDelete = true
  1031. m.AddFolder(cfg)
  1032. m.ServeBackground()
  1033. m.StartFolderRW("default")
  1034. m.ScanFolder("default")
  1035. // Get a currently existing file
  1036. f, ok := m.CurrentGlobalFile("default", "foo")
  1037. if !ok {
  1038. t.Fatal("foo should exist")
  1039. }
  1040. // Mark it for deletion
  1041. f.Flags = protocol.FlagDeleted
  1042. f.Version = f.Version.Update(142) // arbitrary short remote ID
  1043. f.Blocks = nil
  1044. // Send the index
  1045. m.Index(device1, "default", []protocol.FileInfo{f}, 0, nil)
  1046. // Make sure we ignored it
  1047. f, ok = m.CurrentGlobalFile("default", "foo")
  1048. if !ok {
  1049. t.Fatal("foo should exist")
  1050. }
  1051. if f.IsDeleted() {
  1052. t.Fatal("foo should not be marked for deletion")
  1053. }
  1054. }
  1055. func TestUnifySubs(t *testing.T) {
  1056. cases := []struct {
  1057. in []string // input to unifySubs
  1058. exists []string // paths that exist in the database
  1059. out []string // expected output
  1060. }{
  1061. {
  1062. // 0. trailing slashes are cleaned, known paths are just passed on
  1063. []string{"foo/", "bar//"},
  1064. []string{"foo", "bar"},
  1065. []string{"bar", "foo"}, // the output is sorted
  1066. },
  1067. {
  1068. // 1. "foo/bar" gets trimmed as it's covered by foo
  1069. []string{"foo", "bar/", "foo/bar/"},
  1070. []string{"foo", "bar"},
  1071. []string{"bar", "foo"},
  1072. },
  1073. {
  1074. // 2. "" gets simplified to the empty list; ie scan all
  1075. []string{"foo", ""},
  1076. []string{"foo"},
  1077. nil,
  1078. },
  1079. {
  1080. // 3. "foo/bar" is unknown, but it's kept
  1081. // because its parent is known
  1082. []string{"foo/bar"},
  1083. []string{"foo"},
  1084. []string{"foo/bar"},
  1085. },
  1086. {
  1087. // 4. two independent known paths, both are kept
  1088. // "usr/lib" is not a prefix of "usr/libexec"
  1089. []string{"usr/lib", "usr/libexec"},
  1090. []string{"usr", "usr/lib", "usr/libexec"},
  1091. []string{"usr/lib", "usr/libexec"},
  1092. },
  1093. {
  1094. // 5. "usr/lib" is a prefix of "usr/lib/exec"
  1095. []string{"usr/lib", "usr/lib/exec"},
  1096. []string{"usr", "usr/lib", "usr/libexec"},
  1097. []string{"usr/lib"},
  1098. },
  1099. {
  1100. // 6. .stignore and .stfolder are special and are passed on
  1101. // verbatim even though they are unknown
  1102. []string{".stfolder", ".stignore"},
  1103. []string{},
  1104. []string{".stfolder", ".stignore"},
  1105. },
  1106. {
  1107. // 7. but the presense of something else unknown forces an actual
  1108. // scan
  1109. []string{".stfolder", ".stignore", "foo/bar"},
  1110. []string{},
  1111. []string{".stfolder", ".stignore", "foo"},
  1112. },
  1113. {
  1114. // 8. explicit request to scan all
  1115. nil,
  1116. []string{"foo"},
  1117. nil,
  1118. },
  1119. {
  1120. // 9. empty list of subs
  1121. []string{},
  1122. []string{"foo"},
  1123. nil,
  1124. },
  1125. }
  1126. if runtime.GOOS == "windows" {
  1127. // Fixup path separators
  1128. for i := range cases {
  1129. for j, p := range cases[i].in {
  1130. cases[i].in[j] = filepath.FromSlash(p)
  1131. }
  1132. for j, p := range cases[i].exists {
  1133. cases[i].exists[j] = filepath.FromSlash(p)
  1134. }
  1135. for j, p := range cases[i].out {
  1136. cases[i].out[j] = filepath.FromSlash(p)
  1137. }
  1138. }
  1139. }
  1140. for i, tc := range cases {
  1141. exists := func(f string) bool {
  1142. for _, e := range tc.exists {
  1143. if f == e {
  1144. return true
  1145. }
  1146. }
  1147. return false
  1148. }
  1149. out := unifySubs(tc.in, exists)
  1150. if diff, equal := messagediff.PrettyDiff(tc.out, out); !equal {
  1151. t.Errorf("Case %d failed; got %v, expected %v, diff:\n%s", i, out, tc.out, diff)
  1152. }
  1153. }
  1154. }