walk_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 scanner
  7. import (
  8. "bytes"
  9. "crypto/rand"
  10. "fmt"
  11. "io"
  12. "os"
  13. "path/filepath"
  14. "reflect"
  15. "runtime"
  16. rdebug "runtime/debug"
  17. "sort"
  18. "sync"
  19. "testing"
  20. "github.com/syncthing/syncthing/lib/ignore"
  21. "github.com/syncthing/syncthing/lib/osutil"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "github.com/syncthing/syncthing/lib/symlinks"
  24. "golang.org/x/text/unicode/norm"
  25. )
  26. type testfile struct {
  27. name string
  28. size int
  29. hash string
  30. }
  31. type testfileList []testfile
  32. var testdata = testfileList{
  33. {"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
  34. {"dir1", 128, ""},
  35. {filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
  36. {"dir2", 128, ""},
  37. {filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
  38. {"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
  39. {"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
  40. }
  41. var correctIgnores = map[string][]string{
  42. ".": {".*", "quux"},
  43. }
  44. func init() {
  45. // This test runs the risk of entering infinite recursion if it fails.
  46. // Limit the stack size to 10 megs to crash early in that case instead of
  47. // potentially taking down the box...
  48. rdebug.SetMaxStack(10 * 1 << 20)
  49. }
  50. func TestWalkSub(t *testing.T) {
  51. ignores := ignore.New(false)
  52. err := ignores.Load("testdata/.stignore")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. w := Walker{
  57. Dir: "testdata",
  58. Subs: []string{"dir2"},
  59. BlockSize: 128 * 1024,
  60. Matcher: ignores,
  61. Hashers: 2,
  62. }
  63. fchan, err := w.Walk()
  64. var files []protocol.FileInfo
  65. for f := range fchan {
  66. files = append(files, f)
  67. }
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. // The directory contains two files, where one is ignored from a higher
  72. // level. We should see only the directory and one of the files.
  73. if len(files) != 2 {
  74. t.Fatalf("Incorrect length %d != 2", len(files))
  75. }
  76. if files[0].Name != "dir2" {
  77. t.Errorf("Incorrect file %v != dir2", files[0])
  78. }
  79. if files[1].Name != filepath.Join("dir2", "cfile") {
  80. t.Errorf("Incorrect file %v != dir2/cfile", files[1])
  81. }
  82. }
  83. func TestWalk(t *testing.T) {
  84. ignores := ignore.New(false)
  85. err := ignores.Load("testdata/.stignore")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. t.Log(ignores)
  90. w := Walker{
  91. Dir: "testdata",
  92. BlockSize: 128 * 1024,
  93. Matcher: ignores,
  94. Hashers: 2,
  95. }
  96. fchan, err := w.Walk()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. var tmp []protocol.FileInfo
  101. for f := range fchan {
  102. tmp = append(tmp, f)
  103. }
  104. sort.Sort(fileList(tmp))
  105. files := fileList(tmp).testfiles()
  106. if !reflect.DeepEqual(files, testdata) {
  107. t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
  108. }
  109. }
  110. func TestWalkError(t *testing.T) {
  111. w := Walker{
  112. Dir: "testdata-missing",
  113. BlockSize: 128 * 1024,
  114. Hashers: 2,
  115. }
  116. _, err := w.Walk()
  117. if err == nil {
  118. t.Error("no error from missing directory")
  119. }
  120. w = Walker{
  121. Dir: "testdata/bar",
  122. BlockSize: 128 * 1024,
  123. }
  124. _, err = w.Walk()
  125. if err == nil {
  126. t.Error("no error from non-directory")
  127. }
  128. }
  129. func TestVerify(t *testing.T) {
  130. blocksize := 16
  131. // data should be an even multiple of blocksize long
  132. data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
  133. buf := bytes.NewBuffer(data)
  134. progress := newByteCounter()
  135. defer progress.Close()
  136. blocks, err := Blocks(buf, blocksize, 0, progress)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. if exp := len(data) / blocksize; len(blocks) != exp {
  141. t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
  142. }
  143. if int64(len(data)) != progress.Total() {
  144. t.Fatalf("Incorrect counter value %d != %d", len(data), progress.Total())
  145. }
  146. buf = bytes.NewBuffer(data)
  147. err = Verify(buf, blocksize, blocks)
  148. t.Log(err)
  149. if err != nil {
  150. t.Fatal("Unexpected verify failure", err)
  151. }
  152. buf = bytes.NewBuffer(append(data, '\n'))
  153. err = Verify(buf, blocksize, blocks)
  154. t.Log(err)
  155. if err == nil {
  156. t.Fatal("Unexpected verify success")
  157. }
  158. buf = bytes.NewBuffer(data[:len(data)-1])
  159. err = Verify(buf, blocksize, blocks)
  160. t.Log(err)
  161. if err == nil {
  162. t.Fatal("Unexpected verify success")
  163. }
  164. data[42] = 42
  165. buf = bytes.NewBuffer(data)
  166. err = Verify(buf, blocksize, blocks)
  167. t.Log(err)
  168. if err == nil {
  169. t.Fatal("Unexpected verify success")
  170. }
  171. }
  172. func TestNormalization(t *testing.T) {
  173. if runtime.GOOS == "darwin" {
  174. t.Skip("Normalization test not possible on darwin")
  175. return
  176. }
  177. os.RemoveAll("testdata/normalization")
  178. defer os.RemoveAll("testdata/normalization")
  179. tests := []string{
  180. "0-A", // ASCII A -- accepted
  181. "1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
  182. "1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
  183. "2-\xC3\x85", // NFC 'Å' -- accepted
  184. "3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
  185. "4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
  186. "5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
  187. }
  188. numInvalid := 2
  189. if runtime.GOOS == "windows" {
  190. // On Windows, in case 5 the character gets replaced with a
  191. // replacement character \xEF\xBF\xBD at the point it's written to disk,
  192. // which means it suddenly becomes valid (sort of).
  193. numInvalid--
  194. }
  195. numValid := len(tests) - numInvalid
  196. for _, s1 := range tests {
  197. // Create a directory for each of the interesting strings above
  198. if err := osutil.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
  199. t.Fatal(err)
  200. }
  201. for _, s2 := range tests {
  202. // Within each dir, create a file with each of the interesting
  203. // file names. Ensure that the file doesn't exist when it's
  204. // created. This detects and fails if there's file name
  205. // normalization stuff at the filesystem level.
  206. if fd, err := os.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
  207. t.Fatal(err)
  208. } else {
  209. fd.WriteString("test")
  210. fd.Close()
  211. }
  212. }
  213. }
  214. // We can normalize a directory name, but we can't descend into it in the
  215. // same pass due to how filepath.Walk works. So we run the scan twice to
  216. // make sure it all gets done. In production, things will be correct
  217. // eventually...
  218. _, err := walkDir("testdata/normalization")
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. tmp, err := walkDir("testdata/normalization")
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. files := fileList(tmp).testfiles()
  227. // We should have one file per combination, plus the directories
  228. // themselves
  229. expectedNum := numValid*numValid + numValid
  230. if len(files) != expectedNum {
  231. t.Errorf("Expected %d files, got %d", expectedNum, len(files))
  232. }
  233. // The file names should all be in NFC form.
  234. for _, f := range files {
  235. t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
  236. if !norm.NFC.IsNormalString(f.name) {
  237. t.Errorf("File name %q is not NFC normalized", f.name)
  238. }
  239. }
  240. }
  241. func TestIssue1507(t *testing.T) {
  242. w := Walker{}
  243. c := make(chan protocol.FileInfo, 100)
  244. fn := w.walkAndHashFiles(c, c)
  245. fn("", nil, protocol.ErrClosed)
  246. }
  247. func walkDir(dir string) ([]protocol.FileInfo, error) {
  248. w := Walker{
  249. Dir: dir,
  250. BlockSize: 128 * 1024,
  251. AutoNormalize: true,
  252. Hashers: 2,
  253. }
  254. fchan, err := w.Walk()
  255. if err != nil {
  256. return nil, err
  257. }
  258. var tmp []protocol.FileInfo
  259. for f := range fchan {
  260. tmp = append(tmp, f)
  261. }
  262. sort.Sort(fileList(tmp))
  263. return tmp, nil
  264. }
  265. type fileList []protocol.FileInfo
  266. func (l fileList) Len() int {
  267. return len(l)
  268. }
  269. func (l fileList) Less(a, b int) bool {
  270. return l[a].Name < l[b].Name
  271. }
  272. func (l fileList) Swap(a, b int) {
  273. l[a], l[b] = l[b], l[a]
  274. }
  275. func (l fileList) testfiles() testfileList {
  276. testfiles := make(testfileList, len(l))
  277. for i, f := range l {
  278. if len(f.Blocks) > 1 {
  279. panic("simple test case stuff only supports a single block per file")
  280. }
  281. testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
  282. if len(f.Blocks) == 1 {
  283. testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
  284. }
  285. }
  286. return testfiles
  287. }
  288. func (l testfileList) String() string {
  289. var b bytes.Buffer
  290. b.WriteString("{\n")
  291. for _, f := range l {
  292. fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
  293. }
  294. b.WriteString("}")
  295. return b.String()
  296. }
  297. func TestSymlinkTypeEqual(t *testing.T) {
  298. testcases := []struct {
  299. onDiskType symlinks.TargetType
  300. inIndexFlags uint32
  301. equal bool
  302. }{
  303. // File is only equal to file
  304. {symlinks.TargetFile, 0, true},
  305. {symlinks.TargetFile, protocol.FlagDirectory, false},
  306. {symlinks.TargetFile, protocol.FlagSymlinkMissingTarget, false},
  307. // Directory is only equal to directory
  308. {symlinks.TargetDirectory, 0, false},
  309. {symlinks.TargetDirectory, protocol.FlagDirectory, true},
  310. {symlinks.TargetDirectory, protocol.FlagSymlinkMissingTarget, false},
  311. // Unknown is equal to anything
  312. {symlinks.TargetUnknown, 0, true},
  313. {symlinks.TargetUnknown, protocol.FlagDirectory, true},
  314. {symlinks.TargetUnknown, protocol.FlagSymlinkMissingTarget, true},
  315. }
  316. for _, tc := range testcases {
  317. res := SymlinkTypeEqual(tc.onDiskType, protocol.FileInfo{Flags: tc.inIndexFlags})
  318. if res != tc.equal {
  319. t.Errorf("Incorrect result %v for %v, %v", res, tc.onDiskType, tc.inIndexFlags)
  320. }
  321. }
  322. }
  323. var initOnce sync.Once
  324. const (
  325. testdataSize = 17 << 20
  326. testdataName = "_random.data"
  327. )
  328. func BenchmarkHashFile(b *testing.B) {
  329. initOnce.Do(initTestFile)
  330. b.ResetTimer()
  331. for i := 0; i < b.N; i++ {
  332. if _, err := HashFile(testdataName, protocol.BlockSize, testdataSize, nil); err != nil {
  333. b.Fatal(err)
  334. }
  335. }
  336. b.ReportAllocs()
  337. }
  338. func initTestFile() {
  339. fd, err := os.Create(testdataName)
  340. if err != nil {
  341. panic(err)
  342. }
  343. lr := io.LimitReader(rand.Reader, testdataSize)
  344. if _, err := io.Copy(fd, lr); err != nil {
  345. panic(err)
  346. }
  347. if err := fd.Close(); err != nil {
  348. panic(err)
  349. }
  350. }