diff --git a/tests/test_opus_tags.py b/tests/test_opus_tags.py index 9648cd388e..df5dabad69 100644 --- a/tests/test_opus_tags.py +++ b/tests/test_opus_tags.py @@ -128,6 +128,41 @@ def test_rejects_what_it_cannot_rewrite(self): with self.assertRaisesRegex(ValueError, "two Ogg pages"): T.retag(data[:first], T.tags_packet([])) + def test_retag_rejects_serial_mismatch_sequence_gap_and_continuation_flags(self): + with tempfile.TemporaryDirectory() as d: + path = os.path.join(d, "b", "0ABCDEF1.ogg") + make_ogg(path, packets=3, stream_id=0x11111111) + original = Path(path).read_bytes() + raw_pages = [original[s:e] for s, e in T.pages(original)] + p0, p1, p2 = raw_pages[0], raw_pages[1], raw_pages[2] + head_pkt = p0[27 + p0[26]:] + new_tags = T.tags_packet([("TITLE", "new")]) + + cases = [ + ("page 0 sequence != 0", + awc_opus.ogg_page(0x11111111, 2, 0, [head_pkt], 0x02) + p1 + p2, + "sequence number 0"), + ("page 0 serial mismatch", + awc_opus.ogg_page(0x22222222, 0, 0, [head_pkt], 0x02) + p1 + p2, + "serial mismatch"), + ("audio page serial mismatch", + p0 + p1 + awc_opus.ogg_page(0x33333333, 2, 1000, [PACKET], 0x04), + "serial mismatch"), + ("audio page sequence gap", + p0 + p1 + awc_opus.ogg_page(0x11111111, 5, 1000, [PACKET], 0x04), + "non-contiguous"), + ("OpusTags continuation flag", + p0 + awc_opus.ogg_page(0x11111111, 1, 0, [T.tags_packet([])], 0x01) + p2, + "continuation page"), + ("OpusTags EOS flag", + p0 + awc_opus.ogg_page(0x11111111, 1, 0, [T.tags_packet([])], 0x04) + p2, + "BOS/EOS"), + ] + for label, bad_data, pattern in cases: + with self.subTest(case=label): + with self.assertRaisesRegex(ValueError, pattern): + T.retag(bad_data, new_tags) + class TestFolder(unittest.TestCase): def setUp(self): diff --git a/tools/opus_tags.py b/tools/opus_tags.py index 3c17fcdbd1..037a447c20 100644 --- a/tools/opus_tags.py +++ b/tools/opus_tags.py @@ -90,9 +90,22 @@ def retag(data: bytes, packet: bytes) -> bytes: raise ValueError("tags do not fit in one page") it = checked_pages(data) try: - _, (tags_at, tags_end) = next(it), next(it) + (head_at, _head_end), (tags_at, tags_end) = next(it), next(it) except StopIteration: raise ValueError("fewer than two Ogg pages") from None + head_serial, head_seq = struct.unpack_from(" bytes: raise ValueError("the OpusTags packet continues past page 1") if any(segment != 255 for segment in segments[:-1]): raise ValueError("the OpusTags page contains another packet") - for _ in it: # also check the audio tail before publishing any replacement - pass - serial, seq = struct.unpack_from("