Exemplo n.º 1
0
    def test_is_locked(self):
        """Tests the is_locked function."""
        if not unittest.source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()

        bde_volume.open(unittest.source)

        result = bde_volume.is_locked()
        self.assertTrue(result)

        bde_volume.close()

        if unittest.password or unittest.recovery_password:
            bde_volume = pybde.volume()
            if unittest.password:
                bde_volume.set_password(unittest.password)
            if unittest.recovery_password:
                bde_volume.set_recovery_password(unittest.recovery_password)

            bde_volume.open(unittest.source)

            result = bde_volume.is_locked()
            self.assertFalse(result)

            bde_volume.close()
Exemplo n.º 2
0
    def test_get_number_of_key_protectors(self):
        """Tests the get_number_of_key_protectors function and number_of_key_protectors property."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        with DataRangeFileObject(test_source, unittest.offset or 0,
                                 None) as file_object:

            bde_volume = pybde.volume()
            bde_volume.open_file_object(file_object)

            number_of_key_protectors = bde_volume.get_number_of_key_protectors(
            )
            self.assertIsNotNone(number_of_key_protectors)

            self.assertIsNotNone(bde_volume.number_of_key_protectors)

            bde_volume.close()
Exemplo n.º 3
0
    def test_get_description(self):
        """Tests the get_description function and description property."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        with DataRangeFileObject(test_source, unittest.offset or 0,
                                 None) as file_object:

            bde_volume = pybde.volume()
            bde_volume.open_file_object(file_object)

            description = bde_volume.get_description()
            self.assertIsNotNone(description)

            self.assertIsNotNone(bde_volume.description)

            bde_volume.close()
Exemplo n.º 4
0
    def test_is_locked(self):
        """Tests the is_locked function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()

        bde_volume.open(test_source)

        result = bde_volume.is_locked()
        self.assertTrue(result)

        bde_volume.close()

        if unittest.password or unittest.recovery_password:
            bde_volume = pybde.volume()
            if unittest.password:
                bde_volume.set_password(unittest.password)
            if unittest.recovery_password:
                bde_volume.set_recovery_password(unittest.recovery_password)
            if unittest.startup_key:
                bde_volume.read_startup_key(unittest.startup_key)

            bde_volume.open(test_source)

            result = bde_volume.is_locked()
            self.assertFalse(result)

            bde_volume.close()
Exemplo n.º 5
0
  def test_open(self):
    """Tests the open function."""
    if not unittest.source:
      return

    bde_volume = pybde.volume()

    if unittest.password:
      bde_volume.set_password(unittest.password)
    if unittest.recovery_password:
      bde_volume.set_recovery_password(
          unittest.recovery_password)

    bde_volume.open(unittest.source)

    with self.assertRaises(IOError):
      bde_volume.open(unittest.source)

    bde_volume.close()

    with self.assertRaises(TypeError):
      bde_volume.open(None)

    with self.assertRaises(ValueError):
      bde_volume.open(unittest.source, mode="w")
Exemplo n.º 6
0
    def _Open(self, path_spec, mode='rb'):
        """Opens the file system defined by path specification.

    Args:
      path_spec (PathSpec): path specification.
      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 path_spec.HasParent():
            raise errors.PathSpecError(
                u'Unsupported path specification without parent.')

        bde_volume = pybde.volume()
        file_object = resolver.Resolver.OpenFileObject(
            path_spec.parent, resolver_context=self._resolver_context)

        try:
            bde.BDEVolumeOpen(bde_volume, path_spec, file_object,
                              resolver.Resolver.key_chain)
        except:
            file_object.close()
            raise

        self._bde_volume = bde_volume
        self._file_object = file_object
