summaryrefslogtreecommitdiffstats
path: root/nova/tests/test_signer.py
blob: b714218d4ea2fba9d17a4c6038e2f97ecaf026e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Tests for Signer."""

from nova.auth import signer
from nova import exception
from nova import test


class ClassWithStrRepr(object):
    def __repr__(self):
        return 'A string representation'


class SignerTestCase(test.TestCase):
    def setUp(self):
        super(SignerTestCase, self).setUp()
        self.signer = signer.Signer('uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')

    # S3 Authorization Signing input & output examples taken from here:
    # http://docs.amazonwebservices.com/AmazonS3/latest/dev/
    def test_s3_authorization_get(self):
        self.assertEquals('xXjDGYUmKxnwqr5KXNPGldn5LbA=',
                          self.signer.s3_authorization(
                                {'Date': 'Tue, 27 Mar 2007 19:36:42 +0000'},
                                'GET',
                                '/johnsmith/photos/puppy.jpg'))

    def test_s3_authorization_put(self):
        self.assertEquals('hcicpDDvL9SsO6AkvxqmIWkmOuQ=',
                          self.signer.s3_authorization(
                                {'Date': 'Tue, 27 Mar 2007 21:15:45 +0000',
                                 'Content-Length': '94328',
                                 'Content-Type': 'image/jpeg'},
                                'PUT',
                                '/johnsmith/photos/puppy.jpg'))

    def test_generate_HmacSHA256(self):
        self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
                          self.signer.generate(
                                {'SignatureMethod': 'HmacSHA256',
                                 'SignatureVersion': '2'},
                                'GET', 'server', '/foo'))

    def test_generate_HmacSHA1(self):
        self.assertEquals('uJTByiDIcgB65STrS5i2egQgd+U=',
                          self.signer.generate({'SignatureVersion': '2',
                                           'SignatureMethod': 'HmacSHA1'},
                                           'GET', 'server', '/foo'))

    def test_generate_invalid_signature_method_defined(self):
        self.assertRaises(exception.NovaException,
                          self.signer.generate,
                          {'SignatureVersion': '2',
                           'SignatureMethod': 'invalid_method'},
                          'GET', 'server', '/foo')

    def test_generate_no_signature_method_defined(self):
        self.assertRaises(exception.NovaException,
                          self.signer.generate,
                          {'SignatureVersion': '2'},
                          'GET', 'server', '/foo')

    def test_generate_HmacSHA256_missing_hashlib_sha256(self):
        # Stub out haslib.sha256
        import hashlib
        self.stubs.Set(hashlib, 'sha256', None)

        # Create Signer again now that hashlib.sha256 is None
        self.signer = signer.Signer(
                        'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')
        self.assertRaises(exception.NovaException,
                          self.signer.generate,
                          {'SignatureVersion': '2',
                           'SignatureMethod': 'HmacSHA256'},
                          'GET', 'server', '/foo')

    def test_generate_with_unicode_param(self):
        self.assertEquals('clXalhbLZXxEuI32OoX+OeXsN6Mr2q4jzGyIDAr4RZg=',
                          self.signer.generate({'SignatureVersion': u'2',
                                            'SignatureMethod': 'HmacSHA256'},
                                            'GET', 'server', '/foo'))

    def test_generate_with_non_string_or_unicode_param(self):
        self.assertEquals('99IAgCkhTR2aMTgRobnzKGuNxVFSdb7vlQRvnj3Urqk=',
                          self.signer.generate(
                              {'AnotherParam': ClassWithStrRepr(),
                               'SignatureVersion': '2',
                               'SignatureMethod': 'HmacSHA256'},
                              'GET', 'server', '/foo'))

    def test_generate_unknown_version(self):
        self.assertRaises(exception.NovaException,
                self.signer.generate,
                {'SignatureMethod': 'HmacSHA256', 'SignatureVersion': '9'},
                'GET', 'server', '/foo')