def _OpenFileObject(self, path_spec): """Opens the file-like object defined by path specification. Args: path_spec (PathSpec): path specification. Returns: pyvde.volume: LUKSDE volume file-like object. Raises: PathSpecError: if the path specification is incorrect. """ if not path_spec.HasParent(): raise errors.PathSpecError( 'Unsupported path specification without parent.') resolver.Resolver.key_chain.ExtractCredentialsFromPathSpec(path_spec) file_object = resolver.Resolver.OpenFileObject( path_spec.parent, resolver_context=self._resolver_context) luksde_volume = pyluksde.volume() luksde.LUKSDEVolumeOpen(luksde_volume, path_spec, file_object, resolver.Resolver.key_chain) return luksde_volume
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return luksde_volume = pyluksde.volume() # Test open and close. luksde_volume.open(unittest.source) luksde_volume.close() # Test open and close a second time to validate clean up on close. luksde_volume.open(unittest.source) luksde_volume.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. luksde_volume.open_file_object(file_object) luksde_volume.close() # Test open_file_object and close a second time to validate clean up on close. luksde_volume.open_file_object(file_object) luksde_volume.close() # Test open_file_object and close and dereferencing file_object. luksde_volume.open_file_object(file_object) del file_object luksde_volume.close()
def test_read_buffer_at_offset(self): """Tests the read_buffer_at_offset function.""" if not unittest.source: return luksde_volume = pyluksde.volume() luksde_volume.open(unittest.source) file_size = luksde_volume.get_size() # Test normal read. data = luksde_volume.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 = luksde_volume.read_buffer_at_offset(4096, file_size - 16) self.assertIsNotNone(data) self.assertEqual(len(data), 16) with self.assertRaises(ValueError): luksde_volume.read_buffer_at_offset(-1, 0) with self.assertRaises(ValueError): luksde_volume.read_buffer_at_offset(4096, -1) luksde_volume.close() # Test the read without open. with self.assertRaises(IOError): luksde_volume.read_buffer_at_offset(4096, 0)
def pyluksde_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: luksde_volume = pyluksde.volume() if password: luksde_volume.set_password(password) luksde_volume.open(filename, mode) luksde_volume.close() luksde_volume.open(filename, mode) luksde_volume.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def pyluksde_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") luksde_volume = pyluksde.volume() if password: luksde_volume.set_password(password) luksde_volume.open_file_object(file_object, mode) del file_object luksde_volume.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def _Open(self, mode='rb'): """Opens the file system defined by path specification. Args: mode (Optional[str]): file access mode. The default is 'rb' read-only binary. Raises: AccessError: if the access to open the file was denied. IOError: if the file system could not be opened. PathSpecError: if the path specification is incorrect. ValueError: if the path specification is invalid. """ if not self._path_spec.HasParent(): raise errors.PathSpecError( 'Unsupported path specification without parent.') resolver.Resolver.key_chain.ExtractCredentialsFromPathSpec( self._path_spec) luksde_volume = pyluksde.volume() file_object = resolver.Resolver.OpenFileObject( self._path_spec.parent, resolver_context=self._resolver_context) luksde.LUKSDEVolumeOpen(luksde_volume, self._path_spec, file_object, resolver.Resolver.key_chain) self._luksde_volume = luksde_volume self._file_object = file_object
def test_close(self): """Tests the close function.""" if not unittest.source: return luksde_volume = pyluksde.volume() with self.assertRaises(IOError): luksde_volume.close()
def test_seek_offset(self): """Tests the seek_offset function.""" if not unittest.source: return luksde_volume = pyluksde.volume() luksde_volume.open(unittest.source) file_size = luksde_volume.get_size() luksde_volume.seek_offset(16, os.SEEK_SET) offset = luksde_volume.get_offset() self.assertEqual(offset, 16) luksde_volume.seek_offset(16, os.SEEK_CUR) offset = luksde_volume.get_offset() self.assertEqual(offset, 32) luksde_volume.seek_offset(-16, os.SEEK_CUR) offset = luksde_volume.get_offset() self.assertEqual(offset, 16) luksde_volume.seek_offset(-16, os.SEEK_END) offset = luksde_volume.get_offset() self.assertEqual(offset, file_size - 16) luksde_volume.seek_offset(16, os.SEEK_END) offset = luksde_volume.get_offset() self.assertEqual(offset, file_size + 16) # TODO: change IOError into ValueError with self.assertRaises(IOError): luksde_volume.seek_offset(-1, os.SEEK_SET) # TODO: change IOError into ValueError with self.assertRaises(IOError): luksde_volume.seek_offset(-32 - file_size, os.SEEK_CUR) # TODO: change IOError into ValueError with self.assertRaises(IOError): luksde_volume.seek_offset(-32 - file_size, os.SEEK_END) # TODO: change IOError into ValueError with self.assertRaises(IOError): luksde_volume.seek_offset(0, -1) luksde_volume.close() # Test the seek without open. with self.assertRaises(IOError): luksde_volume.seek_offset(16, os.SEEK_SET)
def pyluksde_test_seek_file(filename, password=None): luksde_volume = pyluksde.volume() if password: luksde_volume.set_password(password) luksde_volume.open(filename, "r") result = pyluksde_test_seek(luksde_volume) luksde_volume.close() return result
def pyluksde_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: luksde_volume = pyluksde.volume() if password: luksde_volume.set_password(password) luksde_volume.open(filename, mode) luksde_volume.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pyluksde_volume_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( "pyluksde_volume_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_open(self): """Tests the open function.""" if not unittest.source: return luksde_volume = pyluksde.volume() luksde_volume.open(unittest.source) with self.assertRaises(IOError): luksde_volume.open(unittest.source) luksde_volume.close() with self.assertRaises(TypeError): luksde_volume.open(None) with self.assertRaises(ValueError): luksde_volume.open(unittest.source, mode="w")
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") luksde_volume = pyluksde.volume() luksde_volume.open_file_object(file_object) file_size = luksde_volume.get_size() # Test normal read. data = luksde_volume.read_buffer(size=4096) self.assertIsNotNone(data) self.assertEqual(len(data), min(file_size, 4096)) luksde_volume.close()
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") luksde_volume = pyluksde.volume() luksde_volume.open_file_object(file_object) with self.assertRaises(IOError): luksde_volume.open_file_object(file_object) luksde_volume.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): luksde_volume.open_file_object(None) with self.assertRaises(ValueError): luksde_volume.open_file_object(file_object, mode="w")
def pyluksde_test_seek_file_no_open(filename, password=None): print("Testing seek of offset without open:\t", end="") luksde_volume = pyluksde.volume() if password: luksde_volume.set_password(password) error_string = "" result = False try: luksde_volume.seek(0, os.SEEK_SET) 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_signal_abort(self): """Tests the signal_abort function.""" luksde_volume = pyluksde.volume() luksde_volume.signal_abort()