Debian Codegraph @

test/unit/utils/checksum_test.py

raw
1 from builtins import bytes
2 import encodings.ascii as encoding
3 from unittest.mock import (
4 patch, call, Mock, mock_open
5 )
6 from pytest import raises
7
8 from kiwi.utils.checksum import Checksum
9
10 from kiwi.exceptions import KiwiFileNotFound
11
12
13 class TestChecksum:
14 @patch('os.path.exists')
15 def setup(self, mock_exists):
16 self.ascii = encoding.getregentry().name
17 read_results = [bytes(b''), bytes(b'data'), bytes(b''), bytes(b'data')]
18
19 def side_effect(arg):
20 return read_results.pop()
21
22 self.m_open = mock_open()
23 self.m_open.return_value.read.side_effect = side_effect
24
25 mock_exists.return_value = True
26 self.checksum = Checksum('some-file')
27
28 @patch('os.path.exists')
29 def setup_method(self, cls, mock_exists):
30 self.setup()
31
32 def test_checksum_file_not_found(self):
33 with raises(KiwiFileNotFound):
34 Checksum('some-file')
35
36 @patch('os.path.exists')
37 def test_matches_checksum_file_does_not_exist(self, mock_exists):
38 mock_exists.return_value = False
39 assert self.checksum.matches('sum', 'some-file') is False
40
41 @patch('os.path.exists')
42 def test_matches(self, mock_exists):
43 mock_exists.return_value = True
44
45 self.m_open.return_value.read.side_effect = None
46 self.m_open.return_value.read.return_value = 'sum'
47 with patch('builtins.open', self.m_open, create=True):
48 assert self.checksum.matches('sum', 'some-file') is True
49
50 self.m_open.assert_called_once_with(
51 'some-file', encoding=self.ascii
52 )
53
54 with patch('builtins.open', self.m_open, create=True):
55 assert self.checksum.matches('foo', 'some-file') is False
56
57 @patch('kiwi.path.Path.which')
58 @patch('kiwi.utils.checksum.Compress')
59 @patch('hashlib.sha256')
60 @patch('os.path.getsize')
61 def test_sha256_xz(self, mock_size, mock_sha256, mock_compress, mock_which):
62 checksum = Mock
63 checksum.uncompressed_filename = 'some-file-uncompressed'
64 mock_which.return_value = 'factor'
65 compress = Mock()
66 digest = Mock()
67 digest.block_size = 1024
68 digest._calculate_hash_hexdigest = Mock(
69 return_value=checksum
70 )
71 digest.hexdigest = Mock(
72 return_value='sum'
73 )
74 compress.get_format = Mock(
75 return_value='xz'
76 )
77 mock_size.return_value = 1343225856
78 mock_sha256.return_value = digest
79 mock_compress.return_value = compress
80
81 with patch('builtins.open', self.m_open, create=True):
82 self.checksum.sha256('outfile')
83
84 assert self.m_open.call_args_list == [
85 call('some-file', 'rb'),
86 call('some-file-uncompressed', 'rb'),
87 call('outfile', encoding=self.ascii, mode='w')
88 ]
89 self.m_open.return_value.write.assert_called_once_with(
90 'sum 163968 8192 163968 8192\n'
91 )
92
93 @patch('kiwi.path.Path.which')
94 @patch('kiwi.utils.checksum.Compress')
95 @patch('hashlib.sha512')
96 @patch('os.path.getsize')
97 def test_sha512_xz(self, mock_size, mock_sha512, mock_compress, mock_which):
98 checksum = Mock
99 checksum.uncompressed_filename = 'some-file-uncompressed'
100 mock_which.return_value = 'factor'
101 compress = Mock()
102 digest = Mock()
103 digest.block_size = 1024
104 digest._calculate_hash_hexdigest = Mock(
105 return_value=checksum
106 )
107 digest.hexdigest = Mock(
108 return_value='sum'
109 )
110 compress.get_format = Mock(
111 return_value='xz'
112 )
113 mock_size.return_value = 1343225856
114 mock_sha512.return_value = digest
115 mock_compress.return_value = compress
116
117 with patch('builtins.open', self.m_open, create=True):
118 self.checksum.sha512('outfile')
119
120 assert self.m_open.call_args_list == [
121 call('some-file', 'rb'),
122 call('some-file-uncompressed', 'rb'),
123 call('outfile', encoding=self.ascii, mode='w')
124 ]
125 self.m_open.return_value.write.assert_called_once_with(
126 'sum 163968 8192 163968 8192\n'
127 )
128
129 @patch('kiwi.path.Path.which')
130 @patch('kiwi.utils.checksum.Compress')
131 @patch('hashlib.sha256')
132 @patch('os.path.getsize')
133 def test_sha256_file(
134 self, mock_size, mock_sha256, mock_compress, mock_which
135 ):
136 mock_which.return_value = 'factor'
137 compress = Mock()
138 digest = Mock()
139 digest.block_size = 1024
140 digest.hexdigest = Mock(
141 return_value='sum'
142 )
143 compress.get_format = Mock(
144 return_value=None
145 )
146 mock_size.return_value = 1343225856
147 mock_sha256.return_value = digest
148 mock_compress.return_value = compress
149
150 with patch('builtins.open', self.m_open, create=True):
151 self.checksum.sha256('outfile')
152
153 assert self.m_open.call_args_list == [
154 call('some-file', 'rb'),
155 call('outfile', encoding=self.ascii, mode='w')
156 ]
157 self.m_open.return_value.write.assert_called_once_with(
158 'sum 163968 8192\n'
159 )
160
161 @patch('kiwi.path.Path.which')
162 @patch('kiwi.utils.checksum.Compress')
163 @patch('hashlib.sha256')
164 @patch('os.path.getsize')
165 def test_sha256(
166 self, mock_size, mock_sha256, mock_compress, mock_which
167 ):
168 mock_which.return_value = 'factor'
169 compress = Mock()
170 digest = Mock()
171 digest.block_size = 1024
172 digest.hexdigest = Mock(
173 return_value='sum'
174 )
175 compress.get_format = Mock(
176 return_value=None
177 )
178 mock_size.return_value = 1343225856
179 mock_sha256.return_value = digest
180 mock_compress.return_value = compress
181
182 with patch('builtins.open', self.m_open, create=True):
183 self.checksum.sha256('outfile')
184
185 assert self.m_open.call_args_list == [
186 call('some-file', 'rb'),
187 call('outfile', encoding=self.ascii, mode='w')
188 ]
189 self.m_open.return_value.write.assert_called_once_with(
190 'sum 163968 8192\n'
191 )
192
193 @patch('hashlib.sha256')
194 def test_sha256_plain(self, mock_sha256):
195 digest = Mock()
196 digest.block_size = 1024
197 digest.hexdigest = Mock(
198 return_value='sum'
199 )
200 mock_sha256.return_value = digest
201
202 with patch('builtins.open', self.m_open, create=True):
203 assert self.checksum.sha256() == digest.hexdigest.return_value