def test_tarblock_readinto_multiple_blocks_populates_oversized_buffer_with_content_elements( self): content1 = b'abc' content2 = b'de' self.assertGreaterEqual( len(content1), len(content2), 'Test only valid if second content block is the same size or smaller than the first content block' ) f = io.BytesIO(content1 + content2 + b'\0') length = len(content1) + len(content2) blockLength = len(content1) start = 0 block = utar.TarBlock(f, start, length, blockLength) arbitrary_additional_length = 3 buf = bytearray(blockLength + arbitrary_additional_length) b = next(block) s = b.readinto(buf) self.assertEqual(s, len(content1)) self.assertEqual(buf[0:s], content1) b = next(block) s = b.readinto(buf) self.assertEqual(s, len(content2)) self.assertEqual(buf[0:s], content2)
def test_tarblock_next_no_blocks_left_returns_none(self): f = io.BytesIO(b'abc') start = 0 length = 0 block = utar.TarBlock(f, start, length) with self.assertRaises(StopIteration): next(block)
def test_tarblock_read_content_less_than_block_size(self): content = b'abcdef' f = io.BytesIO(content) start = 0 length = len(content) blockLength = length + 1 block = utar.TarBlock(f, start, length, blockLength) c = block.read() self.assertEqual
def test_tarblock_read_return_block_content(self): content = b'abcdef' blockContent = content + b'\0' f = io.BytesIO(blockContent) start = 0 length = len(content) blockLength = len(blockContent) block = utar.TarBlock(f, start, length, blockLength) c = block.read() self.assertEqual(c, content)
def test_tarblock_constructor_set_properties(self): f = io.BytesIO() start = 3 length = 7 b = utar.TarBlock(f, start, length) self.assertEqual(b._io, f) self.assertEqual(b.Start, start) self.assertEqual(b.Length, length) self.assertEqual(b._current_position, -1) self.assertEqual(b._bytes_remaining, length) self.assertEqual(b._block_length_bytes, utar.BLOCK_LENGTH_BYTES)
def test_tarblock_iter_returns_iterable_and_resets_position_and_bytes_remaining( self): f = io.BytesIO(b'aabbc\0') start = 0 length = 5 blockLength = 2 block = utar.TarBlock(f, start, length) block._current_position = 13 block._bytes_remaining = 17 m = iter(block) self.assertIsInstance(m, Iterable) self.assertEqual(block._current_position, -1) self.assertEqual(block._bytes_remaining, length)
def test_tarblock_readinto_buffer_longer_than_available_content_populates_start_of_buffer( self): content = b'abcdef' blockContent = content + b'\0' f = io.BytesIO(blockContent) start = 0 length = len(content) blockLength = len(blockContent) block = utar.TarBlock(f, start, length, blockLength) arbitrary_additional_length = 3 buf = bytearray(len(content) + arbitrary_additional_length) s = block.readinto(buf) self.assertEqual(s, len(content)) self.assertEqual(buf[0:len(content)], content)
def test_tarblock_readinto_updates_buffer_with_content_and_returns_size_of_data_read( self): content = b'abcdef' blockContent = content + b'\0' f = io.BytesIO(blockContent) start = 0 length = len(content) blockLength = len(blockContent) block = utar.TarBlock(f, start, length, blockLength) buf = bytearray(len(content)) s = block.readinto(buf) self.assertEqual(s, len(content)) self.assertEqual(buf, content)
def test_tarblock_read_iostream_pointer_not_at_block_start_byte_still_able_to_read_block( self): contentHeader = b'this-is-the-header\0' content = b'this-is-the-content' contentFooter = b'\0\0' blockContent = contentHeader + content + contentFooter f = io.BytesIO(blockContent) f.seek(0) start = len(contentHeader) length = len(content) block = utar.TarBlock(f, start, length) c = block.read() self.assertEqual(c, content)
def test_tarblock_next_return_next_available_block(self): f = io.BytesIO(b'aabbc\0') start = 0 length = 5 blockLength = 2 block = utar.TarBlock(f, start, length) block._block_length_bytes = blockLength b = next(block) self.assertEqual(b._current_position, start) self.assertEqual(b._bytes_remaining, length) b = next(block) self.assertEqual(b._current_position, start + blockLength) self.assertEqual(b._bytes_remaining, length - blockLength) b = next(block) self.assertEqual(b._current_position, start + 2 * blockLength) self.assertEqual(b._bytes_remaining, length - 2 * blockLength) with self.assertRaises(StopIteration): next(block)
def test_tarblock_read_multiple_blocks_returns_content_elements(self): content1 = b'abc' content2 = b'de' self.assertGreaterEqual( len(content1), len(content2), 'Test only valid if second content block is the same size or smaller than the first content block' ) f = io.BytesIO(content1 + content2 + b'\0') length = len(content1) + len(content2) blockLength = len(content1) start = 0 block = utar.TarBlock(f, start, length, blockLength) b = next(block) c1 = b.read() b = next(block) c2 = b.read() self.assertEqual(c1, content1) self.assertEqual(c2, content2)