basicfs_windows_test.go 6.4 KB

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