Exemplo n.º 1
0
    def _OpenDatabaseFile(self, path_segments, wal_path=None):
        """Opens a SQLite database file.

    Args:
      path_segments (list[str]): path segments inside the test data directory.
      wal_path (Optional[str]): path to the SQLite WAL file.

    Returns:
      tuple: contains:
          file_entry (dfvfs.FileEntry): file entry of the SQLite database file.
          SQLiteDatabase: SQLite database file.
    """
        file_entry = self._GetTestFileEntry(path_segments)

        wal_file_entry = None
        if wal_path:
            wal_path_spec = path_spec_factory.Factory.NewPathSpec(
                definitions.TYPE_INDICATOR_OS, location=wal_path)
            wal_file_entry = path_spec_resolver.Resolver.OpenFileEntry(
                wal_path_spec)

        database = sqlite.SQLiteDatabase(file_entry.name)
        file_object = file_entry.GetFileObject()
        try:
            if not wal_file_entry:
                database.Open(file_object)
            else:
                wal_file_object = wal_file_entry.GetFileObject()

                # Seek file_object to 0 so we can re-open the database with WAL file.
                file_object.seek(0, os.SEEK_SET)
                try:
                    database.Open(file_object, wal_file_object=wal_file_object)
                finally:
                    wal_file_object.close()

        finally:
            file_object.close()

        return file_entry, database
Exemplo n.º 2
0
    def _ParseDatabaseFileWithPlugin(self,
                                     plugin_object,
                                     path,
                                     cache=None,
                                     knowledge_base_values=None):
        """Parses a file as a SQLite database with a specific plugin.

    Args:
      plugin_object: The plugin object that is used to extract an event
                     generator.
      path: The path to the SQLite database file.
      cache: A cache object (instance of SQLiteCache).
      knowledge_base_values: optional dict containing the knowledge base
                             values. The default is None.

    Returns:
      An event object queue consumer object (instance of
      TestEventObjectQueueConsumer).
    """
        event_queue = queue.SingleThreadedQueue()
        event_queue_consumer = test_lib.TestEventObjectQueueConsumer(
            event_queue)

        parse_error_queue = queue.SingleThreadedQueue()

        parser_context = self._GetParserContext(
            event_queue,
            parse_error_queue,
            knowledge_base_values=knowledge_base_values)
        path_spec = path_spec_factory.Factory.NewPathSpec(
            definitions.TYPE_INDICATOR_OS, location=path)
        file_entry = path_spec_resolver.Resolver.OpenFileEntry(path_spec)

        with sqlite.SQLiteDatabase(file_entry) as database:
            plugin_object.Process(parser_context,
                                  cache=cache,
                                  database=database)

        return event_queue_consumer
Exemplo n.º 3
0
  def testQueryDatabaseWithWAL(self):
    """Tests the Query function on a database with a WAL file."""
    database_file_path = self._GetTestFilePath(['wal_database.db'])
    self._SkipIfPathNotExists(database_file_path)

    database_wal_file_path = self._GetTestFilePath(['wal_database.db-wal'])
    self._SkipIfPathNotExists(database_wal_file_path)

    database = sqlite.SQLiteDatabase('wal_database.db')
    with open(database_file_path, 'rb') as database_file_object:
      with open(database_wal_file_path, 'rb') as wal_file_object:
        database.Open(database_file_object, wal_file_object=wal_file_object)

    row_results = []
    for row in database.Query('SELECT * FROM MyTable'):
      # Note that pysqlite does not accept a Unicode string in row['string'] and
      # will raise "IndexError: Index must be int or string".
      # Also, Field3 needs to be converted to a string if Python 2 is used
      # because it is a read-write buffer.
      field3 = row['Field3']
      if py2to3.PY_2 and field3 is not None:
        field3 = str(field3)
      row_results.append((
          row['Field1'], row['Field2'], field3))

    expected_results = [
        ('Committed Text 1', 1, None),
        ('Committed Text 2', 2, None),
        ('Modified Committed Text 3', 4, None),
        ('Committed Text 4', 5, None),
        ('Committed Text 5', 7, None),
        ('Committed Text 6', 8, None),
        ('Committed Text 7', 9, None),
        ('Unhashable Row 1', 10, b'Binary Text!\x01\x02\x03'),
        ('Unhashable Row 2', 11, b'More Binary Text!\x01\x02\x03'),
        ('New Text 1', 12, None),
        ('New Text 2', 13, None)]

    self.assertEqual(expected_results, row_results)
