Пример #1
0
    def testInternalExportEvents(self):
        """Tests the _ExportEvents function."""
        knowledge_base_object = knowledge_base.KnowledgeBase()
        output_writer = cli_test_lib.TestBinaryOutputWriter()

        formatter_mediator = formatters_mediator.FormatterMediator()

        output_mediator_object = output_mediator.OutputMediator(
            knowledge_base_object, formatter_mediator)

        output_module = TestOutputModule(output_mediator_object)
        output_module.SetOutputWriter(output_writer)

        test_engine = psort.PsortMultiProcessEngine()

        formatters_manager.FormattersManager.RegisterFormatter(
            TestEventFormatter)

        with shared_test_lib.TempDirectory() as temp_directory:
            temp_file = os.path.join(temp_directory, 'storage.plaso')
            self._CreateTestStorageFile(temp_file)

            storage_reader = (storage_factory.StorageFactory.
                              CreateStorageReaderForFile(temp_file))
            storage_reader.ReadPreprocessingInformation(knowledge_base_object)

            test_engine._ExportEvents(storage_reader,
                                      output_module,
                                      deduplicate_events=False)

        formatters_manager.FormattersManager.DeregisterFormatter(
            TestEventFormatter)

        self.assertEqual(len(output_module.events), 17)
        self.assertEqual(len(output_module.macb_groups), 3)
Пример #2
0
    def testParseNumericOption(self):
        """Tests the ParseNumericOption function."""
        output_writer = test_lib.TestBinaryOutputWriter()
        cli_tool = tools.CLITool(output_writer=output_writer)

        options = test_lib.TestOptions()

        numeric_value = cli_tool.ParseNumericOption(options, 'buffer_size')
        self.assertIsNone(numeric_value)

        numeric_value = cli_tool.ParseNumericOption(options,
                                                    'buffer_size',
                                                    default_value=0)
        self.assertEqual(numeric_value, 0)

        options.buffer_size = '10'

        numeric_value = cli_tool.ParseNumericOption(options, 'buffer_size')
        self.assertEqual(numeric_value, 10)

        numeric_value = cli_tool.ParseNumericOption(options,
                                                    'buffer_size',
                                                    base=16)
        self.assertEqual(numeric_value, 16)

        options.buffer_size = 'bogus'

        with self.assertRaises(errors.BadConfigOption):
            cli_tool.ParseNumericOption(options, 'buffer_size')

        options.buffer_size = (1, 'bogus')

        with self.assertRaises(errors.BadConfigOption):
            cli_tool.ParseNumericOption(options, 'buffer_size')
Пример #3
0
    def testPrint(self):
        """Tests the Print function."""
        output_writer = cli_test_lib.TestBinaryOutputWriter(encoding='utf-8')
        test_filter = file_entry_filters.DateTimeFileEntryFilter()

        test_filter.AddDateTimeRange('ctime',
                                     end_time_string='2012-05-25 15:59:25',
                                     start_time_string='2012-05-25 15:59:20')

        test_filter.AddDateTimeRange('atime',
                                     end_time_string='2012-05-25 15:59:25')

        test_filter.AddDateTimeRange('mtime',
                                     start_time_string='2012-05-25 15:59:20')

        test_filter.Print(output_writer)

        expected_output = [(b'\tctime between 2012-05-25 15:59:20.000000 and '
                            b'2012-05-25 15:59:25.000000'),
                           b'\tatime after 2012-05-25 15:59:25.000000',
                           b'\tmtime before 2012-05-25 15:59:20.000000', b'']

        output = output_writer.ReadOutput()

        # Compare the output as list of lines which makes it easier to spot
        # differences.
        self.assertEqual(output.split(b'\n'), expected_output)
Пример #4
0
    def testParseArguments(self):
        """Tests the ParseArguments function."""
        output_writer = test_lib.TestBinaryOutputWriter(encoding='utf-8')
        test_tool = pinfo_tool.PinfoTool(output_writer=output_writer)

        result = test_tool.ParseArguments([])
        self.assertFalse(result)
Пример #5
0
    def testListTimeZones(self):
        """Tests the ListTimeZones function."""
        output_writer = test_lib.TestBinaryOutputWriter()
        cli_tool = tools.CLITool(output_writer=output_writer)

        cli_tool.ListTimeZones()

        string = output_writer.ReadOutput()
        self.assertTrue(string[:4], self._EXPECTED_TIME_ZONE_OPTIONS)
