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 s.testCadaver(c, arvadostest.ActiveToken, func(newCollection arvados.Collection) (string, string, string) {
44 r := "/users/active/foo_file_in_dir/"
45 w := "/users/active/" + newCollection.Name
46 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
51 func (s *IntegrationSuite) testCadaver(c *check.C, password string, pathFunc func(arvados.Collection) (string, string, string)) {
52 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")
54 tempdir, err := ioutil.TempDir("", "keep-web-test-")
55 c.Assert(err, check.IsNil)
56 defer os.RemoveAll(tempdir)
58 localfile, err := ioutil.TempFile(tempdir, "localfile")
59 c.Assert(err, check.IsNil)
60 localfile.Write(testdata)
62 emptyfile, err := ioutil.TempFile(tempdir, "emptyfile")
63 c.Assert(err, check.IsNil)
65 checkfile, err := ioutil.TempFile(tempdir, "checkfile")
66 c.Assert(err, check.IsNil)
68 var newCollection arvados.Collection
69 arv := arvados.NewClientFromEnv()
70 arv.AuthToken = arvadostest.ActiveToken
71 err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
72 c.Assert(err, check.IsNil)
74 readPath, writePath, pdhPath := pathFunc(newCollection)
76 matchToday := time.Now().Format("Jan +2")
78 type testcase struct {
84 for _, trial := range []testcase{
88 match: `(?ms).*dir1 *0 .*`,
93 match: `(?ms).*bar *3.*foo *3 .*`,
96 path: readPath + "_/dir1",
98 match: `(?ms).*bar *3.*foo *3 .*`,
101 path: readPath + "dir1/",
103 match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
107 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
108 match: `(?ms).*Not Found.*`,
112 cmd: "put '" + emptyfile.Name() + "' emptyfile\n",
113 match: `(?ms).*Uploading .* succeeded.*`,
117 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
118 match: `(?ms).*Downloading .* succeeded.*`,
123 cmd: "put '" + localfile.Name() + "' testfile\n",
124 match: `(?ms).*Uploading .* succeeded.*`,
128 cmd: "get testfile '" + checkfile.Name() + "'\n",
129 match: `(?ms).*succeeded.*`,
134 cmd: "move testfile newdir0/\n",
135 match: `(?ms).*Moving .* succeeded.*`,
139 cmd: "move testfile newdir0/\n",
140 match: `(?ms).*Moving .* failed.*`,
145 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
149 cmd: "move newdir0/testfile emptyfile/bogus/\n",
150 match: `(?ms).*Moving .* failed.*`,
154 cmd: "mkcol newdir1\n",
155 match: `(?ms).*Creating .* succeeded.*`,
159 cmd: "move newdir0/testfile newdir1/\n",
160 match: `(?ms).*Moving .* succeeded.*`,
164 cmd: "move newdir1 newdir1/\n",
165 match: `(?ms).*Moving .* failed.*`,
169 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
170 match: `(?ms).*succeeded.*`,
175 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
176 match: `(?ms).*Uploading .* succeeded.*`,
180 cmd: "mkcol newdir2\n",
181 match: `(?ms).*Creating .* succeeded.*`,
185 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
186 match: `(?ms).*Uploading .* succeeded.*`,
190 cmd: "copy newdir2/testfile2 testfile3\n",
191 match: `(?ms).*succeeded.*`,
195 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
196 match: `(?ms).*succeeded.*`,
201 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
202 match: `(?ms).*succeeded.*`,
207 cmd: "rmcol newdir2\n",
208 match: `(?ms).*Deleting collection .* succeeded.*`,
212 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
213 match: `(?ms).*Downloading .* failed.*`,
216 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
217 cmd: "put '" + localfile.Name() + "' foo\n",
218 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
222 cmd: "put '" + localfile.Name() + "' foo\n",
223 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
227 cmd: "move foo bar\n",
228 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
232 cmd: "copy foo bar\n",
233 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
238 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
241 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
243 os.Remove(checkfile.Name())
245 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
247 // cadaver won't try username/password
248 // authentication unless the server responds
249 // 401 to an unauthenticated request, which it
250 // only does in AttachmentOnlyHost,
251 // TrustAllContent, and per-collection vhost
253 s.testServer.Config.AttachmentOnlyHost = s.testServer.Addr
255 cmd.Env = append(os.Environ(), "HOME="+tempdir)
256 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
257 c.Assert(err, check.IsNil)
258 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
259 c.Assert(err, check.IsNil)
260 c.Assert(f.Close(), check.IsNil)
262 cmd.Stdin = bytes.NewBufferString(trial.cmd)
263 stdout, err := cmd.StdoutPipe()
264 c.Assert(err, check.Equals, nil)
265 cmd.Stderr = cmd.Stdout
269 _, err = io.Copy(&buf, stdout)
270 c.Check(err, check.Equals, nil)
272 c.Check(err, check.Equals, nil)
273 c.Check(buf.String(), check.Matches, trial.match)
275 if trial.data == nil {
278 checkfile, err = os.Open(checkfile.Name())
279 c.Assert(err, check.IsNil)
280 checkfile.Seek(0, os.SEEK_SET)
281 got, err := ioutil.ReadAll(checkfile)
282 c.Check(got, check.DeepEquals, trial.data)
283 c.Check(err, check.IsNil)