Paul Kehrer | 874445a | 2016-12-13 11:09:35 -0600 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
| 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | 0c9aed9 | 2017-07-08 21:50:01 -0400 | [diff] [blame] | 7 | import pytest |
| 8 | |
Paul Kehrer | 874445a | 2016-12-13 11:09:35 -0600 | [diff] [blame] | 9 | from cryptography import utils |
| 10 | |
| 11 | |
| 12 | def test_int_from_bytes_bytearray(): |
| 13 | assert utils.int_from_bytes(bytearray(b"\x02\x10"), "big") == 528 |
Alex Gaynor | d2c1268 | 2018-01-10 08:17:09 -0500 | [diff] [blame] | 14 | with pytest.raises(TypeError): |
| 15 | utils.int_from_bytes(["list", "is", "not", "bytes"], "big") |
Alex Gaynor | 0c9aed9 | 2017-07-08 21:50:01 -0400 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class TestCachedProperty(object): |
| 19 | def test_simple(self): |
| 20 | accesses = [] |
| 21 | |
| 22 | class T(object): |
| 23 | @utils.cached_property |
| 24 | def t(self): |
| 25 | accesses.append(None) |
| 26 | return 14 |
| 27 | |
| 28 | assert T.t |
| 29 | t = T() |
| 30 | assert t.t == 14 |
| 31 | assert len(accesses) == 1 |
| 32 | assert t.t == 14 |
| 33 | assert len(accesses) == 1 |
| 34 | |
| 35 | t = T() |
| 36 | assert t.t == 14 |
| 37 | assert len(accesses) == 2 |
| 38 | assert t.t == 14 |
| 39 | assert len(accesses) == 2 |
| 40 | |
| 41 | def test_set(self): |
| 42 | accesses = [] |
| 43 | |
| 44 | class T(object): |
| 45 | @utils.cached_property |
| 46 | def t(self): |
| 47 | accesses.append(None) |
| 48 | return 14 |
| 49 | |
| 50 | t = T() |
| 51 | with pytest.raises(AttributeError): |
| 52 | t.t = None |
| 53 | assert len(accesses) == 0 |
| 54 | assert t.t == 14 |
| 55 | assert len(accesses) == 1 |
| 56 | with pytest.raises(AttributeError): |
| 57 | t.t = None |
| 58 | assert len(accesses) == 1 |
| 59 | assert t.t == 14 |
| 60 | assert len(accesses) == 1 |
Alex Gaynor | 31034a0 | 2017-10-11 22:01:29 -0400 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def test_bit_length(): |
| 64 | assert utils.bit_length(1) == 1 |
| 65 | assert utils.bit_length(11) == 4 |