Пример #6
0
    def testPrintSeparatorLine(self):
        """Tests the PrintSeparatorLine function."""
        output_writer = test_lib.TestBinaryOutputWriter()
        cli_tool = tools.CLITool(output_writer=output_writer)

        cli_tool.PrintSeparatorLine()
        string = output_writer.ReadOutput()
        expected_string = (b'----------------------------------------'
                           b'----------------------------------------\n')
        self.assertEqual(string, expected_string)
Пример #7
0
    def testExportEvents(self):
        """Tests the ExportEvents function."""
        test_file_path = self._GetTestFilePath(['psort_test.plaso'])
        self._SkipIfPathNotExists(test_file_path)

        knowledge_base_object = knowledge_base.KnowledgeBase()
        output_writer = cli_test_lib.TestBinaryOutputWriter()

        formatters_manager.FormattersManager.Reset()
        formatters_directory_path = self._GetDataFilePath(['formatters'])
        formatters_manager.FormattersManager.ReadFormattersFromDirectory(
            formatters_directory_path)

        formatter_mediator = formatters_mediator.FormatterMediator()
        formatter_mediator.SetPreferredLanguageIdentifier('en-US')

        output_mediator_object = output_mediator.OutputMediator(
            knowledge_base_object,
            formatter_mediator,
            data_location=shared_test_lib.TEST_DATA_PATH)

        output_module = dynamic.DynamicOutputModule(output_mediator_object)
        output_module.SetOutputWriter(output_writer)

        configuration = configurations.ProcessingConfiguration()

        storage_reader = storage_factory.StorageFactory.CreateStorageReaderForFile(
            test_file_path)

        test_engine = psort.PsortMultiProcessEngine()
        test_engine.ExportEvents(knowledge_base_object, storage_reader,
                                 output_module, configuration)

        lines = []
        output = output_writer.ReadOutput()
        # TODO: add test output writer that produces strings also see:
        # https://github.com/log2timeline/plaso/issues/1963
        output = codecs.decode(output, 'utf-8')
        for line in output.split('\n'):
            lines.append(line)

        self.assertEqual(len(lines), 22)

        expected_line = ('2014-11-18T01:15:43+00:00,'
                         'Content Modification Time,'
                         'LOG,'
                         'Log File,'
                         '[---] last message repeated 5 times ---,'
                         'syslog,'
                         'OS:/tmp/test/test_data/syslog,'
                         'repeated')
        self.assertEqual(lines[14], expected_line)
Пример #8
0
    def testPrint(self):
        """Tests the Print function."""
        output_writer = cli_test_lib.TestBinaryOutputWriter(encoding='utf-8')
        test_filter = file_entry_filters.NamesFileEntryFilter(['myfile'])

        test_filter.Print(output_writer)

        expected_output = [b'\tnames: myfile', b'']

        output = output_writer.ReadOutput()

        # Compare the output as list of lines which makes it easier to spot
        # differences.
        self.assertEqual(output.split(b'\n'), expected_output)
Пример #9
0
    def testListTimeZones(self):
        """Tests the ListTimeZones function."""
        output_writer = test_lib.TestBinaryOutputWriter()
        cli_tool = tools.CLITool(output_writer=output_writer)

        cli_tool.ListTimeZones()

        string = output_writer.ReadOutput()
        expected_string = (b'\n'
                           b'************************************ Zones '
                           b'*************************************\n'
                           b'                        Timezone : UTC Offset\n'
                           b'----------------------------------------'
                           b'----------------------------------------\n')
        self.assertTrue(string.startswith(expected_string))
Пример #10
0
    def testPrint(self):
        """Tests the Print function."""
        output_writer = cli_test_lib.TestBinaryOutputWriter(encoding='utf-8')

        specification_store = specification.FormatSpecificationStore()
        specification_store.AddNewSpecification('7z')

        test_filter = file_entry_filters.SignaturesFileEntryFilter(
            specification_store, ['7z', 'bzip2'])

        test_filter.Print(output_writer)

        expected_output = [b'\tsignature identifiers: 7z', b'']

        output = output_writer.ReadOutput()

        # Compare the output as list of lines which makes it easier to spot
        # differences.
        self.assertEqual(output.split(b'\n'), expected_output)
