~ubuntu-branches/ubuntu/trusty/python-keystoneclient/trusty-proposed

« back to all changes in this revision

Viewing changes to keystoneclient/tests/test_ec2utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Adam Gandelman, Chuck Short
  • Date: 2013-11-14 10:51:32 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20131114105132-p1o428l7fclasv9e
Tags: 1:0.4.1-0ubuntu1
[ Adam Gandelman ]
* debian/patches: Refreshed.
* debian/patches/use-mox-dependency.patch: Use mox instead of mox3
  dependency.

[ Chuck Short ]
* New upstream release.
* debian/control:
  - open icehouse release.
  - Dropped python-d2to1 and python-httplib2 dependency.
* debian/patches/skip-tests-ubuntu.patch: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 OpenStack Foundation
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
# not use this file except in compliance with the License. You may obtain
 
7
# a copy of the License at
 
8
#
 
9
#      http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
# License for the specific language governing permissions and limitations
 
15
# under the License.
 
16
 
 
17
from __future__ import unicode_literals
 
18
 
 
19
import testtools
 
20
 
 
21
from keystoneclient.contrib.ec2 import utils
 
22
 
 
23
 
 
24
class Ec2SignerTest(testtools.TestCase):
 
25
 
 
26
    def setUp(self):
 
27
        super(Ec2SignerTest, self).setUp()
 
28
        self.access = '966afbde20b84200ae4e62e09acf46b2'
 
29
        self.secret = '89cdf9e94e2643cab35b8b8ac5a51f83'
 
30
        self.signer = utils.Ec2Signer(self.secret)
 
31
 
 
32
    def tearDown(self):
 
33
        super(Ec2SignerTest, self).tearDown()
 
34
 
 
35
    def test_v4_creds_header(self):
 
36
        auth_str = 'AWS4-HMAC-SHA256 blah'
 
37
        credentials = {'host': '127.0.0.1',
 
38
                       'verb': 'GET',
 
39
                       'path': '/v1/',
 
40
                       'params': {},
 
41
                       'headers': {'Authorization': auth_str}}
 
42
        self.assertTrue(self.signer._v4_creds(credentials))
 
43
 
 
44
    def test_v4_creds_param(self):
 
45
        credentials = {'host': '127.0.0.1',
 
46
                       'verb': 'GET',
 
47
                       'path': '/v1/',
 
48
                       'params': {'X-Amz-Algorithm': 'AWS4-HMAC-SHA256'},
 
49
                       'headers': {}}
 
50
        self.assertTrue(self.signer._v4_creds(credentials))
 
51
 
 
52
    def test_v4_creds_false(self):
 
53
        credentials = {'host': '127.0.0.1',
 
54
                       'verb': 'GET',
 
55
                       'path': '/v1/',
 
56
                       'params': {'SignatureVersion': '0',
 
57
                                  'AWSAccessKeyId': self.access,
 
58
                                  'Timestamp': '2012-11-27T11:47:02Z',
 
59
                                  'Action': 'Foo'}}
 
60
        self.assertFalse(self.signer._v4_creds(credentials))
 
61
 
 
62
    def test_generate_0(self):
 
63
        """Test generate function for v0 signature."""
 
64
        credentials = {'host': '127.0.0.1',
 
65
                       'verb': 'GET',
 
66
                       'path': '/v1/',
 
67
                       'params': {'SignatureVersion': '0',
 
68
                                  'AWSAccessKeyId': self.access,
 
69
                                  'Timestamp': '2012-11-27T11:47:02Z',
 
70
                                  'Action': 'Foo'}}
 
71
        signature = self.signer.generate(credentials)
 
72
        expected = 'SmXQEZAUdQw5glv5mX8mmixBtas='
 
73
        self.assertEqual(signature, expected)
 
74
 
 
75
    def test_generate_1(self):
 
76
        """Test generate function for v1 signature."""
 
77
        credentials = {'host': '127.0.0.1',
 
78
                       'verb': 'GET',
 
79
                       'path': '/v1/',
 
80
                       'params': {'SignatureVersion': '1',
 
81
                                  'AWSAccessKeyId': self.access}}
 
82
        signature = self.signer.generate(credentials)
 
83
        expected = 'VRnoQH/EhVTTLhwRLfuL7jmFW9c='
 
84
        self.assertEqual(signature, expected)
 
