Beispiel #1
0
  def NewFileObject(self, resolver_context):
    """Creates a new file-like object.

    Args:
      resolver_context (Context): resolver context.

    Returns:
      FileIO: file-like object.
    """
    return vshadow_file_io.VShadowFile(resolver_context)
Beispiel #2
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 vshadow_file_io.VShadowFile(resolver_context, path_spec)
Beispiel #3
0
    def testOpenClose(self):
        """Test the open and close functionality."""
        path_spec = vshadow_path_spec.VShadowPathSpec(
            parent=self._qcow_path_spec, store_index=1)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        file_object.Open()
        self.assertEqual(file_object.get_size(), 0x40000000)

        path_spec = vshadow_path_spec.VShadowPathSpec(
            parent=self._qcow_path_spec, store_index=13)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        with self.assertRaises(errors.PathSpecError):
            file_object.Open()

        path_spec = vshadow_path_spec.VShadowPathSpec(
            location='/vss1', parent=self._qcow_path_spec)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        file_object.Open()
        self.assertEqual(file_object.get_size(), 0x40000000)

        path_spec = vshadow_path_spec.VShadowPathSpec(
            location='/vss0', parent=self._qcow_path_spec)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        with self.assertRaises(errors.PathSpecError):
            file_object.Open()

        path_spec = vshadow_path_spec.VShadowPathSpec(
            location='/vss13', parent=self._qcow_path_spec)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        with self.assertRaises(errors.PathSpecError):
            file_object.Open()
Beispiel #4
0
    def testRead(self):
        """Test the read functionality."""
        path_spec = vshadow_path_spec.VShadowPathSpec(
            parent=self._qcow_path_spec, store_index=1)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context,
                                                  path_spec)

        file_object.Open()
        self.assertEqual(file_object.get_size(), 0x40000000)

        file_object.seek(0x18e)

        expected_data = b'A disk read error occurred\x00\r\nBOOTMGR is missing'
        self.assertEqual(file_object.read(47), expected_data)
Beispiel #5
0
    def testSeek(self):
        """Test the seek functionality."""
        path_spec = vshadow_path_spec.VShadowPathSpec(
            parent=self._qcow_path_spec, store_index=1)
        file_object = vshadow_file_io.VShadowFile(self._resolver_context)

        file_object.open(path_spec=path_spec)
        self.assertEqual(file_object.get_size(), 0x40000000)

        file_object.seek(0x1e0)
        self.assertEqual(file_object.get_offset(), 0x1e0)
        self.assertEqual(file_object.read(16), b'rl+Alt+Del to re')
        self.assertEqual(file_object.get_offset(), 0x1f0)

        file_object.seek(-40, os.SEEK_END)
        self.assertEqual(file_object.get_offset(), 0x3fffffd8)
        self.assertEqual(file_object.read(8), b'Press Ct')
        self.assertEqual(file_object.get_offset(), 0x3fffffe0)

        file_object.seek(3, os.SEEK_CUR)
        self.assertEqual(file_object.get_offset(), 0x3fffffe3)
        self.assertEqual(file_object.read(7), b'Alt+Del')
        self.assertEqual(file_object.get_offset(), 0x3fffffea)

        # Conforming to the POSIX seek the offset can exceed the file size
        # but reading will result in no data being returned.
        expected_offset = 0x40000000 + 100
        file_object.seek(expected_offset, os.SEEK_SET)
        self.assertEqual(file_object.get_offset(), expected_offset)
        self.assertEqual(file_object.read(20), 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(), expected_offset)

        with self.assertRaises(IOError):
            file_object.seek(10, 5)

        # On error the offset should not change.
        self.assertEqual(file_object.get_offset(), expected_offset)

        file_object.close()