blob: ddea7602c12455e4fa05a059568f884aa70bc594 [file] [log] [blame]
Paul Kehrer874445a2016-12-13 11:09:35 -06001# 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
5from __future__ import absolute_import, division, print_function
6
Alex Gaynor0c9aed92017-07-08 21:50:01 -04007import pytest
8
Paul Kehrer874445a2016-12-13 11:09:35 -06009from cryptography import utils
10
11
12def test_int_from_bytes_bytearray():
13 assert utils.int_from_bytes(bytearray(b"\x02\x10"), "big") == 528
Alex Gaynord2c12682018-01-10 08:17:09 -050014 with pytest.raises(TypeError):
15 utils.int_from_bytes(["list", "is", "not", "bytes"], "big")
Alex Gaynor0c9aed92017-07-08 21:50:01 -040016
17
18class 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 Gaynor31034a02017-10-11 22:01:29 -040061
62
63def test_bit_length():
64 assert utils.bit_length(1) == 1
65 assert utils.bit_length(11) == 4