def __init__(self, pathname, filename='*.spider', **kwargs): """Initialize HoppyParser. :param str pathname: Path to the report directory. :param str filename: Regex matching the report file. """ FileParser.__init__(self, pathname, filename, **kwargs)
def __init__(self, pathname, filename='*.txt', plugin='', light=True, first=True): """Initialize MetasploitParser. :param str pathname: Path to the report directory. :param str filename: Regex matching the report file. :param str plugin: Name of the plugin that generated the report. :param bool first: Only process first file (``True``) or each file that matched (``False``). """ self.__plugin__ = plugin FileParser.__init__(self, pathname, filename, first=first)
def __init__(self, pathname, filename='*.txt', plugin='', first=True): """Initialize MetasploitParser. :param str pathname: Path to the report directory. :param str filename: Regex matching the report file. :param str plugin: Name of the plugin that generated the report. :param bool first: Only process first file (``True``) or each file that matched (``False``). """ self.__plugin__ = plugin FileParser.__init__(self, pathname, filename, first=first)
def test_parser_file_handle_file_with_content(self, mock_recursive_find): try: str_type = basestring # Python 2.x. except NameError: str_type = str # Python 3.x. with mock.patch.object(builtins, 'open', mock.mock_open(read_data='The cake is a lie!\nThe cake is a lie!')): data = FileParser.handle_file('text.txt') self.assertIsInstance(data, str_type) self.assertTrue(data == 'The cake is a lie!\nThe cake is a lie!')
def handle_file(cls, metadatafile, reportfile): """Process the two report files of the Skipfish report. :param str metadatafile: Path to the metadata file. :param str reportfile: Path to the report file. :raises TypeError: if the files have not the right extension. :raises OSError: if an error occurs when reading the files. :raises IOError: if an error occurs when reading the files. :return: Both metadata and report files' contents. :rtype: :class:`tuple` """ if not metadatafile.endswith(cls.__format__) or not reportfile.endswith(cls.__format__): raise TypeError("This parser only supports '%s' files" % cls.__format__) pathname, filename = os.path.split(metadatafile) metadata_stream = FileParser.handle_file(pathname=pathname, filename=filename) pathname, filename = os.path.split(reportfile) report_stream = FileParser.handle_file(pathname=pathname, filename=filename) return (metadata_stream, report_stream)
def test_parser_file_handle_file_with_content(self, mock_recursive_find): try: str_type = basestring # Python 2.x. except NameError: str_type = str # Python 3.x. with mock.patch.object( builtins, 'open', mock.mock_open( read_data='The cake is a lie!\nThe cake is a lie!')): data = FileParser.handle_file('text.txt') self.assertIsInstance(data, str_type) self.assertTrue(data == 'The cake is a lie!\nThe cake is a lie!')
def test_parser_file_init_with_content(self, mock_recursive_find): try: str_type = basestring # Python 2.x. except NameError: str_type = str # Python 3.x. with mock.patch.object( builtins, 'open', mock.mock_open( read_data='The cake is a lie!\nThe cake is a lie!')): my_fileparser = FileParser(pathname='./', filename='text.txt') self.assertIsInstance(my_fileparser.stream, str_type) self.assertTrue(my_fileparser.stream == 'The cake is a lie!\nThe cake is a lie!')
def handle_file(cls, metadatafile, reportfile): """Process the two report files of the Skipfish report. :param str metadatafile: Path to the metadata file. :param str reportfile: Path to the report file. :raises TypeError: if the files have not the right extension. :raises OSError: if an error occurs when reading the files. :raises IOError: if an error occurs when reading the files. :return: Both metadata and report files' contents. :rtype: :class:`tuple` """ if not metadatafile.endswith( cls.__format__) or not reportfile.endswith(cls.__format__): raise TypeError("This parser only supports '%s' files" % cls.__format__) pathname, filename = os.path.split(metadatafile) metadata_stream = FileParser.handle_file(pathname=pathname, filename=filename) pathname, filename = os.path.split(reportfile) report_stream = FileParser.handle_file(pathname=pathname, filename=filename) return (metadata_stream, report_stream)
def parse_report(self): """Parse the results of the report. :return: List of dicts where each one represents a discovery. :rtype: :class:`list` """ self.vulns = [ {'ranking': self.RANKING_SCALE[vuln.get('severity')]} for vuln in self.stream.findall('.//vulnerability')] if not self.light: try: self.vulns.append({ 'ranking': constants.UNKNOWN, 'transactions': self._parse_report_full(FileParser.handle_file(self.pathname, self.__httpfile_format__))}) except (OSError, IOError): # There is no additional file referencing the HTTP requests. We silently pass. pass return self.vulns
def parse_report(self): """Parse the results of the report. :return: List of dicts where each one represents a discovery. :rtype: :class:`list` """ self.vulns = [{ 'ranking': self.RANKING_SCALE[vuln.get('severity')] } for vuln in self.stream.findall('.//vulnerability')] if not self.light: try: self.vulns.append({ 'ranking': constants.UNKNOWN, 'transactions': self._parse_report_full( FileParser.handle_file(self.pathname, self.__httpfile_format__)) }) except (OSError, IOError): # There is no additional file referencing the HTTP requests. We silently pass. pass return self.vulns
def test_parser_file_handle_file_no_file(self, mock_recursive_find): with self.assertRaises(IOError): FileParser.handle_file('/dev/null')
def test_parser_file_init_no_file(self, mock_recursive_find): with self.assertRaises(IOError): FileParser(pathname='/dev/', filename='null')