basicfs_windows_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // +build windows
  7. package fs
  8. import (
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. )
  14. func TestWindowsPaths(t *testing.T) {
  15. testCases := []struct {
  16. input string
  17. expectedRoot string
  18. expectedURI string
  19. }{
  20. {`e:\`, `\\?\e:\`, `e:\`},
  21. {`\\?\e:\`, `\\?\e:\`, `e:\`},
  22. {`\\192.0.2.22\network\share`, `\\192.0.2.22\network\share`, `\\192.0.2.22\network\share`},
  23. }
  24. for _, testCase := range testCases {
  25. fs := newBasicFilesystem(testCase.input)
  26. if fs.root != testCase.expectedRoot {
  27. t.Errorf("root %q != %q", fs.root, testCase.expectedRoot)
  28. }
  29. if fs.URI() != testCase.expectedURI {
  30. t.Errorf("uri %q != %q", fs.URI(), testCase.expectedURI)
  31. }
  32. }
  33. fs := newBasicFilesystem(`relative\path`)
  34. if fs.root == `relative\path` || !strings.HasPrefix(fs.root, "\\\\?\\") {
  35. t.Errorf("%q == %q, expected absolutification", fs.root, `relative\path`)
  36. }
  37. }
  38. func TestResolveWindows83(t *testing.T) {
  39. fs, dir := setup(t)
  40. if isMaybeWin83(dir) {
  41. dir = fs.resolveWin83(dir)
  42. fs = newBasicFilesystem(dir)
  43. }
  44. defer os.RemoveAll(dir)
  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. defer os.RemoveAll(dir)
  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, 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. func TestGetFinalPath(t *testing.T) {
  126. testCases := []struct {
  127. input string
  128. expectedPath string
  129. eqToEvalSyml bool
  130. ignoreMissing bool
  131. }{
  132. {`c:\`, `C:\`, true, false},
  133. {`\\?\c:\`, `C:\`, false, false},
  134. {`c:\wInDows\sYstEm32`, `C:\Windows\System32`, true, false},
  135. {`c:\parent\child`, `C:\parent\child`, false, true},
  136. }
  137. for _, testCase := range testCases {
  138. out, err := getFinalPathName(testCase.input)
  139. if err != nil {
  140. if testCase.ignoreMissing && os.IsNotExist(err) {
  141. continue
  142. }
  143. t.Errorf("getFinalPathName failed at %q with error %s", testCase.input, err)
  144. }
  145. // Trim UNC prefix
  146. if strings.HasPrefix(out, `\\?\UNC\`) {
  147. out = `\` + out[7:]
  148. } else {
  149. out = strings.TrimPrefix(out, `\\?\`)
  150. }
  151. if out != testCase.expectedPath {
  152. t.Errorf("getFinalPathName got wrong path: %q (expected %q)", out, testCase.expectedPath)
  153. }
  154. if testCase.eqToEvalSyml {
  155. evlPath, err1 := filepath.EvalSymlinks(testCase.input)
  156. if err1 != nil || out != evlPath {
  157. t.Errorf("EvalSymlinks got different results %q %s", evlPath, err1)
  158. }
  159. }
  160. }
  161. }