Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: gslib/tests/test_signurl.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gslib/tests/test_setmeta.py ('k') | gslib/tests/test_stat.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 # -*- coding: utf-8 -*-
2 # Copyright 2014 Google Inc. All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Tests for signurl command."""
16 from datetime import timedelta
17 import pkgutil
18
19 import gslib.commands.signurl
20 from gslib.commands.signurl import HAVE_OPENSSL
21 from gslib.exception import CommandException
22 import gslib.tests.testcase as testcase
23 from gslib.tests.testcase.integration_testcase import SkipForS3
24 from gslib.tests.util import ObjectToURI as suri
25 from gslib.tests.util import unittest
26
27
28 # pylint: disable=protected-access
29 @unittest.skipUnless(HAVE_OPENSSL, 'signurl requires pyopenssl.')
30 @SkipForS3('Signed URLs are only supported for gs:// URLs.')
31 class TestSignUrl(testcase.GsUtilIntegrationTestCase):
32 """Integration tests for signurl command."""
33
34 def _GetKsFile(self):
35 if not hasattr(self, 'ks_file'):
36 # Dummy pkcs12 keystore generated with the command
37
38 # openssl req -new -passout pass:notasecret -batch \
39 # -x509 -keyout signed_url_test.key -out signed_url_test.pem \
40 # -subj '/CN=test.apps.googleusercontent.com'
41
42 # &&
43
44 # openssl pkcs12 -export -passin pass:notasecret \
45 # -passout pass:notasecret -inkey signed_url_test.key \
46 # -in signed_url_test.pem -out test.p12
47
48 # &&
49
50 # rm signed_url_test.key signed_url_test.pem
51 contents = pkgutil.get_data('gslib', 'tests/test_data/test.p12')
52 self.ks_file = self.CreateTempFile(contents=contents)
53 return self.ks_file
54
55 def testSignUrlOutput(self):
56 """Tests signurl output of a sample object."""
57
58 object_url = self.CreateObject(contents='z')
59 stdout = self.RunGsUtil(['signurl', '-p', 'notasecret',
60 self._GetKsFile(), suri(object_url)],
61 return_stdout=True)
62
63 self.assertIn(object_url.uri, stdout)
64 self.assertIn('test@developer.gserviceaccount.com', stdout)
65 self.assertIn('Expires=', stdout)
66 self.assertIn('\tGET\t', stdout)
67
68 stdout = self.RunGsUtil(['signurl', '-m', 'PUT', '-p',
69 'notasecret', self._GetKsFile(),
70 'gs://test/test.txt'], return_stdout=True)
71
72 self.assertIn('test@developer.gserviceaccount.com', stdout)
73 self.assertIn('Expires=', stdout)
74 self.assertIn('\tPUT\t', stdout)
75
76 def testSignUrlWithWildcard(self):
77 objs = ['test1', 'test2', 'test3']
78 bucket = self.CreateBucket()
79 obj_urls = []
80
81 for obj_name in objs:
82 obj_urls.append(self.CreateObject(bucket_uri=bucket,
83 object_name=obj_name, contents=''))
84
85 stdout = self.RunGsUtil(['signurl', '-p',
86 'notasecret', self._GetKsFile(),
87 suri(bucket) + '/*'], return_stdout=True)
88
89 # Header, 3 signed urls, trailing newline
90 self.assertEquals(len(stdout.split('\n')), 5)
91
92 for obj_url in obj_urls:
93 self.assertIn(suri(obj_url), stdout)
94
95 def testSignUrlOfNonObjectUrl(self):
96 """Tests the signurl output of a non-existent file."""
97 self.RunGsUtil(['signurl', self._GetKsFile(), 'gs://'],
98 expected_status=1, stdin='notasecret')
99 self.RunGsUtil(['signurl', 'file://tmp/abc'], expected_status=1)
100
101
102 @unittest.skipUnless(HAVE_OPENSSL, 'signurl requires pyopenssl.')
103 class UnitTestSignUrl(testcase.GsUtilUnitTestCase):
104 """Unit tests for the signurl command."""
105
106 def setUp(self):
107 super(UnitTestSignUrl, self).setUp()
108 self.ks_contents = pkgutil.get_data('gslib', 'tests/test_data/test.p12')
109
110 def testDurationSpec(self):
111 tests = [('1h', timedelta(hours=1)),
112 ('2d', timedelta(days=2)),
113 ('5D', timedelta(days=5)),
114 ('35s', timedelta(seconds=35)),
115 ('1h', timedelta(hours=1)),
116 ('33', timedelta(hours=33)),
117 ('22m', timedelta(minutes=22)),
118 ('3.7', None),
119 ('27Z', None),
120 ]
121
122 for inp, expected in tests:
123 try:
124 td = gslib.commands.signurl._DurationToTimeDelta(inp)
125 self.assertEquals(td, expected)
126 except CommandException:
127 if expected is not None:
128 self.fail('{0} failed to parse')
129
130 def testSignPut(self):
131 """Tests the return value of the _GenSignedUrl function with \
132 a PUT method."""
133
134 expected = ('https://storage.googleapis.com/test/test.txt?'
135 'GoogleAccessId=test@developer.gserviceaccount.com'
136 '&Expires=1391816302&Signature=A6QbgTA8cXZCtjy2xCr401bdi0e'
137 '7zChTBQ6BX61L7AfytTGEQDMD%2BbvOQKjX7%2FsEh77cmzcSxOEKqTLUD'
138 'bbkPgPqW3j8sGPSRX9VM58bgj1vt9yU8cRKoegFHXAqsATx2G5rc%2FvEl'
139 'iFp9UWMfVj5TaukqlBAVuzZWlyx0aQa9tCKXRtC9YcxORxG41RfiowA2kd8'
140 'XBTQt4M9XTzpVyr5rVMzfr2LvtGf9UAJvlt8p6T6nThl2vy9%2FwBoPcMFa'
141 'OWQcGTagwjyKWDcI1vQPIFQLGftAcv3QnGZxZTtg8pZW%2FIxRJrBhfFfcA'
142 'c62hDKyaU2YssSMy%2FjUJynWx3TIiJjhg%3D%3D')
143
144 expiration = 1391816302
145 ks, client_id = (gslib.commands.signurl
146 ._ReadKeystore(self.ks_contents, 'notasecret'))
147 signed_url = (gslib.commands.signurl
148 ._GenSignedUrl(ks.get_privatekey(),
149 client_id, 'PUT', '',
150 '', expiration, 'test/test.txt'))
151 self.assertEquals(expected, signed_url)
152
153 def testSignurlPutContentype(self):
154 """Tests the return value of the _GenSignedUrl function with \
155 a PUT method and specified content type."""
156
157 expected = ('https://storage.googleapis.com/test/test.txt?'
158 'GoogleAccessId=test@developer.gserviceaccount.com&'
159 'Expires=1391816302&Signature=APn%2BCCVcQrfc1fKQXrs'
160 'PEZFj9%2FmASO%2BolR8xwgBY6PbWMkcCtrUVFBauP6t4NxqZO'
161 'UnbOFYTZYzul0RC57ZkEWJp3VcyDIHcn6usEE%2FTzUHhbDCDW'
162 'awAkZS7p8kO8IIACuJlF5s9xZmZzaEBtzF0%2BBOsGgBPBlg2y'
163 'zrhFB6cyyAwNiUgmhLQaVkdobnSwtI5QJkvXoIjJb6hhLiVbLC'
164 'rWdgSZVusjAKGlWCJsM%2B4TkCR%2Bi8AnrkECngcMHuJ9mYbS'
165 'XI1VfEmcnRVcfkKkJGZGctaDIWK%2FMTEmfYCW6USt3Zk2WowJ'
166 'SGuJHqEcFz0kyfAlkpmG%2Fl5E1FQROYqLN2kZQ%3D%3D')
167
168 expiration = 1391816302
169 ks, client_id = (gslib.commands.signurl
170 ._ReadKeystore(self.ks_contents,
171 'notasecret'))
172 signed_url = (gslib.commands.signurl
173 ._GenSignedUrl(ks.get_privatekey(),
174 client_id, 'PUT', '',
175 'text/plain', expiration,
176 'test/test.txt'))
177 self.assertEquals(expected, signed_url)
178
179 def testSignurlGet(self):
180 """Tests the return value of the _GenSignedUrl function with \
181 a GET method."""
182
183 expected = ('https://storage.googleapis.com/test/test.txt?'
184 'GoogleAccessId=test@developer.gserviceaccount.com&'
185 'Expires=0&Signature=TCZwe32cU%2BMksmLiSY9shHXQjLs1'
186 'F3y%2F%2F1M0UhiK4qsPRVNZVwI7YWvv2qa2Xa%2BVBBafboF0'
187 '1%2BWvx3ZG316pwpNIRR6y7jNnE0LvQmHE8afbm2VYCi%2B2JS'
188 'ZK2YZFJAyEek8si53jhYQEmaRq1zPfGbX84B2FJ8v4iI%2FTC1'
189 'I9OE5vHF0sWwIR9d73JDrFLjaync7QYFWRExdwvqlQX%2BPO3r'
190 'OG9Ns%2BcQFIN7npnsVjH28yNY9gBzXya8LYmNvUx6bWHWZMiu'
191 'fLwDZ0jejNeDZTOfQGRM%2B0vY7NslzaT06W1wo8P7McSkAZEl'
192 'DCbhR0Vo1fturPMwmAhi88f0qzRzywbg%3D%3D')
193
194 expiration = 0
195 ks, client_id = (gslib.commands.signurl
196 ._ReadKeystore(self.ks_contents,
197 'notasecret'))
198 signed_url = (gslib.commands.signurl
199 ._GenSignedUrl(ks.get_privatekey(),
200 client_id, 'GET', '',
201 '', expiration, 'test/test.txt'))
202 self.assertEquals(expected, signed_url)
OLDNEW
« no previous file with comments | « gslib/tests/test_setmeta.py ('k') | gslib/tests/test_stat.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698