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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the event formatters interface classes."""
from __future__ import unicode_literals
import unittest
from plaso.formatters import interface
from plaso.formatters import mediator
from plaso.lib import definitions
from tests.containers import test_lib as containers_test_lib
from tests.formatters import test_lib
class BrokenConditionalEventFormatter(interface.ConditionalEventFormatter):
"""An event object for testing the conditional event formatter."""
DATA_TYPE = 'test:broken_conditional'
FORMAT_STRING_PIECES = ['{too} {many} formatting placeholders']
SOURCE_SHORT = 'LOG'
SOURCE_LONG = 'Some Text File.'
class ConditionalTestEventFormatter(interface.ConditionalEventFormatter):
"""A test conditional event formatter."""
DATA_TYPE = 'test:event:conditional'
FORMAT_STRING_PIECES = [
'Description: {description}',
'Comment',
'Value: 0x{numeric:02x}',
'Optional: {optional}',
'Text: {text}']
SOURCE_SHORT = 'LOG'
SOURCE_LONG = 'Some Text File.'
class WrongEventFormatter(interface.EventFormatter):
"""An event formatter for testing."""
DATA_TYPE = 'test:wrong'
FORMAT_STRING = 'This format string does not match {body}.'
SOURCE_SHORT = 'FILE'
SOURCE_LONG = 'Weird Log File'
class EnumerationEventFormatterHelperTEst(test_lib.EventFormatterTestCase):
"""Tests for the enumeration event formatter helper."""
def testInitialization(self):
"""Tests the initialization."""
event_formatter_helper = interface.EnumerationEventFormatterHelper()
self.assertIsNotNone(event_formatter_helper)
def testFormatEventValues(self):
"""Tests the FormatEventValues function."""
event_formatter_helper = interface.EnumerationEventFormatterHelper()
event_values = {}
event_formatter_helper.FormatEventValues(event_values)
class FlagsEventFormatterHelperTEst(test_lib.EventFormatterTestCase):
"""Tests for the flags event formatter helper."""
def testInitialization(self):
"""Tests the initialization."""
event_formatter_helper = interface.FlagsEventFormatterHelper()
self.assertIsNotNone(event_formatter_helper)
def testFormatEventValues(self):
"""Tests the FormatEventValues function."""
event_formatter_helper = interface.FlagsEventFormatterHelper()
event_values = {}
event_formatter_helper.FormatEventValues(event_values)
class EventFormatterTest(test_lib.EventFormatterTestCase):
"""Tests for the event formatter."""
_TEST_EVENTS = [
{'data_type': 'test:event',
'description': 'this is beyond words',
'numeric': 12,
'text': 'but we\'re still trying to say something about the event',
'timestamp': 1335791207939596,
'timestamp_desc': definitions.TIME_DESCRIPTION_UNKNOWN}]
def testInitialization(self):
"""Tests the initialization."""
event_formatter = test_lib.TestEventFormatter()
self.assertIsNotNone(event_formatter)
# TODO: add tests for _FormatMessage
# TODO: add tests for _FormatMessages
def testGetFormatStringAttributeNames(self):
"""Tests the GetFormatStringAttributeNames function."""
event_formatter = test_lib.TestEventFormatter()
expected_attribute_names = ['text']
attribute_names = event_formatter.GetFormatStringAttributeNames()
self.assertEqual(sorted(attribute_names), expected_attribute_names)
def testGetMessages(self):
"""Tests the GetMessages function."""
formatter_mediator = mediator.FormatterMediator()
event_formatter = test_lib.TestEventFormatter()
_, event_data, _ = containers_test_lib.CreateEventFromValues(
self._TEST_EVENTS[0])
message, _ = event_formatter.GetMessages(formatter_mediator, event_data)
self.assertEqual(
message, 'but we\'re still trying to say something about the event')
class ConditionalEventFormatterTest(test_lib.EventFormatterTestCase):
"""Tests for the conditional event formatter."""
# pylint: disable=protected-access
_TEST_EVENTS = [
{'data_type': 'test:event:conditional',
'description': 'this is beyond words',
'numeric': 12,
'text': 'but we\'re still trying to say something about the event',
'timestamp': 1335791207939596,
'timestamp_desc': definitions.TIME_DESCRIPTION_UNKNOWN}]
def testCreateFormatStringMaps(self):
"""Tests the _CreateFormatStringMaps function."""
event_formatter = ConditionalTestEventFormatter()
event_formatter._CreateFormatStringMaps()
with self.assertRaises(RuntimeError):
event_formatter = BrokenConditionalEventFormatter()
event_formatter._CreateFormatStringMaps()
def testGetFormatStringAttributeNames(self):
"""Tests the GetFormatStringAttributeNames function."""
event_formatter = ConditionalTestEventFormatter()
expected_attribute_names = sorted([
'description', 'numeric', 'optional', 'text'])
attribute_names = event_formatter.GetFormatStringAttributeNames()
self.assertEqual(sorted(attribute_names), expected_attribute_names)
def testGetMessages(self):
"""Tests the GetMessages function."""
formatter_mediator = mediator.FormatterMediator()
event_formatter = ConditionalTestEventFormatter()
_, event_data, _ = containers_test_lib.CreateEventFromValues(
self._TEST_EVENTS[0])
message, _ = event_formatter.GetMessages(formatter_mediator, event_data)
expected_message = (
'Description: this is beyond words Comment Value: 0x0c '
'Text: but we\'re still trying to say something about the event')
self.assertEqual(message, expected_message)
if __name__ == '__main__':
unittest.main()
|