basicfs_windows_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (C) 2018 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. //go:build windows
  7. // +build windows
  8. package fs
  9. import (
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "syscall"
  14. "testing"
  15. )
  16. func TestWindowsPaths(t *testing.T) {
  17. type testCase struct {
  18. input string
  19. expectedRoot string
  20. expectedURI string
  21. }
  22. testCases := []testCase{
  23. {`e:`, `\\?\e:\`, `e:\`},
  24. {`e:\`, `\\?\e:\`, `e:\`},
  25. {`e:\\`, `\\?\e:\`, `e:\`},
  26. {`\\?\e:`, `\\?\e:\`, `e:\`},
  27. {`\\?\e:\`, `\\?\e:\`, `e:\`},
  28. {`\\?\e:\\`, `\\?\e:\`, `e:\`},
  29. {`e:\x`, `\\?\e:\x`, `e:\x`},
  30. {`e:\x\`, `\\?\e:\x`, `e:\x`},
  31. {`e:\x\\`, `\\?\e:\x`, `e:\x`},
  32. {`\\192.0.2.22\network\share`, `\\192.0.2.22\network\share`, `\\192.0.2.22\network\share`},
  33. }
  34. for i, testCase := range testCases {
  35. fs := newBasicFilesystem(testCase.input)
  36. if fs.root != testCase.expectedRoot {
  37. t.Errorf("test %d: root: expected `%s`, got `%s`", i, testCase.expectedRoot, fs.root)
  38. }
  39. if fs.URI() != testCase.expectedURI {
  40. t.Errorf("test %d: uri: expected `%s`, got `%s`", i, testCase.expectedURI, fs.URI())
  41. }
  42. }
  43. fs := newBasicFilesystem(`relative\path`)
  44. if fs.root == `relative\path` || !strings.HasPrefix(fs.root, "\\\\?\\") {
  45. t.Errorf("%q == %q, expected absolutification", fs.root, `relative\path`)
  46. }
  47. }
  48. func TestResolveWindows83(t *testing.T) {
  49. fs, dir := setup(t)
  50. if isMaybeWin83(dir) {
  51. dir = fs.resolveWin83(dir)
  52. fs = newBasicFilesystem(dir)
  53. }
  54. shortAbs, _ := fs.rooted("LFDATA~1")
  55. long := "LFDataTool"
  56. longAbs, _ := fs.rooted(long)
  57. deleted, _ := fs.rooted(filepath.Join("foo", "LFDATA~1"))
  58. notShort, _ := fs.rooted(filepath.Join("foo", "bar", "baz"))
  59. fd, err := fs.Create(long)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. fd.Close()
  64. if res := fs.resolveWin83(shortAbs); res != longAbs {
  65. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, shortAbs, res, longAbs)
  66. }
  67. if res := fs.resolveWin83(deleted); res != filepath.Dir(deleted) {
  68. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, deleted, res, filepath.Dir(deleted))
  69. }
  70. if res := fs.resolveWin83(notShort); res != notShort {
  71. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, notShort, res, notShort)
  72. }
  73. }
  74. func TestIsWindows83(t *testing.T) {
  75. fs, dir := setup(t)
  76. if isMaybeWin83(dir) {
  77. dir = fs.resolveWin83(dir)
  78. fs = newBasicFilesystem(dir)
  79. }
  80. tempTop, _ := fs.rooted(TempName("baz"))
  81. tempBelow, _ := fs.rooted(filepath.Join("foo", "bar", TempName("baz")))
  82. short, _ := fs.rooted(filepath.Join("LFDATA~1", TempName("baz")))
  83. tempAndShort, _ := fs.rooted(filepath.Join("LFDATA~1", TempName("baz")))
  84. for _, f := range []string{tempTop, tempBelow} {
  85. if isMaybeWin83(f) {
  86. t.Errorf(`"%v" is not a windows 8.3 path"`, f)
  87. }
  88. }
  89. for _, f := range []string{short, tempAndShort} {
  90. if !isMaybeWin83(f) {
  91. t.Errorf(`"%v" is not a windows 8.3 path"`, f)
  92. }
  93. }
  94. }
  95. func TestRelUnrootedCheckedWindows(t *testing.T) {
  96. testCases := []struct {
  97. root string
  98. abs string
  99. expectedRel string
  100. }{
  101. {`c:\`, `c:\foo`, `foo`},
  102. {`C:\`, `c:\foo`, `foo`},
  103. {`C:\`, `C:\foo`, `foo`},
  104. {`c:\`, `C:\foo`, `foo`},
  105. {`\\?c:\`, `\\?c:\foo`, `foo`},
  106. {`\\?C:\`, `\\?c:\foo`, `foo`},
  107. {`\\?C:\`, `\\?C:\foo`, `foo`},
  108. {`\\?c:\`, `\\?C:\foo`, `foo`},
  109. {`c:\foo`, `c:\foo\bar`, `bar`},
  110. {`c:\foo`, `c:\foo\bAr`, `bAr`},
  111. {`c:\foO`, `c:\Foo\bar`, `bar`},
  112. {`c:\foO`, `c:\fOo\bAr`, `bAr`},
  113. {`c:\foO`, `c:\fOo`, ``},
  114. {`C:\foO`, `c:\fOo`, ``},
  115. }
  116. for _, tc := range testCases {
  117. if res := rel(tc.abs, tc.root); res != tc.expectedRel {
  118. t.Errorf(`rel("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  119. }
  120. // unrootedChecked really just wraps rel, and does not care about
  121. // the actual root of that filesystem, but should not return an error
  122. // on these test cases.
  123. for _, root := range []string{tc.root, strings.ToLower(tc.root), strings.ToUpper(tc.root)} {
  124. fs := BasicFilesystem{root: root}
  125. if res, err := fs.unrootedChecked(tc.abs, []string{tc.root}); err != nil {
  126. t.Errorf(`Unexpected error from unrootedChecked("%v", "%v"): %v (fs.root: %v)`, tc.abs, tc.root, err, root)
  127. } else if res != tc.expectedRel {
  128. t.Errorf(`unrootedChecked("%v", "%v") == "%v", expected "%v" (fs.root: %v)`, tc.abs, tc.root, res, tc.expectedRel, root)
  129. }
  130. }
  131. }
  132. }
  133. // TestMultipleRoot checks that fs.unrootedChecked returns the correct path
  134. // when given more than one possible root path.
  135. func TestMultipleRoot(t *testing.T) {
  136. root := `c:\foO`
  137. roots := []string{root, `d:\`}
  138. rel := `bar`
  139. path := filepath.Join(root, rel)
  140. fs := BasicFilesystem{root: root}
  141. if res, err := fs.unrootedChecked(path, roots); err != nil {
  142. t.Errorf(`Unexpected error from unrootedChecked("%v", "%v"): %v (fs.root: %v)`, path, roots, err, root)
  143. } else if res != rel {
  144. t.Errorf(`unrootedChecked("%v", "%v") == "%v", expected "%v" (fs.root: %v)`, path, roots, res, rel, root)
  145. }
  146. }
  147. func TestGetFinalPath(t *testing.T) {
  148. testCases := []struct {
  149. input string
  150. expectedPath string
  151. eqToEvalSyml bool
  152. ignoreMissing bool
  153. }{
  154. {`c:\`, `C:\`, true, false},
  155. {`\\?\c:\`, `C:\`, false, false},
  156. {`c:\wInDows\sYstEm32`, `C:\Windows\System32`, true, false},
  157. {`c:\parent\child`, `C:\parent\child`, false, true},
  158. }
  159. for _, testCase := range testCases {
  160. out, err := getFinalPathName(testCase.input)
  161. if err != nil {
  162. if testCase.ignoreMissing && os.IsNotExist(err) {
  163. continue
  164. }
  165. t.Errorf("getFinalPathName failed at %q with error %s", testCase.input, err)
  166. }
  167. // Trim UNC prefix
  168. if strings.HasPrefix(out, `\\?\UNC\`) {
  169. out = `\` + out[7:]
  170. } else {
  171. out = strings.TrimPrefix(out, `\\?\`)
  172. }
  173. if out != testCase.expectedPath {
  174. t.Errorf("getFinalPathName got wrong path: %q (expected %q)", out, testCase.expectedPath)
  175. }
  176. if testCase.eqToEvalSyml {
  177. evlPath, err1 := filepath.EvalSymlinks(testCase.input)
  178. if err1 != nil || out != evlPath {
  179. t.Errorf("EvalSymlinks got different results %q %s", evlPath, err1)
  180. }
  181. }
  182. }
  183. }
  184. func TestRemoveWindowsDirIcon(t *testing.T) {
  185. // Try to delete a folder with a custom icon with os.Remove (simulated by the readonly file attribute)
  186. fs, dir := setup(t)
  187. relativePath := "folder_with_icon"
  188. path := filepath.Join(dir, relativePath)
  189. if err := os.Mkdir(path, os.ModeDir); err != nil {
  190. t.Fatal(err)
  191. }
  192. ptr, err := syscall.UTF16PtrFromString(path)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. if err := syscall.SetFileAttributes(ptr, uint32(syscall.FILE_ATTRIBUTE_DIRECTORY+syscall.FILE_ATTRIBUTE_READONLY)); err != nil {
  197. t.Fatal(err)
  198. }
  199. if err := fs.Remove(relativePath); err != nil {
  200. t.Fatal(err)
  201. }
  202. }