Пример #1
0
    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 ntfs_file_io.NTFSFile(resolver_context, path_spec)
Пример #2
0
    def testOpenCloseMFTEntry(self):
        """Test the open and close functionality using a MFT entry."""
        path_spec = ntfs_path_spec.NTFSPathSpec(
            mft_attribute=1,
            mft_entry=self._MFT_ENTRY_PASSWORDS_TXT,
            parent=self._qcow_path_spec)
        file_object = ntfs_file_io.NTFSFile(self._resolver_context)

        file_object.open(path_spec=path_spec)
        self.assertEqual(file_object.get_size(), 116)
        file_object.close()
Пример #3
0
    def testOpenCloseLocation(self):
        """Test the open and close functionality using a location."""
        path_spec = ntfs_path_spec.NTFSPathSpec(location='\\password.txt',
                                                parent=self._qcow_path_spec)
        file_object = ntfs_file_io.NTFSFile(self._resolver_context)

        file_object.open(path_spec=path_spec)
        self.assertEqual(file_object.get_size(), 116)
        file_object.close()

        # Try open with a path specification that has no parent.
        path_spec.parent = None

        with self.assertRaises(errors.PathSpecError):
            self._TestOpenCloseLocation(path_spec)
Пример #4
0
    def testSeek(self):
        """Test the seek functionality."""
        path_spec = ntfs_path_spec.NTFSPathSpec(
            location='/a_directory/another_file',
            mft_attribute=2,
            mft_entry=self._MFT_ENTRY_ANOTHER_FILE,
            parent=self._qcow_path_spec)
        file_object = ntfs_file_io.NTFSFile(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()
Пример #5
0
    def testRead(self):
        """Test the read functionality."""
        path_spec = ntfs_path_spec.NTFSPathSpec(
            location='\\passwords.txt',
            mft_attribute=2,
            mft_entry=self._MFT_ENTRY_PASSWORDS_TXT,
            parent=self._qcow_path_spec)
        file_object = ntfs_file_io.NTFSFile(self._resolver_context)

        file_object.open(path_spec=path_spec)
        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)

        # TODO: add boundary scenarios.

        file_object.close()