def test_parse(self): self.assertEqual(None, id3_header.parse_size("")) self.assertEqual(None, id3_header.parse_size("short")) self.assertEqual(None, id3_header.parse_size("1234567890")) # An ID3 header that claims to have size 0 is considered invalid. zero_hdr = id3_header.create_test_header(0) self.assertEqual(None, id3_header.parse_size(zero_hdr)) for test_size in (1, 1<<7, 1<<14, 1<<21): test_hdr = id3_header.create_test_header(test_size) for offset in (0, 5): data = "x" * offset + test_hdr self.assertEqual(test_size, id3_header.parse_size(data, offset=offset))
def test_parse(self): self.assertEqual(None, id3_header.parse_size("")) self.assertEqual(None, id3_header.parse_size("short")) self.assertEqual(None, id3_header.parse_size("1234567890")) # An ID3 header that claims to have size 0 is considered invalid. zero_hdr = id3_header.create_test_header(0) self.assertEqual(None, id3_header.parse_size(zero_hdr)) for test_size in (1, 1 << 7, 1 << 14, 1 << 21): test_hdr = id3_header.create_test_header(test_size) for offset in (0, 5): data = "x" * offset + test_hdr self.assertEqual(test_size, id3_header.parse_size(data, offset=offset))
def test_test_header(self): for raw_size, cooked_size in self.size_test_cases: self.assertEqual(self.prefix + raw_size, id3_header.create_test_header(cooked_size))
def test_split(self): raw_hdr, hdr = mp3_header_test.VALID_MP3_HEADERS.items()[0] frame_data = raw_hdr.ljust(hdr.frame_size, "a") # Set up a fragment of a header partial_header = raw_hdr[:3] short_frame = frame_data[:25] assert len(short_frame) < len(frame_data) id3_data = id3_header.create_test_header(77).ljust(77, "b") # An ID3 tag with a valid frame tag stashed inside. evil_id3_data = id3_header.create_test_header(50) + raw_hdr evil_id3_data = evil_id3_data.ljust(50, "c") for seq in ( [ frame_data ], [ frame_data, frame_data ], [ 'junk', frame_data ], [ 'junk', frame_data, frame_data ], [ 'junk', frame_data, frame_data, 'junk' ], [ 'junk', frame_data, frame_data, 'junk', frame_data ], # Check handling of truncated headers and frames. [ partial_header ], [ 'junk', partial_header ], [ 'junk', short_frame ], [ frame_data, partial_header ], [ frame_data, short_frame ], [ frame_data, 'junk', short_frame ], [ frame_data, 'junk', partial_header], # ID3 headers mixed in [ id3_data, frame_data ], [ frame_data, id3_data ], [ id3_data, frame_data ], [ id3_data, frame_data, id3_data ], [ evil_id3_data, frame_data, "junk" ], [ "junk", frame_data, evil_id3_data, frame_data ], [ "junk", frame_data, evil_id3_data, frame_data, "junk" ], [ "junk" + evil_id3_data, id3_data, frame_data, evil_id3_data ], # Some longer sequences 500 * [ frame_data ], 500 * [ "junk", frame_data, id3_data, frame_data ] ): data = ''.join(seq) stream = cStringIO.StringIO(data) split_stream = list(mp3_frame.split(stream)) split_stream_from_blocks = list(mp3_frame.split_blocks(iter(seq))) split_stream_from_one_block = mp3_frame.split_one_block(data) # Make sure that the sequences of header/frame data pairs # returned by mp3_frame.split(), mp3_frame.split_blocks() # and mp3_frame.split_one_block() matche what we would # expect. self.assertEqual(len(seq), len(split_stream)) for expected_data, (actual_hdr, data) in zip(seq, split_stream): self.assertEqual(expected_data, data) if expected_data == frame_data: self.assertTrue(actual_hdr is not None) self.assertTrue(actual_hdr.match(hdr)) self.assertEqual(hdr.frame_size, len(frame_data)) else: self.assertTrue(actual_hdr is None) self.assertEqual(len(seq), len(split_stream_from_blocks)) for (hdr1, data1), (hdr2, data2) in zip(split_stream, split_stream_from_blocks): self.assertEqual(str(hdr1), str(hdr2)) self.assertEqual(data1, data2) self.assertEqual(len(seq), len(split_stream_from_one_block)) for (hdr1, data1), (hdr2, data2) in zip( split_stream, split_stream_from_one_block): self.assertEqual(str(hdr1), str(hdr2)) self.assertEqual(data1, data2)