def testOpenCloseLocation(self): """Test the open and close functionality using a location.""" path_spec = path_spec_factory.Factory.NewPathSpec( definitions.TYPE_INDICATOR_APFS, location='/passwords.txt', parent=self._apfs_container_path_spec) file_object = apfs_file_io.APFSFile(self._resolver_context, path_spec) file_object.Open() self.assertEqual(file_object.get_size(), 116) # Try open with a path specification that has no parent. path_spec.parent = None file_object = apfs_file_io.APFSFile(self._resolver_context, path_spec) with self.assertRaises(errors.PathSpecError): file_object.Open()
def NewFileObject(self, resolver_context): """Creates a new file-like object. Args: resolver_context (Context): resolver context. Returns: FileIO: file-like object. """ return apfs_file_io.APFSFile(resolver_context)
def testOpenCloseIdentifier(self): """Test the open and close functionality using an identifier.""" path_spec = path_spec_factory.Factory.NewPathSpec( definitions.TYPE_INDICATOR_APFS, identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._apfs_container_path_spec) file_object = apfs_file_io.APFSFile(self._resolver_context, path_spec) file_object.Open() self.assertEqual(file_object.get_size(), 116)
def NewFileObject(self, resolver_context, path_spec): """Creates a new file input/output (IO) object. Args: resolver_context (Context): resolver context. path_spec (PathSpec): a path specification. Returns: FileIO: file input/output (IO) object. """ return apfs_file_io.APFSFile(resolver_context, path_spec)
def testRead(self): """Test the read functionality.""" path_spec = path_spec_factory.Factory.NewPathSpec( definitions.TYPE_INDICATOR_APFS, location='/passwords.txt', identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._apfs_container_path_spec) file_object = apfs_file_io.APFSFile(self._resolver_context, path_spec) file_object.Open() read_buffer = file_object.read() expected_buffer = ( b'place,user,password\n' b'bank,joesmith,superrich\n' b'alarm system,-,1234\n' b'treasure chest,-,1111\n' b'uber secret laire,admin,admin\n') self.assertEqual(read_buffer, expected_buffer)
def testSeek(self): """Test the seek functionality.""" path_spec = path_spec_factory.Factory.NewPathSpec( definitions.TYPE_INDICATOR_APFS, location='/a_directory/another_file', identifier=self._IDENTIFIER_ANOTHER_FILE, parent=self._apfs_container_path_spec) file_object = apfs_file_io.APFSFile(self._resolver_context) file_object.open(path_spec=path_spec) self.assertEqual(file_object.get_size(), 22) file_object.seek(10) self.assertEqual(file_object.read(5), b'other') self.assertEqual(file_object.get_offset(), 15) file_object.seek(-10, os.SEEK_END) self.assertEqual(file_object.read(5), b'her f') file_object.seek(2, os.SEEK_CUR) self.assertEqual(file_object.read(2), b'e.') # Conforming to the POSIX seek the offset can exceed the file size # but reading will result in no data being returned. file_object.seek(300, os.SEEK_SET) self.assertEqual(file_object.get_offset(), 300) self.assertEqual(file_object.read(2), b'') with self.assertRaises(IOError): file_object.seek(-10, os.SEEK_SET) # On error the offset should not change. self.assertEqual(file_object.get_offset(), 300) with self.assertRaises(IOError): file_object.seek(10, 5) # On error the offset should not change. self.assertEqual(file_object.get_offset(), 300) file_object.close()