Skip to content

Commit

Permalink
add test for bad checksum (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teque5 committed Sep 10, 2024
1 parent 6f3483e commit bd26060
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def calculate_hash(self):
new_hash = sigmf_hash.calculate_sha512(self.data_file, offset=self.data_offset, size=self.data_size_bytes)
else:
new_hash = sigmf_hash.calculate_sha512(fileobj=self.data_buffer, offset=self.data_offset, size=self.data_size_bytes)
if old_hash:
if old_hash is not None:
if old_hash != new_hash:
raise SigMFFileError("Calculated file hash does not match associated metadata.")

Expand Down
12 changes: 10 additions & 2 deletions tests/test_sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import numpy as np

from sigmf import sigmffile, utils
from sigmf import error, sigmffile, utils
from sigmf.sigmffile import SigMFFile

from .testdata import *
Expand Down Expand Up @@ -51,6 +51,14 @@ def test_iterator_basic(self):
count += 1
self.assertEqual(count, len(self.sigmf_object))

def test_checksum(self):
"""Ensure checksum fails when incorrect or empty string."""
for new_checksum in ("", "a", 0):
bad_checksum_metadata = copy.deepcopy(TEST_METADATA)
bad_checksum_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.HASH_KEY] = new_checksum
with self.assertRaises(error.SigMFFileError):
_ = SigMFFile(bad_checksum_metadata, self.temp_path_data)


class TestAnnotationHandling(unittest.TestCase):
def test_get_annotations_with_index(self):
Expand All @@ -64,7 +72,7 @@ def test_get_annotations_with_index(self):
[
{SigMFFile.START_INDEX_KEY: 0, SigMFFile.LENGTH_INDEX_KEY: 16},
{SigMFFile.START_INDEX_KEY: 1},
]
],
)

def test__count_samples_from_annotation(self):
Expand Down

0 comments on commit bd26060

Please sign in to comment.