Skip to content

Commit a0488e0

Browse files
committed
test
1 parent d48dd67 commit a0488e0

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import random
5+
import struct
56
import tempfile
67
import unittest
78
from collections import defaultdict
@@ -806,6 +807,35 @@ def test_invalid_file_path(self):
806807
reader.replay_samples(RawCollector())
807808

808809

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+
809839
class TestBinaryEncodings(BinaryFormatTestBase):
810840
"""Tests specifically targeting different stack encodings."""
811841

0 commit comments

Comments
 (0)