|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import random |
| 5 | +import struct |
5 | 6 | import tempfile |
6 | 7 | import unittest |
7 | 8 | from collections import defaultdict |
@@ -806,6 +807,35 @@ def test_invalid_file_path(self): |
806 | 807 | reader.replay_samples(RawCollector()) |
807 | 808 |
|
808 | 809 |
|
| 810 | +class TestBinaryFormatValidation(BinaryFormatTestBase): |
| 811 | + """Tests for malformed binary files.""" |
| 812 | + |
| 813 | + HDR_OFF_THREADS = 32 |
| 814 | + |
| 815 | + def test_replay_rejects_more_threads_than_declared(self): |
| 816 | + """Replay rejects files with more unique threads than the header declares.""" |
| 817 | + threads = [ |
| 818 | + make_thread(1, [make_frame("t1.py", 10, "t1")]), |
| 819 | + make_thread(2, [make_frame("t2.py", 20, "t2")]), |
| 820 | + ] |
| 821 | + samples = [[make_interpreter(0, threads)]] |
| 822 | + filename = self.create_binary_file(samples, compression="none") |
| 823 | + |
| 824 | + with open(filename, "r+b") as raw: |
| 825 | + raw.seek(self.HDR_OFF_THREADS) |
| 826 | + raw.write(struct.pack("=I", 1)) |
| 827 | + |
| 828 | + with BinaryReader(filename) as reader: |
| 829 | + self.assertEqual(reader.get_info()["thread_count"], 1) |
| 830 | + with self.assertRaises(ValueError) as cm: |
| 831 | + reader.replay_samples(RawCollector()) |
| 832 | + self.assertEqual( |
| 833 | + str(cm.exception), |
| 834 | + "Invalid thread count: sample data contains more unique " |
| 835 | + "threads than declared in header (declared 1, found at least 2)", |
| 836 | + ) |
| 837 | + |
| 838 | + |
809 | 839 | class TestBinaryEncodings(BinaryFormatTestBase): |
810 | 840 | """Tests specifically targeting different stack encodings.""" |
811 | 841 |
|
|
0 commit comments