File size: 6,675 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2018 Google LLC
#
# 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
#
#     https://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 functools
import itertools
from unittest import mock

import pytest  # type: ignore

import google_crc32c

EMPTY = b""
EMPTY_CRC = 0x00000000

# From: https://tools.ietf.org/html/rfc3720#appendix-B.4
#
#   32 bytes of zeroes:
#
#     Byte:        0  1  2  3
#
#        0:       00 00 00 00
#      ...
#       28:       00 00 00 00
#
#      CRC:       aa 36 91 8a

ALL_ZEROS = b"\x00" * 32
ALL_ZEROS_CRC = 0x8A9136AA

#   32 bytes of ones:
#
#     Byte:        0  1  2  3
#
#        0:       ff ff ff ff
#      ...
#       28:       ff ff ff ff
#
#      CRC:       43 ab a8 62

ALL_ONES = b"\xff" * 32
ALL_ONES_CRC = 0x62A8AB43

#
#   32 bytes of incrementing 00..1f:
#
#     Byte:        0  1  2  3
#
#        0:       00 01 02 03
#      ...
#       28:       1c 1d 1e 1f
#
#      CRC:       4e 79 dd 46

INCREMENTING = bytes(range(32))
INCREMENTING_CRC = 0x46DD794E

#
#   32 bytes of decrementing 1f..00:
#
#     Byte:        0  1  2  3
#
#        0:       1f 1e 1d 1c
#      ...
#       28:       03 02 01 00
#
#      CRC:       5c db 3f 11

DECREMENTING = bytes(reversed(range(32)))
DECREMENTING_CRC = 0x113FDB5C

#
#    An iSCSI - SCSI Read (10) Command PDU
#
#    Byte:        0  1  2  3
#
#       0:       01 c0 00 00
#       4:       00 00 00 00
#       8:       00 00 00 00
#      12:       00 00 00 00
#      16:       14 00 00 00
#      20:       00 00 04 00
#      24:       00 00 00 14
#      28:       00 00 00 18
#      32:       28 00 00 00
#      36:       00 00 00 00
#      40:       02 00 00 00
#      44:       00 00 00 00
#
#     CRC:       56 3a 96 d9

ISCSI_SCSI_READ_10_COMMAND_PDU = [
    0x01,
    0xC0,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x14,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x04,
    0x00,
    0x00,
    0x00,
    0x00,
    0x14,
    0x00,
    0x00,
    0x00,
    0x18,
    0x28,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x02,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
]
ISCSI_LENGTH = len(ISCSI_SCSI_READ_10_COMMAND_PDU)
ISCSI_BYTES = bytes(ISCSI_SCSI_READ_10_COMMAND_PDU)
ISCSI_CRC = 0xD9963A56


def iscsi_chunks(chunksize):
    for index in itertools.islice(range(ISCSI_LENGTH), 0, None, chunksize):
        yield ISCSI_SCSI_READ_10_COMMAND_PDU[index : index + chunksize]


_EXPECTED = [
    (EMPTY, EMPTY_CRC),
    (ALL_ZEROS, ALL_ZEROS_CRC),
    (ALL_ONES, ALL_ONES_CRC),
    (INCREMENTING, INCREMENTING_CRC),
    (DECREMENTING, DECREMENTING_CRC),
    (ISCSI_SCSI_READ_10_COMMAND_PDU, ISCSI_CRC),
    (ISCSI_BYTES, ISCSI_CRC),
]


def test_extend_w_empty_chunk():
    crc = 123
    assert google_crc32c.extend(crc, b"") == crc


def test_extend_w_multiple_chunks():
    crc = 0

    for chunk in iscsi_chunks(7):
        chunk_bytes = bytes(chunk)
        crc = google_crc32c.extend(crc, chunk_bytes)

    assert crc == ISCSI_CRC


def test_extend_w_reduce():
    chunks = [bytes(chunk) for chunk in iscsi_chunks(3)]
    assert functools.reduce(google_crc32c.extend, chunks, 0) == ISCSI_CRC


@pytest.mark.parametrize("chunk, expected", _EXPECTED)
def test_value(chunk, expected):
    assert google_crc32c.value(bytes(chunk)) == expected


def pytest_generate_tests(metafunc):
    if "_crc32c" in metafunc.fixturenames:
        metafunc.parametrize("_crc32c", ["python", "cext"], indirect=True)

@pytest.fixture
def _crc32c(request):
    if request.param == "python":
        from google_crc32c import python
        return python
    elif request.param == "cext":
        from google_crc32c import cext
        return cext
    else:
        raise ValueError("invalid internal test config")


class TestChecksum(object):
    @staticmethod
    def test_ctor_defaults(_crc32c):
        helper = google_crc32c.Checksum()
        assert helper._crc == 0

    @staticmethod
    def test_ctor_explicit(_crc32c):
        chunk = b"DEADBEEF"
        helper = google_crc32c.Checksum(chunk)
        assert helper._crc == google_crc32c.value(chunk)

    @staticmethod
    def test_update(_crc32c):
        chunk = b"DEADBEEF"
        helper = google_crc32c.Checksum()
        helper.update(chunk)
        assert helper._crc == google_crc32c.value(chunk)

    @staticmethod
    def test_update_w_multiple_chunks(_crc32c):
        helper = google_crc32c.Checksum()

        for index in itertools.islice(range(ISCSI_LENGTH), 0, None, 7):
            chunk = ISCSI_SCSI_READ_10_COMMAND_PDU[index : index + 7]
            helper.update(bytes(chunk))

        assert helper._crc == ISCSI_CRC

    @staticmethod
    def test_digest_zero(_crc32c):
        helper = google_crc32c.Checksum()
        assert helper.digest() == b"\x00" * 4

    @staticmethod
    def test_digest_nonzero(_crc32c):
        helper = google_crc32c.Checksum()
        helper._crc = 0x01020304
        assert helper.digest() == b"\x01\x02\x03\x04"

    @staticmethod
    def test_hexdigest_zero(_crc32c):
        helper = google_crc32c.Checksum()
        assert helper.hexdigest() == b"00" * 4

    @staticmethod
    def test_hexdigest_nonzero(_crc32c):
        helper = google_crc32c.Checksum()
        helper._crc = 0x091A3B2C
        assert helper.hexdigest() == b"091a3b2c"

    @staticmethod
    def test_copy(_crc32c):
        chunk = b"DEADBEEF"
        helper = google_crc32c.Checksum(chunk)
        clone = helper.copy()
        before = helper._crc
        helper.update(b"FACEDACE")
        assert clone._crc == before

    @staticmethod
    @pytest.mark.parametrize("chunksize", [1, 3, 5, 7, 11, 13, ISCSI_LENGTH])
    def test_consume_stream(_crc32c, chunksize):
        helper = google_crc32c.Checksum()
        expected = [bytes(chunk) for chunk in iscsi_chunks(chunksize)]
        stream = mock.Mock(spec=["read"])
        stream.read.side_effect = expected + [b""]

        found = list(helper.consume(stream, chunksize))

        assert helper._crc == ISCSI_CRC
        assert found == expected
        for call in stream.read.call_args_list:
            assert call == mock.call(chunksize)