~bgh/nova/qmanager-ipam-fixes

« back to all changes in this revision

Viewing changes to nova/tests/test_signer.py

  • Committer: Gerrit Code Review
  • Author(s): Jenkins
  • Date: 2011-09-28 15:25:24 UTC
  • mfrom: (1640.1.1)
  • Revision ID: git-v1:4fb1ac26cb15ea7e21ab9873e1dd253da0c77157
Merge "Signer no longer fails if hashlib.sha256 is not available. test_signer unit test added."

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
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
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
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
 
17
#    under the License.
 
18
 
 
19
"""Tests for Signer."""
 
20
 
 
21
from nova import exception
 
22
from nova import test
 
23
from nova.auth import signer
 
24
 
 
25
 
 
26
class ClassWithStrRepr(object):
 
27
    def __repr__(self):
 
28
        return 'A string representation'
 
29
 
 
30
 
 
31
class SignerTestCase(test.TestCase):
 
32
    def setUp(self):
 
33
        super(SignerTestCase, self).setUp()
 
34
        self.signer = signer.Signer('uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')
 
35
 
 
36
    def tearDown(self):
 
37
        super(SignerTestCase, self).tearDown()
 
38
 
 
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'},
 
45
                                'GET',
 
46
                                '/johnsmith/photos/puppy.jpg'))
 
47
 
 
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'},
 
54
                                'PUT',
 
55
                                '/johnsmith/photos/puppy.jpg'))
 
56
 
 
57
    def test_generate_using_version_2(self):
 
58
        self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
 
59
                          self.signer.generate(
 
60
                                {'SignatureMethod': 'HmacSHA256',
 
61
                                 'SignatureVersion': '2'},
 
62
                                'GET', 'server', '/foo'))
 
63
 
 
64
    def test_generate_force_HmacSHA1(self):
 
65
        # Stub out haslib.sha256
 
66
        import hashlib
 
67
        self.stubs.Set(hashlib, 'sha256', None)
 
68
 
 
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'))
 
75
 
 
76
    def test_generate_with_unicode_param(self):
 
77
        self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
 
78
                          self.signer.generate({'SignatureVersion': u'2'},
 
79
                                               'GET', 'server', '/foo'))
 
80
 
 
81
    def test_generate_with_non_string_or_unicode_param(self):
 
82
        self.assertEquals('99IAgCkhTR2aMTgRobnzKGuNxVFSdb7vlQRvnj3Urqk=',
 
83
                          self.signer.generate(
 
84
                              {'AnotherParam': ClassWithStrRepr(),
 
85
                               'SignatureVersion': '2'},
 
86
                              'GET', 'server', '/foo'))
 
87
 
 
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')