basicfs_windows_test.go 5.8 KB

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