summaryrefslogtreecommitdiff
path: root/osprofiler/tests/unit/test_utils.py
blob: ffb7b09e9b39eea4273ab311955229f463f2034f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2014 Mirantis Inc.
# 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.

import base64
import hashlib
import hmac
import uuid

import mock

from osprofiler import _utils as utils
from osprofiler.tests import test


class UtilsTestCase(test.TestCase):

    def test_split(self):
        self.assertEqual([1, 2], utils.split([1, 2]))
        self.assertEqual(["A", "B"], utils.split("A, B"))
        self.assertEqual(["A", " B"], utils.split("A, B", strip=False))

    def test_split_wrong_type(self):
        self.assertRaises(TypeError, utils.split, 1)

    def test_binary_encode_and_decode(self):
        self.assertEqual("text",
                         utils.binary_decode(utils.binary_encode("text")))

    def test_binary_encode_invalid_type(self):
        self.assertRaises(TypeError, utils.binary_encode, 1234)

    def test_binary_encode_binary_type(self):
        binary = utils.binary_encode("text")
        self.assertEqual(binary, utils.binary_encode(binary))

    def test_binary_decode_invalid_type(self):
        self.assertRaises(TypeError, utils.binary_decode, 1234)

    def test_binary_decode_text_type(self):
        self.assertEqual("text", utils.binary_decode("text"))

    def test_generate_hmac(self):
        hmac_key = "secrete"
        data = "my data"

        h = hmac.new(utils.binary_encode(hmac_key), digestmod=hashlib.sha1)
        h.update(utils.binary_encode(data))

        self.assertEqual(h.hexdigest(), utils.generate_hmac(data, hmac_key))

    def test_signed_pack_unpack(self):
        hmac = "secret"
        data = {"some": "data"}

        packed_data, hmac_data = utils.signed_pack(data, hmac)

        process_data = utils.signed_unpack(packed_data, hmac_data, [hmac])
        self.assertIn("hmac_key", process_data)
        process_data.pop("hmac_key")
        self.assertEqual(data, process_data)

    def test_signed_pack_unpack_many_keys(self):
        keys = ["secret", "secret2", "secret3"]
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, keys[-1])

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertEqual(keys[-1], process_data["hmac_key"])

    def test_signed_pack_unpack_many_wrong_keys(self):
        keys = ["secret", "secret2", "secret3"]
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, "password")

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertIsNone(process_data)

    def test_signed_unpack_wrong_key(self):
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, "secret")

        self.assertIsNone(utils.signed_unpack(packed_data, hmac_data, "wrong"))

    def test_signed_unpack_no_key_or_hmac_data(self):
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, "secret")
        self.assertIsNone(utils.signed_unpack(packed_data, hmac_data, None))
        self.assertIsNone(utils.signed_unpack(packed_data, None, "secret"))
        self.assertIsNone(utils.signed_unpack(packed_data, " ", "secret"))

    @mock.patch("osprofiler._utils.generate_hmac")
    def test_singed_unpack_generate_hmac_failed(self, mock_generate_hmac):
        mock_generate_hmac.side_effect = Exception
        self.assertIsNone(utils.signed_unpack("data", "hmac_data", "hmac_key"))

    def test_signed_unpack_invalid_json(self):
        hmac = "secret"
        data = base64.urlsafe_b64encode(utils.binary_encode("not_a_json"))
        hmac_data = utils.generate_hmac(data, hmac)

        self.assertIsNone(utils.signed_unpack(data, hmac_data, hmac))

    def test_shorten_id_with_valid_uuid(self):
        valid_id = "4e3e0ec6-2938-40b1-8504-09eb1d4b0dee"

        uuid_obj = uuid.UUID(valid_id)

        with mock.patch("uuid.UUID") as mock_uuid:
            mock_uuid.return_value = uuid_obj

            result = utils.shorten_id(valid_id)
            expected = 9584796812364680686

            self.assertEqual(expected, result)

    @mock.patch("oslo_utils.uuidutils.generate_uuid")
    def test_shorten_id_with_invalid_uuid(self, mock_gen_uuid):
        invalid_id = "invalid"
        mock_gen_uuid.return_value = "1c089ea8-28fe-4f3d-8c00-f6daa2bc32f1"

        result = utils.shorten_id(invalid_id)
        expected = 10088334584203457265

        self.assertEqual(expected, result)

    def test_itersubclasses(self):

        class A(object):
            pass

        class B(A):
            pass

        class C(A):
            pass

        class D(C):
            pass

        self.assertEqual([B, C, D], list(utils.itersubclasses(A)))

        class E(type):
            pass

        self.assertEqual([], list(utils.itersubclasses(E)))