class TIFFFile(TestCase): has_tags = os.path.join(DATA_DIR, 'with-id3.aif') no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif') def setUp(self): self.file_1 = open(self.has_tags, 'rb') self.iff_1 = IFFFile(self.file_1) self.file_2 = open(self.no_tags, 'rb') self.iff_2 = IFFFile(self.file_2) self.tmp_1_name = get_temp_copy(self.has_tags) self.file_1_tmp = open(self.tmp_1_name, 'rb+') self.iff_1_tmp = IFFFile(self.file_1_tmp) self.tmp_2_name = get_temp_copy(self.no_tags) self.file_2_tmp = open(self.tmp_2_name, 'rb+') self.iff_2_tmp = IFFFile(self.file_2_tmp) def tearDown(self): self.file_1.close() self.file_2.close() self.file_1_tmp.close() self.file_2_tmp.close() os.unlink(self.tmp_1_name) os.unlink(self.tmp_2_name) def test_has_chunks(self): self.failUnless(u'FORM' in self.iff_1) self.failUnless(u'COMM' in self.iff_1) self.failUnless(u'SSND' in self.iff_1) self.failUnless(u'ID3' in self.iff_1) self.failUnless(u'FORM' in self.iff_2) self.failUnless(u'COMM' in self.iff_2) self.failUnless(u'SSND' in self.iff_2) def test_is_chunks(self): self.failUnless(isinstance(self.iff_1[u'FORM'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'COMM'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'SSND'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'ID3'], IFFChunk)) def test_chunk_size(self): self.failUnlessEqual(self.iff_1[u'FORM'].size, 17096) self.failUnlessEqual(self.iff_2[u'FORM'].size, 16054) def test_chunk_data_size(self): self.failUnlessEqual(self.iff_1[u'FORM'].data_size, 17088) self.failUnlessEqual(self.iff_2[u'FORM'].data_size, 16046) def test_FORM_chunk_resize(self): self.iff_1_tmp[u'FORM'].resize(17000) self.failUnlessEqual( IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000) self.iff_2_tmp[u'FORM'].resize(0) self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 0) def test_child_chunk_resize(self): self.iff_1_tmp[u'ID3'].resize(128) id3 = self.iff_1_tmp[u'ID3'] id3.write(b"\xff" * 128) self.assertEqual(id3.read(), b"\xff" * 128) self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128) self.failUnlessEqual( IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182) def test_chunk_delete(self): del self.iff_1_tmp[u'ID3'] self.failIf(u'ID3' in self.iff_1_tmp) self.failIf(u'ID3' in IFFFile(self.file_1_tmp)) self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054) del self.iff_2_tmp[u'SSND'] self.failIf(u'SSND' in self.iff_2_tmp) self.failIf(u'SSND' in IFFFile(self.file_2_tmp)) self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38) def test_insert_chunk(self): self.iff_2_tmp.insert_chunk(u'ID3') new_iff = IFFFile(self.file_2_tmp) self.failUnless(u'ID3' in new_iff) self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk)) self.failUnlessEqual(new_iff[u'FORM'].size, 16062) self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054) self.failUnlessEqual(new_iff[u'ID3'].size, 8) self.failUnlessEqual(new_iff[u'ID3'].data_size, 0)
class TIFFFile(TestCase): has_tags = os.path.join('tests', 'data', 'with-id3.aif') no_tags = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif') def setUp(self): self.file_1 = open(self.has_tags, 'rb') self.iff_1 = IFFFile(self.file_1) self.file_2 = open(self.no_tags, 'rb') self.iff_2 = IFFFile(self.file_2) fd_1, tmp_1_name = mkstemp(suffix='.aif') shutil.copy(self.has_tags, tmp_1_name) self.file_1_tmp = open(tmp_1_name, 'rb+') self.iff_1_tmp = IFFFile(self.file_1_tmp) fd_2, tmp_2_name = mkstemp(suffix='.aif') shutil.copy(self.no_tags, tmp_2_name) self.file_2_tmp = open(tmp_2_name, 'rb+') self.iff_2_tmp = IFFFile(self.file_2_tmp) def test_has_chunks(self): self.failUnless('FORM' in self.iff_1) self.failUnless('COMM' in self.iff_1) self.failUnless('SSND' in self.iff_1) self.failUnless('ID3' in self.iff_1) self.failUnless('FORM' in self.iff_2) self.failUnless('COMM' in self.iff_2) self.failUnless('SSND' in self.iff_2) def test_is_chunks(self): self.failUnless(isinstance(self.iff_1['FORM'], IFFChunk)) self.failUnless(isinstance(self.iff_1['COMM'], IFFChunk)) self.failUnless(isinstance(self.iff_1['SSND'], IFFChunk)) self.failUnless(isinstance(self.iff_1['ID3'], IFFChunk)) def test_chunk_size(self): self.failUnlessEqual(self.iff_1['FORM'].size, 17096) self.failUnlessEqual(self.iff_2['FORM'].size, 16054) def test_chunk_data_size(self): self.failUnlessEqual(self.iff_1['FORM'].data_size, 17088) self.failUnlessEqual(self.iff_2['FORM'].data_size, 16046) def test_FORM_chunk_resize(self): self.iff_1_tmp['FORM'].resize(17000) self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 17000) self.iff_2_tmp['FORM'].resize(0) self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].data_size, 0) def test_child_chunk_resize(self): self.iff_1_tmp['ID3'].resize(128) self.failUnlessEqual(IFFFile(self.file_1_tmp)['ID3'].data_size, 128) self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 16182) def test_chunk_delete(self): del self.iff_1_tmp['ID3'] self.failIf('ID3' in self.iff_1_tmp) self.failIf('ID3' in IFFFile(self.file_1_tmp)) self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].size, 16054) del self.iff_2_tmp['SSND'] self.failIf('SSND' in self.iff_2_tmp) self.failIf('SSND' in IFFFile(self.file_2_tmp)) self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].size, 38) def test_insert_chunk(self): self.iff_2_tmp.insert_chunk('ID3') new_iff = IFFFile(self.file_2_tmp) self.failUnless('ID3' in new_iff) self.failUnless(isinstance(new_iff['ID3'], IFFChunk)) self.failUnlessEqual(new_iff['FORM'].size, 16062) self.failUnlessEqual(new_iff['FORM'].data_size, 16054) self.failUnlessEqual(new_iff['ID3'].size, 8) self.failUnlessEqual(new_iff['ID3'].data_size, 0) def tearDown(self): self.file_1.close() self.file_2.close() self.file_1_tmp.close() self.file_2_tmp.close()
class TIFFFile(TestCase): has_tags = os.path.join(DATA_DIR, 'with-id3.aif') no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif') def setUp(self): self.file_1 = open(self.has_tags, 'rb') self.iff_1 = IFFFile(self.file_1) self.file_2 = open(self.no_tags, 'rb') self.iff_2 = IFFFile(self.file_2) self.tmp_1_name = get_temp_copy(self.has_tags) self.file_1_tmp = open(self.tmp_1_name, 'rb+') self.iff_1_tmp = IFFFile(self.file_1_tmp) self.tmp_2_name = get_temp_copy(self.no_tags) self.file_2_tmp = open(self.tmp_2_name, 'rb+') self.iff_2_tmp = IFFFile(self.file_2_tmp) def tearDown(self): self.file_1.close() self.file_2.close() self.file_1_tmp.close() self.file_2_tmp.close() os.unlink(self.tmp_1_name) os.unlink(self.tmp_2_name) def test_has_chunks(self): self.failUnless(u'FORM' in self.iff_1) self.failUnless(u'COMM' in self.iff_1) self.failUnless(u'SSND' in self.iff_1) self.failUnless(u'ID3' in self.iff_1) self.failUnless(u'FORM' in self.iff_2) self.failUnless(u'COMM' in self.iff_2) self.failUnless(u'SSND' in self.iff_2) def test_is_chunks(self): self.failUnless(isinstance(self.iff_1[u'FORM'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'COMM'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'SSND'], IFFChunk)) self.failUnless(isinstance(self.iff_1[u'ID3'], IFFChunk)) def test_chunk_size(self): self.failUnlessEqual(self.iff_1[u'FORM'].size, 17096) self.failUnlessEqual(self.iff_2[u'FORM'].size, 16054) def test_chunk_data_size(self): self.failUnlessEqual(self.iff_1[u'FORM'].data_size, 17088) self.failUnlessEqual(self.iff_2[u'FORM'].data_size, 16046) def test_FORM_chunk_resize(self): self.iff_1_tmp[u'FORM'].resize(17000) self.failUnlessEqual( IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000) self.iff_2_tmp[u'FORM'].resize(4) self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 4) def test_child_chunk_resize(self): self.iff_1_tmp[u'ID3'].resize(128) id3 = self.iff_1_tmp[u'ID3'] id3.write(b"\xff" * 128) self.assertEqual(id3.read(), b"\xff" * 128) self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128) self.failUnlessEqual( IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182) def test_chunk_delete(self): del self.iff_1_tmp[u'ID3'] self.failIf(u'ID3' in self.iff_1_tmp) self.failIf(u'ID3' in IFFFile(self.file_1_tmp)) self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054) del self.iff_2_tmp[u'SSND'] self.failIf(u'SSND' in self.iff_2_tmp) self.failIf(u'SSND' in IFFFile(self.file_2_tmp)) self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38) def test_insert_chunk(self): self.iff_2_tmp.insert_chunk(u'ID3') new_iff = IFFFile(self.file_2_tmp) self.failUnless(u'ID3' in new_iff) self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk)) self.failUnlessEqual(new_iff[u'FORM'].size, 16062) self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054) self.failUnlessEqual(new_iff[u'ID3'].size, 8) self.failUnlessEqual(new_iff[u'ID3'].data_size, 0) def test_insert_padded_chunks(self): padded = self.iff_2_tmp.insert_chunk(u'TST1') unpadded = self.iff_2_tmp.insert_chunk(u'TST2') # The second chunk needs no padding unpadded.resize(4) self.failUnlessEqual(4, unpadded.data_size) self.failUnlessEqual(0, unpadded.padding()) self.failUnlessEqual(12, unpadded.size) # Resize the first chunk so it needs padding padded.resize(3) self.failUnlessEqual(3, padded.data_size) self.failUnlessEqual(1, padded.padding()) self.failUnlessEqual(12, padded.size) self.failUnlessEqual(padded.offset + padded.size, unpadded.offset) # Verify the padding byte gets written correctly self.file_2_tmp.seek(padded.data_offset) self.file_2_tmp.write(b'ABCD') padded.write(b'ABC') self.file_2_tmp.seek(padded.data_offset) self.failUnlessEqual(b'ABC\x00', self.file_2_tmp.read(4)) # Verify the second chunk got not overwritten self.file_2_tmp.seek(unpadded.offset) self.failUnlessEqual(b'TST2', self.file_2_tmp.read(4)) def test_delete_padded_chunks(self): iff_file = self.iff_2_tmp iff_file.insert_chunk(u'TST') # Resize to odd length, should insert 1 padding byte iff_file[u'TST'].resize(3) # Insert another chunk after the first one iff_file.insert_chunk(u'TST2') iff_file[u'TST2'].resize(2) self.failUnlessEqual(iff_file[u'FORM'].size, 16076) self.failUnlessEqual(iff_file[u'FORM'].data_size, 16068) self.failUnlessEqual(iff_file[u'TST'].size, 12) self.failUnlessEqual(iff_file[u'TST'].data_size, 3) self.failUnlessEqual(iff_file[u'TST'].data_offset, 16062) self.failUnlessEqual(iff_file[u'TST2'].size, 10) self.failUnlessEqual(iff_file[u'TST2'].data_size, 2) self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16074) # Delete the odd chunk iff_file.delete_chunk(u'TST') self.failUnlessEqual(iff_file[u'FORM'].size, 16064) self.failUnlessEqual(iff_file[u'FORM'].data_size, 16056) self.failUnlessEqual(iff_file[u'TST2'].size, 10) self.failUnlessEqual(iff_file[u'TST2'].data_size, 2) self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16062) # Reloading the file should give the same results new_iff_file = IFFFile(self.file_2_tmp) self.failUnlessEqual(new_iff_file[u'FORM'].size, iff_file[u'FORM'].size) self.failUnlessEqual(new_iff_file[u'TST2'].size, iff_file[u'TST2'].size) self.failUnlessEqual(new_iff_file[u'TST2'].data_size, iff_file[u'TST2'].data_size) self.failUnlessEqual(new_iff_file[u'TST2'].data_offset, iff_file[u'TST2'].data_offset)