util_test.go 3.9 KB

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