File: test_checksum.py

package info (click to toggle)
swift 2.36.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,676 kB
  • sloc: python: 286,220; javascript: 1,059; sh: 619; pascal: 295; makefile: 81; xml: 32
file content (417 lines) | stat: -rw-r--r-- 19,474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright (c) 2024 NVIDIA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import unittest
from unittest import mock

import zlib

from swift.common.utils import checksum
from test.debug_logger import debug_logger
from test.unit import requires_crc32c, requires_crc64nvme


class TestModuleFunctions(unittest.TestCase):
    def test_find_isal_sys_package_preferred(self):
        with mock.patch('ctypes.util.find_library', return_value='my-isal.so'):
            with mock.patch('ctypes.CDLL', return_value='fake') as mock_cdll:
                self.assertEqual('fake', checksum.find_isal())
        self.assertEqual([mock.call('my-isal.so')], mock_cdll.call_args_list)

    @unittest.skipIf(
        sys.version_info.major == 3 and sys.version_info.minor < 8,
        "importlib.metadata not available until py3.8")
    def test_find_isal_pyeclib_install_found(self):
        mock_pkg = mock.MagicMock()
        mock_pkg.locate = mock.MagicMock(return_value='fake-pkg')
        with mock.patch('ctypes.util.find_library', return_value=None):
            with mock.patch('ctypes.CDLL', return_value='fake') as mock_cdll:
                with mock.patch('importlib.metadata.files',
                                return_value=[mock_pkg]):
                    self.assertEqual('fake', checksum.find_isal())
        self.assertEqual([mock.call('fake-pkg')], mock_cdll.call_args_list)

    @unittest.skipIf(
        sys.version_info.major == 3 and sys.version_info.minor < 8,
        "importlib.metadata not available until py3.8")
    def test_find_isal_pyeclib_install_not_found(self):
        mock_pkg = mock.MagicMock()
        mock_pkg.locate = mock.MagicMock(return_value='fake-pkg')
        with mock.patch('ctypes.util.find_library', return_value=None):
            with mock.patch('importlib.metadata.files', return_value=[]):
                self.assertIsNone(checksum.find_isal())

    @unittest.skipIf(
        sys.version_info.major == 3 and sys.version_info.minor < 8,
        "importlib.metadata not available until py3.8")
    def test_find_isal_pyeclib_dist_missing_files(self):
        with mock.patch('ctypes.util.find_library', return_value=None):
            with mock.patch('importlib.metadata.files', return_value=None):
                self.assertIsNone(checksum.find_isal())

    @unittest.skipIf(
        sys.version_info.major == 3 and sys.version_info.minor < 8,
        "importlib.metadata not available until py3.8")
    def test_find_isal_pyeclib_dist_info_missing(self):
        from importlib.metadata import PackageNotFoundError
        with mock.patch('ctypes.util.find_library', return_value=None):
            with mock.patch('importlib.metadata.files',
                            side_effect=PackageNotFoundError):
                self.assertIsNone(checksum.find_isal())


