File: analysis_engine.py

package info (click to toggle)
plaso 20241006-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 673,228 kB
  • sloc: python: 91,831; sh: 557; xml: 97; makefile: 17; sql: 14; vhdl: 11
file content (178 lines) | stat: -rw-r--r-- 5,979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the task-based multi-process processing analysis engine."""

import os
import shutil
import unittest

from plaso.analysis import tagging
from plaso.containers import sessions
from plaso.engine import configurations
from plaso.lib import definitions
from plaso.multi_process import analysis_engine
from plaso.storage import factory as storage_factory

from tests import test_lib as shared_test_lib
from tests.filters import test_lib as filters_test_lib
from tests.multi_process import test_lib


class AnalysisEngineMultiProcessEngineTest(test_lib.MultiProcessingTestCase):
  """Tests for the task-based multi-process processing analysis engine."""

  # pylint: disable=protected-access

  def testInternalAnalyzeEvents(self):
    """Tests the _AnalyzeEvents function."""
    test_file_path = self._GetTestFilePath(['psort_test.plaso'])
    self._SkipIfPathNotExists(test_file_path)

    test_tagging_file_path = self._GetTestFilePath([
        'tagging_file', 'valid.txt'])
    self._SkipIfPathNotExists(test_tagging_file_path)

    session = sessions.Session()

    analysis_plugin = tagging.TaggingAnalysisPlugin()
    analysis_plugin.SetAndLoadTagFile(test_tagging_file_path)

    analysis_plugins = {'tagging': analysis_plugin}

    configuration = configurations.ProcessingConfiguration()
    test_engine = analysis_engine.AnalysisMultiProcessEngine()

    with shared_test_lib.TempDirectory() as temp_directory:
      temp_file = os.path.join(temp_directory, 'storage.plaso')
      shutil.copyfile(test_file_path, temp_file)

      storage_writer = storage_factory.StorageFactory.CreateStorageWriter(
          definitions.DEFAULT_STORAGE_FORMAT)

      test_engine._processing_configuration = configuration
      test_engine._session = session

      test_engine._storage_file_path = temp_directory
      test_engine._StartTaskStorage(definitions.STORAGE_FORMAT_SQLITE)

      test_engine._StartAnalysisProcesses(analysis_plugins)

      storage_writer.Open(path=temp_file)

      try:
        events_counter = test_engine._AnalyzeEvents(
            storage_writer, analysis_plugins)
      finally:
        storage_writer.Close()

      test_engine._StopAnalysisProcesses()

    self.assertIsNotNone(events_counter)
    self.assertEqual(events_counter['Events filtered'], 0)
    self.assertEqual(events_counter['Events processed'], 38)

  # TODO: add test for _CheckStatusAnalysisProcess.
  # TODO: add test for _StartAnalysisProcesses.
  # TODO: add test for _StatusUpdateThreadMain.
  # TODO: add test for _StopAnalysisProcesses.
  # TODO: add test for _UpdateProcessingStatus.

  def testAnalyzeEvents(self):
    """Tests the AnalyzeEvents function."""
    test_file_path = self._GetTestFilePath(['psort_test.plaso'])
    self._SkipIfPathNotExists(test_file_path)

    test_tagging_file_path = self._GetTestFilePath([
        'tagging_file', 'valid.txt'])
    self._SkipIfPathNotExists(test_tagging_file_path)

    session = sessions.Session()

    data_location = ''

    analysis_plugin = tagging.TaggingAnalysisPlugin()
    analysis_plugin.SetAndLoadTagFile(test_tagging_file_path)

    analysis_plugins = {'tagging': analysis_plugin}

    configuration = configurations.ProcessingConfiguration()
    test_engine = analysis_engine.AnalysisMultiProcessEngine()

    with shared_test_lib.TempDirectory() as temp_directory:
      temp_file = os.path.join(temp_directory, 'storage.plaso')
      shutil.copyfile(test_file_path, temp_file)

      storage_writer = storage_factory.StorageFactory.CreateStorageWriter(
          definitions.DEFAULT_STORAGE_FORMAT)

      storage_writer.Open(path=temp_file)

      try:
        number_of_reports = storage_writer.GetNumberOfAttributeContainers(
            'analysis_report')
        self.assertEqual(number_of_reports, 2)

        test_engine.AnalyzeEvents(
            session, storage_writer, data_location, analysis_plugins,
            configuration, storage_file_path=temp_directory)

        number_of_reports = storage_writer.GetNumberOfAttributeContainers(
            'analysis_report')
        self.assertEqual(number_of_reports, 3)

      finally:
        storage_writer.Close()

  def testAnalyzeEventsWithEventFilter(self):
    """Tests the AnalyzeEvents function with an event filter."""
    test_file_path = self._GetTestFilePath(['psort_test.plaso'])
    self._SkipIfPathNotExists(test_file_path)

    test_tagging_file_path = self._GetTestFilePath([
        'tagging_file', 'valid.txt'])
    self._SkipIfPathNotExists(test_tagging_file_path)

    session = sessions.Session()

    data_location = ''

    analysis_plugin = tagging.TaggingAnalysisPlugin()
    analysis_plugin.SetAndLoadTagFile(test_tagging_file_path)

    analysis_plugins = {'tagging': analysis_plugin}

    configuration = configurations.ProcessingConfiguration()
    test_engine = analysis_engine.AnalysisMultiProcessEngine()
    test_filter = filters_test_lib.TestEventFilter()

    with shared_test_lib.TempDirectory() as temp_directory:
      temp_file = os.path.join(temp_directory, 'storage.plaso')
      shutil.copyfile(test_file_path, temp_file)

      storage_writer = storage_factory.StorageFactory.CreateStorageWriter(
          definitions.DEFAULT_STORAGE_FORMAT)

      storage_writer.Open(path=temp_file)

      try:
        number_of_reports = storage_writer.GetNumberOfAttributeContainers(
            'analysis_report')
        self.assertEqual(number_of_reports, 2)

        test_engine.AnalyzeEvents(
            session, storage_writer, data_location, analysis_plugins,
            configuration, event_filter=test_filter,
            storage_file_path=temp_directory)

        number_of_reports = storage_writer.GetNumberOfAttributeContainers(
            'analysis_report')
        self.assertEqual(number_of_reports, 3)

      finally:
        storage_writer.Close()

  # TODO: add bogus data location test.


if __name__ == '__main__':
  unittest.main()