def test_server_scan_completed(self):
        output_file = StringIO()
        generator = ConsoleOutputGenerator(output_file)

        server_info = MockServerConnectivityInfo()
        plugin_result_1 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandOne(),
                                               u'Plugin ûnicôdé output', None)
        plugin_result_2 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandTwo(),
                                               u'other plugin Output', None)
        server_scan = CompletedServerScan(server_info,
                                          [plugin_result_1, plugin_result_2])
        generator.server_scan_completed(server_scan)

        received_output = output_file.getvalue()
        output_file.close()

        # Ensure the console output displayed the server's info
        self.assertIn(server_info.hostname, received_output.lower())
        self.assertIn(str(server_info.port), received_output)
        self.assertIn(server_info.ip_address, received_output.lower())

        # Ensure the console output displayed the plugin text outputs
        self.assertIn(plugin_result_1.text_output, received_output)
        self.assertIn(plugin_result_2.text_output, received_output)
    def test(self):
        """The final output only gets written at the end, when calling scans_completed(). Hence we need to call all the
        methods in the right order and validate the final output at the end.
        """
        output_file = StringIO()
        generator = XmlOutputGenerator(output_file)

        failed_parsing = ServerStringParsingError(
            supplied_server_string='www.badpãrsing.com',
            error_message='Pãrsing error'
        )
        generator.command_line_parsed(set(), MockCommandLineValues(), [failed_parsing])

        failed_scan = ServerConnectivityError(
            server_info=MockServerConnectivityTester(hostname='unibadeéè.com'),
            error_message='Some érrôr'
        )
        generator.server_connectivity_test_failed(failed_scan)

        server_info = MockServerConnectivityInfo()
        generator.server_connectivity_test_succeeded(server_info)

        generator.scans_started()

        plugin_xml_out_1 = Element('plugin1', attrib={'test1': 'value1'})
        plugin_xml_out_1.text = 'Plugin ûnicôdé output'
        plugin_result_1 = MockPluginScanResult(server_info, MockPluginScanCommandOne(), '', plugin_xml_out_1)
        plugin_xml_out_2 = Element('plugin2', attrib={'test2': 'value2'})
        plugin_xml_out_2.text = 'other plugin Output'
        plugin_result_2 = MockPluginScanResult(server_info, MockPluginScanCommandTwo(), '', plugin_xml_out_2)

        server_scan = CompletedServerScan(server_info, [plugin_result_1, plugin_result_2])
        generator.server_scan_completed(server_scan)

        scan_time = 1.3
        generator.scans_completed(scan_time)

        received_output = output_file.getvalue()
        output_file.close()

        # Ensure the output properly listed the parsing error
        self.assertIn('www.badpãrsing.com', received_output)
        self.assertIn('Pãrsing error', received_output)

        # Ensure the output properly listed the connectivity error
        self.assertIn('unibadeéè.com', received_output)
        self.assertIn('Some érrôr', received_output)

        # Ensure the output properly listed the online domain
        self.assertIn(server_info.hostname, received_output)
        self.assertIn(str(server_info.port), received_output)
        self.assertIn(server_info.ip_address, received_output)

        # Ensure the output displayed the plugin's XML output
        self.assertIn(plugin_result_1.scan_command.get_cli_argument(), received_output)
        self.assertIn(plugin_result_2.scan_command.get_cli_argument(), received_output)
        self.assertIn(plugin_result_1.as_xml().text, received_output)
        self.assertIn(plugin_result_2.as_xml().text, received_output)

        # Ensure the console output displayed the total scan time
        self.assertIn('totalScanTime="{}"'.format(scan_time), received_output)
    def test(self):
        """The final output only gets written at the end, when calling scans_completed(). Hence we need to call all the
        methods in the right order and validate the final output at the end.
        """
        output_file = StringIO()
        generator = JsonOutputGenerator(output_file)

        generator.command_line_parsed(None, MockCommandLineValues())

        failed_scan = FailedServerScan(
            server_string=u'unibadeéè.com',
            connection_exception=ServerConnectivityError(
                error_msg=u'Some érrôr'))
        generator.server_connectivity_test_failed(failed_scan)

        server_info = MockServerConnectivityInfo()
        generator.server_connectivity_test_succeeded(server_info)

        generator.scans_started()

        # noinspection PyTypeChecker
        plugin_result_1 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandOne(),
                                               u'Plugin ûnicôdé output', None)
        # noinspection PyTypeChecker
        plugin_result_2 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandTwo(),
                                               u'other plugin Output', None)
        # noinspection PyTypeChecker
        server_scan = CompletedServerScan(server_info,
                                          [plugin_result_1, plugin_result_2])
        generator.server_scan_completed(server_scan)

        scan_time = 1.3
        generator.scans_completed(scan_time)

        received_output = output_file.getvalue()
        output_file.close()

        # Ensure the output properly listed the connectivity error with unicode escaped as \u sequences
        self.assertIn(json.dumps(u'unibadeéè.com', ensure_ascii=True),
                      received_output)
        self.assertIn(json.dumps(u'Some érrôr', ensure_ascii=True),
                      received_output)

        # Ensure the output properly listed the online domain
        self.assertIn(json.dumps(server_info.hostname, ensure_ascii=True),
                      received_output)
        self.assertIn(str(server_info.port), received_output)
        self.assertIn(server_info.ip_address, received_output)

        # Ensure the output displayed the plugin's attributes as JSON
        self.assertIn(plugin_result_1.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn(plugin_result_2.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn(u'"text_output":', received_output)
        self.assertIn(
            json.dumps(plugin_result_1.text_output, ensure_ascii=True),
            received_output)
        self.assertIn(plugin_result_2.text_output, received_output)

        # Ensure the console output displayed the total scan time
        self.assertIn(str(scan_time), received_output)
        self.assertIn(
            u'"network_timeout": "{}"'.format(MockCommandLineValues().timeout),
            received_output)
        self.assertIn(
            u'"network_max_retries": "{}"'.format(
                MockCommandLineValues().nb_retries), received_output)
    def test(self):
        """The final output only gets written at the end, when calling scans_completed(). Hence we need to call all the
        methods in the right order and validate the final output at the end.
        """
        output_file = StringIO()
        generator = JsonOutputGenerator(output_file)

        failed_parsing = ServerStringParsingError(
            supplied_server_string='www.badpãrsing.com',
            error_message='Pãrsing error')
        generator.command_line_parsed(set(), MockCommandLineValues(),
                                      [failed_parsing])

        failed_scan = ServerConnectivityError(
            server_info=MockServerConnectivityTester(hostname='unibadeéè.com'),
            error_message='Some érrôr')
        generator.server_connectivity_test_failed(failed_scan)

        server_info = MockServerConnectivityInfo()
        generator.server_connectivity_test_succeeded(server_info)

        generator.scans_started()

        plugin_result_1 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandOne(),
                                               'Plugin ûnicôdé output', None)
        plugin_result_2 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandTwo(),
                                               'other plugin Output', None)
        server_scan = CompletedServerScan(server_info,
                                          [plugin_result_1, plugin_result_2])
        generator.server_scan_completed(server_scan)

        scan_time = 1.3
        generator.scans_completed(scan_time)

        received_output = output_file.getvalue()
        output_file.close()

        # Ensure the output properly listed the parsing error with unicode escaped as \u sequences
        self.assertIn('www.badp\\u00e3rsing.com', received_output)
        self.assertIn('P\\u00e3rsing error', received_output)

        # Ensure the output properly listed the connectivity error with unicode escaped as \u sequences
        self.assertIn('unibade\\u00e9\\u00e8.com:443', received_output)
        self.assertIn('Some \\u00e9rr\\u00f4r', received_output)

        # Ensure the output properly listed the online domain
        self.assertIn(json.dumps(server_info.hostname, ensure_ascii=True),
                      received_output)
        self.assertIn(str(server_info.port), received_output)
        self.assertIn(server_info.ip_address, received_output)

        # Ensure the output displayed the plugin's attributes as JSON
        self.assertIn(plugin_result_1.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn(plugin_result_2.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn('"text_output":', received_output)
        self.assertIn(
            json.dumps(plugin_result_1.text_output, ensure_ascii=True),
            received_output)
        self.assertIn(plugin_result_2.text_output, received_output)

        # Ensure the console output displayed the total scan time
        self.assertIn(str(scan_time), received_output)
Exemple #5
0
    def test(self):
        """The final output only gets written at the end, when calling scans_completed(). Hence we need to call all the
        methods in the right order and validate the final output at the end.
        """
        output_file = StringIO()
        generator = XmlOutputGenerator(output_file)

        generator.command_line_parsed(None, MockCommandLineValues())

        failed_scan = FailedServerScan(
            server_string=u'unibadeéè.com',
            connection_exception=ServerConnectivityError(
                error_msg=u'Some érrôr'))
        generator.server_connectivity_test_failed(failed_scan)

        server_info = MockServerConnectivityInfo()
        generator.server_connectivity_test_succeeded(server_info)

        generator.scans_started()

        plugin_xml_out_1 = Element(u'plugin1', attrib={u'test1': u'value1'})
        plugin_xml_out_1.text = u'Plugin ûnicôdé output'
        plugin_result_1 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandOne(), u'',
                                               plugin_xml_out_1)
        plugin_xml_out_2 = Element(u'plugin2', attrib={u'test2': u'value2'})
        plugin_xml_out_2.text = u'other plugin Output'
        plugin_result_2 = MockPluginScanResult(server_info,
                                               MockPluginScanCommandTwo(), u'',
                                               plugin_xml_out_2)

        # noinspection PyTypeChecker
        server_scan = CompletedServerScan(server_info,
                                          [plugin_result_1, plugin_result_2])
        generator.server_scan_completed(server_scan)

        scan_time = 1.3
        generator.scans_completed(scan_time)

        received_output = output_file.getvalue().decode('utf-8')
        output_file.close()

        # Ensure the output properly listed the connectivity error with unicode escaped as \u sequences
        self.assertIn(u'unibadeéè.com', received_output)
        self.assertIn(u'Some érrôr', received_output)

        # Ensure the output properly listed the online domain
        self.assertIn(server_info.hostname, received_output)
        self.assertIn(str(server_info.port), received_output)
        self.assertIn(server_info.ip_address, received_output)

        # Ensure the output displayed the plugin's XML output
        self.assertIn(plugin_result_1.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn(plugin_result_2.scan_command.get_cli_argument(),
                      received_output)
        self.assertIn(plugin_result_1.as_xml().text, received_output)
        self.assertIn(plugin_result_2.as_xml().text, received_output)

        # Ensure the console output displayed the total scan time
        self.assertIn('totalScanTime="{}"'.format(scan_time), received_output)
        self.assertIn(
            'networkTimeout="{}"'.format(MockCommandLineValues().timeout),
            received_output)
        self.assertIn(
            'networkMaxRetries="{}"'.format(
                MockCommandLineValues().nb_retries), received_output)