1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
19
"""Tests for Signer."""
21
from nova import exception
23
from nova.auth import signer
26
class ClassWithStrRepr(object):
28
return 'A string representation'
31
class SignerTestCase(test.TestCase):
33
super(SignerTestCase, self).setUp()
34
self.signer = signer.Signer('uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')
37
super(SignerTestCase, self).tearDown()
39
# S3 Authorization Signing input & output examples taken from here:
40
# http://docs.amazonwebservices.com/AmazonS3/latest/dev/
41
def test_s3_authorization_get(self):
42
self.assertEquals('xXjDGYUmKxnwqr5KXNPGldn5LbA=',
43
self.signer.s3_authorization(
44
{'Date': 'Tue, 27 Mar 2007 19:36:42 +0000'},
46
'/johnsmith/photos/puppy.jpg'))
48
def test_s3_authorization_put(self):
49
self.assertEquals('hcicpDDvL9SsO6AkvxqmIWkmOuQ=',
50
self.signer.s3_authorization(
51
{'Date': 'Tue, 27 Mar 2007 21:15:45 +0000',
52
'Content-Length': '94328',
53
'Content-Type': 'image/jpeg'},
55
'/johnsmith/photos/puppy.jpg'))
57
def test_generate_using_version_2(self):
58
self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
60
{'SignatureMethod': 'HmacSHA256',
61
'SignatureVersion': '2'},
62
'GET', 'server', '/foo'))
64
def test_generate_force_HmacSHA1(self):
65
# Stub out haslib.sha256
67
self.stubs.Set(hashlib, 'sha256', None)
69
# Create Signer again now that hashlib.sha256 is None
70
self.signer = signer.Signer(
71
'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')
72
self.assertEquals('uJTByiDIcgB65STrS5i2egQgd+U=',
73
self.signer.generate({'SignatureVersion': '2'},
74
'GET', 'server', '/foo'))
76
def test_generate_with_unicode_param(self):
77
self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
78
self.signer.generate({'SignatureVersion': u'2'},
79
'GET', 'server', '/foo'))
81
def test_generate_with_non_string_or_unicode_param(self):
82
self.assertEquals('99IAgCkhTR2aMTgRobnzKGuNxVFSdb7vlQRvnj3Urqk=',
84
{'AnotherParam': ClassWithStrRepr(),
85
'SignatureVersion': '2'},
86
'GET', 'server', '/foo'))
88
def test_generate_unknown_version(self):
89
with self.assertRaises(exception.Error):
90
self.signer.generate({'SignatureMethod': 'HmacSHA256',
91
'SignatureVersion': '9'},
92
'GET', 'server', '/foo')