| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Tests for the artifact definitions registry.""" |
| 3 |
|
| 4 |
import io |
| 5 |
import unittest |
| 6 |
|
| 7 |
from artifacts import errors |
| 8 |
from artifacts import reader |
| 9 |
from artifacts import registry |
| 10 |
from artifacts import source_type |
| 11 |
|
| 12 |
from tests import test_lib |
| 13 |
|
| 14 |
|
| 15 |
class TestSourceType(source_type.SourceType): |
| 16 |
"""Class that implements a test source type.""" |
| 17 |
|
| 18 |
TYPE_INDICATOR = 'test' |
| 19 |
|
| 20 |
def __init__(self, test=None): |
| 21 |
"""Initializes the source type object. |
| 22 |
|
| 23 |
Args: |
| 24 |
test (Optional[str]): test string. |
| 25 |
|
| 26 |
Raises: |
| 27 |
FormatError: when test is not set. |
| 28 |
""" |
| 29 |
if not test: |
| 30 |
raise errors.FormatError('Missing test value.') |
| 31 |
|
| 32 |
super(TestSourceType, self).__init__() |
| 33 |
self.test = test |
| 34 |
|
| 35 |
def AsDict(self): |
| 36 |
"""Represents a source type as a dictionary. |
| 37 |
|
| 38 |
Returns: |
| 39 |
dict[str, str]: source type attributes. |
| 40 |
""" |
| 41 |
return {'test': self.test} |
| 42 |
|
| 43 |
|
| 44 |
class ArtifactDefinitionsRegistryTest(test_lib.BaseTestCase): |
| 45 |
"""Tests for the artifact definitions registry.""" |
| 46 |
|
| 47 |
# pylint: disable=protected-access |
| 48 |
|
| 49 |
def testArtifactDefinitionsRegistry(self): |
| 50 |
"""Tests the ArtifactDefinitionsRegistry functions.""" |
| 51 |
test_file = self._GetTestFilePath(['definitions.yaml']) |
| 52 |
self._SkipIfPathNotExists(test_file) |
| 53 |
|
| 54 |
artifact_registry = registry.ArtifactDefinitionsRegistry() |
| 55 |
|
| 56 |
artifact_reader = reader.YamlArtifactsReader() |
| 57 |
|
| 58 |
for artifact_definition in artifact_reader.ReadFile(test_file): |
| 59 |
artifact_registry.RegisterDefinition(artifact_definition) |
| 60 |
|
| 61 |
# Make sure the test file got turned into artifacts. |
| 62 |
self.assertEqual(len(artifact_registry.GetDefinitions()), 7) |
| 63 |
|
| 64 |
artifact_definition = artifact_registry.GetDefinitionByName('EventLogs') |
| 65 |
self.assertIsNotNone(artifact_definition) |
| 66 |
|
| 67 |
# Try to register something already registered |
| 68 |
with self.assertRaises(KeyError): |
| 69 |
artifact_registry.RegisterDefinition(artifact_definition) |
| 70 |
|
| 71 |
# Deregister |
| 72 |
artifact_registry.DeregisterDefinition(artifact_definition) |
| 73 |
|
| 74 |
# Check it is gone |
| 75 |
with self.assertRaises(KeyError): |
| 76 |
artifact_registry.DeregisterDefinition(artifact_definition) |
| 77 |
|
| 78 |
self.assertEqual(len(artifact_registry.GetDefinitions()), 6) |
| 79 |
|
| 80 |
test_artifact_definition = artifact_registry.GetDefinitionByName( |
| 81 |
'SecurityEventLogEvtxFile') |
| 82 |
self.assertIsNotNone(test_artifact_definition) |
| 83 |
|
| 84 |
self.assertEqual(test_artifact_definition.name, 'SecurityEventLogEvtxFile') |
| 85 |
self.assertEqual(test_artifact_definition.aliases, ['SecurityEventLogEvtx']) |
| 86 |
|
| 87 |
expected_description = ( |
| 88 |
'Windows Security Event log for Vista or later systems.') |
| 89 |
self.assertEqual(test_artifact_definition.description, expected_description) |
| 90 |
|
| 91 |
bad_args = io.BytesIO( |
| 92 |
b'name: SecurityEventLogEvtx\n' |
| 93 |
b'doc: Windows Security Event log for Vista or later systems.\n' |
| 94 |
b'sources:\n' |
| 95 |
b'- type: FILE\n' |
| 96 |
b' attributes: {broken: [\'%%environ_systemroot%%\\System32\\' |
| 97 |
b'winevt\\Logs\\Security.evtx\']}\n' |
| 98 |
b'supported_os: [Windows]\n' |
| 99 |
b'urls: [\'http://www.forensicswiki.org/wiki/\n' |
| 100 |
b'Windows_XML_Event_Log_(EVTX)\']\n') |
| 101 |
|
| 102 |
generator = artifact_reader.ReadFileObject(bad_args) |
| 103 |
with self.assertRaises(errors.FormatError): |
| 104 |
next(generator) |
| 105 |
|
| 106 |
def testSourceTypeFunctions(self): |
| 107 |
"""Tests the source type functions.""" |
| 108 |
number_of_source_types = len( |
| 109 |
registry.ArtifactDefinitionsRegistry._source_type_classes) |
| 110 |
|
| 111 |
registry.ArtifactDefinitionsRegistry.RegisterSourceType(TestSourceType) |
| 112 |
|
| 113 |
self.assertEqual( |
| 114 |
len(registry.ArtifactDefinitionsRegistry._source_type_classes), |
| 115 |
number_of_source_types + 1) |
| 116 |
|
| 117 |
with self.assertRaises(KeyError): |
| 118 |
registry.ArtifactDefinitionsRegistry.RegisterSourceType(TestSourceType) |
| 119 |
|
| 120 |
registry.ArtifactDefinitionsRegistry.DeregisterSourceType(TestSourceType) |
| 121 |
|
| 122 |
self.assertEqual( |
| 123 |
len(registry.ArtifactDefinitionsRegistry._source_type_classes), |
| 124 |
number_of_source_types) |
| 125 |
|
| 126 |
registry.ArtifactDefinitionsRegistry.RegisterSourceTypes([TestSourceType]) |
| 127 |
|
| 128 |
self.assertEqual( |
| 129 |
len(registry.ArtifactDefinitionsRegistry._source_type_classes), |
| 130 |
number_of_source_types + 1) |
| 131 |
|
| 132 |
with self.assertRaises(KeyError): |
| 133 |
registry.ArtifactDefinitionsRegistry.RegisterSourceTypes([TestSourceType]) |
| 134 |
|
| 135 |
source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( |
| 136 |
'test', {'test': 'test123'}) |
| 137 |
|
| 138 |
self.assertIsNotNone(source_object) |
| 139 |
self.assertEqual(source_object.test, 'test123') |
| 140 |
|
| 141 |
with self.assertRaises(errors.FormatError): |
| 142 |
source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( |
| 143 |
'test', {}) |
| 144 |
|
| 145 |
with self.assertRaises(errors.FormatError): |
| 146 |
source_object = registry.ArtifactDefinitionsRegistry.CreateSourceType( |
| 147 |
'bogus', {}) |
| 148 |
|
| 149 |
registry.ArtifactDefinitionsRegistry.DeregisterSourceType(TestSourceType) |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == '__main__': |
| 153 |
unittest.main() |