1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/arvadostest"
20 check "gopkg.in/check.v1"
23 func (s *IntegrationSuite) TestCadaverHTTPAuth(c *check.C) {
24 s.testCadaver(c, arvadostest.ActiveToken, func(newCollection arvados.Collection) (string, string, string) {
25 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/"
26 w := "/c=" + newCollection.UUID + "/"
27 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
32 func (s *IntegrationSuite) TestCadaverPathAuth(c *check.C) {
33 s.testCadaver(c, "", func(newCollection arvados.Collection) (string, string, string) {
34 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
35 w := "/c=" + newCollection.UUID + "/t=" + arvadostest.ActiveToken + "/"
36 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arvadostest.ActiveToken + "/"
41 func (s *IntegrationSuite) TestCadaverUserProject(c *check.C) {
42 rpath := "/users/active/foo_file_in_dir/"
43 s.testCadaver(c, arvadostest.ActiveToken, func(newCollection arvados.Collection) (string, string, string) {
44 wpath := "/users/active/" + newCollection.Name
45 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
46 return rpath, wpath, pdh
47 }, func(path string) bool {
48 // Skip tests that rely on writes, because /users/
50 return !strings.HasPrefix(path, rpath) || strings.HasPrefix(path, rpath+"_/")
54 func (s *IntegrationSuite) testCadaver(c *check.C, password string, pathFunc func(arvados.Collection) (string, string, string), skip func(string) bool) {
55 testdata := []byte("the human tragedy consists in the necessity of living with the consequences of actions performed under the pressure of compulsions we do not understand")
57 tempdir, err := ioutil.TempDir("", "keep-web-test-")
58 c.Assert(err, check.IsNil)
59 defer os.RemoveAll(tempdir)
61 localfile, err := ioutil.TempFile(tempdir, "localfile")
62 c.Assert(err, check.IsNil)
63 localfile.Write(testdata)
65 emptyfile, err := ioutil.TempFile(tempdir, "emptyfile")
66 c.Assert(err, check.IsNil)
68 checkfile, err := ioutil.TempFile(tempdir, "checkfile")
69 c.Assert(err, check.IsNil)
71 var newCollection arvados.Collection
72 arv := arvados.NewClientFromEnv()
73 arv.AuthToken = arvadostest.ActiveToken
74 err = arv.RequestAndDecode(&newCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{}})
75 c.Assert(err, check.IsNil)
77 readPath, writePath, pdhPath := pathFunc(newCollection)
79 matchToday := time.Now().Format("Jan +2")
81 type testcase struct {
87 for _, trial := range []testcase{
91 match: `(?ms).*dir1 *0 .*`,
96 match: `(?ms).*bar *3.*foo *3 .*`,
99 path: readPath + "_/dir1",
101 match: `(?ms).*bar *3.*foo *3 .*`,
104 path: readPath + "dir1/",
106 match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
110 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
111 match: `(?ms).*Not Found.*`,
115 cmd: "put '" + emptyfile.Name() + "' emptyfile\n",
116 match: `(?ms).*Uploading .* succeeded.*`,
120 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
121 match: `(?ms).*Downloading .* succeeded.*`,
126 cmd: "put '" + localfile.Name() + "' testfile\n",
127 match: `(?ms).*Uploading .* succeeded.*`,
131 cmd: "get testfile '" + checkfile.Name() + "'\n",
132 match: `(?ms).*succeeded.*`,
137 cmd: "move testfile \"test &#!%20 file\"\n",
138 match: `(?ms).*Moving .* succeeded.*`,
142 cmd: "move \"test &#!%20 file\" testfile\n",
143 match: `(?ms).*Moving .* succeeded.*`,
147 cmd: "move testfile newdir0/\n",
148 match: `(?ms).*Moving .* succeeded.*`,
152 cmd: "move testfile newdir0/\n",
153 match: `(?ms).*Moving .* failed.*`,
157 cmd: "lock newdir0/testfile\n",
158 match: `(?ms).*Locking .* succeeded.*`,
162 cmd: "unlock newdir0/testfile\nasdf\n",
163 match: `(?ms).*Unlocking .* succeeded.*`,
168 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
172 cmd: "move newdir0/testfile emptyfile/bogus/\n",
173 match: `(?ms).*Moving .* failed.*`,
177 cmd: "mkcol newdir1\n",
178 match: `(?ms).*Creating .* succeeded.*`,
182 cmd: "move newdir1/ newdir1x/\n",
183 match: `(?ms).*Moving .* succeeded.*`,
187 cmd: "move newdir1x newdir1\n",
188 match: `(?ms).*Moving .* succeeded.*`,
192 cmd: "move newdir0/testfile newdir1/\n",
193 match: `(?ms).*Moving .* succeeded.*`,
197 cmd: "move newdir1 newdir1/\n",
198 match: `(?ms).*Moving .* failed.*`,
202 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
203 match: `(?ms).*succeeded.*`,
208 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
209 match: `(?ms).*Uploading .* succeeded.*`,
213 cmd: "mkcol newdir2\n",
214 match: `(?ms).*Creating .* succeeded.*`,
218 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
219 match: `(?ms).*Uploading .* succeeded.*`,
223 cmd: "copy newdir2/testfile2 testfile3\n",
224 match: `(?ms).*succeeded.*`,
228 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
229 match: `(?ms).*succeeded.*`,
234 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
235 match: `(?ms).*succeeded.*`,
240 cmd: "rmcol newdir2\n",
241 match: `(?ms).*Deleting collection .* succeeded.*`,
245 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
246 match: `(?ms).*Downloading .* failed.*`,
249 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
250 cmd: "put '" + localfile.Name() + "' foo\n",
251 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
255 cmd: "put '" + localfile.Name() + "' foo\n",
256 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
260 cmd: "move foo bar\n",
261 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
265 cmd: "copy foo bar\n",
266 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
271 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
276 match: `(?ms).*Locking .* failed:.*405 Method Not Allowed.*`,
279 c.Logf("%s %+v", s.testServer.URL, trial)
280 if skip != nil && skip(trial.path) {
285 os.Remove(checkfile.Name())
287 stdout := s.runCadaver(c, password, trial.path, trial.cmd)
288 c.Check(stdout, check.Matches, trial.match)
290 if trial.data == nil {
293 checkfile, err = os.Open(checkfile.Name())
294 c.Assert(err, check.IsNil)
295 checkfile.Seek(0, os.SEEK_SET)
296 got, err := ioutil.ReadAll(checkfile)
297 c.Check(got, check.DeepEquals, trial.data)
298 c.Check(err, check.IsNil)
302 func (s *IntegrationSuite) TestCadaverByID(c *check.C) {
303 for _, path := range []string{"/by_id", "/by_id/"} {
304 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
305 c.Check(stdout, check.Matches, `(?ms).*collection is empty.*`)
307 for _, path := range []string{
308 "/by_id/" + arvadostest.FooCollectionPDH,
309 "/by_id/" + arvadostest.FooCollectionPDH + "/",
310 "/by_id/" + arvadostest.FooCollection,
311 "/by_id/" + arvadostest.FooCollection + "/",
313 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
314 c.Check(stdout, check.Matches, `(?ms).*\s+foo\s+3 .*`)
318 func (s *IntegrationSuite) TestCadaverUsersDir(c *check.C) {
319 for _, path := range []string{"/"} {
320 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
321 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+by_id\s+0 .*`)
322 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+users\s+0 .*`)
324 for _, path := range []string{"/users", "/users/"} {
325 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
326 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+active.*`)
328 for _, path := range []string{"/users/active", "/users/active/"} {
329 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
330 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+A Project\s+0 .*`)
331 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+bar_file\s+0 .*`)
333 for _, path := range []string{"/users/admin", "/users/doesnotexist", "/users/doesnotexist/"} {
334 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
335 c.Check(stdout, check.Matches, `(?ms).*404 Not Found.*`)
339 func (s *IntegrationSuite) runCadaver(c *check.C, password, path, stdin string) string {
340 tempdir, err := ioutil.TempDir("", "keep-web-test-")
341 c.Assert(err, check.IsNil)
342 defer os.RemoveAll(tempdir)
344 cmd := exec.Command("cadaver", s.testServer.URL+path)
346 // cadaver won't try username/password authentication
347 // unless the server responds 401 to an
348 // unauthenticated request, which it only does in
349 // AttachmentOnlyHost, TrustAllContent, and
350 // per-collection vhost cases.
351 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = s.testServer.URL[7:]
353 cmd.Env = append(os.Environ(), "HOME="+tempdir)
354 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
355 c.Assert(err, check.IsNil)
356 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
357 c.Assert(err, check.IsNil)
358 c.Assert(f.Close(), check.IsNil)
360 cmd.Stdin = bytes.NewBufferString(stdin)
361 stdout, err := cmd.StdoutPipe()
362 c.Assert(err, check.Equals, nil)
363 cmd.Stderr = cmd.Stdout
367 _, err = io.Copy(&buf, stdout)
368 c.Check(err, check.Equals, nil)
370 c.Check(err, check.Equals, nil)