Skip to content

Commit bd26060

Browse files
authored
add test for bad checksum (#68)
1 parent 6f3483e commit bd26060

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

sigmf/sigmffile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def calculate_hash(self):
505505
new_hash = sigmf_hash.calculate_sha512(self.data_file, offset=self.data_offset, size=self.data_size_bytes)
506506
else:
507507
new_hash = sigmf_hash.calculate_sha512(fileobj=self.data_buffer, offset=self.data_offset, size=self.data_size_bytes)
508-
if old_hash:
508+
if old_hash is not None:
509509
if old_hash != new_hash:
510510
raise SigMFFileError("Calculated file hash does not match associated metadata.")
511511

tests/test_sigmffile.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import numpy as np
1818

19-
from sigmf import sigmffile, utils
19+
from sigmf import error, sigmffile, utils
2020
from sigmf.sigmffile import SigMFFile
2121

2222
from .testdata import *
@@ -51,6 +51,14 @@ def test_iterator_basic(self):
5151
count += 1
5252
self.assertEqual(count, len(self.sigmf_object))
5353

54+
def test_checksum(self):
55+
"""Ensure checksum fails when incorrect or empty string."""
56+
for new_checksum in ("", "a", 0):
57+
bad_checksum_metadata = copy.deepcopy(TEST_METADATA)
58+
bad_checksum_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.HASH_KEY] = new_checksum
59+
with self.assertRaises(error.SigMFFileError):
60+
_ = SigMFFile(bad_checksum_metadata, self.temp_path_data)
61+
5462

5563
class TestAnnotationHandling(unittest.TestCase):
5664
def test_get_annotations_with_index(self):
@@ -64,7 +72,7 @@ def test_get_annotations_with_index(self):
6472
[
6573
{SigMFFile.START_INDEX_KEY: 0, SigMFFile.LENGTH_INDEX_KEY: 16},
6674
{SigMFFile.START_INDEX_KEY: 1},
67-
]
75+
],
6876
)
6977

7078
def test__count_samples_from_annotation(self):

0 commit comments

Comments
 (0)