def test_seek_offset(self): """Tests the seek_offset function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") vsgpt_partition = pyvsgpt.partition() vsgpt_partition.open(test_source) size = vsgpt_partition.get_size() vsgpt_partition.seek_offset(16, os.SEEK_SET) offset = vsgpt_partition.get_offset() self.assertEqual(offset, 16) vsgpt_partition.seek_offset(16, os.SEEK_CUR) offset = vsgpt_partition.get_offset() self.assertEqual(offset, 32) vsgpt_partition.seek_offset(-16, os.SEEK_CUR) offset = vsgpt_partition.get_offset() self.assertEqual(offset, 16) if size > 16: vsgpt_partition.seek_offset(-16, os.SEEK_END) offset = vsgpt_partition.get_offset() self.assertEqual(offset, size - 16) vsgpt_partition.seek_offset(16, os.SEEK_END) offset = vsgpt_partition.get_offset() self.assertEqual(offset, size + 16) # TODO: change IOError into ValueError with self.assertRaises(IOError): vsgpt_partition.seek_offset(-1, os.SEEK_SET) # TODO: change IOError into ValueError with self.assertRaises(IOError): vsgpt_partition.seek_offset(-32 - size, os.SEEK_CUR) # TODO: change IOError into ValueError with self.assertRaises(IOError): vsgpt_partition.seek_offset(-32 - size, os.SEEK_END) # TODO: change IOError into ValueError with self.assertRaises(IOError): vsgpt_partition.seek_offset(0, -1) vsgpt_partition.close() # Test the seek without open. with self.assertRaises(IOError): vsgpt_partition.seek_offset(16, os.SEEK_SET)
def test_get_offset(self): """Tests the get_offset function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") vsgpt_partition = pyvsgpt.partition() vsgpt_partition.open(test_source) offset = vsgpt_partition.get_offset() self.assertIsNotNone(offset) vsgpt_partition.close()
def test_get_size(self): """Tests the get_size function and size property.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") vsgpt_partition = pyvsgpt.partition() vsgpt_partition.open(test_source) size = vsgpt_partition.get_size() self.assertIsNotNone(size) self.assertIsNotNone(vsgpt_partition.size) vsgpt_partition.close()
def test_read_buffer_file_object(self): """Tests the read_buffer function on a file-like object.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") if not os.path.isfile(test_source): raise unittest.SkipTest("source not a regular file") vsgpt_partition = pyvsgpt.partition() with open(test_source, "rb") as file_object: vsgpt_partition.open_file_object(file_object) size = vsgpt_partition.get_size() # Test normal read. data = vsgpt_partition.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), min(size, 4096)) vsgpt_partition.close()
def test_read_buffer(self): """Tests the read_buffer function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") vsgpt_partition = pyvsgpt.partition() vsgpt_partition.open(test_source) size = vsgpt_partition.get_size() if size < 4096: # Test read without maximum size. vsgpt_partition.seek_offset(0, os.SEEK_SET) data = vsgpt_partition.read_buffer() self.assertIsNotNone(data) self.assertEqual(len(data), size) # Test read with maximum size. vsgpt_partition.seek_offset(0, os.SEEK_SET) data = vsgpt_partition.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), min(size, 4096)) if size > 8: vsgpt_partition.seek_offset(-8, os.SEEK_END) # Read buffer on size boundary. data = vsgpt_partition.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), 8) # Read buffer beyond size boundary. data = vsgpt_partition.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), 0) # Stress test read buffer. vsgpt_partition.seek_offset(0, os.SEEK_SET) remaining_size = size for _ in range(1024): read_size = int(random.random() * 4096) data = vsgpt_partition.read_buffer(size=read_size) self.assertIsNotNone(data) data_size = len(data) if read_size > remaining_size: read_size = remaining_size self.assertEqual(data_size, read_size) remaining_size -= data_size if not remaining_size: vsgpt_partition.seek_offset(0, os.SEEK_SET) remaining_size = size with self.assertRaises(ValueError): vsgpt_partition.read_buffer(size=-1) vsgpt_partition.close() # Test the read without open. with self.assertRaises(IOError): vsgpt_partition.read_buffer(size=4096)
def test_read_buffer_at_offset(self): """Tests the read_buffer_at_offset function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") vsgpt_partition = pyvsgpt.partition() vsgpt_partition.open(test_source) size = vsgpt_partition.get_size() # Test normal read. data = vsgpt_partition.read_buffer_at_offset(4096, 0) self.assertIsNotNone(data) self.assertEqual(len(data), min(size, 4096)) if size > 8: # Read buffer on size boundary. data = vsgpt_partition.read_buffer_at_offset(4096, size - 8) self.assertIsNotNone(data) self.assertEqual(len(data), 8) # Read buffer beyond size boundary. data = vsgpt_partition.read_buffer_at_offset(4096, size + 8) self.assertIsNotNone(data) self.assertEqual(len(data), 0) # Stress test read buffer. for _ in range(1024): random_number = random.random() media_offset = int(random_number * size) read_size = int(random_number * 4096) data = vsgpt_partition.read_buffer_at_offset(read_size, media_offset) self.assertIsNotNone(data) remaining_size = size - media_offset data_size = len(data) if read_size > remaining_size: read_size = remaining_size self.assertEqual(data_size, read_size) remaining_size -= data_size if not remaining_size: vsgpt_partition.seek_offset(0, os.SEEK_SET) with self.assertRaises(ValueError): vsgpt_partition.read_buffer_at_offset(-1, 0) with self.assertRaises(ValueError): vsgpt_partition.read_buffer_at_offset(4096, -1) vsgpt_partition.close() # Test the read without open. with self.assertRaises(IOError): vsgpt_partition.read_buffer_at_offset(4096, 0)