Пример #11
0
    def testWrite(self):
        """Tests the Write function."""
        output_writer = test_lib.TestBinaryOutputWriter()

        # Table with columns.
        table_view = views.MarkdownTableView(
            column_names=['Name', 'Description'], title='Title')
        table_view.AddRow(['First name', 'The first name in the table'])
        table_view.AddRow(['Second name', 'The second name in the table'])

        table_view.Write(output_writer)
        string = output_writer.ReadOutput()
        expected_string = (b'### Title\n'
                           b'\n'
                           b'Name | Description\n'
                           b'--- | ---\n'
                           b'First name | The first name in the table\n'
                           b'Second name | The second name in the table\n'
                           b'\n')

        # Splitting the string makes it easier to see differences.
        self.assertEqual(string.split(b'\n'), expected_string.split(b'\n'))

        # Table without columns.
        table_view = views.MarkdownTableView(title='Title')
        table_view.AddRow(['Name', 'The name in the table'])
        table_view.AddRow(['Description', 'The description in the table'])

        table_view.Write(output_writer)
        string = output_writer.ReadOutput()
        expected_string = (
            b'### Title\n'
            b'\n'
            b'<table>\n'
            b'<tr><th nowrap style="text-align:left;vertical-align:top">Name</th>'
            b'<td>The name in the table</td></tr>\n'
            b'<tr><th nowrap style="text-align:left;vertical-align:top">Description'
            b'</th><td>The description in the table</td></tr>\n'
            b'</table>\n'
            b'\n')

        # Splitting the string makes it easier to see differences.
        self.assertEqual(string.split(b'\n'), expected_string.split(b'\n'))
Пример #12
0
  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)
Пример #13
0
    def testWrite(self):
        """Tests the Write function."""
        output_writer = test_lib.TestBinaryOutputWriter()

        table_view = views.CLITabularTableView(
            column_names=['Name', 'Description'])
        table_view.AddRow(['First name', 'The first name in the table'])
        table_view.AddRow(['Second name', 'The second name in the table'])

        table_view.Write(output_writer)
        string = output_writer.ReadOutput()

        expected_strings = [
            b'Name            Description',
            b'First name      The first name in the table',
            b'Second name     The second name in the table', b''
        ]

        if not sys.platform.startswith('win'):
            expected_strings[0] = b'\x1b[1mName            Description\x1b[0m'

        self.assertEqual(string.split(b'\n'), expected_strings)
Пример #14
0
    def testWrite(self):
        """Tests the Write function."""
        output_writer = test_lib.TestBinaryOutputWriter()

        # Table with columns.
        table_view = views.CLITableView(column_names=['Name', 'Description'],
                                        title='Title')
        table_view.AddRow(['First name', 'The first name in the table'])
        table_view.AddRow(['Second name', 'The second name in the table'])

        table_view.Write(output_writer)
        string = output_writer.ReadOutput()
        expected_string = (b'\n'
                           b'************************************ '
                           b'Title '
                           b'*************************************\n'
                           b'       Name : Description\n'
                           b'----------------------------------------'
                           b'----------------------------------------\n'
                           b' First name : The first name in the table\n'
                           b'Second name : The second name in the table\n'
                           b'----------------------------------------'
                           b'----------------------------------------\n')

        # Splitting the string makes it easier to see differences.
        self.assertEqual(string.split(b'\n'), expected_string.split(b'\n'))

        # Table without columns.
        table_view = views.CLITableView(title='Title')
        table_view.AddRow(['Name', 'The name in the table'])
        table_view.AddRow(['Description', 'The description in the table'])

        table_view.Write(output_writer)
        string = output_writer.ReadOutput()
        expected_string = (b'\n'
                           b'************************************ '
                           b'Title '
                           b'*************************************\n'
                           b'       Name : The name in the table\n'
                           b'Description : The description in the table\n'
                           b'----------------------------------------'
                           b'----------------------------------------\n')

        # Splitting the string makes it easier to see differences.
        self.assertEqual(string.split(b'\n'), expected_string.split(b'\n'))

        # TODO: add test without title.

        # Table with a too large title.
        # TODO: determine if this is the desired behavior.
        title = (
            'In computer programming, a string is traditionally a sequence '
            'of characters, either as a literal constant or as some kind of '
            'variable.')
        table_view = views.CLITableView(column_names=['Name', 'Description'],
                                        title=title)
        table_view.AddRow(['First name', 'The first name in the table'])
        table_view.AddRow(['Second name', 'The second name in the table'])

        with self.assertRaises(RuntimeError):
            table_view.Write(output_writer)