• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from __future__ import absolute_import, division, print_function
6
7import pytest
8
9from cryptography import utils
10
11
12def test_int_from_bytes_bytearray():
13    assert utils.int_from_bytes(bytearray(b"\x02\x10"), "big") == 528
14    with pytest.raises(TypeError):
15        utils.int_from_bytes(["list", "is", "not", "bytes"], "big")
16
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
61