Exemplo n.º 7
0
    def _Open(self, path_spec=None, mode='rb'):
        """Opens the file system object defined by path specification.

    Args:
      path_spec: optional path specification (instance of path.PathSpec).
                 The default is None.
      mode: optional 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 object could not be opened.
      PathSpecError: if the path specification is incorrect.
      ValueError: if the path specification is invalid.
    """
        if not path_spec.HasParent():
            raise errors.PathSpecError(
                u'Unsupported path specification without parent.')

        bde_volume = pybde.volume()
        file_object = resolver.Resolver.OpenFileObject(
            path_spec.parent, resolver_context=self._resolver_context)

        bde.BdeVolumeOpen(bde_volume, path_spec, file_object,
                          resolver.Resolver.key_chain)

        self._bde_volume = bde_volume
        self._file_object = file_object
Exemplo n.º 8
0
def pybde_test_multi_open_close_file(filename, mode, password=None, recovery_password=None):
    print("Testing multi open close of: {0:s} with access: {1:s}\t".format(filename, get_mode_string(mode)))

    result = True
    try:
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open(filename, mode)
        bde_volume.close()
        bde_volume.open(filename, mode)
        bde_volume.close()

    except Exception as exception:
        print(str(exception))
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 9
0
    def test_open(self):
        """Tests the open function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        bde_volume.open(test_source)

        with self.assertRaises(IOError):
            bde_volume.open(test_source)

        bde_volume.close()

        with self.assertRaises(TypeError):
            bde_volume.open(None)

        with self.assertRaises(ValueError):
            bde_volume.open(test_source, mode="w")
Exemplo n.º 10
0
  def _Open(self, path_spec, mode='rb'):
    """Opens the file system object defined by path specification.

    Args:
      path_spec: a path specification (instance of path.PathSpec).
      mode: optional 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 object could not be opened.
      PathSpecError: if the path specification is incorrect.
      ValueError: if the path specification is invalid.
    """
    if not path_spec.HasParent():
      raise errors.PathSpecError(
          u'Unsupported path specification without parent.')

    bde_volume = pybde.volume()
    file_object = resolver.Resolver.OpenFileObject(
        path_spec.parent, resolver_context=self._resolver_context)

    try:
      bde.BDEVolumeOpen(
          bde_volume, path_spec, file_object, resolver.Resolver.key_chain)
    except:
      file_object.close()
      raise

    self._bde_volume = bde_volume
    self._file_object = file_object
Exemplo n.º 11
0
    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")

        bde_volume = pybde.volume()

        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        bde_volume.open_file_object(file_object)

        file_size = bde_volume.get_size()

        # Test normal read.
        data = bde_volume.read_buffer(size=4096)

        self.assertIsNotNone(data)
        self.assertEqual(len(data), min(file_size, 4096))

        bde_volume.close()
Exemplo n.º 12
0
    def test_open_file_object(self):
        """Tests the open_file_object function."""
        if not unittest.source:
            return

        file_object = open(unittest.source, "rb")

        bde_volume = pybde.volume()

        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        bde_volume.open_file_object(file_object)

        # TODO: fix
        # with self.assertRaises(IOError):
        #   bde_volume.open_file_object(file_object)

        bde_volume.close()

        # TODO: change IOError into TypeError
        with self.assertRaises(IOError):
            bde_volume.open_file_object(None)

        with self.assertRaises(ValueError):
            bde_volume.open_file_object(file_object, mode="w")
Exemplo n.º 13
0
  def _OpenFileObject(self, path_spec):
    """Opens the file-like object defined by path specification.

    Args:
      path_spec (PathSpec): path specification.

    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.')

    resolver.Resolver.key_chain.ExtractCredentialsFromPathSpec(path_spec)

    file_object = resolver.Resolver.OpenFileObject(
        path_spec.parent, resolver_context=self._resolver_context)
    bde_volume = pybde.volume()

    bde.BDEVolumeOpen(
        bde_volume, path_spec, file_object, resolver.Resolver.key_chain)
    return bde_volume
