webdav_test.go 9.1 KB

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