file_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package webdav
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. )
  11. func TestDir(t *testing.T) {
  12. testCases := []struct {
  13. dir, name, want string
  14. }{
  15. {"/", "", "/"},
  16. {"/", "/", "/"},
  17. {"/", ".", "/"},
  18. {"/", "./a", "/a"},
  19. {"/", "..", "/"},
  20. {"/", "..", "/"},
  21. {"/", "../", "/"},
  22. {"/", "../.", "/"},
  23. {"/", "../a", "/a"},
  24. {"/", "../..", "/"},
  25. {"/", "../bar/a", "/bar/a"},
  26. {"/", "../baz/a", "/baz/a"},
  27. {"/", "...", "/..."},
  28. {"/", ".../a", "/.../a"},
  29. {"/", ".../..", "/"},
  30. {"/", "a", "/a"},
  31. {"/", "a/./b", "/a/b"},
  32. {"/", "a/../../b", "/b"},
  33. {"/", "a/../b", "/b"},
  34. {"/", "a/b", "/a/b"},
  35. {"/", "a/b/c/../../d", "/a/d"},
  36. {"/", "a/b/c/../../../d", "/d"},
  37. {"/", "a/b/c/../../../../d", "/d"},
  38. {"/", "a/b/c/d", "/a/b/c/d"},
  39. {"/foo/bar", "", "/foo/bar"},
  40. {"/foo/bar", "/", "/foo/bar"},
  41. {"/foo/bar", ".", "/foo/bar"},
  42. {"/foo/bar", "./a", "/foo/bar/a"},
  43. {"/foo/bar", "..", "/foo/bar"},
  44. {"/foo/bar", "../", "/foo/bar"},
  45. {"/foo/bar", "../.", "/foo/bar"},
  46. {"/foo/bar", "../a", "/foo/bar/a"},
  47. {"/foo/bar", "../..", "/foo/bar"},
  48. {"/foo/bar", "../bar/a", "/foo/bar/bar/a"},
  49. {"/foo/bar", "../baz/a", "/foo/bar/baz/a"},
  50. {"/foo/bar", "...", "/foo/bar/..."},
  51. {"/foo/bar", ".../a", "/foo/bar/.../a"},
  52. {"/foo/bar", ".../..", "/foo/bar"},
  53. {"/foo/bar", "a", "/foo/bar/a"},
  54. {"/foo/bar", "a/./b", "/foo/bar/a/b"},
  55. {"/foo/bar", "a/../../b", "/foo/bar/b"},
  56. {"/foo/bar", "a/../b", "/foo/bar/b"},
  57. {"/foo/bar", "a/b", "/foo/bar/a/b"},
  58. {"/foo/bar", "a/b/c/../../d", "/foo/bar/a/d"},
  59. {"/foo/bar", "a/b/c/../../../d", "/foo/bar/d"},
  60. {"/foo/bar", "a/b/c/../../../../d", "/foo/bar/d"},
  61. {"/foo/bar", "a/b/c/d", "/foo/bar/a/b/c/d"},
  62. {"/foo/bar/", "", "/foo/bar"},
  63. {"/foo/bar/", "/", "/foo/bar"},
  64. {"/foo/bar/", ".", "/foo/bar"},
  65. {"/foo/bar/", "./a", "/foo/bar/a"},
  66. {"/foo/bar/", "..", "/foo/bar"},
  67. {"/foo//bar///", "", "/foo/bar"},
  68. {"/foo//bar///", "/", "/foo/bar"},
  69. {"/foo//bar///", ".", "/foo/bar"},
  70. {"/foo//bar///", "./a", "/foo/bar/a"},
  71. {"/foo//bar///", "..", "/foo/bar"},
  72. {"/x/y/z", "ab/c\x00d/ef", ""},
  73. {".", "", "."},
  74. {".", "/", "."},
  75. {".", ".", "."},
  76. {".", "./a", "a"},
  77. {".", "..", "."},
  78. {".", "..", "."},
  79. {".", "../", "."},
  80. {".", "../.", "."},
  81. {".", "../a", "a"},
  82. {".", "../..", "."},
  83. {".", "../bar/a", "bar/a"},
  84. {".", "../baz/a", "baz/a"},
  85. {".", "...", "..."},
  86. {".", ".../a", ".../a"},
  87. {".", ".../..", "."},
  88. {".", "a", "a"},
  89. {".", "a/./b", "a/b"},
  90. {".", "a/../../b", "b"},
  91. {".", "a/../b", "b"},
  92. {".", "a/b", "a/b"},
  93. {".", "a/b/c/../../d", "a/d"},
  94. {".", "a/b/c/../../../d", "d"},
  95. {".", "a/b/c/../../../../d", "d"},
  96. {".", "a/b/c/d", "a/b/c/d"},
  97. {"", "", "."},
  98. {"", "/", "."},
  99. {"", ".", "."},
  100. {"", "./a", "a"},
  101. {"", "..", "."},
  102. }
  103. for _, tc := range testCases {
  104. d := Dir(filepath.FromSlash(tc.dir))
  105. if got := filepath.ToSlash(d.resolve(tc.name)); got != tc.want {
  106. t.Errorf("dir=%q, name=%q: got %q, want %q", tc.dir, tc.name, got, tc.want)
  107. }
  108. }
  109. }
  110. func TestWalk(t *testing.T) {
  111. type walkStep struct {
  112. name, frag string
  113. final bool
  114. }
  115. testCases := []struct {
  116. dir string
  117. want []walkStep
  118. }{
  119. {"", []walkStep{
  120. {"", "", true},
  121. }},
  122. {"/", []walkStep{
  123. {"", "", true},
  124. }},
  125. {"/a", []walkStep{
  126. {"", "a", true},
  127. }},
  128. {"/a/", []walkStep{
  129. {"", "a", true},
  130. }},
  131. {"/a/b", []walkStep{
  132. {"", "a", false},
  133. {"a", "b", true},
  134. }},
  135. {"/a/b/", []walkStep{
  136. {"", "a", false},
  137. {"a", "b", true},
  138. }},
  139. {"/a/b/c", []walkStep{
  140. {"", "a", false},
  141. {"a", "b", false},
  142. {"b", "c", true},
  143. }},
  144. // The following test case is the one mentioned explicitly
  145. // in the method description.
  146. {"/foo/bar/x", []walkStep{
  147. {"", "foo", false},
  148. {"foo", "bar", false},
  149. {"bar", "x", true},
  150. }},
  151. }
  152. for _, tc := range testCases {
  153. fs := NewMemFS().(*memFS)
  154. parts := strings.Split(tc.dir, "/")
  155. for p := 2; p < len(parts); p++ {
  156. d := strings.Join(parts[:p], "/")
  157. if err := fs.Mkdir(d, 0666); err != nil {
  158. t.Errorf("tc.dir=%q: mkdir: %q: %v", tc.dir, d, err)
  159. }
  160. }
  161. i := 0
  162. err := fs.walk("test", tc.dir, func(dir *memFSNode, frag string, final bool) error {
  163. got := walkStep{
  164. name: dir.name,
  165. frag: frag,
  166. final: final,
  167. }
  168. want := tc.want[i]
  169. if got != want {
  170. return fmt.Errorf("got %+v, want %+v", got, want)
  171. }
  172. i++
  173. return nil
  174. })
  175. if err != nil {
  176. t.Errorf("tc.dir=%q: %v", tc.dir, err)
  177. }
  178. }
  179. }