Skip to content

Commit

Permalink
fix: tests and license headers
Browse files Browse the repository at this point in the history
  • Loading branch information
crwilcox committed Mar 18, 2020
1 parent 896b67f commit 37d9ab7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
5 changes: 0 additions & 5 deletions src/crc32c/__init__.py
Expand Up @@ -12,13 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import struct
import warnings

# NOTE: ``__config__`` **must** be the first import because it (may)
# modify the search path used to locate shared libraries.
import crc32c.__config__

_SLOW_CRC32C_WARNING = (
"Currently using crcmod in pure python form. This is a slow "
"implementation. If you can compile a c extension, you will have much "
Expand Down
20 changes: 19 additions & 1 deletion src/crc32c/cffi.py
@@ -1,6 +1,24 @@
# 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 struct

# NOTE: ``__config__`` **must** be the first import because it (may)
# modify the search path used to locate shared libraries.
import crc32c.__config__
import crc32c._crc32c_cffi


def extend(crc, chunk):
"""Update an existing CRC checksum with new chunk of data.
Expand Down
20 changes: 12 additions & 8 deletions src/crc32c/python.py
@@ -1,12 +1,16 @@
# crc32c.Checksum(b"blah").digest()
# b's\x0c\xb4\xe4'
# Copyright 2020 Google LLC
#
# >>> import crcmod
# >>> c = crcmod.predefined.Crc("crc-32c")
# >>> c.update(b"blah")
# >>> c.digest()
# b's\x0c\xb4\xe4'

# 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 array
import struct
Expand Down
27 changes: 13 additions & 14 deletions tests/test___init__.py
Expand Up @@ -18,7 +18,6 @@
import pytest

import crc32c
from crc32c import python

EMPTY = b""
EMPTY_CRC = 0x00000000
Expand Down Expand Up @@ -194,11 +193,11 @@ def test_value(chunk, expected):


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

@pytest.fixture
def crc32c(request):
def _crc32c(request):
if request.param == "python":
from crc32c import python
return python
Expand All @@ -214,25 +213,25 @@ def crc32c(request):

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

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

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

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

for index in itertools.islice(range(ISCSI_LENGTH), 0, None, 7):
Expand All @@ -242,29 +241,29 @@ def test_update_w_multiple_chunks(crc32c):
assert helper._crc == ISCSI_CRC

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

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

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

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

@staticmethod
def test_copy(crc32c):
def test_copy(_crc32c):
chunk = b"DEADBEEF"
helper = crc32c.Checksum(chunk)
clone = helper.copy()
Expand All @@ -274,7 +273,7 @@ def test_copy(crc32c):

@staticmethod
@pytest.mark.parametrize("chunksize", [1, 3, 5, 7, 11, 13, ISCSI_LENGTH])
def test_consume_stream(crc32c, chunksize):
def test_consume_stream(_crc32c, chunksize):
helper = crc32c.Checksum()
expected = list(iscsi_chunks(chunksize))
stream = mock.Mock(spec=["read"])
Expand Down

0 comments on commit 37d9ab7

Please sign in to comment.