basicfs_windows_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. defer os.RemoveAll(dir)
  41. shortAbs, _ := fs.rooted("LFDATA~1")
  42. long := "LFDataTool"
  43. longAbs, _ := fs.rooted(long)
  44. deleted, _ := fs.rooted(filepath.Join("foo", "LFDATA~1"))
  45. notShort, _ := fs.rooted(filepath.Join("foo", "bar", "baz"))
  46. fd, err := fs.Create(long)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. fd.Close()
  51. if res := fs.resolveWin83(shortAbs); res != longAbs {
  52. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, shortAbs, res, longAbs)
  53. }
  54. if res := fs.resolveWin83(deleted); res != filepath.Dir(deleted) {
  55. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, deleted, res, filepath.Dir(deleted))
  56. }
  57. if res := fs.resolveWin83(notShort); res != notShort {
  58. t.Errorf(`Resolving for 8.3 names of "%v" resulted in "%v", expected "%v"`, notShort, res, notShort)
  59. }
  60. }
  61. func TestIsWindows83(t *testing.T) {
  62. fs, dir := setup(t)
  63. defer os.RemoveAll(dir)
  64. tempTop, _ := fs.rooted(TempName("baz"))
  65. tempBelow, _ := fs.rooted(filepath.Join("foo", "bar", TempName("baz")))
  66. short, _ := fs.rooted(filepath.Join("LFDATA~1", TempName("baz")))
  67. tempAndShort, _ := fs.rooted(filepath.Join("LFDATA~1", TempName("baz")))
  68. for _, f := range []string{tempTop, tempBelow} {
  69. if isMaybeWin83(f) {
  70. t.Errorf(`"%v" is not a windows 8.3 path"`, f)
  71. }
  72. }
  73. for _, f := range []string{short, tempAndShort} {
  74. if !isMaybeWin83(f) {
  75. t.Errorf(`"%v" is not a windows 8.3 path"`, f)
  76. }
  77. }
  78. }
  79. func TestRelUnrootedCheckedWindows(t *testing.T) {
  80. testCases := []struct {
  81. root string
  82. abs string
  83. expectedRel string
  84. }{
  85. {`c:\`, `c:\foo`, `foo`},
  86. {`C:\`, `c:\foo`, `foo`},
  87. {`C:\`, `C:\foo`, `foo`},
  88. {`c:\`, `C:\foo`, `foo`},
  89. {`\\?c:\`, `\\?c:\foo`, `foo`},
  90. {`\\?C:\`, `\\?c:\foo`, `foo`},
  91. {`\\?C:\`, `\\?C:\foo`, `foo`},
  92. {`\\?c:\`, `\\?C:\foo`, `foo`},
  93. {`c:\foo`, `c:\foo\bar`, `bar`},
  94. {`c:\foo`, `c:\foo\bAr`, `bAr`},
  95. {`c:\foO`, `c:\Foo\bar`, `bar`},
  96. {`c:\foO`, `c:\fOo\bAr`, `bAr`},
  97. {`c:\foO`, `c:\fOo`, ``},
  98. {`C:\foO`, `c:\fOo`, ``},
  99. }
  100. for _, tc := range testCases {
  101. if res := rel(tc.abs, tc.root); res != tc.expectedRel {
  102. t.Errorf(`rel("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  103. }
  104. // unrootedChecked really just wraps rel, and does not care about
  105. // the actual root of that filesystem, but should not panic on these
  106. // test cases.
  107. fs := BasicFilesystem{root: tc.root}
  108. if res := fs.unrootedChecked(tc.abs, tc.root); res != tc.expectedRel {
  109. t.Errorf(`unrootedChecked("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  110. }
  111. fs = BasicFilesystem{root: strings.ToLower(tc.root)}
  112. if res := fs.unrootedChecked(tc.abs, tc.root); res != tc.expectedRel {
  113. t.Errorf(`unrootedChecked("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  114. }
  115. fs = BasicFilesystem{root: strings.ToUpper(tc.root)}
  116. if res := fs.unrootedChecked(tc.abs, tc.root); res != tc.expectedRel {
  117. t.Errorf(`unrootedChecked("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  118. }
  119. }
  120. }