|
2 | 2 |
|
3 | 3 | import io
|
4 | 4 | import os
|
| 5 | +import shutil |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | import pytest
|
@@ -247,3 +248,35 @@ def test_numpy2_dtype_poly_and_linear():
|
247 | 248 | # test linearconverter (no exception)
|
248 | 249 | converter = converters.LinearConverter(*coeffs)
|
249 | 250 | converted = converter.convert(field_array)
|
| 251 | + |
| 252 | + |
| 253 | +@pytest.mark.parametrize("pkt_class", [FixedLength, VariableLength]) |
| 254 | +@pytest.mark.parametrize("num_garbage_bytes", list(range(1, 11))) |
| 255 | +def test_readahead_primary_header_IndexError(pkt_class, num_garbage_bytes): |
| 256 | + """Fixes IndexError from reading primary header in packet iteration when |
| 257 | + file ends abruptly. |
| 258 | +
|
| 259 | + See: https://github.com/CCSDSPy/ccsdspy/issues/139 |
| 260 | + """ |
| 261 | + pkt = VariableLength( |
| 262 | + [ |
| 263 | + PacketArray( |
| 264 | + name="data", |
| 265 | + data_type="uint", |
| 266 | + bit_length=16, |
| 267 | + array_shape="expand", |
| 268 | + ), |
| 269 | + PacketField(name="footer", data_type="uint", bit_length=16), |
| 270 | + ] |
| 271 | + ) |
| 272 | + |
| 273 | + dir_path = os.path.dirname(os.path.realpath(__file__)) |
| 274 | + bin_path = os.path.join(dir_path, "data", "var_length", "var_length_packets_with_footer.bin") |
| 275 | + new_path = os.path.join(dir_path, "data", "garbage_at_end.bin") |
| 276 | + shutil.copy(bin_path, new_path) |
| 277 | + |
| 278 | + with open(new_path, "ab") as fh: |
| 279 | + fh.write(b"a" * num_garbage_bytes) |
| 280 | + |
| 281 | + with pytest.warns(UserWarning, match="File appears truncated"): |
| 282 | + result = pkt.load(new_path) |
0 commit comments