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)
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 Main(): """The main function.""" input_reader = cli_tools.StdinInputReader() tool = psort_tool.PsortTool(input_reader=input_reader) if not tool.ParseArguments(sys.argv[1:]): return False if tool.show_troubleshooting: print('Using Python version {0!s}'.format(sys.version)) print() print('Path: {0:s}'.format(os.path.abspath(__file__))) print() print(tool.GetVersionInformation()) print() dependencies.CheckDependencies(verbose_output=True) print('Also see: https://plaso.readthedocs.io/en/latest/sources/user/' 'Troubleshooting.html') return True try: tool.CheckOutDated() except KeyboardInterrupt: return False have_list_option = False if tool.list_analysis_plugins: tool.ListAnalysisPlugins() have_list_option = True if tool.list_output_modules: tool.ListOutputModules() have_list_option = True if tool.list_language_identifiers: tool.ListLanguageIdentifiers() have_list_option = True if tool.list_timezones: tool.ListTimeZones() have_list_option = True if have_list_option: return True try: tool.ProcessStorage() except (KeyboardInterrupt, errors.UserAbort): logging.warning('Aborted by user.') return False except errors.BadConfigOption as exception: logging.warning(exception) return False return True
def testAddOutputTimeZoneOption(self): """Tests the AddOutputTimeZoneOption 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.AddOutputTimeZoneOption(argument_parser) output = self._RunArgparseFormatHelp(argument_parser) self.assertEqual(output, self._EXPECTED_OUTPUT_TIME_ZONE_OPTION)
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 testParseOutputTimeZoneOption(self): """Tests the _ParseOutputTimeZoneOption function.""" test_tool = psort_tool.PsortTool() options = test_lib.TestOptions() test_tool._ParseOutputTimeZoneOption(options) self.assertIsNone(test_tool._output_time_zone) options.output_time_zone = 'list' test_tool._ParseOutputTimeZoneOption(options) self.assertIsNone(test_tool._output_time_zone) options.output_time_zone = 'CET' test_tool._ParseOutputTimeZoneOption(options) self.assertEqual(test_tool._output_time_zone, 'CET')
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.data_location = shared_test_lib.DATA_PATH options.dynamic_time = True 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: lines = [line.strip() for line in file_object] self.assertTrue(input_reader.read_called) self.assertEqual(TestOutputModuleMissingParameters.missing, 'foobar') self.assertEqual(TestOutputModuleMissingParameters.parameters, 'foobar') expected_line = ( '2021-06-23T07:42:09.168590698+00:00,Last Access Time,FILE,File stat,' 'OS:/tmp/test/test_data/syslog Type: file,filestat,' 'OS:/tmp/test/test_data/syslog,-') self.assertIn(expected_line, lines) output_manager.OutputManager.DeregisterOutput( TestOutputModuleMissingParameters) helpers_manager.ArgumentHelperManager.DeregisterHelper( TestOutputModuleArgumentHelper)
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)
def Main(): """The main function.""" multiprocessing.freeze_support() input_reader = cli_tools.StdinInputReader() tool = psort_tool.PsortTool(input_reader=input_reader) if not tool.ParseArguments(): return False have_list_option = False if tool.list_analysis_plugins: tool.ListAnalysisPlugins() have_list_option = True if tool.list_output_modules: tool.ListOutputModules() have_list_option = True if tool.list_language_identifiers: tool.ListLanguageIdentifiers() have_list_option = True if tool.list_timezones: tool.ListTimeZones() have_list_option = True if have_list_option: return True try: tool.ProcessStorage() except (KeyboardInterrupt, errors.UserAbort): logging.warning('Aborted by user.') return False except errors.BadConfigOption as exception: logging.warning(exception) return False return True
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)