85
 
 
86
    def test_generate_v2_SHA256(self):
 
87
        """Test generate function for v2 signature, SHA256."""
 
88
        credentials = {'host': '127.0.0.1',
 
89
                       'verb': 'GET',
 
90
                       'path': '/v1/',
 
91
                       'params': {'SignatureVersion': '2',
 
92
                                  'AWSAccessKeyId': self.access}}
 
93
        signature = self.signer.generate(credentials)
 
94
        expected = 'odsGmT811GffUO0Eu13Pq+xTzKNIjJ6NhgZU74tYX/w='
 
95
        self.assertEqual(signature, expected)
 
96
 
 
97
    def test_generate_v2_SHA1(self):
 
98
        """Test generate function for v2 signature, SHA1."""
 
99
        credentials = {'host': '127.0.0.1',
 
100
                       'verb': 'GET',
 
101
                       'path': '/v1/',
 
102
                       'params': {'SignatureVersion': '2',
 
103
                                  'AWSAccessKeyId': self.access}}
 
104
        self.signer.hmac_256 = None
 
105
        signature = self.signer.generate(credentials)
 
106
        expected = 'ZqCxMI4ZtTXWI175743mJ0hy/Gc='
 
107
        self.assertEqual(signature, expected)
 
108
 
 
109
    def test_generate_v4(self):
 
110
        """Test v4 generator with data from AWS docs example.
 
111
 
 
112
        see:
 
113
        http://docs.aws.amazon.com/general/latest/gr/
 
114
        sigv4-create-canonical-request.html
 
115
        and
 
116
        http://docs.aws.amazon.com/general/latest/gr/
 
117
        sigv4-signed-request-examples.html
 
118
        """
 
119
        # Create a new signer object with the AWS example key
 
120
        secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
 
121
        signer = utils.Ec2Signer(secret)
 
122
 
 
123
        body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
 
124
                     '1bf2c23245fa365ef83fe8f1f955085e2')
 
125
        auth_str = ('AWS4-HMAC-SHA256 '
 
126
                    'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
 
127
                    'us-east-1/iam/aws4_request,'
 
128
                    'SignedHeaders=content-type;host;x-amz-date,')
 
129
        headers = {'Content-type':
 
130
                   'application/x-www-form-urlencoded; charset=utf-8',
 
131
                   'X-Amz-Date': '20110909T233600Z',
 
132
                   'Host': 'iam.amazonaws.com',
 
133
                   'Authorization': auth_str}
 
134
        # Note the example in the AWS docs is inconsistent, previous
 
135
        # examples specify no query string, but the final POST example
 
136
        # does, apparently incorrectly since an empty parameter list
 
137
        # aligns all steps and the final signature with the examples
 
138
        params = {}
 
139
        credentials = {'host': 'iam.amazonaws.com',
 
140
                       'verb': 'POST',
 
141
                       'path': '/',
 
142
                       'params': params,
 
143
                       'headers': headers,
 
144
                       'body_hash': body_hash}
 
145
        signature = signer.generate(credentials)
 
146
        expected = ('ced6826de92d2bdeed8f846f0bf508e8'
 
147
                    '559e98e4b0199114b84c54174deb456c')
 
148
        self.assertEqual(signature, expected)
 
149
 
 
150
    def test_generate_v4_port(self):
 
151
        """Test v4 generator with host:port format."""
 
152
        # Create a new signer object with the AWS example key
 
153
        secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
 
154
        signer = utils.Ec2Signer(secret)
 
155
 
 
156
        body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
 
157
                     '1bf2c23245fa365ef83fe8f1f955085e2')
 
158
        auth_str = ('AWS4-HMAC-SHA256 '
 
159
                    'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
 
160
                    'us-east-1/iam/aws4_request,'
 
161
                    'SignedHeaders=content-type;host;x-amz-date,')
 
162
        headers = {'Content-type':
 
163
                   'application/x-www-form-urlencoded; charset=utf-8',
 
164
                   'X-Amz-Date': '20110909T233600Z',
 
165
                   'Host': 'foo:8000',
 
166
                   'Authorization': auth_str}
 
167
        # Note the example in the AWS docs is inconsistent, previous
 
168
        # examples specify no query string, but the final POST example
 
169
        # does, apparently incorrectly since an empty parameter list
 
170
        # aligns all steps and the final signature with the examples
 
171
        params = {}
 
