|
16 | 16 | Unit tests for oauth2client.xsrfutil.
|
17 | 17 | """
|
18 | 18 |
|
| 19 | +import base64 |
19 | 20 | import unittest
|
20 | 21 |
|
| 22 | +import mock |
| 23 | + |
| 24 | +from oauth2client._helpers import _to_bytes |
21 | 25 | from oauth2client import xsrfutil
|
22 | 26 |
|
23 | 27 | # Jan 17 2008, 5:40PM
|
24 |
| -TEST_KEY = 'test key' |
| 28 | +TEST_KEY = b'test key' |
| 29 | +# Jan. 17, 2008 22:40:32.081230 UTC |
25 | 30 | TEST_TIME = 1200609642081230
|
26 | 31 | TEST_USER_ID_1 = 123832983
|
27 | 32 | TEST_USER_ID_2 = 938297432
|
28 |
| -TEST_ACTION_ID_1 = 'some_action' |
29 |
| -TEST_ACTION_ID_2 = 'some_other_action' |
30 |
| -TEST_EXTRA_INFO_1 = 'extra_info_1' |
31 |
| -TEST_EXTRA_INFO_2 = 'more_extra_info' |
| 33 | +TEST_ACTION_ID_1 = b'some_action' |
| 34 | +TEST_ACTION_ID_2 = b'some_other_action' |
| 35 | +TEST_EXTRA_INFO_1 = b'extra_info_1' |
| 36 | +TEST_EXTRA_INFO_2 = b'more_extra_info' |
32 | 37 |
|
33 | 38 |
|
34 | 39 | __author__ = '[email protected] (Joe Gregorio)'
|
35 | 40 |
|
36 | 41 |
|
| 42 | +class Test_generate_token(unittest.TestCase): |
| 43 | + |
| 44 | + def test_bad_positional(self): |
| 45 | + # Need 2 positional arguments. |
| 46 | + self.assertRaises(TypeError, xsrfutil.generate_token, None) |
| 47 | + # At most 2 positional arguments. |
| 48 | + self.assertRaises(TypeError, xsrfutil.generate_token, None, None, None) |
| 49 | + |
| 50 | + def test_it(self): |
| 51 | + digest = b'foobar' |
| 52 | + curr_time = 1440449755.74 |
| 53 | + digester = mock.MagicMock() |
| 54 | + digester.digest = mock.MagicMock(name='digest', return_value=digest) |
| 55 | + with mock.patch('oauth2client.xsrfutil.hmac') as hmac: |
| 56 | + hmac.new = mock.MagicMock(name='new', return_value=digester) |
| 57 | + token = xsrfutil.generate_token(TEST_KEY, |
| 58 | + TEST_USER_ID_1, |
| 59 | + action_id=TEST_ACTION_ID_1, |
| 60 | + when=TEST_TIME) |
| 61 | + hmac.new.assert_called_once_with(TEST_KEY) |
| 62 | + digester.digest.assert_called_once_with() |
| 63 | + |
| 64 | + expected_digest_calls = [ |
| 65 | + mock.call.update(_to_bytes(str(TEST_USER_ID_1))), |
| 66 | + mock.call.update(xsrfutil.DELIMITER), |
| 67 | + mock.call.update(TEST_ACTION_ID_1), |
| 68 | + mock.call.update(xsrfutil.DELIMITER), |
| 69 | + mock.call.update(_to_bytes(str(TEST_TIME))), |
| 70 | + ] |
| 71 | + self.assertEqual(digester.method_calls, expected_digest_calls) |
| 72 | + |
| 73 | + expected_token_as_bytes = (digest + xsrfutil.DELIMITER + |
| 74 | + _to_bytes(str(TEST_TIME))) |
| 75 | + expected_token = base64.urlsafe_b64encode( |
| 76 | + expected_token_as_bytes) |
| 77 | + self.assertEqual(token, expected_token) |
| 78 | + |
| 79 | + def test_with_system_time(self): |
| 80 | + digest = b'foobar' |
| 81 | + curr_time = 1440449755.74 |
| 82 | + digester = mock.MagicMock() |
| 83 | + digester.digest = mock.MagicMock(name='digest', return_value=digest) |
| 84 | + with mock.patch('oauth2client.xsrfutil.hmac') as hmac: |
| 85 | + hmac.new = mock.MagicMock(name='new', return_value=digester) |
| 86 | + |
| 87 | + with mock.patch('oauth2client.xsrfutil.time') as time: |
| 88 | + time.time = mock.MagicMock(name='time', return_value=curr_time) |
| 89 | + # when= is omitted |
| 90 | + token = xsrfutil.generate_token(TEST_KEY, |
| 91 | + TEST_USER_ID_1, |
| 92 | + action_id=TEST_ACTION_ID_1) |
| 93 | + |
| 94 | + hmac.new.assert_called_once_with(TEST_KEY) |
| 95 | + time.time.assert_called_once_with() |
| 96 | + digester.digest.assert_called_once_with() |
| 97 | + |
| 98 | + expected_digest_calls = [ |
| 99 | + mock.call.update(_to_bytes(str(TEST_USER_ID_1))), |
| 100 | + mock.call.update(xsrfutil.DELIMITER), |
| 101 | + mock.call.update(TEST_ACTION_ID_1), |
| 102 | + mock.call.update(xsrfutil.DELIMITER), |
| 103 | + mock.call.update(_to_bytes(str(int(curr_time)))), |
| 104 | + ] |
| 105 | + self.assertEqual(digester.method_calls, expected_digest_calls) |
| 106 | + |
| 107 | + expected_token_as_bytes = (digest + xsrfutil.DELIMITER + |
| 108 | + _to_bytes(str(int(curr_time)))) |
| 109 | + expected_token = base64.urlsafe_b64encode( |
| 110 | + expected_token_as_bytes) |
| 111 | + self.assertEqual(token, expected_token) |
| 112 | + |
| 113 | + |
| 114 | +class Test_validate_token(unittest.TestCase): |
| 115 | + |
| 116 | + def test_bad_positional(self): |
| 117 | + # Need 3 positional arguments. |
| 118 | + self.assertRaises(TypeError, xsrfutil.validate_token, None, None) |
| 119 | + # At most 3 positional arguments. |
| 120 | + self.assertRaises(TypeError, xsrfutil.validate_token, |
| 121 | + None, None, None, None) |
| 122 | + |
| 123 | + def test_no_token(self): |
| 124 | + key = token = user_id = None |
| 125 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id)) |
| 126 | + |
| 127 | + def test_token_not_valid_base64(self): |
| 128 | + key = user_id = None |
| 129 | + token = b'a' # Bad padding |
| 130 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id)) |
| 131 | + |
| 132 | + def test_token_non_integer(self): |
| 133 | + key = user_id = None |
| 134 | + token = base64.b64encode(b'abc' + xsrfutil.DELIMITER + b'xyz') |
| 135 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id)) |
| 136 | + |
| 137 | + def test_token_too_old_implicit_current_time(self): |
| 138 | + token_time = 123456789 |
| 139 | + curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS + 1 |
| 140 | + |
| 141 | + key = user_id = None |
| 142 | + token = base64.b64encode(_to_bytes(str(token_time))) |
| 143 | + with mock.patch('oauth2client.xsrfutil.time') as time: |
| 144 | + time.time = mock.MagicMock(name='time', return_value=curr_time) |
| 145 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id)) |
| 146 | + time.time.assert_called_once_with() |
| 147 | + |
| 148 | + def test_token_too_old_explicit_current_time(self): |
| 149 | + token_time = 123456789 |
| 150 | + curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS + 1 |
| 151 | + |
| 152 | + key = user_id = None |
| 153 | + token = base64.b64encode(_to_bytes(str(token_time))) |
| 154 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id, |
| 155 | + current_time=curr_time)) |
| 156 | + |
| 157 | + def test_token_length_differs_from_generated(self): |
| 158 | + token_time = 123456789 |
| 159 | + # Make sure it isn't too old. |
| 160 | + curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS - 1 |
| 161 | + |
| 162 | + key = object() |
| 163 | + user_id = object() |
| 164 | + action_id = object() |
| 165 | + token = base64.b64encode(_to_bytes(str(token_time))) |
| 166 | + generated_token = b'a' |
| 167 | + # Make sure the token length comparison will fail. |
| 168 | + self.assertNotEqual(len(token), len(generated_token)) |
| 169 | + |
| 170 | + with mock.patch('oauth2client.xsrfutil.generate_token', |
| 171 | + return_value=generated_token) as gen_tok: |
| 172 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id, |
| 173 | + current_time=curr_time, |
| 174 | + action_id=action_id)) |
| 175 | + gen_tok.assert_called_once_with(key, user_id, action_id=action_id, |
| 176 | + when=token_time) |
| 177 | + |
| 178 | + def test_token_differs_from_generated_but_same_length(self): |
| 179 | + token_time = 123456789 |
| 180 | + # Make sure it isn't too old. |
| 181 | + curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS - 1 |
| 182 | + |
| 183 | + key = object() |
| 184 | + user_id = object() |
| 185 | + action_id = object() |
| 186 | + token = base64.b64encode(_to_bytes(str(token_time))) |
| 187 | + # It is encoded as b'MTIzNDU2Nzg5', which has length 12. |
| 188 | + generated_token = b'M' * 12 |
| 189 | + # Make sure the token length comparison will succeed, but the token |
| 190 | + # comparison will fail. |
| 191 | + self.assertEqual(len(token), len(generated_token)) |
| 192 | + self.assertNotEqual(token, generated_token) |
| 193 | + |
| 194 | + with mock.patch('oauth2client.xsrfutil.generate_token', |
| 195 | + return_value=generated_token) as gen_tok: |
| 196 | + self.assertFalse(xsrfutil.validate_token(key, token, user_id, |
| 197 | + current_time=curr_time, |
| 198 | + action_id=action_id)) |
| 199 | + gen_tok.assert_called_once_with(key, user_id, action_id=action_id, |
| 200 | + when=token_time) |
| 201 | + |
| 202 | + def test_success(self): |
| 203 | + token_time = 123456789 |
| 204 | + # Make sure it isn't too old. |
| 205 | + curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS - 1 |
| 206 | + |
| 207 | + key = object() |
| 208 | + user_id = object() |
| 209 | + action_id = object() |
| 210 | + token = base64.b64encode(_to_bytes(str(token_time))) |
| 211 | + with mock.patch('oauth2client.xsrfutil.generate_token', |
| 212 | + return_value=token) as gen_tok: |
| 213 | + self.assertTrue(xsrfutil.validate_token(key, token, user_id, |
| 214 | + current_time=curr_time, |
| 215 | + action_id=action_id)) |
| 216 | + gen_tok.assert_called_once_with(key, user_id, action_id=action_id, |
| 217 | + when=token_time) |
| 218 | + |
| 219 | + |
37 | 220 | class XsrfUtilTests(unittest.TestCase):
|
38 | 221 | """Test xsrfutil functions."""
|
39 | 222 |
|
|
0 commit comments