Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for bad checksum #68

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,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
Loading