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: "mkcol newdir0/\n",
148 match: `(?ms).*Creating .* succeeded.*`,
152 cmd: "move testfile newdir0/\n",
153 match: `(?ms).*Moving .* succeeded.*`,
157 cmd: "move testfile newdir0/\n",
158 match: `(?ms).*Moving .* failed.*`,
162 cmd: "lock newdir0/testfile\n",
163 match: `(?ms).*Locking .* succeeded.*`,
167 cmd: "unlock newdir0/testfile\nasdf\n",
168 match: `(?ms).*Unlocking .* succeeded.*`,
173 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
177 cmd: "move newdir0/testfile emptyfile/bogus/\n",
178 match: `(?ms).*Moving .* failed.*`,
182 cmd: "mkcol newdir1\n",
183 match: `(?ms).*Creating .* succeeded.*`,
187 cmd: "move newdir1/ newdir1x/\n",
188 match: `(?ms).*Moving .* succeeded.*`,
192 cmd: "move newdir1x newdir1\n",
193 match: `(?ms).*Moving .* succeeded.*`,
197 cmd: "move newdir0/testfile newdir1/\n",
198 match: `(?ms).*Moving .* succeeded.*`,
202 cmd: "move newdir1 newdir1/\n",
203 match: `(?ms).*Moving .* failed.*`,
207 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
208 match: `(?ms).*succeeded.*`,
213 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
214 match: `(?ms).*Uploading .* succeeded.*`,
218 cmd: "mkcol newdir2\n",
219 match: `(?ms).*Creating .* succeeded.*`,
223 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
224 match: `(?ms).*Uploading .* succeeded.*`,
228 cmd: "copy newdir2/testfile2 testfile3\n",
229 match: `(?ms).*succeeded.*`,
233 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
234 match: `(?ms).*succeeded.*`,
239 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
240 match: `(?ms).*succeeded.*`,
245 cmd: "rmcol newdir2\n",
246 match: `(?ms).*Deleting collection .* succeeded.*`,
250 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
251 match: `(?ms).*Downloading .* failed.*`,
254 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
255 cmd: "put '" + localfile.Name() + "' foo\n",
256 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
260 cmd: "put '" + localfile.Name() + "' foo\n",
261 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
265 cmd: "move foo bar\n",
266 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
270 cmd: "copy foo bar\n",
271 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
276 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
281 match: `(?ms).*Locking .* failed:.*405 Method Not Allowed.*`,
284 c.Logf("%s %+v", s.testServer.URL, trial)
285 if skip != nil && skip(trial.path) {
290 os.Remove(checkfile.Name())
292 stdout := s.runCadaver(c, password, trial.path, trial.cmd)
293 c.Check(stdout, check.Matches, trial.match)
295 if trial.data == nil {
298 checkfile, err = os.Open(checkfile.Name())
299 c.Assert(err, check.IsNil)
300 checkfile.Seek(0, os.SEEK_SET)
301 got, err := ioutil.ReadAll(checkfile)
302 c.Check(got, check.DeepEquals, trial.data)
303 c.Check(err, check.IsNil)
307 func (s *IntegrationSuite) TestCadaverByID(c *check.C) {
308 for _, path := range []string{"/by_id", "/by_id/"} {
309 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
310 c.Check(stdout, check.Matches, `(?ms).*collection is empty.*`)
312 for _, path := range []string{
313 "/by_id/" + arvadostest.FooCollectionPDH,
314 "/by_id/" + arvadostest.FooCollectionPDH + "/",
315 "/by_id/" + arvadostest.FooCollection,
316 "/by_id/" + arvadostest.FooCollection + "/",
318 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
319 c.Check(stdout, check.Matches, `(?ms).*\s+foo\s+3 .*`)
323 func (s *IntegrationSuite) TestCadaverUsersDir(c *check.C) {
324 for _, path := range []string{"/"} {
325 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
326 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+by_id\s+0 .*`)
327 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+users\s+0 .*`)
329 for _, path := range []string{"/users", "/users/"} {
330 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
331 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+active.*`)
333 for _, path := range []string{"/users/active", "/users/active/"} {
334 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
335 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+A Project\s+0 .*`)
336 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+bar_file\s+0 .*`)
338 for _, path := range []string{"/users/admin", "/users/doesnotexist", "/users/doesnotexist/"} {
339 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
340 c.Check(stdout, check.Matches, `(?ms).*404 Not Found.*`)
344 func (s *IntegrationSuite) runCadaver(c *check.C, password, path, stdin string) string {
345 tempdir, err := ioutil.TempDir("", "keep-web-test-")
346 c.Assert(err, check.IsNil)
347 defer os.RemoveAll(tempdir)
349 cmd := exec.Command("cadaver", s.testServer.URL+path)
351 // cadaver won't try username/password authentication
352 // unless the server responds 401 to an
353 // unauthenticated request, which it only does in
354 // AttachmentOnlyHost, TrustAllContent, and
355 // per-collection vhost cases.
356 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = s.testServer.URL[7:]
358 cmd.Env = append(os.Environ(), "HOME="+tempdir)
359 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
360 c.Assert(err, check.IsNil)
361 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
362 c.Assert(err, check.IsNil)
363 c.Assert(f.Close(), check.IsNil)
365 cmd.Stdin = bytes.NewBufferString(stdin)
366 stdout, err := cmd.StdoutPipe()
367 c.Assert(err, check.Equals, nil)
368 cmd.Stderr = cmd.Stdout
372 _, err = io.Copy(&buf, stdout)
373 c.Check(err, check.Equals, nil)
375 c.Check(err, check.Equals, nil)