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 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")
58 tempdir, err := ioutil.TempDir("", "keep-web-test-")
59 c.Assert(err, check.IsNil)
60 defer os.RemoveAll(tempdir)
62 localfile, err := ioutil.TempFile(tempdir, "localfile")
63 c.Assert(err, check.IsNil)
64 localfile.Write(testdata)
66 emptyfile, err := ioutil.TempFile(tempdir, "emptyfile")
67 c.Assert(err, check.IsNil)
69 checkfile, err := ioutil.TempFile(tempdir, "checkfile")
70 c.Assert(err, check.IsNil)
72 var newCollection arvados.Collection
73 arv := arvados.NewClientFromEnv()
74 arv.AuthToken = arvadostest.ActiveToken
75 err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
76 c.Assert(err, check.IsNil)
78 readPath, writePath, pdhPath := pathFunc(newCollection)
80 matchToday := time.Now().Format("Jan +2")
82 type testcase struct {
88 for _, trial := range []testcase{
92 match: `(?ms).*dir1 *0 .*`,
97 match: `(?ms).*bar *3.*foo *3 .*`,
100 path: readPath + "_/dir1",
102 match: `(?ms).*bar *3.*foo *3 .*`,
105 path: readPath + "dir1/",
107 match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
111 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
112 match: `(?ms).*Not Found.*`,
116 cmd: "put '" + emptyfile.Name() + "' emptyfile\n",
117 match: `(?ms).*Uploading .* succeeded.*`,
121 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
122 match: `(?ms).*Downloading .* succeeded.*`,
127 cmd: "put '" + localfile.Name() + "' testfile\n",
128 match: `(?ms).*Uploading .* succeeded.*`,
132 cmd: "get testfile '" + checkfile.Name() + "'\n",
133 match: `(?ms).*succeeded.*`,
138 cmd: "move testfile newdir0/\n",
139 match: `(?ms).*Moving .* succeeded.*`,
143 cmd: "move testfile newdir0/\n",
144 match: `(?ms).*Moving .* failed.*`,
149 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
153 cmd: "move newdir0/testfile emptyfile/bogus/\n",
154 match: `(?ms).*Moving .* failed.*`,
158 cmd: "mkcol newdir1\n",
159 match: `(?ms).*Creating .* succeeded.*`,
163 cmd: "move newdir0/testfile newdir1/\n",
164 match: `(?ms).*Moving .* succeeded.*`,
168 cmd: "move newdir1 newdir1/\n",
169 match: `(?ms).*Moving .* failed.*`,
173 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
174 match: `(?ms).*succeeded.*`,
179 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
180 match: `(?ms).*Uploading .* succeeded.*`,
184 cmd: "mkcol newdir2\n",
185 match: `(?ms).*Creating .* succeeded.*`,
189 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
190 match: `(?ms).*Uploading .* succeeded.*`,
194 cmd: "copy newdir2/testfile2 testfile3\n",
195 match: `(?ms).*succeeded.*`,
199 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
200 match: `(?ms).*succeeded.*`,
205 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
206 match: `(?ms).*succeeded.*`,
211 cmd: "rmcol newdir2\n",
212 match: `(?ms).*Deleting collection .* succeeded.*`,
216 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
217 match: `(?ms).*Downloading .* failed.*`,
220 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
221 cmd: "put '" + localfile.Name() + "' foo\n",
222 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
226 cmd: "put '" + localfile.Name() + "' foo\n",
227 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
231 cmd: "move foo bar\n",
232 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
236 cmd: "copy foo bar\n",
237 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
242 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
245 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
246 if skip != nil && skip(trial.path) {
251 os.Remove(checkfile.Name())
253 stdout := s.runCadaver(c, password, trial.path, trial.cmd)
254 c.Check(stdout, check.Matches, trial.match)
256 if trial.data == nil {
259 checkfile, err = os.Open(checkfile.Name())
260 c.Assert(err, check.IsNil)
261 checkfile.Seek(0, os.SEEK_SET)
262 got, err := ioutil.ReadAll(checkfile)
263 c.Check(got, check.DeepEquals, trial.data)
264 c.Check(err, check.IsNil)
268 func (s *IntegrationSuite) TestCadaverByID(c *check.C) {
269 for _, path := range []string{"/by_id", "/by_id/"} {
270 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
271 c.Check(stdout, check.Matches, `(?ms).*collection is empty.*`)
273 for _, path := range []string{
274 "/by_id/" + arvadostest.FooPdh,
275 "/by_id/" + arvadostest.FooPdh + "/",
276 "/by_id/" + arvadostest.FooCollection,
277 "/by_id/" + arvadostest.FooCollection + "/",
279 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
280 c.Check(stdout, check.Matches, `(?ms).*\s+foo\s+3 .*`)
284 func (s *IntegrationSuite) TestCadaverUsersDir(c *check.C) {
285 for _, path := range []string{"/"} {
286 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
287 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+by_id\s+0 .*`)
288 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+users\s+0 .*`)
290 for _, path := range []string{"/users", "/users/"} {
291 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
292 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+active.*`)
294 for _, path := range []string{"/users/active", "/users/active/"} {
295 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
296 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+A Project\s+0 .*`)
297 c.Check(stdout, check.Matches, `(?ms).*Coll:\s+bar_file\s+0 .*`)
299 for _, path := range []string{"/users/admin", "/users/doesnotexist", "/users/doesnotexist/"} {
300 stdout := s.runCadaver(c, arvadostest.ActiveToken, path, "ls")
301 c.Check(stdout, check.Matches, `(?ms).*404 Not Found.*`)
305 func (s *IntegrationSuite) runCadaver(c *check.C, password, path, stdin string) string {
306 tempdir, err := ioutil.TempDir("", "keep-web-test-")
307 c.Assert(err, check.IsNil)
308 defer os.RemoveAll(tempdir)
310 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+path)
312 // cadaver won't try username/password authentication
313 // unless the server responds 401 to an
314 // unauthenticated request, which it only does in
315 // AttachmentOnlyHost, TrustAllContent, and
316 // per-collection vhost cases.
317 s.testServer.Config.AttachmentOnlyHost = s.testServer.Addr
319 cmd.Env = append(os.Environ(), "HOME="+tempdir)
320 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
321 c.Assert(err, check.IsNil)
322 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
323 c.Assert(err, check.IsNil)
324 c.Assert(f.Close(), check.IsNil)
326 cmd.Stdin = bytes.NewBufferString(stdin)
327 stdout, err := cmd.StdoutPipe()
328 c.Assert(err, check.Equals, nil)
329 cmd.Stderr = cmd.Stdout
333 _, err = io.Copy(&buf, stdout)
334 c.Check(err, check.Equals, nil)
336 c.Check(err, check.Equals, nil)