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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the psort CLI tool."""
from __future__ import unicode_literals
import argparse
import io
import os
import unittest
try:
import resource
except ImportError:
resource = None
from plaso.cli import psort_tool
from plaso.cli.helpers import interface as helpers_interface
from plaso.cli.helpers import manager as helpers_manager
from plaso.lib import errors
from plaso.output import interface as output_interface
from plaso.output import manager as output_manager
from tests import test_lib as shared_test_lib
from tests.cli import test_lib
class TestInputReader(object):
"""Test input reader."""
def __init__(self):
"""Initialize the reader."""
super(TestInputReader, self).__init__()
self.read_called = False
def Read(self):
"""Mock a read operation by user."""
self.read_called = True
return 'foobar'
class TestOutputModuleArgumentHelper(helpers_interface.ArgumentsHelper):
"""Test argument helper for the test output module."""
NAME = 'test_missing'
@classmethod
def AddArguments(cls, argument_group):
"""Mock the add argument section."""
pass # pylint: disable=unnecessary-pass
# pylint: disable=arguments-differ
@classmethod
def ParseOptions(cls, options, output_module):
"""Provide a test parse options section."""
if not isinstance(output_module, TestOutputModuleMissingParameters):
raise errors.BadConfigObject((
'Output module is not an instance of '
'TestOutputModuleMissingParameters'))
missing = getattr(options, 'missing', None)
if missing:
output_module.SetMissingValue('missing', missing)
parameters = getattr(options, 'parameters', None)
if parameters:
output_module.SetMissingValue('parameters', parameters)
class TestOutputModuleMissingParameters(output_interface.LinearOutputModule):
"""Test output module that is missing some parameters."""
NAME = 'test_missing'
_HEADER = (
'date,time,timezone,MACB,source,sourcetype,type,user,host,'
'short,desc,version,filename,inode,notes,format,extra\n')
# For test purpose assign these as class attributes.
missing = None
parameters = None
def GetMissingArguments(self):
"""Return a list of missing parameters."""
missing_parameters = []
if self.missing is None:
missing_parameters.append('missing')
if self.parameters is None:
missing_parameters.append('parameters')
return missing_parameters
@classmethod
def SetMissingValue(cls, attribute, value):
"""Set missing value."""
setattr(cls, attribute, value)
def WriteEventBody(self, event):
"""Writes the body of an event object to the output.
Args:
event (EventObject): event.
"""
message, _ = self._output_mediator.GetFormattedMessages(event)
source_short, source_long = self._output_mediator.GetFormattedSources(event)
self._output_writer.Write('{0:s}/{1:s} {2:s}\n'.format(
source_short, source_long, message))
def WriteHeader(self):
"""Writes the header to the output."""
self._output_writer.Write(self._HEADER)
class PsortToolTest(test_lib.CLIToolTestCase):
"""Tests for the psort tool."""
if resource is None:
_EXPECTED_PROCESSING_OPTIONS = """\
usage: psort_test.py [--temporary_directory DIRECTORY] [--disable_zeromq]
[--worker-memory-limit SIZE]
Test argument parser.
optional arguments:
--disable_zeromq, --disable-zeromq
Disable queueing using ZeroMQ. A Multiprocessing queue
will be used instead.
--temporary_directory DIRECTORY, --temporary-directory DIRECTORY
Path to the directory that should be used to store
temporary files created during processing.
--worker-memory-limit SIZE, --worker_memory_limit SIZE
Maximum amount of memory (data segment and shared
memory) a worker process is allowed to consume in
bytes, where 0 represents no limit. The default limit
is 2147483648 (2 GiB). If a worker process exceeds
this limit is is killed by the main (foreman) process.
"""
else:
_EXPECTED_PROCESSING_OPTIONS = """\
usage: psort_test.py [--process_memory_limit SIZE]
[--temporary_directory DIRECTORY] [--disable_zeromq]
[--worker-memory-limit SIZE]
Test argument parser.
optional arguments:
--disable_zeromq, --disable-zeromq
Disable queueing using ZeroMQ. A Multiprocessing queue
will be used instead.
--process_memory_limit SIZE, --process-memory-limit SIZE
Maximum amount of memory (data segment) a process is
allowed to allocate in bytes, where 0 represents no
limit. The default limit is 4294967296 (4 GiB). This
applies to both the main (foreman) process and the
worker processes. This limit is enforced by the
operating system and will supersede the worker memory
limit (--worker_memory_limit).
--temporary_directory DIRECTORY, --temporary-directory DIRECTORY
Path to the directory that should be used to store
temporary files created during processing.
--worker-memory-limit SIZE, --worker_memory_limit SIZE
Maximum amount of memory (data segment and shared
memory) a worker process is allowed to consume in
bytes, where 0 represents no limit. The default limit
is 2147483648 (2 GiB). If a worker process exceeds
this limit is is killed by the main (foreman) process.
"""
# TODO: add test for _CreateOutputModule.
# TODO: add test for _FormatStatusTableRow.
# TODO: add test for _GetAnalysisPlugins.
# TODO: add test for _ParseAnalysisPluginOptions.
# TODO: add test for _ParseProcessingOptions.
# TODO: add test for _ParseInformationalOptions.
# TODO: add test for _PrintStatusHeader.
# TODO: add test for _PrintStatusUpdate.
# TODO: add test for _PrintStatusUpdateStream.
def testAddProcessingOptions(self):
"""Tests the AddProcessingOptions function."""
argument_parser = argparse.ArgumentParser(
prog='psort_test.py',
description='Test argument parser.', add_help=False,
formatter_class=test_lib.SortedArgumentsHelpFormatter)
test_tool = psort_tool.PsortTool()
test_tool.AddProcessingOptions(argument_parser)
output = self._RunArgparseFormatHelp(argument_parser)
self.assertEqual(output, self._EXPECTED_PROCESSING_OPTIONS)
def testListLanguageIdentifiers(self):
"""Tests the ListLanguageIdentifiers function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = psort_tool.PsortTool(output_writer=output_writer)
test_tool.ListLanguageIdentifiers()
output = output_writer.ReadOutput()
number_of_tables = 0
lines = []
for line in output.split('\n'):
line = line.strip()
lines.append(line)
if line.startswith('*****') and line.endswith('*****'):
number_of_tables += 1
self.assertIn('Language identifiers', lines[1])
lines = frozenset(lines)
self.assertEqual(number_of_tables, 1)
expected_line = 'en : English'
self.assertIn(expected_line, lines)
def testParseArguments(self):
"""Tests the ParseArguments function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = psort_tool.PsortTool(output_writer=output_writer)
result = test_tool.ParseArguments()
self.assertFalse(result)
# TODO: check output.
# TODO: improve test coverage.
def testParseOptions(self):
"""Tests the ParseOptions function."""
output_writer = test_lib.TestBinaryOutputWriter(encoding='utf-8')
test_tool = psort_tool.PsortTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.output_format = 'null'
options.storage_file = self._GetTestFilePath(['psort_test.plaso'])
test_tool.ParseOptions(options)
options = test_lib.TestOptions()
with self.assertRaises(errors.BadConfigOption):
test_tool.ParseOptions(options)
options = test_lib.TestOptions()
options.storage_file = self._GetTestFilePath(['psort_test.plaso'])
with self.assertRaises(errors.BadConfigOption):
test_tool.ParseOptions(options)
# TODO: improve test coverage.
def testProcessStorageWithMissingParameters(self):
"""Tests the ProcessStorage function with parameters missing."""
encoding = 'utf-8'
input_reader = TestInputReader()
output_writer = test_lib.TestOutputWriter(encoding=encoding)
test_tool = psort_tool.PsortTool(
input_reader=input_reader, output_writer=output_writer)
options = test_lib.TestOptions()
options.storage_file = self._GetTestFilePath(['psort_test.plaso'])
options.output_format = 'test_missing'
output_manager.OutputManager.RegisterOutput(
TestOutputModuleMissingParameters)
helpers_manager.ArgumentHelperManager.RegisterHelper(
TestOutputModuleArgumentHelper)
lines = []
with shared_test_lib.TempDirectory() as temp_directory:
temp_file_name = os.path.join(temp_directory, 'output.txt')
options.write = temp_file_name
test_tool.ParseOptions(options)
test_tool.ProcessStorage()
with io.open(temp_file_name, 'rt', encoding=encoding) as file_object:
for line in file_object.readlines():
lines.append(line.strip())
self.assertTrue(input_reader.read_called)
self.assertEqual(TestOutputModuleMissingParameters.missing, 'foobar')
self.assertEqual(TestOutputModuleMissingParameters.parameters, 'foobar')
expected_line = (
'FILE/OS Metadata Modification Time OS:/tmp/test/test_data/syslog '
'Type: file')
self.assertIn(expected_line, lines)
output_manager.OutputManager.DeregisterOutput(
TestOutputModuleMissingParameters)
helpers_manager.ArgumentHelperManager.DeregisterHelper(
TestOutputModuleArgumentHelper)
if __name__ == '__main__':
unittest.main()
|