You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

344 lines
9.2 KiB

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