1
0

util_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "math/rand"
  9. "testing"
  10. "unicode"
  11. "unicode/utf8"
  12. "github.com/syncthing/syncthing/lib/build"
  13. )
  14. func TestCommonPrefix(t *testing.T) {
  15. test := func(first, second, expect string) {
  16. t.Helper()
  17. res := CommonPrefix(first, second)
  18. if res != expect {
  19. t.Errorf("Expected %s got %s", expect, res)
  20. }
  21. }
  22. if build.IsWindows {
  23. test(`c:\Audrius\Downloads`, `c:\Audrius\Docs`, `c:\Audrius`)
  24. test(`c:\Audrius\Downloads`, `C:\Audrius\Docs`, ``) // Case differences :(
  25. test(`C:\Audrius-a\Downloads`, `C:\Audrius-b\Docs`, `C:\`)
  26. test(`\\?\C:\Audrius-a\Downloads`, `\\?\C:\Audrius-b\Docs`, `\\?\C:\`)
  27. test(`\\?\C:\Audrius\Downloads`, `\\?\C:\Audrius\Docs`, `\\?\C:\Audrius`)
  28. test(`Audrius-a\Downloads`, `Audrius-b\Docs`, ``)
  29. test(`Audrius\Downloads`, `Audrius\Docs`, `Audrius`)
  30. test(`c:\Audrius\Downloads`, `Audrius\Docs`, ``)
  31. test(`c:\`, `c:\`, `c:\`)
  32. test(`\\?\c:\`, `\\?\c:\`, `\\?\c:\`)
  33. } else {
  34. test(`/Audrius/Downloads`, `/Audrius/Docs`, `/Audrius`)
  35. test(`/Audrius\Downloads`, `/Audrius\Docs`, `/`)
  36. test(`/Audrius-a/Downloads`, `/Audrius-b/Docs`, `/`)
  37. test(`Audrius\Downloads`, `Audrius\Docs`, ``) // Windows separators
  38. test(`Audrius/Downloads`, `Audrius/Docs`, `Audrius`)
  39. test(`Audrius-a\Downloads`, `Audrius-b\Docs`, ``)
  40. test(`/Audrius/Downloads`, `Audrius/Docs`, ``)
  41. test(`/`, `/`, `/`)
  42. }
  43. test(`Audrius`, `Audrius`, `Audrius`)
  44. test(`.`, `.`, `.`)
  45. }
  46. func TestWindowsInvalidFilename(t *testing.T) {
  47. cases := []struct {
  48. name string
  49. err error
  50. }{
  51. {`asdf.txt`, nil},
  52. {`nul`, errInvalidFilenameWindowsReservedName},
  53. {`nul.txt`, errInvalidFilenameWindowsReservedName},
  54. {`nul.jpg.txt`, errInvalidFilenameWindowsReservedName},
  55. {`some.nul.jpg`, nil},
  56. {`foo>bar.txt`, errInvalidFilenameWindowsReservedChar},
  57. {`foo \bar.txt`, errInvalidFilenameWindowsSpacePeriod},
  58. {`foo.\bar.txt`, errInvalidFilenameWindowsSpacePeriod},
  59. {`foo.d\bar.txt`, nil},
  60. {`foo.d\bar .txt`, nil},
  61. {`foo.d\bar. txt`, nil},
  62. }
  63. for _, tc := range cases {
  64. err := WindowsInvalidFilename(tc.name)
  65. if err != tc.err {
  66. t.Errorf("For %q, got %v, expected %v", tc.name, err, tc.err)
  67. }
  68. }
  69. }
  70. func TestSanitizePath(t *testing.T) {
  71. cases := [][2]string{
  72. {"", ""},
  73. {"foo", "foo"},
  74. {`\*/foo\?/bar[{!@$%^&*#()}]`, "foo bar ()"},
  75. {"Räksmörgås", "Räksmörgås"},
  76. {`Räk \/ smörgås`, "Räk smörgås"},
  77. {"هذا هو *\x07?اسم الملف", "هذا هو اسم الملف"},
  78. {`../foo.txt`, `.. foo.txt`},
  79. {" \t \n filename in \t space\r", "filename in space"},
  80. {"你\xff好", `你 好`},
  81. {"\000 foo", "foo"},
  82. }
  83. for _, tc := range cases {
  84. res := SanitizePath(tc[0])
  85. if res != tc[1] {
  86. t.Errorf("SanitizePath(%q) => %q, expected %q", tc[0], res, tc[1])
  87. }
  88. }
  89. }
  90. // Fuzz test: SanitizePath must always return strings of printable UTF-8
  91. // characters when fed random data.
  92. //
  93. // Note that space is considered printable, but other whitespace runes are not.
  94. func TestSanitizePathFuzz(t *testing.T) {
  95. buf := make([]byte, 128)
  96. for i := 0; i < 100; i++ {
  97. rand.Read(buf)
  98. path := SanitizePath(string(buf))
  99. if !utf8.ValidString(path) {
  100. t.Errorf("SanitizePath(%q) => %q, not valid UTF-8", buf, path)
  101. continue
  102. }
  103. for _, c := range path {
  104. if !unicode.IsPrint(c) {
  105. t.Errorf("non-printable rune %q in sanitized path", c)
  106. }
  107. }
  108. }
  109. }
  110. func benchmarkWindowsInvalidFilename(b *testing.B, name string) {
  111. for i := 0; i < b.N; i++ {
  112. WindowsInvalidFilename(name)
  113. }
  114. }
  115. func BenchmarkWindowsInvalidFilenameValid(b *testing.B) {
  116. benchmarkWindowsInvalidFilename(b, "License.txt.gz")
  117. }
  118. func BenchmarkWindowsInvalidFilenameNUL(b *testing.B) {
  119. benchmarkWindowsInvalidFilename(b, "nul.txt.gz")
  120. }