def test_group_padding(self): blocks = [Padding(b"\x00" * 20), Padding(b"\x00" * 30), MetadataBlock(b"foobar")] blocks[-1].code = 0 length1 = len(MetadataBlock.writeblocks(blocks)) MetadataBlock.group_padding(blocks) length2 = len(MetadataBlock.writeblocks(blocks)) self.failUnlessEqual(length1, length2) self.failUnlessEqual(len(blocks), 2)
class TPadding(TestCase): def setUp(self): self.b = Padding("\x00" * 100) def test_padding(self): self.failUnlessEqual(self.b.write(), "\x00" * 100) def test_blank(self): self.failIf(Padding().write()) def test_empty(self): self.failIf(Padding("").write()) def test_repr(self): repr(Padding()) def test_change(self): self.b.length = 20 self.failUnlessEqual(self.b.write(), "\x00" * 20)
def test_flac_sort_pics_after_tags(self): pic1 = Picture() pic2 = Picture() pic3 = Picture() tags = VCFLACDict() pad = Padding() blocks = [] vorbis.flac_sort_pics_after_tags(blocks) self.assertEqual([], blocks) blocks = [tags] vorbis.flac_sort_pics_after_tags(blocks) self.assertEqual([tags], blocks) blocks = [tags, pad, pic1] vorbis.flac_sort_pics_after_tags(blocks) self.assertEqual([tags, pad, pic1], blocks) blocks = [pic1, pic2, tags, pad, pic3] vorbis.flac_sort_pics_after_tags(blocks) self.assertEqual([tags, pic1, pic2, pad, pic3], blocks) blocks = [pic1, pic2, pad, pic3] vorbis.flac_sort_pics_after_tags(blocks) self.assertEqual([pic1, pic2, pad, pic3], blocks)
def get_header(self, filename=None): f = self.fileobj f.seek(0) # Ensure we've got padding at the end, and only at the end. # If adding makes it too large, we'll scale it down later. self.metadata_blocks.append(Padding('\x00' * 1020)) MetadataBlock.group_padding(self.metadata_blocks) header = self.__check_header(f) # "fLaC" and maybe ID3 available = self.__find_audio_offset(f) - header data = MetadataBlock.writeblocks(self.metadata_blocks) if len(data) > available: # If we have too much data, see if we can reduce padding. padding = self.metadata_blocks[-1] newlength = padding.length - (len(data) - available) if newlength > 0: padding.length = newlength data = MetadataBlock.writeblocks(self.metadata_blocks) assert len(data) == available elif len(data) < available: # If we have too little data, increase padding. self.metadata_blocks[-1].length += (available - len(data)) data = MetadataBlock.writeblocks(self.metadata_blocks) assert len(data) == available self.__offset = len("fLaC" + data) return("fLaC" + data)
def get_header(self, filename=None): f = self.fileobj f.seek(0) self.__check_header(f) self.__offset = self.__find_audio_offset(f) data = bytearray(b'fLaC') for block in self.metadata_blocks: if isinstance(block, Padding): continue data += MetadataBlock._writeblock(block) padding = Padding() padding.length = 1020 data += MetadataBlock._writeblock(padding, is_last=True) return bytes(data)
class TPadding(TestCase): def setUp(self): self.b = Padding(b"\x00" * 100) def test_padding(self): self.failUnlessEqual(self.b.write(), b"\x00" * 100) def test_blank(self): self.failIf(Padding().write()) def test_empty(self): self.failIf(Padding(b"").write()) def test_repr(self): repr(Padding()) def test_change(self): self.b.length = 20 self.failUnlessEqual(self.b.write(), b"\x00" * 20)
def test_save_multiple_padding(self): # we don't touch existing padding blocks on save, but will # replace them in the file with one at the end def num_padding(f): blocks = f.metadata_blocks return len([b for b in blocks if isinstance(b, Padding)]) num_blocks = num_padding(self.flac) self.assertEqual(num_blocks, 1) block = Padding() block.length = 42 self.flac.metadata_blocks.append(block) block = Padding() block.length = 24 self.flac.metadata_blocks.append(block) self.flac.save() self.assertEqual(num_padding(self.flac), num_blocks + 2) new = FLAC(self.flac.filename) self.assertEqual(num_padding(new), 1) self.assertTrue(isinstance(new.metadata_blocks[-1], Padding))
def setUp(self): self.b = Padding("\x00" * 100) def test_padding(self): self.failUnlessEqual(self.b.write(), "\x00" * 100)
def setUp(self): self.b = Padding(b"\x00" * 100)
def test_too_large_padding(self): block = Padding() self.assertEqual( len(MetadataBlock._writeblocks([block], 0, 0, lambda x: 2**24)), 2**24 - 1 + 4)
def test_repr(self): repr(Padding())
def test_empty(self): self.failIf(Padding(b"").write())
def test_blank(self): self.failIf(Padding().write())
def test_writeblocks(self): blocks = [Padding(b"\x00" * 20), Padding(b"\x00" * 30)] self.failUnlessEqual(len(MetadataBlock.writeblocks(blocks)), 58)