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 newdir0/testfile newdir1/\n",
176 match: `(?ms).*Moving .* succeeded.*`,
180 cmd: "move newdir1 newdir1/\n",
181 match: `(?ms).*Moving .* failed.*`,
185 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
186 match: `(?ms).*succeeded.*`,
191 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
192 match: `(?ms).*Uploading .* succeeded.*`,
196 cmd: "mkcol newdir2\n",
197 match: `(?ms).*Creating .* succeeded.*`,
201 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
202 match: `(?ms).*Uploading .* succeeded.*`,
206 cmd: "copy newdir2/testfile2 testfile3\n",
207 match: `(?ms).*succeeded.*`,
211 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
212 match: `(?ms).*succeeded.*`,
217 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
218 match: `(?ms).*succeeded.*`,
223 cmd: "rmcol newdir2\n",
224 match: `(?ms).*Deleting collection .* succeeded.*`,
228 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
229 match: `(?ms).*Downloading .* failed.*`,
232 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
233 cmd: "put '" + localfile.Name() + "' foo\n",
234 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
238 cmd: "put '" + localfile.Name() + "' foo\n",
239 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
243 cmd: "move foo bar\n",
244 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
248 cmd: "copy foo bar\n",
249 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
254 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
259 match: `(?ms).*Locking .* failed:.*405 Method Not Allowed.*`,
262 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
263 if skip != nil && skip(trial.path) {
268 os.Remove(checkfile.Name())
270 stdout := s.runCadaver(c, password, trial.path, trial.cmd)
271 c.Check(stdout, check.Matches, trial.match)
273 if trial.data == nil {
276 checkfile, err = os.Open(checkfile.Name())
277 c.Assert(err, check.IsNil)
278 checkfile.Seek(0, os.SEEK_SET)
279 got, err := ioutil.ReadAll(checkfile)
280 c.Check(got, check.DeepEquals, trial.data)
281 c.Check(err, check.IsNil)
285 func (s *IntegrationSuite) TestCadaverByID(c *check.C) {
286 for _, path := range []string{"/by_id", "/by_id/"} {
287 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
288 c.Check(stdout, check.Matches, `(?ms).*collection is empty.*`)
290 for _, path := range []string{
291 "/by_id/" + arvadostest.FooPdh,
292 "/by_id/" + arvadostest.FooPdh + "/",
293 "/by_id/" + arvadostest.FooCollection,
294 "/by_id/" + arvadostest.FooCollection + "/",
296 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
297 c.Check(stdout, check.Matches, `(?ms).*\s+foo\s+3 .*`)
301 func (s *IntegrationSuite) TestCadaverUsersDir(c *check.C) {
302 for _, path := range []string{"/"} {
303 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
304 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+by_id\s+0 .*`)
305 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+users\s+0 .*`)
307 for _, path := range []string{"/users", "/users/"} {
308 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
309 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+active.*`)
311 for _, path := range []string{"/users/active", "/users/active/"} {
312 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
313 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+A Project\s+0 .*`)
314 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+bar_file\s+0 .*`)
316 for _, path := range []string{"/users/admin", "/users/doesnotexist", "/users/doesnotexist/"} {
317 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
318 c.Check(stdout, check.Matches, `(?ms).*404 Not Found.*`)
322 func (s *IntegrationSuite) runCadaver(c *check.C, password, path, stdin string) string {
323 tempdir, err := ioutil.TempDir("", "keep-web-test-")
324 c.Assert(err, check.IsNil)
325 defer os.RemoveAll(tempdir)
327 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+path)
329 // cadaver won't try username/password authentication
330 // unless the server responds 401 to an
331 // unauthenticated request, which it only does in
332 // AttachmentOnlyHost, TrustAllContent, and
333 // per-collection vhost cases.
334 s.testServer.Config.AttachmentOnlyHost = s.testServer.Addr
336 cmd.Env = append(os.Environ(), "HOME="+tempdir)
337 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
338 c.Assert(err, check.IsNil)
339 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
340 c.Assert(err, check.IsNil)
341 c.Assert(f.Close(), check.IsNil)
343 cmd.Stdin = bytes.NewBufferString(stdin)
344 stdout, err := cmd.StdoutPipe()
345 c.Assert(err, check.Equals, nil)
346 cmd.Stderr = cmd.Stdout
350 _, err = io.Copy(&buf, stdout)
351 c.Check(err, check.Equals, nil)
353 c.Check(err, check.Equals, nil)