webdav_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Copyright 2015 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. "context"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "net/http/httptest"
  12. "net/url"
  13. "os"
  14. "reflect"
  15. "regexp"
  16. "sort"
  17. "strings"
  18. "testing"
  19. )
  20. // TODO: add tests to check XML responses with the expected prefix path
  21. func TestPrefix(t *testing.T) {
  22. const dst, blah = "Destination", "blah blah blah"
  23. // createLockBody comes from the example in Section 9.10.7.
  24. const createLockBody = `<?xml version="1.0" encoding="utf-8" ?>
  25. <D:lockinfo xmlns:D='DAV:'>
  26. <D:lockscope><D:exclusive/></D:lockscope>
  27. <D:locktype><D:write/></D:locktype>
  28. <D:owner>
  29. <D:href>http://example.org/~ejw/contact.html</D:href>
  30. </D:owner>
  31. </D:lockinfo>
  32. `
  33. do := func(method, urlStr string, body string, wantStatusCode int, headers ...string) (http.Header, error) {
  34. var bodyReader io.Reader
  35. if body != "" {
  36. bodyReader = strings.NewReader(body)
  37. }
  38. req, err := http.NewRequest(method, urlStr, bodyReader)
  39. if err != nil {
  40. return nil, err
  41. }
  42. for len(headers) >= 2 {
  43. req.Header.Add(headers[0], headers[1])
  44. headers = headers[2:]
  45. }
  46. res, err := http.DefaultTransport.RoundTrip(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. defer res.Body.Close()
  51. if res.StatusCode != wantStatusCode {
  52. return nil, fmt.Errorf("got status code %d, want %d", res.StatusCode, wantStatusCode)
  53. }
  54. return res.Header, nil
  55. }
  56. prefixes := []string{
  57. "/",
  58. "/a/",
  59. "/a/b/",
  60. "/a/b/c/",
  61. }
  62. ctx := context.Background()
  63. for _, prefix := range prefixes {
  64. fs := NewMemFS()
  65. h := &Handler{
  66. FileSystem: fs,
  67. LockSystem: NewMemLS(),
  68. }
  69. mux := http.NewServeMux()
  70. if prefix != "/" {
  71. h.Prefix = prefix
  72. }
  73. mux.Handle(prefix, h)
  74. srv := httptest.NewServer(mux)
  75. defer srv.Close()
  76. // The script is:
  77. // MKCOL /a
  78. // MKCOL /a/b
  79. // PUT /a/b/c
  80. // COPY /a/b/c /a/b/d
  81. // MKCOL /a/b/e
  82. // MOVE /a/b/d /a/b/e/f
  83. // LOCK /a/b/e/g
  84. // PUT /a/b/e/g
  85. // which should yield the (possibly stripped) filenames /a/b/c,
  86. // /a/b/e/f and /a/b/e/g, plus their parent directories.
  87. wantA := map[string]int{
  88. "/": http.StatusCreated,
  89. "/a/": http.StatusMovedPermanently,
  90. "/a/b/": http.StatusNotFound,
  91. "/a/b/c/": http.StatusNotFound,
  92. }[prefix]
  93. if _, err := do("MKCOL", srv.URL+"/a", "", wantA); err != nil {
  94. t.Errorf("prefix=%-9q MKCOL /a: %v", prefix, err)
  95. continue
  96. }
  97. wantB := map[string]int{
  98. "/": http.StatusCreated,
  99. "/a/": http.StatusCreated,
  100. "/a/b/": http.StatusMovedPermanently,
  101. "/a/b/c/": http.StatusNotFound,
  102. }[prefix]
  103. if _, err := do("MKCOL", srv.URL+"/a/b", "", wantB); err != nil {
  104. t.Errorf("prefix=%-9q MKCOL /a/b: %v", prefix, err)
  105. continue
  106. }
  107. wantC := map[string]int{
  108. "/": http.StatusCreated,
  109. "/a/": http.StatusCreated,
  110. "/a/b/": http.StatusCreated,
  111. "/a/b/c/": http.StatusMovedPermanently,
  112. }[prefix]
  113. if _, err := do("PUT", srv.URL+"/a/b/c", blah, wantC); err != nil {
  114. t.Errorf("prefix=%-9q PUT /a/b/c: %v", prefix, err)
  115. continue
  116. }
  117. wantD := map[string]int{
  118. "/": http.StatusCreated,
  119. "/a/": http.StatusCreated,
  120. "/a/b/": http.StatusCreated,
  121. "/a/b/c/": http.StatusMovedPermanently,
  122. }[prefix]
  123. if _, err := do("COPY", srv.URL+"/a/b/c", "", wantD, dst, srv.URL+"/a/b/d"); err != nil {
  124. t.Errorf("prefix=%-9q COPY /a/b/c /a/b/d: %v", prefix, err)
  125. continue
  126. }
  127. wantE := map[string]int{
  128. "/": http.StatusCreated,
  129. "/a/": http.StatusCreated,
  130. "/a/b/": http.StatusCreated,
  131. "/a/b/c/": http.StatusNotFound,
  132. }[prefix]
  133. if _, err := do("MKCOL", srv.URL+"/a/b/e", "", wantE); err != nil {
  134. t.Errorf("prefix=%-9q MKCOL /a/b/e: %v", prefix, err)
  135. continue
  136. }
  137. wantF := map[string]int{
  138. "/": http.StatusCreated,
  139. "/a/": http.StatusCreated,
  140. "/a/b/": http.StatusCreated,
  141. "/a/b/c/": http.StatusNotFound,
  142. }[prefix]
  143. if _, err := do("MOVE", srv.URL+"/a/b/d", "", wantF, dst, srv.URL+"/a/b/e/f"); err != nil {
  144. t.Errorf("prefix=%-9q MOVE /a/b/d /a/b/e/f: %v", prefix, err)
  145. continue
  146. }
  147. var lockToken string
  148. wantG := map[string]int{
  149. "/": http.StatusCreated,
  150. "/a/": http.StatusCreated,
  151. "/a/b/": http.StatusCreated,
  152. "/a/b/c/": http.StatusNotFound,
  153. }[prefix]
  154. if h, err := do("LOCK", srv.URL+"/a/b/e/g", createLockBody, wantG); err != nil {
  155. t.Errorf("prefix=%-9q LOCK /a/b/e/g: %v", prefix, err)
  156. continue
  157. } else {
  158. lockToken = h.Get("Lock-Token")
  159. }
  160. ifHeader := fmt.Sprintf("<%s/a/b/e/g> (%s)", srv.URL, lockToken)
  161. wantH := map[string]int{
  162. "/": http.StatusCreated,
  163. "/a/": http.StatusCreated,
  164. "/a/b/": http.StatusCreated,
  165. "/a/b/c/": http.StatusNotFound,
  166. }[prefix]
  167. if _, err := do("PUT", srv.URL+"/a/b/e/g", blah, wantH, "If", ifHeader); err != nil {
  168. t.Errorf("prefix=%-9q PUT /a/b/e/g: %v", prefix, err)
  169. continue
  170. }
  171. got, err := find(ctx, nil, fs, "/")
  172. if err != nil {
  173. t.Errorf("prefix=%-9q find: %v", prefix, err)
  174. continue
  175. }
  176. sort.Strings(got)
  177. want := map[string][]string{
  178. "/": {"/", "/a", "/a/b", "/a/b/c", "/a/b/e", "/a/b/e/f", "/a/b/e/g"},
  179. "/a/": {"/", "/b", "/b/c", "/b/e", "/b/e/f", "/b/e/g"},
  180. "/a/b/": {"/", "/c", "/e", "/e/f", "/e/g"},
  181. "/a/b/c/": {"/"},
  182. }[prefix]
  183. if !reflect.DeepEqual(got, want) {
  184. t.Errorf("prefix=%-9q find:\ngot %v\nwant %v", prefix, got, want)
  185. continue
  186. }
  187. }
  188. }
  189. func TestEscapeXML(t *testing.T) {
  190. // These test cases aren't exhaustive, and there is more than one way to
  191. // escape e.g. a quot (as "&#34;" or "&quot;") or an apos. We presume that
  192. // the encoding/xml package tests xml.EscapeText more thoroughly. This test
  193. // here is just a sanity check for this package's escapeXML function, and
  194. // its attempt to provide a fast path (and avoid a bytes.Buffer allocation)
  195. // when escaping filenames is obviously a no-op.
  196. testCases := map[string]string{
  197. "": "",
  198. " ": " ",
  199. "&": "&amp;",
  200. "*": "*",
  201. "+": "+",
  202. ",": ",",
  203. "-": "-",
  204. ".": ".",
  205. "/": "/",
  206. "0": "0",
  207. "9": "9",
  208. ":": ":",
  209. "<": "&lt;",
  210. ">": "&gt;",
  211. "A": "A",
  212. "_": "_",
  213. "a": "a",
  214. "~": "~",
  215. "\u0201": "\u0201",
  216. "&amp;": "&amp;amp;",
  217. "foo&<b/ar>baz": "foo&amp;&lt;b/ar&gt;baz",
  218. }
  219. for in, want := range testCases {
  220. if got := escapeXML(in); got != want {
  221. t.Errorf("in=%q: got %q, want %q", in, got, want)
  222. }
  223. }
  224. }
  225. func TestFilenameEscape(t *testing.T) {
  226. hrefRe := regexp.MustCompile(`<D:href>([^<]*)</D:href>`)
  227. displayNameRe := regexp.MustCompile(`<D:displayname>([^<]*)</D:displayname>`)
  228. do := func(method, urlStr string) (string, string, error) {
  229. req, err := http.NewRequest(method, urlStr, nil)
  230. if err != nil {
  231. return "", "", err
  232. }
  233. res, err := http.DefaultClient.Do(req)
  234. if err != nil {
  235. return "", "", err
  236. }
  237. defer res.Body.Close()
  238. b, err := io.ReadAll(res.Body)
  239. if err != nil {
  240. return "", "", err
  241. }
  242. hrefMatch := hrefRe.FindStringSubmatch(string(b))
  243. if len(hrefMatch) != 2 {
  244. return "", "", errors.New("D:href not found")
  245. }
  246. displayNameMatch := displayNameRe.FindStringSubmatch(string(b))
  247. if len(displayNameMatch) != 2 {
  248. return "", "", errors.New("D:displayname not found")
  249. }
  250. return hrefMatch[1], displayNameMatch[1], nil
  251. }
  252. testCases := []struct {
  253. name, wantHref, wantDisplayName string
  254. }{{
  255. name: `/foo%bar`,
  256. wantHref: `/foo%25bar`,
  257. wantDisplayName: `foo%bar`,
  258. }, {
  259. name: `/こんにちわ世界`,
  260. wantHref: `/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%82%8F%E4%B8%96%E7%95%8C`,
  261. wantDisplayName: `こんにちわ世界`,
  262. }, {
  263. name: `/Program Files/`,
  264. wantHref: `/Program%20Files/`,
  265. wantDisplayName: `Program Files`,
  266. }, {
  267. name: `/go+lang`,
  268. wantHref: `/go+lang`,
  269. wantDisplayName: `go+lang`,
  270. }, {
  271. name: `/go&lang`,
  272. wantHref: `/go&amp;lang`,
  273. wantDisplayName: `go&amp;lang`,
  274. }, {
  275. name: `/go<lang`,
  276. wantHref: `/go%3Clang`,
  277. wantDisplayName: `go&lt;lang`,
  278. }, {
  279. name: `/`,
  280. wantHref: `/`,
  281. wantDisplayName: ``,
  282. }}
  283. ctx := context.Background()
  284. fs := NewMemFS()
  285. for _, tc := range testCases {
  286. if tc.name != "/" {
  287. if strings.HasSuffix(tc.name, "/") {
  288. if err := fs.Mkdir(ctx, tc.name, 0755); err != nil {
  289. t.Fatalf("name=%q: Mkdir: %v", tc.name, err)
  290. }
  291. } else {
  292. f, err := fs.OpenFile(ctx, tc.name, os.O_CREATE, 0644)
  293. if err != nil {
  294. t.Fatalf("name=%q: OpenFile: %v", tc.name, err)
  295. }
  296. f.Close()
  297. }
  298. }
  299. }
  300. srv := httptest.NewServer(&Handler{
  301. FileSystem: fs,
  302. LockSystem: NewMemLS(),
  303. })
  304. defer srv.Close()
  305. u, err := url.Parse(srv.URL)
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. for _, tc := range testCases {
  310. u.Path = tc.name
  311. gotHref, gotDisplayName, err := do("PROPFIND", u.String())
  312. if err != nil {
  313. t.Errorf("name=%q: PROPFIND: %v", tc.name, err)
  314. continue
  315. }
  316. if gotHref != tc.wantHref {
  317. t.Errorf("name=%q: got href %q, want %q", tc.name, gotHref, tc.wantHref)
  318. }
  319. if gotDisplayName != tc.wantDisplayName {
  320. t.Errorf("name=%q: got dispayname %q, want %q", tc.name, gotDisplayName, tc.wantDisplayName)
  321. }
  322. }
  323. }