Exemplo n.º 14
0
    def test_open_close(self):
        """Tests the open and close functions."""
        if not unittest.source:
            return

        bde_volume = pybde.volume()

        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        # Test open and close.
        bde_volume.open(unittest.source)
        bde_volume.close()

        # Test open and close a second time to validate clean up on close.
        bde_volume.open(unittest.source)
        bde_volume.close()

        file_object = open(unittest.source, "rb")

        # Test open_file_object and close.
        bde_volume.open_file_object(file_object)
        bde_volume.close()

        # Test open_file_object and close a second time to validate clean up on close.
        bde_volume.open_file_object(file_object)
        bde_volume.close()

        # Test open_file_object and close and dereferencing file_object.
        bde_volume.open_file_object(file_object)
        del file_object
        bde_volume.close()
Exemplo n.º 15
0
def pybde_test_multi_open_close_file(filename,
                                     mode,
                                     password=None,
                                     recovery_password=None):
    print("Testing multi open close of: {0:s} with access: {1:s}\t".format(
        filename, get_mode_string(mode)))

    result = True
    try:
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open(filename, mode)
        bde_volume.close()
        bde_volume.open(filename, mode)
        bde_volume.close()

    except Exception as exception:
        print(str(exception))
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 16
0
  def _OpenFileObject(self, path_spec):
    """Opens the file-like object defined by path specification.

    Args:
      path_spec (PathSpec): path specification.

    Returns:
      pyvde.volume: BDE 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)
    bde_volume = pybde.volume()

    bde.BDEVolumeOpen(
        bde_volume, path_spec, file_object, resolver.Resolver.key_chain)
    return bde_volume
Exemplo n.º 17
0
    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")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        with DataRangeFileObject(test_source, unittest.offset or 0,
                                 None) as file_object:

            bde_volume.open_file_object(file_object)

            with self.assertRaises(IOError):
                bde_volume.open_file_object(file_object)

            bde_volume.close()

            with self.assertRaises(TypeError):
                bde_volume.open_file_object(None)

            with self.assertRaises(ValueError):
                bde_volume.open_file_object(file_object, mode="w")
Exemplo n.º 18
0
def pybde_test_single_open_close_file_object_with_dereference(
        filename, mode, password=None, recovery_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")
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open_file_object(file_object, mode)
        del file_object
        bde_volume.close()

    except Exception as exception:
        print(str(exception))
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 19
0
    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")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        with DataRangeFileObject(test_source, unittest.offset or 0,
                                 None) as file_object:
            bde_volume.open_file_object(file_object)

            size = bde_volume.get_size()

            # Test normal read.
            data = bde_volume.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), min(size, 4096))

            bde_volume.close()
Exemplo n.º 20
0
def pybde_test_single_open_close_file(
    filename, mode, password=None, recovery_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))

  try:
    bde_volume = pybde.volume()

    if password:
      bde_volume.set_password(password)
    if recovery_password:
      bde_volume.set_recovery_password(recovery_password)

    bde_volume.open(filename, mode)
    bde_volume.close()

  except TypeError, exception:
    if (not filename and
        exception.message == "pybde_volume_open: unsupported string object type."):
      pass

    else:
      print "(FAIL)"
      return False
Exemplo n.º 21
0
def pybde_test_single_open_close_file_object_with_dereference(filename, mode, password=None, recovery_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")
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open_file_object(file_object, mode)
        del file_object
        bde_volume.close()

    except Exception as exception:
        print(str(exception))
        result = False

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 22
0
    def test_get_offset(self):
        """Tests the get_offset function."""
        if not unittest.source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        with DataRangeFileObject(unittest.source, unittest.offset or 0,
                                 None) as file_object:

            bde_volume = pybde.volume()
            bde_volume.open_file_object(file_object)

            offset = bde_volume.get_offset()
            self.assertIsNotNone(offset)

            bde_volume.close()
Exemplo n.º 23
0
def pybde_test_single_open_close_file(filename,
                                      mode,
                                      password=None,
                                      recovery_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:
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open(filename, mode)
        bde_volume.close()

    except TypeError as exception:
        expected_message = ("{0:s}: unsupported string object type."
                            ).format("pybde_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("pybde_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
Exemplo n.º 24
0
def pybde_test_seek_file(filename, password=None, recovery_password=None):
  bde_volume = pybde.volume()

  if password:
    bde_volume.set_password(password)
  if recovery_password:
    bde_volume.set_recovery_password(recovery_password)

  bde_volume.open(filename, "r")
  result = pybde_test_seek(bde_volume)
  bde_volume.close()

  return result
Exemplo n.º 25
0
def pybde_test_seek_file(filename, password=None, recovery_password=None):
    bde_volume = pybde.volume()

    if password:
        bde_volume.set_password(password)
    if recovery_password:
        bde_volume.set_recovery_password(recovery_password)

    bde_volume.open(filename, "r")
    result = pybde_test_seek(bde_volume)
    bde_volume.close()

    return result
Exemplo n.º 26
0
    def test_close(self):
        """Tests the close function."""
        if not unittest.source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        with self.assertRaises(IOError):
            bde_volume.close()
Exemplo n.º 27
0
    def test_get_encryption_method(self):
        """Tests the get_encryption_method function and encryption_method property."""
        if not unittest.source:
            raise unittest.SkipTest("missing source")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)

        with DataRangeFileObject(unittest.source, unittest.offset or 0,
                                 None) as file_object:

            bde_volume = pybde.volume()
            bde_volume.open_file_object(file_object)

            encryption_method = bde_volume.get_encryption_method()
            self.assertIsNotNone(encryption_method)

            self.assertIsNotNone(bde_volume.encryption_method)

            bde_volume.close()
Exemplo n.º 28
0
def pybde_test_single_open_close_file(filename, mode, password=None, recovery_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:
        bde_volume = pybde.volume()

        if password:
            bde_volume.set_password(password)
        if recovery_password:
            bde_volume.set_recovery_password(recovery_password)

        bde_volume.open(filename, mode)
        bde_volume.close()

    except TypeError as exception:
        expected_message = ("{0:s}: unsupported string object type.").format("pybde_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("pybde_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
Exemplo n.º 29
0
  def test_read_buffer(self):
    """Tests the read_buffer function."""
    if not unittest.source:
      return

    bde_volume = pybde.volume()

    if unittest.password:
      bde_volume.set_password(unittest.password)
    if unittest.recovery_password:
      bde_volume.set_recovery_password(
          unittest.recovery_password)

    bde_volume.open(unittest.source)

    file_size = bde_volume.get_size()

    # Test normal read.
    data = bde_volume.read_buffer(size=4096)

    self.assertIsNotNone(data)
    self.assertEqual(len(data), min(file_size, 4096))

    if file_size < 4096:
      data = bde_volume.read_buffer()

      self.assertIsNotNone(data)
      self.assertEqual(len(data), file_size)

    # Test read beyond file size.
    if file_size > 16:
      bde_volume.seek_offset(-16, os.SEEK_END)

      data = bde_volume.read_buffer(size=4096)

      self.assertIsNotNone(data)
      self.assertEqual(len(data), 16)

    with self.assertRaises(ValueError):
      bde_volume.read_buffer(size=-1)

    bde_volume.close()

    # Test the read without open.
    with self.assertRaises(IOError):
      bde_volume.read_buffer(size=4096)
Exemplo n.º 30
0
    def test_open_close(self):
        """Tests the open and close functions."""
        test_source = unittest.source
        if not test_source:
            return

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        # Test open and close.
        bde_volume.open(test_source)
        bde_volume.close()

        # Test open and close a second time to validate clean up on close.
        bde_volume.open(test_source)
        bde_volume.close()

        if os.path.isfile(test_source):
            with open(test_source, "rb") as file_object:

                # Test open_file_object and close.
                bde_volume.open_file_object(file_object)
                bde_volume.close()

                # Test open_file_object and close a second time to validate clean up on close.
                bde_volume.open_file_object(file_object)
                bde_volume.close()

                # Test open_file_object and close and dereferencing file_object.
                bde_volume.open_file_object(file_object)
                del file_object
                bde_volume.close()
Exemplo n.º 31
0
    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)
        bde_volume = pybde.volume()
        bde.BdeVolumeOpen(bde_volume, path_spec, file_object,
                          resolver.Resolver.key_chain)
        return bde_volume
Exemplo n.º 32
0
def pybde_test_single_open_close_file_object(
    filename, mode, password=None, recovery_password=None):
  print ("Testing single open close of file-like object of: {0:s} with access: "
         "{1:s}\t").format(filename, get_mode_string(mode))

  try:
    file_object = open(filename, mode)
    bde_volume = pybde.volume()

    if password:
      bde_volume.set_password(password)
    if recovery_password:
      bde_volume.set_recovery_password(recovery_password)

    bde_volume.open_file_object(file_object, mode)
    bde_volume.close()

  except:
    print "(FAIL)"
    return False

  print "(PASS)"
  return True
Exemplo n.º 33
0
def pybde_test_seek_file_no_open(
    filename, password=None, recovery_password=None):
  print("Testing seek of offset without open:\n")

  bde_volume = pybde.volume()

  if password:
    bde_volume.set_password(password)
  if recovery_password:
    bde_volume.set_recovery_password(recovery_password)

  result = False
  try:
    bde_volume.seek(0, os.SEEK_SET)
  except Exception as exception:
    print(str(exception))
    result = True

  if not result:
    print("(FAIL)")
  else:
    print("(PASS)")
  return result
Exemplo n.º 34
0
def pybde_test_multi_open_close_file(
    filename, mode, password=None, recovery_password=None):
  print "Testing multi open close of: {0:s} with access: {1:s}\t".format(
      filename, get_mode_string(mode))

  try:
    bde_volume = pybde.volume()

    if password:
      bde_volume.set_password(password)
    if recovery_password:
      bde_volume.set_recovery_password(recovery_password)

    bde_volume.open(filename, mode)
    bde_volume.close()
    bde_volume.open(filename, mode)
    bde_volume.close()

  except:
    print "(FAIL)"
    return False

  print "(PASS)"
  return True
Exemplo n.º 35
0
def pybde_test_seek_file_no_open(filename,
                                 password=None,
                                 recovery_password=None):
    print("Testing seek of offset without open:\n")

    bde_volume = pybde.volume()

    if password:
        bde_volume.set_password(password)
    if recovery_password:
        bde_volume.set_recovery_password(recovery_password)

    result = False
    try:
        bde_volume.seek(0, os.SEEK_SET)
    except Exception as exception:
        print(str(exception))
        result = True

    if not result:
        print("(FAIL)")
    else:
        print("(PASS)")
    return result
Exemplo n.º 36
0
    def test_seek_offset(self):
        """Tests the seek_offset function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        bde_volume.open(test_source)

        size = bde_volume.get_size()

        bde_volume.seek_offset(16, os.SEEK_SET)

        offset = bde_volume.get_offset()
        self.assertEqual(offset, 16)

        bde_volume.seek_offset(16, os.SEEK_CUR)

        offset = bde_volume.get_offset()
        self.assertEqual(offset, 32)

        bde_volume.seek_offset(-16, os.SEEK_CUR)

        offset = bde_volume.get_offset()
        self.assertEqual(offset, 16)

        if size > 16:
            bde_volume.seek_offset(-16, os.SEEK_END)

            offset = bde_volume.get_offset()
            self.assertEqual(offset, size - 16)

        bde_volume.seek_offset(16, os.SEEK_END)

        offset = bde_volume.get_offset()
        self.assertEqual(offset, size + 16)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            bde_volume.seek_offset(-1, os.SEEK_SET)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            bde_volume.seek_offset(-32 - size, os.SEEK_CUR)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            bde_volume.seek_offset(-32 - size, os.SEEK_END)

        # TODO: change IOError into ValueError
        with self.assertRaises(IOError):
            bde_volume.seek_offset(0, -1)

        bde_volume.close()

        # Test the seek without open.
        with self.assertRaises(IOError):
            bde_volume.seek_offset(16, os.SEEK_SET)
