model_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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", config.FolderTypeReadWrite)
  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.StartFolder("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.StartFolder("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.StartFolder("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.StartFolder("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. Type: config.FolderTypeReadOnly,
  515. RescanIntervalS: 1,
  516. }
  517. cfg := config.Wrap("/tmp/test", config.Configuration{
  518. Folders: []config.FolderConfiguration{fcfg},
  519. Devices: []config.DeviceConfiguration{
  520. {
  521. DeviceID: device1,
  522. },
  523. },
  524. })
  525. os.RemoveAll(fcfg.RawPath)
  526. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil)
  527. m.AddFolder(fcfg)
  528. m.StartFolder("default")
  529. m.ServeBackground()
  530. waitFor := func(status string) error {
  531. timeout := time.Now().Add(2 * time.Second)
  532. for {
  533. if time.Now().After(timeout) {
  534. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  535. }
  536. _, _, err := m.State("default")
  537. if err == nil && status == "" {
  538. return nil
  539. }
  540. if err != nil && err.Error() == status {
  541. return nil
  542. }
  543. time.Sleep(10 * time.Millisecond)
  544. }
  545. }
  546. if err := waitFor("folder path missing"); err != nil {
  547. t.Error(err)
  548. return
  549. }
  550. os.Mkdir(fcfg.RawPath, 0700)
  551. if err := waitFor("folder marker missing"); err != nil {
  552. t.Error(err)
  553. return
  554. }
  555. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  556. if err != nil {
  557. t.Error(err)
  558. return
  559. }
  560. fd.Close()
  561. if err := waitFor(""); err != nil {
  562. t.Error(err)
  563. return
  564. }
  565. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  566. if err := waitFor("folder marker missing"); err != nil {
  567. t.Error(err)
  568. return
  569. }
  570. os.Remove(fcfg.RawPath)
  571. if err := waitFor("folder path missing"); err != nil {
  572. t.Error(err)
  573. return
  574. }
  575. }
  576. func TestRWScanRecovery(t *testing.T) {
  577. ldb := db.OpenMemory()
  578. set := db.NewFileSet("default", ldb)
  579. set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
  580. {Name: "dummyfile"},
  581. })
  582. fcfg := config.FolderConfiguration{
  583. ID: "default",
  584. RawPath: "testdata/rwtestfolder",
  585. Type: config.FolderTypeReadWrite,
  586. RescanIntervalS: 1,
  587. }
  588. cfg := config.Wrap("/tmp/test", config.Configuration{
  589. Folders: []config.FolderConfiguration{fcfg},
  590. Devices: []config.DeviceConfiguration{
  591. {
  592. DeviceID: device1,
  593. },
  594. },
  595. })
  596. os.RemoveAll(fcfg.RawPath)
  597. m := NewModel(cfg, protocol.LocalDeviceID, "device", "syncthing", "dev", ldb, nil)
  598. m.AddFolder(fcfg)
  599. m.StartFolder("default")
  600. m.ServeBackground()
  601. waitFor := func(status string) error {
  602. timeout := time.Now().Add(2 * time.Second)
  603. for {
  604. if time.Now().After(timeout) {
  605. return fmt.Errorf("Timed out waiting for status: %s, current status: %s", status, m.cfg.Folders()["default"].Invalid)
  606. }
  607. _, _, err := m.State("default")
  608. if err == nil && status == "" {
  609. return nil
  610. }
  611. if err != nil && err.Error() == status {
  612. return nil
  613. }
  614. time.Sleep(10 * time.Millisecond)
  615. }
  616. }
  617. if err := waitFor("folder path missing"); err != nil {
  618. t.Error(err)
  619. return
  620. }
  621. os.Mkdir(fcfg.RawPath, 0700)
  622. if err := waitFor("folder marker missing"); err != nil {
  623. t.Error(err)
  624. return
  625. }
  626. fd, err := os.Create(filepath.Join(fcfg.RawPath, ".stfolder"))
  627. if err != nil {
  628. t.Error(err)
  629. return
  630. }
  631. fd.Close()
  632. if err := waitFor(""); err != nil {
  633. t.Error(err)
  634. return
  635. }
  636. os.Remove(filepath.Join(fcfg.RawPath, ".stfolder"))
  637. if err := waitFor("folder marker missing"); err != nil {
  638. t.Error(err)
  639. return
  640. }
  641. os.Remove(fcfg.RawPath)
  642. if err := waitFor("folder path missing"); err != nil {
  643. t.Error(err)
  644. return
  645. }
  646. }
  647. func TestGlobalDirectoryTree(t *testing.T) {
  648. db := db.OpenMemory()
  649. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  650. m.AddFolder(defaultFolderConfig)
  651. m.ServeBackground()
  652. b := func(isfile bool, path ...string) protocol.FileInfo {
  653. flags := uint32(protocol.FlagDirectory)
  654. blocks := []protocol.BlockInfo{}
  655. if isfile {
  656. flags = 0
  657. 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}}}
  658. }
  659. return protocol.FileInfo{
  660. Name: filepath.Join(path...),
  661. Flags: flags,
  662. Modified: 0x666,
  663. Blocks: blocks,
  664. }
  665. }
  666. filedata := []interface{}{time.Unix(0x666, 0), 0xa}
  667. testdata := []protocol.FileInfo{
  668. b(false, "another"),
  669. b(false, "another", "directory"),
  670. b(true, "another", "directory", "afile"),
  671. b(false, "another", "directory", "with"),
  672. b(false, "another", "directory", "with", "a"),
  673. b(true, "another", "directory", "with", "a", "file"),
  674. b(true, "another", "directory", "with", "file"),
  675. b(true, "another", "file"),
  676. b(false, "other"),
  677. b(false, "other", "rand"),
  678. b(false, "other", "random"),
  679. b(false, "other", "random", "dir"),
  680. b(false, "other", "random", "dirx"),
  681. b(false, "other", "randomx"),
  682. b(false, "some"),
  683. b(false, "some", "directory"),
  684. b(false, "some", "directory", "with"),
  685. b(false, "some", "directory", "with", "a"),
  686. b(true, "some", "directory", "with", "a", "file"),
  687. b(true, "rootfile"),
  688. }
  689. expectedResult := map[string]interface{}{
  690. "another": map[string]interface{}{
  691. "directory": map[string]interface{}{
  692. "afile": filedata,
  693. "with": map[string]interface{}{
  694. "a": map[string]interface{}{
  695. "file": filedata,
  696. },
  697. "file": filedata,
  698. },
  699. },
  700. "file": filedata,
  701. },
  702. "other": map[string]interface{}{
  703. "rand": map[string]interface{}{},
  704. "random": map[string]interface{}{
  705. "dir": map[string]interface{}{},
  706. "dirx": map[string]interface{}{},
  707. },
  708. "randomx": map[string]interface{}{},
  709. },
  710. "some": map[string]interface{}{
  711. "directory": map[string]interface{}{
  712. "with": map[string]interface{}{
  713. "a": map[string]interface{}{
  714. "file": filedata,
  715. },
  716. },
  717. },
  718. },
  719. "rootfile": filedata,
  720. }
  721. mm := func(data interface{}) string {
  722. bytes, err := json.Marshal(data)
  723. if err != nil {
  724. panic(err)
  725. }
  726. return string(bytes)
  727. }
  728. m.Index(device1, "default", testdata, 0, nil)
  729. result := m.GlobalDirectoryTree("default", "", -1, false)
  730. if mm(result) != mm(expectedResult) {
  731. t.Errorf("Does not match:\n%#v\n%#v", result, expectedResult)
  732. }
  733. result = m.GlobalDirectoryTree("default", "another", -1, false)
  734. if mm(result) != mm(expectedResult["another"]) {
  735. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult["another"]))
  736. }
  737. result = m.GlobalDirectoryTree("default", "", 0, false)
  738. currentResult := map[string]interface{}{
  739. "another": map[string]interface{}{},
  740. "other": map[string]interface{}{},
  741. "some": map[string]interface{}{},
  742. "rootfile": filedata,
  743. }
  744. if mm(result) != mm(currentResult) {
  745. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  746. }
  747. result = m.GlobalDirectoryTree("default", "", 1, false)
  748. currentResult = map[string]interface{}{
  749. "another": map[string]interface{}{
  750. "directory": map[string]interface{}{},
  751. "file": filedata,
  752. },
  753. "other": map[string]interface{}{
  754. "rand": map[string]interface{}{},
  755. "random": map[string]interface{}{},
  756. "randomx": map[string]interface{}{},
  757. },
  758. "some": map[string]interface{}{
  759. "directory": map[string]interface{}{},
  760. },
  761. "rootfile": filedata,
  762. }
  763. if mm(result) != mm(currentResult) {
  764. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  765. }
  766. result = m.GlobalDirectoryTree("default", "", -1, true)
  767. currentResult = map[string]interface{}{
  768. "another": map[string]interface{}{
  769. "directory": map[string]interface{}{
  770. "with": map[string]interface{}{
  771. "a": map[string]interface{}{},
  772. },
  773. },
  774. },
  775. "other": map[string]interface{}{
  776. "rand": map[string]interface{}{},
  777. "random": map[string]interface{}{
  778. "dir": map[string]interface{}{},
  779. "dirx": map[string]interface{}{},
  780. },
  781. "randomx": map[string]interface{}{},
  782. },
  783. "some": map[string]interface{}{
  784. "directory": map[string]interface{}{
  785. "with": map[string]interface{}{
  786. "a": map[string]interface{}{},
  787. },
  788. },
  789. },
  790. }
  791. if mm(result) != mm(currentResult) {
  792. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  793. }
  794. result = m.GlobalDirectoryTree("default", "", 1, true)
  795. currentResult = map[string]interface{}{
  796. "another": map[string]interface{}{
  797. "directory": map[string]interface{}{},
  798. },
  799. "other": map[string]interface{}{
  800. "rand": map[string]interface{}{},
  801. "random": map[string]interface{}{},
  802. "randomx": map[string]interface{}{},
  803. },
  804. "some": map[string]interface{}{
  805. "directory": map[string]interface{}{},
  806. },
  807. }
  808. if mm(result) != mm(currentResult) {
  809. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  810. }
  811. result = m.GlobalDirectoryTree("default", "another", 0, false)
  812. currentResult = map[string]interface{}{
  813. "directory": map[string]interface{}{},
  814. "file": filedata,
  815. }
  816. if mm(result) != mm(currentResult) {
  817. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  818. }
  819. result = m.GlobalDirectoryTree("default", "some/directory", 0, false)
  820. currentResult = map[string]interface{}{
  821. "with": map[string]interface{}{},
  822. }
  823. if mm(result) != mm(currentResult) {
  824. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  825. }
  826. result = m.GlobalDirectoryTree("default", "some/directory", 1, false)
  827. currentResult = map[string]interface{}{
  828. "with": map[string]interface{}{
  829. "a": map[string]interface{}{},
  830. },
  831. }
  832. if mm(result) != mm(currentResult) {
  833. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  834. }
  835. result = m.GlobalDirectoryTree("default", "some/directory", 2, false)
  836. currentResult = map[string]interface{}{
  837. "with": map[string]interface{}{
  838. "a": map[string]interface{}{
  839. "file": filedata,
  840. },
  841. },
  842. }
  843. if mm(result) != mm(currentResult) {
  844. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  845. }
  846. result = m.GlobalDirectoryTree("default", "another", -1, true)
  847. currentResult = map[string]interface{}{
  848. "directory": map[string]interface{}{
  849. "with": map[string]interface{}{
  850. "a": map[string]interface{}{},
  851. },
  852. },
  853. }
  854. if mm(result) != mm(currentResult) {
  855. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  856. }
  857. // No prefix matching!
  858. result = m.GlobalDirectoryTree("default", "som", -1, false)
  859. currentResult = map[string]interface{}{}
  860. if mm(result) != mm(currentResult) {
  861. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  862. }
  863. }
  864. func TestGlobalDirectorySelfFixing(t *testing.T) {
  865. db := db.OpenMemory()
  866. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  867. m.AddFolder(defaultFolderConfig)
  868. m.ServeBackground()
  869. b := func(isfile bool, path ...string) protocol.FileInfo {
  870. flags := uint32(protocol.FlagDirectory)
  871. blocks := []protocol.BlockInfo{}
  872. if isfile {
  873. flags = 0
  874. 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}}}
  875. }
  876. return protocol.FileInfo{
  877. Name: filepath.Join(path...),
  878. Flags: flags,
  879. Modified: 0x666,
  880. Blocks: blocks,
  881. }
  882. }
  883. filedata := []interface{}{time.Unix(0x666, 0).Format(time.RFC3339), 0xa}
  884. testdata := []protocol.FileInfo{
  885. b(true, "another", "directory", "afile"),
  886. b(true, "another", "directory", "with", "a", "file"),
  887. b(true, "another", "directory", "with", "file"),
  888. b(false, "other", "random", "dirx"),
  889. b(false, "other", "randomx"),
  890. b(false, "some", "directory", "with", "x"),
  891. b(true, "some", "directory", "with", "a", "file"),
  892. b(false, "this", "is", "a", "deep", "invalid", "directory"),
  893. b(true, "xthis", "is", "a", "deep", "invalid", "file"),
  894. }
  895. expectedResult := map[string]interface{}{
  896. "another": map[string]interface{}{
  897. "directory": map[string]interface{}{
  898. "afile": filedata,
  899. "with": map[string]interface{}{
  900. "a": map[string]interface{}{
  901. "file": filedata,
  902. },
  903. "file": filedata,
  904. },
  905. },
  906. },
  907. "other": map[string]interface{}{
  908. "random": map[string]interface{}{
  909. "dirx": map[string]interface{}{},
  910. },
  911. "randomx": map[string]interface{}{},
  912. },
  913. "some": map[string]interface{}{
  914. "directory": map[string]interface{}{
  915. "with": map[string]interface{}{
  916. "a": map[string]interface{}{
  917. "file": filedata,
  918. },
  919. "x": map[string]interface{}{},
  920. },
  921. },
  922. },
  923. "this": map[string]interface{}{
  924. "is": map[string]interface{}{
  925. "a": map[string]interface{}{
  926. "deep": map[string]interface{}{
  927. "invalid": map[string]interface{}{
  928. "directory": map[string]interface{}{},
  929. },
  930. },
  931. },
  932. },
  933. },
  934. "xthis": map[string]interface{}{
  935. "is": map[string]interface{}{
  936. "a": map[string]interface{}{
  937. "deep": map[string]interface{}{
  938. "invalid": map[string]interface{}{
  939. "file": filedata,
  940. },
  941. },
  942. },
  943. },
  944. },
  945. }
  946. mm := func(data interface{}) string {
  947. bytes, err := json.Marshal(data)
  948. if err != nil {
  949. panic(err)
  950. }
  951. return string(bytes)
  952. }
  953. m.Index(device1, "default", testdata, 0, nil)
  954. result := m.GlobalDirectoryTree("default", "", -1, false)
  955. if mm(result) != mm(expectedResult) {
  956. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(expectedResult))
  957. }
  958. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, false)
  959. currentResult := map[string]interface{}{
  960. "invalid": map[string]interface{}{
  961. "file": filedata,
  962. },
  963. }
  964. if mm(result) != mm(currentResult) {
  965. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  966. }
  967. result = m.GlobalDirectoryTree("default", "xthis/is/a/deep", -1, true)
  968. currentResult = map[string]interface{}{
  969. "invalid": map[string]interface{}{},
  970. }
  971. if mm(result) != mm(currentResult) {
  972. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  973. }
  974. // !!! This is actually BAD, because we don't have enough level allowance
  975. // to accept this file, hence the tree is left unbuilt !!!
  976. result = m.GlobalDirectoryTree("default", "xthis", 1, false)
  977. currentResult = map[string]interface{}{}
  978. if mm(result) != mm(currentResult) {
  979. t.Errorf("Does not match:\n%s\n%s", mm(result), mm(currentResult))
  980. }
  981. }
  982. func genDeepFiles(n, d int) []protocol.FileInfo {
  983. rand.Seed(int64(n))
  984. files := make([]protocol.FileInfo, n)
  985. t := time.Now().Unix()
  986. for i := 0; i < n; i++ {
  987. path := ""
  988. for i := 0; i <= d; i++ {
  989. path = filepath.Join(path, strconv.Itoa(rand.Int()))
  990. }
  991. sofar := ""
  992. for _, path := range filepath.SplitList(path) {
  993. sofar = filepath.Join(sofar, path)
  994. files[i] = protocol.FileInfo{
  995. Name: sofar,
  996. }
  997. i++
  998. }
  999. files[i].Modified = t
  1000. files[i].Blocks = []protocol.BlockInfo{{0, 100, []byte("some hash bytes")}}
  1001. }
  1002. return files
  1003. }
  1004. func BenchmarkTree_10000_50(b *testing.B) {
  1005. benchmarkTree(b, 10000, 50)
  1006. }
  1007. func BenchmarkTree_100_50(b *testing.B) {
  1008. benchmarkTree(b, 100, 50)
  1009. }
  1010. func BenchmarkTree_100_10(b *testing.B) {
  1011. benchmarkTree(b, 100, 10)
  1012. }
  1013. func benchmarkTree(b *testing.B, n1, n2 int) {
  1014. db := db.OpenMemory()
  1015. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  1016. m.AddFolder(defaultFolderConfig)
  1017. m.ServeBackground()
  1018. m.ScanFolder("default")
  1019. files := genDeepFiles(n1, n2)
  1020. m.Index(device1, "default", files, 0, nil)
  1021. b.ResetTimer()
  1022. for i := 0; i < b.N; i++ {
  1023. m.GlobalDirectoryTree("default", "", -1, false)
  1024. }
  1025. b.ReportAllocs()
  1026. }
  1027. func TestIgnoreDelete(t *testing.T) {
  1028. db := db.OpenMemory()
  1029. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  1030. // This folder should ignore external deletes
  1031. cfg := defaultFolderConfig
  1032. cfg.IgnoreDelete = true
  1033. m.AddFolder(cfg)
  1034. m.ServeBackground()
  1035. m.StartFolder("default")
  1036. m.ScanFolder("default")
  1037. // Get a currently existing file
  1038. f, ok := m.CurrentGlobalFile("default", "foo")
  1039. if !ok {
  1040. t.Fatal("foo should exist")
  1041. }
  1042. // Mark it for deletion
  1043. f.Flags = protocol.FlagDeleted
  1044. f.Version = f.Version.Update(142) // arbitrary short remote ID
  1045. f.Blocks = nil
  1046. // Send the index
  1047. m.Index(device1, "default", []protocol.FileInfo{f}, 0, nil)
  1048. // Make sure we ignored it
  1049. f, ok = m.CurrentGlobalFile("default", "foo")
  1050. if !ok {
  1051. t.Fatal("foo should exist")
  1052. }
  1053. if f.IsDeleted() {
  1054. t.Fatal("foo should not be marked for deletion")
  1055. }
  1056. }
  1057. func TestUnifySubs(t *testing.T) {
  1058. cases := []struct {
  1059. in []string // input to unifySubs
  1060. exists []string // paths that exist in the database
  1061. out []string // expected output
  1062. }{
  1063. {
  1064. // 0. trailing slashes are cleaned, known paths are just passed on
  1065. []string{"foo/", "bar//"},
  1066. []string{"foo", "bar"},
  1067. []string{"bar", "foo"}, // the output is sorted
  1068. },
  1069. {
  1070. // 1. "foo/bar" gets trimmed as it's covered by foo
  1071. []string{"foo", "bar/", "foo/bar/"},
  1072. []string{"foo", "bar"},
  1073. []string{"bar", "foo"},
  1074. },
  1075. {
  1076. // 2. "" gets simplified to the empty list; ie scan all
  1077. []string{"foo", ""},
  1078. []string{"foo"},
  1079. nil,
  1080. },
  1081. {
  1082. // 3. "foo/bar" is unknown, but it's kept
  1083. // because its parent is known
  1084. []string{"foo/bar"},
  1085. []string{"foo"},
  1086. []string{"foo/bar"},
  1087. },
  1088. {
  1089. // 4. two independent known paths, both are kept
  1090. // "usr/lib" is not a prefix of "usr/libexec"
  1091. []string{"usr/lib", "usr/libexec"},
  1092. []string{"usr", "usr/lib", "usr/libexec"},
  1093. []string{"usr/lib", "usr/libexec"},
  1094. },
  1095. {
  1096. // 5. "usr/lib" is a prefix of "usr/lib/exec"
  1097. []string{"usr/lib", "usr/lib/exec"},
  1098. []string{"usr", "usr/lib", "usr/libexec"},
  1099. []string{"usr/lib"},
  1100. },
  1101. {
  1102. // 6. .stignore and .stfolder are special and are passed on
  1103. // verbatim even though they are unknown
  1104. []string{".stfolder", ".stignore"},
  1105. []string{},
  1106. []string{".stfolder", ".stignore"},
  1107. },
  1108. {
  1109. // 7. but the presense of something else unknown forces an actual
  1110. // scan
  1111. []string{".stfolder", ".stignore", "foo/bar"},
  1112. []string{},
  1113. []string{".stfolder", ".stignore", "foo"},
  1114. },
  1115. {
  1116. // 8. explicit request to scan all
  1117. nil,
  1118. []string{"foo"},
  1119. nil,
  1120. },
  1121. {
  1122. // 9. empty list of subs
  1123. []string{},
  1124. []string{"foo"},
  1125. nil,
  1126. },
  1127. }
  1128. if runtime.GOOS == "windows" {
  1129. // Fixup path separators
  1130. for i := range cases {
  1131. for j, p := range cases[i].in {
  1132. cases[i].in[j] = filepath.FromSlash(p)
  1133. }
  1134. for j, p := range cases[i].exists {
  1135. cases[i].exists[j] = filepath.FromSlash(p)
  1136. }
  1137. for j, p := range cases[i].out {
  1138. cases[i].out[j] = filepath.FromSlash(p)
  1139. }
  1140. }
  1141. }
  1142. for i, tc := range cases {
  1143. exists := func(f string) bool {
  1144. for _, e := range tc.exists {
  1145. if f == e {
  1146. return true
  1147. }
  1148. }
  1149. return false
  1150. }
  1151. out := unifySubs(tc.in, exists)
  1152. if diff, equal := messagediff.PrettyDiff(tc.out, out); !equal {
  1153. t.Errorf("Case %d failed; got %v, expected %v, diff:\n%s", i, out, tc.out, diff)
  1154. }
  1155. }
  1156. }