|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | import random |
| 6 | +import struct |
6 | 7 | import tempfile |
7 | 8 | import unittest |
8 | 9 | from collections import defaultdict |
@@ -941,6 +942,35 @@ def test_writer_total_samples_after_close_returns_zero(self): |
941 | 942 | self.assertEqual(w.total_samples, 0) |
942 | 943 |
|
943 | 944 |
|
| 945 | +class TestBinaryFormatValidation(BinaryFormatTestBase): |
| 946 | + """Tests for malformed binary files.""" |
| 947 | + |
| 948 | + HDR_OFF_THREADS = 32 |
| 949 | + |
| 950 | + def test_replay_rejects_more_threads_than_declared(self): |
| 951 | + """Replay rejects files with more unique threads than the header declares.""" |
| 952 | + threads = [ |
| 953 | + make_thread(1, [make_frame("t1.py", 10, "t1")]), |
| 954 | + make_thread(2, [make_frame("t2.py", 20, "t2")]), |
| 955 | + ] |
| 956 | + samples = [[make_interpreter(0, threads)]] |
| 957 | + filename = self.create_binary_file(samples, compression="none") |
| 958 | + |
| 959 | + with open(filename, "r+b") as raw: |
| 960 | + raw.seek(self.HDR_OFF_THREADS) |
| 961 | + raw.write(struct.pack("=I", 1)) |
| 962 | + |
| 963 | + with BinaryReader(filename) as reader: |
| 964 | + self.assertEqual(reader.get_info()["thread_count"], 1) |
| 965 | + with self.assertRaises(ValueError) as cm: |
| 966 | + reader.replay_samples(RawCollector()) |
| 967 | + self.assertEqual( |
| 968 | + str(cm.exception), |
| 969 | + "Invalid thread count: sample data contains more unique " |
| 970 | + "threads than declared in header (declared 1, found at least 2)", |
| 971 | + ) |
| 972 | + |
| 973 | + |
944 | 974 | class TestBinaryEncodings(BinaryFormatTestBase): |
945 | 975 | """Tests specifically targeting different stack encodings.""" |
946 | 976 |
|
|
0 commit comments