Skip to content

Commit

Permalink
Add test with truncated files
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Sep 8, 2023
1 parent 0a9f63d commit a27590a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions corsikaio/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ def iter_blocks(f):
buffer_size, = RECORD_MARKER.unpack(data)

data = f.read(buffer_size)
if len(data) == 0:
if is_fortran_file:
if is_fortran_file:
if len(data) < buffer_size:
raise IOError("Read less bytes than expected, file seems to be truncated")
else:

else:
if len(data) == 0:
return

n_blocks = len(data) // BLOCK_SIZE_BYTES
n_blocks, rest = divmod(len(data), BLOCK_SIZE_BYTES)
if rest != 0:
raise IOError("Read less bytes than expected, file seems to be truncated")

for block in range(n_blocks):
start = block * BLOCK_SIZE_BYTES
stop = start + BLOCK_SIZE_BYTES
block = data[start:stop]
if len(block) < BLOCK_SIZE_BYTES:
raise IOError("Read less bytes than expected, file seems to be truncated")
yield block

# read trailing record marker
Expand Down
14 changes: 14 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,17 @@ def test_versions():

block = read_block(f, buffer_size)
assert get_version(block, EVTH_VERSION_POSITION) == version



@pytest.mark.parametrize("size", (100, 1000, 5000))
def test_iter_blocks_truncated(size, tmp_path, dummy_file):
path = tmp_path / f"test_truncated_{size}.dat"

with path.open("wb") as out, dummy_file.open("rb") as infile:
out.write(infile.read(size))

with pytest.raises(IOError, match="file seems to be truncated"):
with path.open("rb") as f:
for _ in iter_blocks(f):
pass

0 comments on commit a27590a

Please sign in to comment.