Exemplo n.º 4
0
  def _OpenDatabaseFile(self, path_segments, wal_path_segments=None):
    """Opens a SQLite database file.

    Args:
      path_segments (list[str]): path segments inside the test data directory.
      wal_path_segments (list[str]): path segments inside the test data
          directory of the SQLite WAL file.

    Returns:
      tuple: containing:
          file_entry (dfvfs.FileEntry): file entry of the SQLite database file.
          SQLiteDatabase: SQLite database file.

    Raises:
      SkipTest: if the path inside the test data directory does not exist and
          the test should be skipped.
    """
    file_entry = self._GetTestFileEntry(path_segments)

    wal_file_entry = None
    if wal_path_segments:
      wal_file_entry = self._GetTestFileEntry(wal_path_segments)

    database = sqlite.SQLiteDatabase(file_entry.name)
    file_object = file_entry.GetFileObject()

    if not wal_file_entry:
      database.Open(file_object)
    else:
      wal_file_object = wal_file_entry.GetFileObject()

      # Seek file_object to 0 so we can re-open the database with WAL file.
      file_object.seek(0, os.SEEK_SET)
      database.Open(file_object, wal_file_object=wal_file_object)

    return file_entry, database
Exemplo n.º 5
0
    def _ParseDatabaseFileWithPlugin(self,
                                     path_segments,
                                     plugin_object,
                                     cache=None,
                                     knowledge_base_values=None,
                                     wal_path=None):
        """Parses a file as a SQLite database with a specific plugin.

    Args:
      path_segments: a list of strings containinge the path segments inside
                     the test data directory.
      plugin_object: The plugin object that is used to extract an event
                     generator.
      cache: optional cache object (instance of SQLiteCache).
      knowledge_base_values: optional dict containing the knowledge base
                             values.
      wal_path: optional string containing the path to the SQLite WAL file.

    Returns:
      A storage writer object (instance of FakeStorageWriter).
    """
        session = sessions.Session()
        storage_writer = fake_storage.FakeStorageWriter(session)
        storage_writer.Open()

        file_entry = self._GetTestFileEntry(path_segments)
        parser_mediator = self._CreateParserMediator(
            storage_writer,
            file_entry=file_entry,
            knowledge_base_values=knowledge_base_values)

        parser_mediator.SetFileEntry(file_entry)

        wal_file_entry = None
        if wal_path:
            wal_path_spec = path_spec_factory.Factory.NewPathSpec(
                definitions.TYPE_INDICATOR_OS, location=wal_path)
            wal_file_entry = path_spec_resolver.Resolver.OpenFileEntry(
                wal_path_spec)

        # AppendToParserChain needs to be run after SetFileEntry.
        parser_mediator.AppendToParserChain(plugin_object)

        database = sqlite.SQLiteDatabase(file_entry.name)
        database_wal = None

        file_object = file_entry.GetFileObject()
        try:
            database.Open(file_object)
            if wal_file_entry:
                database_wal = sqlite.SQLiteDatabase(file_entry.name)
                wal_file_object = wal_file_entry.GetFileObject()
                # Seek file_object to 0 so we can re-open the database with WAL file.
                file_object.seek(0)
                try:
                    database_wal.Open(file_object,
                                      wal_file_object=wal_file_object)
                finally:
                    wal_file_object.close()
        finally:
            file_object.close()

        try:
            plugin_object.Process(parser_mediator,
                                  cache=cache,
                                  database=database,
                                  database_wal=database_wal,
                                  wal_file_entry=wal_file_entry)
        finally:
            database.Close()
            if database_wal:
                database_wal.Close()

        return storage_writer