1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
21 check "gopkg.in/check.v1"
24 func (s *IntegrationSuite) TestCadaverHTTPAuth(c *check.C) {
25 s.testCadaver(c, arvadostest.ActiveToken, func(newCollection arvados.Collection) (string, string, string) {
26 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/"
27 w := "/c=" + newCollection.UUID + "/"
28 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
33 func (s *IntegrationSuite) TestCadaverPathAuth(c *check.C) {
34 s.testCadaver(c, "", func(newCollection arvados.Collection) (string, string, string) {
35 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
36 w := "/c=" + newCollection.UUID + "/t=" + arvadostest.ActiveToken + "/"
37 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arvadostest.ActiveToken + "/"
42 func (s *IntegrationSuite) TestCadaverUserProject(c *check.C) {
43 rpath := "/users/active/foo_file_in_dir/"
44 s.testCadaver(c, arvadostest.ActiveToken, func(newCollection arvados.Collection) (string, string, string) {
45 wpath := "/users/active/" + newCollection.Name
46 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
47 return rpath, wpath, pdh
48 }, func(path string) bool {
49 // Skip tests that rely on writes, because /users/
51 return !strings.HasPrefix(path, rpath) || strings.HasPrefix(path, rpath+"_/")
55 func (s *IntegrationSuite) testCadaver(c *check.C, password string, pathFunc func(arvados.Collection) (string, string, string), skip func(string) bool) {
56 s.testServer.Config.AnonymousTokens = []string{arvadostest.AnonymousToken}
58 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")
60 tempdir, err := ioutil.TempDir("", "keep-web-test-")
61 c.Assert(err, check.IsNil)
62 defer os.RemoveAll(tempdir)
64 localfile, err := ioutil.TempFile(tempdir, "localfile")
65 c.Assert(err, check.IsNil)
66 localfile.Write(testdata)
68 emptyfile, err := ioutil.TempFile(tempdir, "emptyfile")
69 c.Assert(err, check.IsNil)
71 checkfile, err := ioutil.TempFile(tempdir, "checkfile")
72 c.Assert(err, check.IsNil)
74 var newCollection arvados.Collection
75 arv := arvados.NewClientFromEnv()
76 arv.AuthToken = arvadostest.ActiveToken
77 err = arv.RequestAndDecode(&newCollection, "POST", "arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
78 c.Assert(err, check.IsNil)
80 readPath, writePath, pdhPath := pathFunc(newCollection)
82 matchToday := time.Now().Format("Jan +2")
84 type testcase struct {
90 for _, trial := range []testcase{
94 match: `(?ms).*dir1 *0 .*`,
99 match: `(?ms).*bar *3.*foo *3 .*`,
102 path: readPath + "_/dir1",
104 match: `(?ms).*bar *3.*foo *3 .*`,
107 path: readPath + "dir1/",
109 match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
113 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
114 match: `(?ms).*Not Found.*`,
118 cmd: "put '" + emptyfile.Name() + "' emptyfile\n",
119 match: `(?ms).*Uploading .* succeeded.*`,
123 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
124 match: `(?ms).*Downloading .* succeeded.*`,
129 cmd: "put '" + localfile.Name() + "' testfile\n",
130 match: `(?ms).*Uploading .* succeeded.*`,
134 cmd: "get testfile '" + checkfile.Name() + "'\n",
135 match: `(?ms).*succeeded.*`,
140 cmd: "move testfile newdir0/\n",
141 match: `(?ms).*Moving .* succeeded.*`,
145 cmd: "move testfile newdir0/\n",
146 match: `(?ms).*Moving .* failed.*`,
150 cmd: "lock newdir0/testfile\n",
151 match: `(?ms).*Locking .* succeeded.*`,
155 cmd: "unlock newdir0/testfile\nasdf\n",
156 match: `(?ms).*Unlocking .* succeeded.*`,
161 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
165 cmd: "move newdir0/testfile emptyfile/bogus/\n",
166 match: `(?ms).*Moving .* failed.*`,
170 cmd: "mkcol newdir1\n",
171 match: `(?ms).*Creating .* succeeded.*`,
175 cmd: "move newdir1/ newdir1x/\n",
176 match: `(?ms).*Moving .* succeeded.*`,
180 cmd: "move newdir1x newdir1\n",
181 match: `(?ms).*Moving .* succeeded.*`,
185 cmd: "move newdir0/testfile newdir1/\n",
186 match: `(?ms).*Moving .* succeeded.*`,
190 cmd: "move newdir1 newdir1/\n",
191 match: `(?ms).*Moving .* failed.*`,
195 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
196 match: `(?ms).*succeeded.*`,
201 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
202 match: `(?ms).*Uploading .* succeeded.*`,
206 cmd: "mkcol newdir2\n",
207 match: `(?ms).*Creating .* succeeded.*`,
211 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
212 match: `(?ms).*Uploading .* succeeded.*`,
216 cmd: "copy newdir2/testfile2 testfile3\n",
217 match: `(?ms).*succeeded.*`,
221 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
222 match: `(?ms).*succeeded.*`,
227 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
228 match: `(?ms).*succeeded.*`,
233 cmd: "rmcol newdir2\n",
234 match: `(?ms).*Deleting collection .* succeeded.*`,
238 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
239 match: `(?ms).*Downloading .* failed.*`,
242 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
243 cmd: "put '" + localfile.Name() + "' foo\n",
244 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
248 cmd: "put '" + localfile.Name() + "' foo\n",
249 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
253 cmd: "move foo bar\n",
254 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
258 cmd: "copy foo bar\n",
259 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
264 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
269 match: `(?ms).*Locking .* failed:.*405 Method Not Allowed.*`,
272 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
273 if skip != nil && skip(trial.path) {
278 os.Remove(checkfile.Name())
280 stdout := s.runCadaver(c, password, trial.path, trial.cmd)
281 c.Check(stdout, check.Matches, trial.match)
283 if trial.data == nil {
286 checkfile, err = os.Open(checkfile.Name())
287 c.Assert(err, check.IsNil)
288 checkfile.Seek(0, os.SEEK_SET)
289 got, err := ioutil.ReadAll(checkfile)
290 c.Check(got, check.DeepEquals, trial.data)
291 c.Check(err, check.IsNil)
295 func (s *IntegrationSuite) TestCadaverByID(c *check.C) {
296 for _, path := range []string{"/by_id", "/by_id/"} {
297 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
298 c.Check(stdout, check.Matches, `(?ms).*collection is empty.*`)
300 for _, path := range []string{
301 "/by_id/" + arvadostest.FooPdh,
302 "/by_id/" + arvadostest.FooPdh + "/",
303 "/by_id/" + arvadostest.FooCollection,
304 "/by_id/" + arvadostest.FooCollection + "/",
306 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
307 c.Check(stdout, check.Matches, `(?ms).*\s+foo\s+3 .*`)
311 func (s *IntegrationSuite) TestCadaverUsersDir(c *check.C) {
312 for _, path := range []string{"/"} {
313 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
314 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+by_id\s+0 .*`)
315 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+users\s+0 .*`)
317 for _, path := range []string{"/users", "/users/"} {
318 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
319 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+active.*`)
321 for _, path := range []string{"/users/active", "/users/active/"} {
322 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
323 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+A Project\s+0 .*`)
324 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+bar_file\s+0 .*`)
326 for _, path := range []string{"/users/admin", "/users/doesnotexist", "/users/doesnotexist/"} {
327 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
328 c.Check(stdout, check.Matches, `(?ms).*404 Not Found.*`)
332 func (s *IntegrationSuite) runCadaver(c *check.C, password, path, stdin string) string {
333 tempdir, err := ioutil.TempDir("", "keep-web-test-")
334 c.Assert(err, check.IsNil)
335 defer os.RemoveAll(tempdir)
337 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+path)
339 // cadaver won't try username/password authentication
340 // unless the server responds 401 to an
341 // unauthenticated request, which it only does in
342 // AttachmentOnlyHost, TrustAllContent, and
343 // per-collection vhost cases.
344 s.testServer.Config.AttachmentOnlyHost = s.testServer.Addr
346 cmd.Env = append(os.Environ(), "HOME="+tempdir)
347 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
348 c.Assert(err, check.IsNil)
349 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
350 c.Assert(err, check.IsNil)
351 c.Assert(f.Close(), check.IsNil)
353 cmd.Stdin = bytes.NewBufferString(stdin)
354 stdout, err := cmd.StdoutPipe()
355 c.Assert(err, check.Equals, nil)
356 cmd.Stderr = cmd.Stdout
360 _, err = io.Copy(&buf, stdout)
361 c.Check(err, check.Equals, nil)
363 c.Check(err, check.Equals, nil)