basicfs_windows_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }