def _OpenFileObject(self, path_spec): """Opens the file-like object defined by path specification. Args: path_spec (PathSpec): path specification. Returns: pyqcow.file: a file-like object. Raises: PathSpecError: if the path specification is incorrect. """ if not path_spec.HasParent(): raise errors.PathSpecError( 'Unsupported path specification without parent.') file_object = resolver.Resolver.OpenFileObject( path_spec.parent, resolver_context=self._resolver_context) qcow_file = pyqcow.file() qcow_file.open_file_object(file_object) if qcow_file.backing_filename: # pylint: disable=using-constant-test file_system = resolver.Resolver.OpenFileSystem( path_spec.parent, resolver_context=self._resolver_context) self._OpenParentFile(file_system, path_spec.parent, qcow_file) return qcow_file
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return qcow_file = pyqcow.file() # Test open and close. qcow_file.open(unittest.source) qcow_file.close() # Test open and close a second time to validate clean up on close. qcow_file.open(unittest.source) qcow_file.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. qcow_file.open_file_object(file_object) qcow_file.close() # Test open_file_object and close a second time to validate clean up on close. qcow_file.open_file_object(file_object) qcow_file.close() # Test open_file_object and close and dereferencing file_object. qcow_file.open_file_object(file_object) del file_object qcow_file.close()
def test_open_close(self): """Tests the open and close functions.""" test_source = unittest.source if not test_source: return qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) # Test open and close. qcow_file.open(test_source) qcow_file.close() # Test open and close a second time to validate clean up on close. qcow_file.open(test_source) qcow_file.close() if os.path.isfile(test_source): with open(test_source, "rb") as file_object: # Test open_file_object and close. qcow_file.open_file_object(file_object) qcow_file.close() # Test open_file_object and close a second time to validate clean up on close. qcow_file.open_file_object(file_object) qcow_file.close() # Test open_file_object and close and dereferencing file_object. qcow_file.open_file_object(file_object) del file_object qcow_file.close()
def pyqcow_test_single_open_close_file_object_with_dereference( filename, mode, password=None): print(("Testing single open close of file-like object with dereference " "of: {0:s} with access: {1:s}\t").format( filename, get_mode_string(mode))) result = True try: file_object = open(filename, "rb") qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open_file_object(file_object, mode) del file_object qcow_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def pyqcow_test_multi_open_close_file(filename, mode, password=None): print("Testing multi open close of: {0:s} with access: {1:s}\t".format( filename, get_mode_string(mode))) result = True try: qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open(filename, mode) qcow_file.close() qcow_file.open(filename, mode) qcow_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open_file_object(self): """Tests the open_file_object function.""" 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") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) with open(test_source, "rb") as file_object: qcow_file.open_file_object(file_object) with self.assertRaises(IOError): qcow_file.open_file_object(file_object) qcow_file.close() with self.assertRaises(TypeError): qcow_file.open_file_object(None) with self.assertRaises(ValueError): qcow_file.open_file_object(file_object, mode="w")
def test_read_buffer_at_offset(self): """Tests the read_buffer_at_offset function.""" if not unittest.source: return qcow_file = pyqcow.file() qcow_file.open(unittest.source) file_size = qcow_file.get_media_size() # Test normal read. data = qcow_file.read_buffer_at_offset(4096, 0) self.assertIsNotNone(data) self.assertEqual(len(data), min(file_size, 4096)) # Test read beyond file size. if file_size > 16: data = qcow_file.read_buffer_at_offset(4096, file_size - 16) self.assertIsNotNone(data) self.assertEqual(len(data), 16) with self.assertRaises(ValueError): qcow_file.read_buffer_at_offset(-1, 0) with self.assertRaises(ValueError): qcow_file.read_buffer_at_offset(4096, -1) qcow_file.close() # Test the read without open. with self.assertRaises(IOError): qcow_file.read_buffer_at_offset(4096, 0)
def read_qcow2_file_without_backing_file(file, offset, length): assert os.path.exists(file) qcow_file = pyqcow.file() qcow_file.open(file) qcow_file.seek(offset) data = qcow_file.read(length) qcow_file.close() return data
def pyqcow_test_read_file(filename): qcow_file = pyqcow.file() qcow_file.open(filename, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def test_close(self): """Tests the close function.""" if not unittest.source: return qcow_file = pyqcow.file() with self.assertRaises(IOError): qcow_file.close()
def pyqcow_test_read_file(filename): """Tests the read function with a file.""" qcow_file = pyqcow.file() qcow_file.open(filename, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def test_seek_offset(self): """Tests the seek_offset function.""" if not unittest.source: return qcow_file = pyqcow.file() qcow_file.open(unittest.source) file_size = qcow_file.get_media_size() qcow_file.seek_offset(16, os.SEEK_SET) offset = qcow_file.get_offset() self.assertEqual(offset, 16) qcow_file.seek_offset(16, os.SEEK_CUR) offset = qcow_file.get_offset() self.assertEqual(offset, 32) qcow_file.seek_offset(-16, os.SEEK_CUR) offset = qcow_file.get_offset() self.assertEqual(offset, 16) qcow_file.seek_offset(-16, os.SEEK_END) offset = qcow_file.get_offset() self.assertEqual(offset, file_size - 16) qcow_file.seek_offset(16, os.SEEK_END) offset = qcow_file.get_offset() self.assertEqual(offset, file_size + 16) # TODO: change IOError into ValueError with self.assertRaises(IOError): qcow_file.seek_offset(-1, os.SEEK_SET) # TODO: change IOError into ValueError with self.assertRaises(IOError): qcow_file.seek_offset(-32 - file_size, os.SEEK_CUR) # TODO: change IOError into ValueError with self.assertRaises(IOError): qcow_file.seek_offset(-32 - file_size, os.SEEK_END) # TODO: change IOError into ValueError with self.assertRaises(IOError): qcow_file.seek_offset(0, -1) qcow_file.close() # Test the seek without open. with self.assertRaises(IOError): qcow_file.seek_offset(16, os.SEEK_SET)
def pyqcow_test_read_file_object(filename): file_object = open(filename, "rb") qcow_file = pyqcow.file() qcow_file.open_file_object(file_object, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def pyqcow_test_read_file_object(filename): """Tests the read function with a file-like object.""" file_object = open(filename, "rb") qcow_file = pyqcow.file() qcow_file.open_file_object(file_object, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def pyqcow_test_seek_file(filename, password=None): qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open(filename, "r") result = pyqcow_test_seek(qcow_file) qcow_file.close() return result
def pyqcow_test_read_file(filename, password=None): qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open(filename, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def test_close(self): """Tests the close function.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) with self.assertRaises(IOError): qcow_file.close()
def pyqcow_test_read_file_object(filename, password=None): file_object = open(filename, "rb") qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open_file_object(file_object, "r") result = pyqcow_test_read(qcow_file) qcow_file.close() return result
def pyqcow_test_seek_file_object(filename, password=None): file_object = open(filename, "rb") qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open_file_object(file_object, "r") result = pyqcow_test_seek(qcow_file) qcow_file.close() return result
def test_is_locked(self): """Tests the is_locked function.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() qcow_file.open(unittest.source) _ = qcow_file.is_locked() qcow_file.close()
def _OpenParentFile(self, file_system, path_spec, qcow_file): """Opens the parent file. Args: file_system (FileSystem): file system of the QCOW file. path_spec (PathSpec): path specification of the QCOW file. qcow_file (pyqcow.file): QCOW file. Raises: PathSpecError: if the path specification is incorrect. """ location = getattr(path_spec, 'location', None) if not location: raise errors.PathSpecError( 'Unsupported path specification without location.') location_path_segments = file_system.SplitPath(location) location_path_segments.pop() location_path_segments.append(qcow_file.backing_filename) parent_file_location = file_system.JoinPath(location_path_segments) # Note that we don't want to set the keyword arguments when not used # because the path specification base class will check for unused # keyword arguments and raise. kwargs = path_spec_factory.Factory.GetProperties(path_spec) kwargs['location'] = parent_file_location if path_spec.parent is not None: kwargs['parent'] = path_spec.parent parent_file_path_spec = path_spec_factory.Factory.NewPathSpec( path_spec.type_indicator, **kwargs) if not file_system.FileEntryExistsByPathSpec(parent_file_path_spec): return file_object = resolver.Resolver.OpenFileObject( parent_file_path_spec, resolver_context=self._resolver_context) qcow_parent_file = pyqcow.file() qcow_parent_file.open_file_object(file_object) if qcow_parent_file.backing_filename: # pylint: disable=using-constant-test self._OpenParentFile( file_system, parent_file_path_spec, qcow_parent_file) qcow_file.set_parent(qcow_parent_file) self._parent_qcow_files.append(qcow_parent_file) self._sub_file_objects.append(file_object)
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") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) with open(test_source, "rb") as file_object: qcow_file.open_file_object(file_object) qcow_parent_file = None if qcow_file.backing_filename: qcow_parent_file = pyqcow.file() parent_filename = os.path.join(os.path.dirname(test_source), qcow_file.backing_filename) qcow_parent_file.open(parent_filename, "r") qcow_file.set_parent(qcow_parent_file) media_size = qcow_file.get_media_size() # Test normal read. data = qcow_file.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), min(media_size, 4096)) qcow_file.close() if qcow_parent_file: qcow_parent_file.close()
def test_get_backing_filename(self): """Tests the get_backing_filename function and backing_filename property.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() qcow_file.open(unittest.source) _ = qcow_file.get_backing_filename() _ = qcow_file.backing_filename qcow_file.close()
def pyqcow_test_single_open_close_file(filename, mode, password=None): if not filename: filename_string = "None" else: filename_string = filename print("Testing single open close of: {0:s} with access: {1:s}\t".format( filename_string, get_mode_string(mode))) result = True try: qcow_file = pyqcow.file() if password: qcow_file.set_password(password) qcow_file.open(filename, mode) qcow_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pyqcow_file_open") if not filename and str(exception) == expected_message: pass else: print(str(exception)) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pyqcow_file_open") if mode != "w" or str(exception) != expected_message: print(str(exception)) result = False except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_get_offset(self): """Tests the get_offset function.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) qcow_file.open(unittest.source) offset = qcow_file.get_offset() self.assertIsNotNone(offset) qcow_file.close()
def test_get_media_size(self): """Tests the get_media_size function and media_size property.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) qcow_file.open(unittest.source) media_size = qcow_file.get_media_size() self.assertIsNotNone(media_size) self.assertIsNotNone(qcow_file.media_size) qcow_file.close()
def test_open(self): """Tests the open function.""" if not unittest.source: return qcow_file = pyqcow.file() qcow_file.open(unittest.source) with self.assertRaises(IOError): qcow_file.open(unittest.source) qcow_file.close() with self.assertRaises(TypeError): qcow_file.open(None) with self.assertRaises(ValueError): qcow_file.open(unittest.source, mode="w")
def _OpenFileObject(self, path_spec): """Opens the file-like object defined by path specification. Args: path_spec: the path specification (instance of PathSpec). Returns: A file-like object. Raises: PathSpecError: if the path specification is incorrect. """ if not path_spec.HasParent(): raise errors.PathSpecError(u"Unsupported path specification without parent.") file_object = resolver.Resolver.OpenFileObject(path_spec.parent, resolver_context=self._resolver_context) qcow_file = pyqcow.file() qcow_file.open_file_object(file_object) return qcow_file
def test_read_buffer_file_object(self): """Tests the read_buffer function on a file-like object.""" if not unittest.source: return file_object = open(unittest.source, "rb") qcow_file = pyqcow.file() qcow_file.open_file_object(file_object) file_size = qcow_file.get_media_size() # Test normal read. data = qcow_file.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), min(file_size, 4096)) qcow_file.close()
def pyqcow_test_seek_file_no_open(filename, password=None): print("Testing seek of offset without open:\n") qcow_file = pyqcow.file() if password: qcow_file.set_password(password) result = False try: qcow_file.seek(0, os.SEEK_SET) except Exception as exception: print(str(exception)) result = True if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open(self): """Tests the open function.""" if not unittest.source: raise unittest.SkipTest("missing source") qcow_file = pyqcow.file() if unittest.password: qcow_file.set_password(unittest.password) qcow_file.open(unittest.source) with self.assertRaises(IOError): qcow_file.open(unittest.source) qcow_file.close() with self.assertRaises(TypeError): qcow_file.open(None) with self.assertRaises(ValueError): qcow_file.open(unittest.source, mode="w")
def pyqcow_test_read_file_no_open(filename): print("Testing read of offset without open:\t", end="") qcow_file = pyqcow.file() error_string = "" result = False try: qcow_file.read(size=4096) except Exception as exception: error_string = str(exception) result = True if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def _OpenFileObject(self, path_spec): """Opens the file-like object defined by path specification. Args: path_spec: the path specification (instance of path.PathSpec). Returns: A file-like object. Raises: PathSpecError: if the path specification is incorrect. """ if not path_spec.HasParent(): raise errors.PathSpecError( u'Unsupported path specification without parent.') file_object = resolver.Resolver.OpenFileObject( path_spec.parent, resolver_context=self._resolver_context) qcow_file = pyqcow.file() qcow_file.open_file_object(file_object) return qcow_file
def pyqcow_test_read_file_no_open(filename): """Tests the read function with a file without open.""" description = "Testing read of without open:\t" print(description, end="") qcow_file = pyqcow.file() error_string = None result = False try: qcow_file.read(size=4096) except Exception as exception: error_string = str(exception) result = True if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") qcow_file = pyqcow.file() qcow_file.open_file_object(file_object) # TODO: change MemoryError into IOError with self.assertRaises(MemoryError): qcow_file.open_file_object(file_object) qcow_file.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): qcow_file.open_file_object(None) with self.assertRaises(ValueError): qcow_file.open_file_object(file_object, mode="w")
def pyqcow_test_read_file_no_open(filename, password=None): print("Testing read of offset without open:\t", end="") qcow_file = pyqcow.file() if password: qcow_file.set_password(password) error_string = "" result = False try: qcow_file.read(size=4096) except Exception as exception: error_string = str(exception) result = True if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def __init__(self, filename): self._qcow_file = pyqcow.file() self._qcow_file.open(filename) super(QcowImgInfo, self).__init__( url='', type=pytsk3.TSK_IMG_TYPE_EXTERNAL)