# If you're curious about the 0xe3069283, see "check" at
# https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-iscsi
class TestCRC32C(unittest.TestCase):
    def check_crc_func(self, impl):
        self.assertEqual(impl(b"123456789"), 0xe3069283)
        # Check that we can save/continue
        partial = impl(b"12345")
        self.assertEqual(impl(b"6789", partial), 0xe3069283)

    @unittest.skipIf(checksum.crc32c_anycrc is None, 'No anycrc CRC32C')
    def test_anycrc(self):
        self.check_crc_func(checksum.crc32c_anycrc)
        # Check preferences -- beats out reference, but not kernel or ISA-L
        if checksum.crc32c_isal is None and checksum.crc32c_kern is None:
            self.assertIs(checksum._select_crc32c_impl(),
                          checksum.crc32c_anycrc)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern(self):
        self.check_crc_func(checksum.crc32c_kern)
        # Check preferences -- beats out reference and anycrc, but not ISA-L
        if checksum.crc32c_isal is None:
            self.assertIs(checksum._select_crc32c_impl(), checksum.crc32c_kern)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_close_happy_path(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_socket = mock.MagicMock()
        mock_socket.recv.return_value = b'1234'
        mock_crc32c_socket.accept.return_value = (mock_socket, None)
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            checksum.crc32c_kern(b'x')
        self.assertEqual([mock.call()],
                         mock_socket.close.call_args_list)
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_close_after_bind_error(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_crc32c_socket.bind.side_effect = OSError('boom')
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            with self.assertRaises(OSError) as cm:
                checksum.crc32c_kern(b'x')
        self.assertEqual('boom', str(cm.exception))
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_close_after_setsockopt_error(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_crc32c_socket.setsockopt.side_effect = OSError('boom')
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            with self.assertRaises(OSError) as cm:
                checksum.crc32c_kern(b'x')
        self.assertEqual('boom', str(cm.exception))
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_close_after_accept_error(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_crc32c_socket.accept.side_effect = OSError('boom')
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            with self.assertRaises(OSError) as cm:
                checksum.crc32c_kern(b'x')
        self.assertEqual('boom', str(cm.exception))
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_after_sendall_error(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_socket = mock.MagicMock()
        mock_socket.sendall.side_effect = OSError('boom')
        mock_crc32c_socket.accept.return_value = (mock_socket, None)
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            with self.assertRaises(OSError) as cm:
                checksum.crc32c_kern(b'x')
        self.assertEqual('boom', str(cm.exception))
        self.assertEqual([mock.call()],
                         mock_socket.close.call_args_list)
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_kern is None, 'No kernel CRC32C')
    def test_kern_socket_after_recv_error(self):
        mock_crc32c_socket = mock.MagicMock()
        mock_socket = mock.MagicMock()
        mock_socket.recv.side_effect = OSError('boom')
        mock_crc32c_socket.accept.return_value = (mock_socket, None)
        with mock.patch('swift.common.utils.checksum.socket.socket',
                        return_value=mock_crc32c_socket):
            with self.assertRaises(OSError) as cm:
                checksum.crc32c_kern(b'x')
        self.assertEqual('boom', str(cm.exception))
        self.assertEqual([mock.call()],
                         mock_socket.close.call_args_list)
        self.assertEqual([mock.call()],
                         mock_crc32c_socket.close.call_args_list)

    @unittest.skipIf(checksum.crc32c_isal is None, 'No ISA-L CRC32C')
    def test_isal(self):
        self.check_crc_func(checksum.crc32c_isal)
        # Check preferences -- ISA-L always wins
        self.assertIs(checksum._select_crc32c_impl(), checksum.crc32c_isal)


class TestCRC64NVME(unittest.TestCase):
    def check_crc_func(self, impl):
        self.assertEqual(impl(b"123456789"), 0xae8b14860a799888)
        # Check that we can save/continue
        partial = impl(b"12345")
        self.assertEqual(impl(b"6789", partial), 0xae8b14860a799888)

    @unittest.skipIf(checksum.crc64nvme_anycrc is None, 'No anycrc CRC64NVME')
    def test_anycrc(self):
        self.check_crc_func(checksum.crc64nvme_anycrc)
        if checksum.crc64nvme_isal is None:
            self.assertIs(checksum._select_crc64nvme_impl(),
                          checksum.crc64nvme_anycrc)

    @unittest.skipIf(checksum.crc64nvme_isal is None, 'No ISA-L CRC64NVME')
    def test_isal(self):
        self.check_crc_func(checksum.crc64nvme_isal)
        # Check preferences -- ISA-L always wins
        self.assertIs(checksum._select_crc64nvme_impl(),
                      checksum.crc64nvme_isal)


class TestCRCHasher(unittest.TestCase):
    def setUp(self):
        self.logger = debug_logger()

    def test_base_crc_hasher(self):
        func = mock.MagicMock(return_value=0xbad1)
        hasher = checksum.CRCHasher('fake', func)
        self.assertEqual('fake', hasher.name)
        self.assertEqual(32, hasher.width)
        self.assertEqual(0, hasher.crc)
        self.assertEqual(b'\x00\x00\x00\x00', hasher.digest())
        self.assertEqual('00000000', hasher.hexdigest())

        hasher.update(b'123456789')
        self.assertEqual(0xbad1, hasher.crc)
        self.assertEqual(b'\x00\x00\xba\xd1', hasher.digest())
        self.assertEqual('0000bad1', hasher.hexdigest())

    def test_crc32_hasher(self):
        # See CRC-32/ISO-HDLC at
        # https://reveng.sourceforge.io/crc-catalogue/17plus.htm
        hasher = checksum.crc32()
        self.assertEqual('crc32', hasher.name)
        self.assertEqual(4, hasher.digest_size)
        self.assertEqual(zlib.crc32, hasher.crc_func)
        self.assertEqual(32, hasher.width)
        self.assertEqual(0, hasher.crc)
        self.assertEqual(b'\x00\x00\x00\x00', hasher.digest())
        self.assertEqual('00000000', hasher.hexdigest())

        hasher.update(b'123456789')
        self.assertEqual(0xcbf43926, hasher.crc)
        self.assertEqual(b'\xcb\xf4\x39\x26', hasher.digest())
        self.assertEqual('cbf43926', hasher.hexdigest())

    def test_crc32_hasher_contructed_with_data(self):
        hasher = checksum.crc32(b'123456789')
        self.assertEqual(zlib.crc32, hasher.crc_func)
        self.assertEqual(0xcbf43926, hasher.crc)
        self.assertEqual(b'\xcb\xf4\x39\x26', hasher.digest())
        self.assertEqual('cbf43926', hasher.hexdigest())

    def test_crc32_hasher_initial_value(self):
        hasher = checksum.crc32(initial_value=0xcbf43926)
        self.assertEqual(zlib.crc32, hasher.crc_func)
        self.assertEqual(0xcbf43926, hasher.crc)
        self.assertEqual(b'\xcb\xf4\x39\x26', hasher.digest())
        self.assertEqual('cbf43926', hasher.hexdigest())

    def test_crc32_hasher_copy(self):
        hasher = checksum.crc32(b'123456789')
        self.assertEqual(4, hasher.digest_size)
        self.assertEqual('cbf43926', hasher.hexdigest())
        hasher_copy = hasher.copy()
        self.assertEqual('crc32', hasher.name)
        self.assertEqual(zlib.crc32, hasher_copy.crc_func)
        self.assertEqual('cbf43926', hasher_copy.hexdigest())
        hasher_copy.update(b'foo')
        self.assertEqual('cbf43926', hasher.hexdigest())
        self.assertEqual('04e7e407', hasher_copy.hexdigest())
        hasher.update(b'bar')
        self.assertEqual('fe6b0d8c', hasher.hexdigest())
        self.assertEqual('04e7e407', hasher_copy.hexdigest())

    @requires_crc32c
    def test_crc32c_hasher(self):
        # See CRC-32/ISCSI at
        # https://reveng.sourceforge.io/crc-catalogue/17plus.htm
        hasher = checksum.crc32c()
        self.assertEqual('crc32c', hasher.name)
        self.assertEqual(32, hasher.width)
        self.assertEqual(0, hasher.crc)
        self.assertEqual(b'\x00\x00\x00\x00', hasher.digest())
        self.assertEqual('00000000', hasher.hexdigest())

        hasher.update(b'123456789')
        self.assertEqual(0xe3069283, hasher.crc)
        self.assertEqual(b'\xe3\x06\x92\x83', hasher.digest())
        self.assertEqual('e3069283', hasher.hexdigest())

    @requires_crc32c
    def test_crc32c_hasher_constructed_with_data(self):
        hasher = checksum.crc32c(b'123456789')
        self.assertEqual(0xe3069283, hasher.crc)
        self.assertEqual(b'\xe3\x06\x92\x83', hasher.digest())
        self.assertEqual('e3069283', hasher.hexdigest())

    @requires_crc32c
    def test_crc32c_hasher_initial_value(self):
        hasher = checksum.crc32c(initial_value=0xe3069283)
        self.assertEqual(0xe3069283, hasher.crc)
        self.assertEqual(b'\xe3\x06\x92\x83', hasher.digest())
        self.assertEqual('e3069283', hasher.hexdigest())

    @requires_crc32c
    def test_crc32c_hasher_copy(self):
        hasher = checksum.crc32c(b'123456789')
        self.assertEqual('e3069283', hasher.hexdigest())
        hasher_copy = hasher.copy()
        self.assertEqual('crc32c', hasher_copy.name)
        self.assertIs(hasher.crc_func, hasher_copy.crc_func)
        self.assertEqual('e3069283', hasher_copy.hexdigest())
        hasher_copy.update(b'foo')
        self.assertEqual('e3069283', hasher.hexdigest())
        self.assertEqual('6b2fc5b0', hasher_copy.hexdigest())
        hasher.update(b'bar')
        self.assertEqual('ae5c789c', hasher.hexdigest())
        self.assertEqual('6b2fc5b0', hasher_copy.hexdigest())

    def test_crc32c_hasher_selects_kern_impl(self):
        scuc = 'swift.common.utils.checksum'
        with mock.patch(scuc + '.crc32c_isal', None), \
                mock.patch(scuc + '.crc32c_kern') as mock_kern, \
                mock.patch(scuc + '.crc32c_anycrc', None):
            mock_kern.__name__ = 'crc32c_kern'
            self.assertIs(mock_kern, checksum.crc32c().crc_func)
            checksum.log_selected_implementation(self.logger)
        self.assertIn('Using crc32c_kern implementation for CRC32C.',
                      self.logger.get_lines_for_level('info'))

    def test_crc32c_hasher_selects_anycrc_impl(self):
        scuc = 'swift.common.utils.checksum'
        with mock.patch(scuc + '.crc32c_isal', None), \
                mock.patch(scuc + '.crc32c_kern', None), \
                mock.patch(scuc + '.crc32c_anycrc') as mock_anycrc:
            mock_anycrc.__name__ = 'crc32c_anycrc'
            self.assertIs(mock_anycrc, checksum.crc32c().crc_func)
            checksum.log_selected_implementation(self.logger)
        self.assertIn('Using crc32c_anycrc implementation for CRC32C.',
                      self.logger.get_lines_for_level('info'))

    def test_crc32c_hasher_selects_isal_impl(self):
        scuc = 'swift.common.utils.checksum'
        with mock.patch(scuc + '.crc32c_isal') as mock_isal, \
                mock.patch(scuc + '.crc32c_kern'), \
                mock.patch(scuc + '.crc32c_anycrc'):
            mock_isal.__name__ = 'crc32c_isal'
            self.assertIs(mock_isal, checksum.crc32c().crc_func)
            checksum.log_selected_implementation(self.logger)
        self.assertIn('Using crc32c_isal implementation for CRC32C.',
                      self.logger.get_lines_for_level('info'))

    @requires_crc64nvme
    def test_crc64nvme_hasher(self):
        # See CRC-64/NVME at
        # https://reveng.sourceforge.io/crc-catalogue/17plus.htm
        hasher = checksum.crc64nvme()
        self.assertEqual('crc64nvme', hasher.name)
        self.assertEqual(8, hasher.digest_size)
        self.assertEqual(64, hasher.width)
        self.assertEqual(0, hasher.crc)
        self.assertEqual(b'\x00\x00\x00\x00\x00\x00\x00\x00', hasher.digest())
        self.assertEqual('0000000000000000', hasher.hexdigest())

        hasher.update(b'123456789')
        self.assertEqual(0xae8b14860a799888, hasher.crc)
        self.assertEqual(b'\xae\x8b\x14\x86\x0a\x79\x98\x88', hasher.digest())
        self.assertEqual('ae8b14860a799888', hasher.hexdigest())

    @requires_crc64nvme
    def test_crc64nvme_hasher_constructed_with_data(self):
        hasher = checksum.crc64nvme(b'123456789')
        self.assertEqual(b'\xae\x8b\x14\x86\x0a\x79\x98\x88', hasher.digest())
        self.assertEqual('ae8b14860a799888', hasher.hexdigest())

    @requires_crc64nvme
    def test_crc64nvme_hasher_initial_value(self):
        hasher = checksum.crc64nvme(initial_value=0xae8b14860a799888)
        self.assertEqual(b'\xae\x8b\x14\x86\x0a\x79\x98\x88', hasher.digest())
        self.assertEqual('ae8b14860a799888', hasher.hexdigest())

    @requires_crc64nvme
    def test_crc64nvme_hasher_copy(self):
        hasher = checksum.crc64nvme(b'123456789')
        self.assertEqual('ae8b14860a799888', hasher.hexdigest())
        hasher_copy = hasher.copy()
        self.assertEqual('crc64nvme', hasher_copy.name)
        self.assertIs(hasher.crc_func, hasher_copy.crc_func)
        self.assertEqual('ae8b14860a799888', hasher_copy.hexdigest())
        hasher_copy.update(b'foo')
        self.assertEqual('ae8b14860a799888', hasher.hexdigest())
        self.assertEqual('673ece0d56523f46', hasher_copy.hexdigest())
        hasher.update(b'bar')
        self.assertEqual('0991d5edf1b0062e', hasher.hexdigest())
        self.assertEqual('673ece0d56523f46', hasher_copy.hexdigest())

    def test_crc64nvme_hasher_selects_anycrc_impl(self):
        scuc = 'swift.common.utils.checksum'
        with mock.patch(scuc + '.crc64nvme_isal', None), \
                mock.patch(scuc + '.crc64nvme_anycrc') as mock_anycrc:
            mock_anycrc.__name__ = 'crc64nvme_anycrc'
            self.assertIs(mock_anycrc,
                          checksum.crc64nvme().crc_func)
            checksum.log_selected_implementation(self.logger)
        self.assertIn(
            'Using crc64nvme_anycrc implementation for CRC64NVME.',
            self.logger.get_lines_for_level('info'))

    def test_crc64nvme_hasher_selects_isal_impl(self):
        scuc = 'swift.common.utils.checksum'
        with mock.patch(scuc + '.crc64nvme_isal') as mock_isal, \
                mock.patch(scuc + '.crc64nvme_anycrc'):
            mock_isal.__name__ = 'crc64nvme_isal'
            self.assertIs(mock_isal, checksum.crc64nvme().crc_func)
            checksum.log_selected_implementation(self.logger)
        self.assertIn(
            'Using crc64nvme_isal implementation for CRC64NVME.',
            self.logger.get_lines_for_level('info'))