172
        credentials = {'host': 'foo:8000',
 
173
                       'verb': 'POST',
 
174
                       'path': '/',
 
175
                       'params': params,
 
176
                       'headers': headers,
 
177
                       'body_hash': body_hash}
 
178
        signature = signer.generate(credentials)
 
179
 
 
180
        expected = ('26dd92ea79aaa49f533d13b1055acdc'
 
181
                    'd7d7321460d64621f96cc79c4f4d4ab2b')
 
182
        self.assertEqual(signature, expected)
 
183
 
 
184
    def test_generate_v4_port_strip(self):
 
185
        """Test v4 generator with host:port format, but for an old
 
186
        (<2.9.3) version of boto, where the port should be stripped
 
187
        to match boto behavior.
 
188
        """
 
189
        # Create a new signer object with the AWS example key
 
190
        secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
 
191
        signer = utils.Ec2Signer(secret)
 
192
 
 
193
        body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
 
194
                     '1bf2c23245fa365ef83fe8f1f955085e2')
 
195
        auth_str = ('AWS4-HMAC-SHA256 '
 
196
                    'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
 
197
                    'us-east-1/iam/aws4_request,'
 
198
                    'SignedHeaders=content-type;host;x-amz-date,')
 
199
        headers = {'Content-type':
 
200
                   'application/x-www-form-urlencoded; charset=utf-8',
 
201
                   'X-Amz-Date': '20110909T233600Z',
 
202
                   'Host': 'foo:8000',
 
203
                   'Authorization': auth_str,
 
204
                   'User-Agent': 'Boto/2.9.2 (linux2)'}
 
205
        # Note the example in the AWS docs is inconsistent, previous
 
206
        # examples specify no query string, but the final POST example
 
207
        # does, apparently incorrectly since an empty parameter list
 
208
        # aligns all steps and the final signature with the examples
 
209
        params = {}
 
210
        credentials = {'host': 'foo:8000',
 
211
                       'verb': 'POST',
 
212
                       'path': '/',
 
213
                       'params': params,
 
214
                       'headers': headers,
 
215
                       'body_hash': body_hash}
 
216
        signature = signer.generate(credentials)
 
217
 
 
218
        expected = ('9a4b2276a5039ada3b90f72ea8ec1745'
 
219
                    '14b92b909fb106b22ad910c5d75a54f4')
 
220
        self.assertEqual(expected, signature)
 
221
 
 
222
    def test_generate_v4_port_nostrip(self):
 
223
        """Test v4 generator with host:port format, but for an new
 
224
        (>=2.9.3) version of boto, where the port should not be stripped.
 
225
        """
 
226
        # Create a new signer object with the AWS example key
 
227
        secret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
 
228
        signer = utils.Ec2Signer(secret)
 
229
 
 
230
        body_hash = ('b6359072c78d70ebee1e81adcbab4f0'
 
231
                     '1bf2c23245fa365ef83fe8f1f955085e2')
 
232
        auth_str = ('AWS4-HMAC-SHA256 '
 
233
                    'Credential=AKIAIOSFODNN7EXAMPLE/20110909/'
 
234
                    'us-east-1/iam/aws4_request,'
 
235
                    'SignedHeaders=content-type;host;x-amz-date,')
 
236
        headers = {'Content-type':
 
237
                   'application/x-www-form-urlencoded; charset=utf-8',
 
238
                   'X-Amz-Date': '20110909T233600Z',
 
239
                   'Host': 'foo:8000',
 
240
                   'Authorization': auth_str,
 
241
                   'User-Agent': 'Boto/2.9.3 (linux2)'}
 
242
        # Note the example in the AWS docs is inconsistent, previous
 
243
        # examples specify no query string, but the final POST example
 
244
        # does, apparently incorrectly since an empty parameter list
 
245
        # aligns all steps and the final signature with the examples
 
246
        params = {}
 
247
        credentials = {'host': 'foo:8000',
 
248
                       'verb': 'POST',
 
249
                       'path': '/',
 
250
                       'params': params,
 
251
                       'headers': headers,
 
252
                       'body_hash': body_hash}
 
253
        signature = signer.generate(credentials)
 
254
 
 
255
        expected = ('26dd92ea79aaa49f533d13b1055acdc'
 
256
                    'd7d7321460d64621f96cc79c4f4d4ab2b')
 
257
        self.assertEqual(expected, signature)