Debian Codegraph @

test/unit/utils/veritysetup_test.py

raw
1 import io
2 from textwrap import dedent
3 from pytest import raises
4 from unittest.mock import (
5 patch, Mock, MagicMock, call
6 )
7
8 from kiwi.utils.veritysetup import VeritySetup
9 from kiwi.exceptions import KiwiOffsetError
10
11 import kiwi.defaults as defaults
12
13
14 class TestVeritySetup:
15 @patch('os.path.getsize')
16 def setup(self, mock_os_path_getsize):
17 mock_os_path_getsize.return_value = 4096
18 self.veritysetup = VeritySetup('image_file', data_blocks=10)
19 self.veritysetup_full = VeritySetup('image_file')
20 self.verity_dict = {
21 'UUID': '',
22 'Hashtype': '1',
23 'Datablocks': '10',
24 'Datablocksize': '4096',
25 'Hashblocksize': '4096',
26 'Hashalgorithm': 'sha256',
27 'Salt': 'fb074d1db50...',
28 'Roothash': 'e2728628377...'
29 }
30
31 @patch('os.path.getsize')
32 def setup_method(self, cls, mock_os_path_getsize):
33 self.setup()
34
35 @patch('kiwi.utils.veritysetup.Command.run')
36 def test_format(self, mock_Command_run):
37 self.veritysetup.format()
38 mock_Command_run.assert_called_once_with(
39 [
40 'veritysetup', 'format', 'image_file', 'image_file',
41 '--hash-offset=4096',
42 '--hash-block-size=4096', '--data-blocks=10',
43 '--data-block-size=4096',
44 ]
45 )
46
47 @patch('kiwi.utils.veritysetup.Command.run')
48 @patch('kiwi.utils.veritysetup.Temporary.new_file')
49 @patch('os.path.getsize')
50 def test_get_hash_byte_size(
51 self, mock_os_path_getsize, mock_Temporary_new_file, mock_Command_run
52 ):
53 tempfile = Mock()
54 tempfile.name = 'tempfile'
55 mock_Temporary_new_file.return_value = tempfile
56 assert self.veritysetup.get_hash_byte_size() == \
57 mock_os_path_getsize.return_value
58 mock_Command_run.assert_called_once_with(
59 [
60 'veritysetup', 'format', 'image_file', 'tempfile',
61 '--hash-block-size=4096', '--data-blocks=10',
62 '--data-block-size=4096',
63 ]
64 )
65
66 @patch('kiwi.utils.veritysetup.Command.run')
67 def test_format_full(self, mock_Command_run):
68 verity_call = Mock()
69 verity_call.output = dedent('''\n
70 VERITY header information for mysquash.img
71 UUID:
72 Hash type: 1
73 Data blocks: 10
74 Data block size: 4096
75 Hash block size: 4096
76 Hash algorithm: sha256
77 Salt: fb074d1db50...
78 Root hash: e2728628377...
79 ''')
80 mock_Command_run.return_value = verity_call
81 assert self.veritysetup_full.format() == self.verity_dict
82 mock_Command_run.assert_called_once_with(
83 [
84 'veritysetup', 'format', 'image_file', 'image_file',
85 '--hash-offset=4096',
86 '--hash-block-size=4096'
87 ]
88 )
89
90 def test_store_credentials(self):
91 self.veritysetup.verity_call = Mock()
92 self.veritysetup.verity_dict = self.verity_dict
93 target_block_id = Mock()
94 target_block_id.get_blkid.return_value = 'uuid'
95 with patch('builtins.open', create=True) as mock_open:
96 mock_open.return_value = MagicMock(spec=io.IOBase)
97 file_handle = mock_open.return_value.__enter__.return_value
98 self.veritysetup.store_credentials(
99 'credentials_file', target_block_id
100 )
101 assert file_handle.write.call_args_list == [
102 call('Datablocks: 10\n'),
103 call('Datablocksize: 4096\n'),
104 call('Hashalgorithm: sha256\n'),
105 call('Hashblocksize: 4096\n'),
106 call('Hashtype: 1\n'),
107 call('Roothash: e2728628377...\n'),
108 call('Salt: fb074d1db50...\n'),
109 call('UUID: \n'),
110 call('Root hashoffset: 4096'),
111 call('\n'),
112 call('Superblock:'),
113 call('\n')
114 ]
115
116 @patch('kiwi.utils.veritysetup.BlockID')
117 def test_get_block_storage_filesystem(self, mock_BlockID):
118 block_id = Mock()
119 mock_BlockID.return_value = block_id
120 block_id.get_filesystem.return_value = 'ext3'
121 assert self.veritysetup.get_block_storage_filesystem() == 'ext3'
122 block_id.get_filesystem.side_effect = Exception
123 assert self.veritysetup.get_block_storage_filesystem() == ''
124
125 @patch('os.path.getsize')
126 def test_write_verification_metadata_raises(self, mock_os_path_getsize):
127 mock_os_path_getsize.return_value = 8192
128 metadata_file = Mock()
129 self.veritysetup.verification_metadata_file = metadata_file
130 with raises(KiwiOffsetError):
131 self.veritysetup.write_verification_metadata('/some/device')
132
133 @patch('os.path.getsize')
134 def test_write_verification_metadata(self, mock_os_path_getsize):
135 mock_os_path_getsize.return_value = 4096
136 metadata_file = Mock()
137 metadata_file.name = 'metadata_file'
138 self.veritysetup.verification_metadata_file = metadata_file
139 with patch('builtins.open', create=True) as mock_open:
140 mock_open.return_value = MagicMock(spec=io.IOBase)
141 file_handle = mock_open.return_value.__enter__.return_value
142 self.veritysetup.write_verification_metadata('/some/device')
143 assert mock_open.call_args_list == [
144 call('metadata_file', 'rb'),
145 call('/some/device', 'r+b')
146 ]
147 file_handle.seek.assert_called_once_with(
148 -defaults.DM_METADATA_OFFSET, 2
149 )
150 file_handle.write.assert_called_once_with(
151 file_handle.read.return_value
152 )
153
154 @patch.object(VeritySetup, 'get_block_storage_filesystem')
155 def test_create_verity_verification_metadata(
156 self, mock_get_block_storage_filesystem
157 ):
158 mock_get_block_storage_filesystem.return_value = 'ext4'
159 self.veritysetup.verity_dict = self.verity_dict
160 with patch('builtins.open', create=True) as mock_open:
161 mock_open.return_value = MagicMock(spec=io.IOBase)
162 file_handle = mock_open.return_value.__enter__.return_value
163 self.veritysetup.create_verity_verification_metadata()
164 assert file_handle.write.call_args_list == [
165 call(b'1 ext4 ro verity'),
166 call(b'\xff'),
167 call(b'1 4096 4096 10 1 sha256 e2728628377... fb074d1db50...'),
168 call(b'\xff'),
169 call(b'\x00')
170 ]
171
172 @patch('kiwi.utils.veritysetup.Signature')
173 def test_sign_verification_metadata(self, mock_Signature):
174 metadata_file = Mock()
175 metadata_file.name = 'metadata_file'
176 self.veritysetup.verification_metadata_file = metadata_file
177 self.veritysetup.sign_verification_metadata()
178 mock_Signature.return_value.sign.assert_called_once_with()