Exemplo n.º 37
0
    def test_read_buffer_at_offset(self):
        """Tests the read_buffer_at_offset function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        bde_volume.open(test_source)

        size = bde_volume.get_size()

        # Test normal read.
        data = bde_volume.read_buffer_at_offset(4096, 0)

        self.assertIsNotNone(data)
        self.assertEqual(len(data), min(size, 4096))

        if size > 8:
            # Read buffer on size boundary.
            data = bde_volume.read_buffer_at_offset(4096, size - 8)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 8)

            # Read buffer beyond size boundary.
            data = bde_volume.read_buffer_at_offset(4096, size + 8)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 0)

        # Stress test read buffer.
        for _ in range(1024):
            random_number = random.random()

            media_offset = int(random_number * size)
            read_size = int(random_number * 4096)

            data = bde_volume.read_buffer_at_offset(read_size, media_offset)

            self.assertIsNotNone(data)

            remaining_size = size - media_offset

            data_size = len(data)

            if read_size > remaining_size:
                read_size = remaining_size

            self.assertEqual(data_size, read_size)

            remaining_size -= data_size

            if not remaining_size:
                bde_volume.seek_offset(0, os.SEEK_SET)

        with self.assertRaises(ValueError):
            bde_volume.read_buffer_at_offset(-1, 0)

        with self.assertRaises(ValueError):
            bde_volume.read_buffer_at_offset(4096, -1)

        bde_volume.close()

        # Test the read without open.
        with self.assertRaises(IOError):
            bde_volume.read_buffer_at_offset(4096, 0)
Exemplo n.º 38
0
    def test_read_buffer(self):
        """Tests the read_buffer function."""
        test_source = unittest.source
        if not test_source:
            raise unittest.SkipTest("missing source")

        if unittest.offset:
            raise unittest.SkipTest("source defines offset")

        bde_volume = pybde.volume()
        if unittest.password:
            bde_volume.set_password(unittest.password)
        if unittest.recovery_password:
            bde_volume.set_recovery_password(unittest.recovery_password)
        if unittest.startup_key:
            bde_volume.read_startup_key(unittest.startup_key)

        bde_volume.open(test_source)

        size = bde_volume.get_size()

        if size < 4096:
            # Test read without maximum size.
            bde_volume.seek_offset(0, os.SEEK_SET)

            data = bde_volume.read_buffer()

            self.assertIsNotNone(data)
            self.assertEqual(len(data), size)

        # Test read with maximum size.
        bde_volume.seek_offset(0, os.SEEK_SET)

        data = bde_volume.read_buffer(size=4096)

        self.assertIsNotNone(data)
        self.assertEqual(len(data), min(size, 4096))

        if size > 8:
            bde_volume.seek_offset(-8, os.SEEK_END)

            # Read buffer on size boundary.
            data = bde_volume.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 8)

            # Read buffer beyond size boundary.
            data = bde_volume.read_buffer(size=4096)

            self.assertIsNotNone(data)
            self.assertEqual(len(data), 0)

        # Stress test read buffer.
        bde_volume.seek_offset(0, os.SEEK_SET)

        remaining_size = size

        for _ in range(1024):
            read_size = int(random.random() * 4096)

            data = bde_volume.read_buffer(size=read_size)

            self.assertIsNotNone(data)

            data_size = len(data)

            if read_size > remaining_size:
                read_size = remaining_size

            self.assertEqual(data_size, read_size)

            remaining_size -= data_size

            if not remaining_size:
                bde_volume.seek_offset(0, os.SEEK_SET)

                remaining_size = size

        with self.assertRaises(ValueError):
            bde_volume.read_buffer(size=-1)

        bde_volume.close()

        # Test the read without open.
        with self.assertRaises(IOError):
            bde_volume.read_buffer(size=4096)
Exemplo n.º 39
0
    def test_close(self):
        """Tests the close function."""
        if not unittest.source:
            return

        bde_volume = pybde.volume()
Exemplo n.º 40
0
    def test_signal_abort(self):
        """Tests the signal_abort function."""
        bde_volume = pybde.volume()

        bde_volume.signal_abort()