def test_write_results_csv() -> None: """ Test if we can successfully save our results somewhere in CSV :param get_results: results to save :return: None """ filemanager = GrinderFileManager() filemanager.write_results_csv( results_to_write=get_results, dest_dir=str(TestDefaultFilemanagerValues.TEST_RESULTS_PATH), csv_dir=str(Path("filemanager").joinpath("csv")), csv_file=TestDefaultFilemanagerValues.TEST_RESULTS_CSV_FILE, ) assert TestDefaultFilemanagerValues.TEST_RESULTS_CSV_FILE in listdir( TestDefaultFilemanagerValues.TEST_RESULTS_PATH_CSV ) with open( TestDefaultFilemanagerValues.TEST_RESULTS_PATH_CSV.joinpath( TestDefaultFilemanagerValues.TEST_RESULTS_CSV_FILE ), mode="r", ) as result_file: file_contains = result_file.read().split("\n") assert ( file_contains[0] == "product,vendor,query,port,proto,ip,lat,lng,country,vulnerabilities,nmap_scan,scripts" ) assert file_contains[-1] == "" for line in file_contains[1:-1]: for field in line.split(","): assert isinstance(field, str) and field != ""
def test_write_results_csv_exploits_to_cve_success() -> None: """ Test how we can save exploits info to csv :return: None """ with open( TestDefaultFilemanagerValues.TEST_DATA_RESULTS_JSON_PATH.joinpath( TestDefaultFilemanagerValues.TEST_EXPLOITS_JSON_FILE ), mode="r", ) as vulners_results: vulners_by_vulnerabilities = load(vulners_results) with open( TestDefaultFilemanagerValues.TEST_DATA_RESULTS_JSON_PATH.joinpath( "results.json" ), mode="r", ) as hosts_results: hosts = load(hosts_results) hosts = {host.get("ip"): host for host in hosts} filemanager = GrinderFileManager() filemanager.write_results_csv_exploits_to_cve( results_to_write=vulners_by_vulnerabilities, hosts_results=hosts, dest_dir=str(TestDefaultFilemanagerValues.TEST_RESULTS_PATH), csv_dir=str(Path("filemanager").joinpath("csv")), csv_file=TestDefaultFilemanagerValues.TEST_EXPLOITS_CSV_FILE, )
def save_pie_chart(self, relative_path: str, filename: str) -> None: filemanager = GrinderFileManager() filemanager.write_results_png( self.plot, png_file=filename, dest_dir=DefaultValues.RESULTS_DIRECTORY, sub_dir=relative_path, )
def test_filemanager_get_queries_error() -> None: """ Check if we can properly handle errors in case of unsuccessful query file loading :return: None """ filemanager = GrinderFileManager() with raises(GrinderFileManagerOpenError): filemanager.get_queries(queries_file="not_exists.json")
def test_load_data_from_file_error() -> None: """ Check how we handle errors in case when file can't be loaded or parsed :return: None """ filemanager = GrinderFileManager() with raises(GrinderFileManagerOpenError): filemanager.load_data_from_file( load_dir="totally", load_json_dir="wrong", load_file="path.json" )
def test_filemanager_get_malformed_queries() -> None: """ Check case when input json is exists, but file itself is malformed or contains some unparseable constructions :return: None """ filemanager = GrinderFileManager() with raises(GrinderFileManagerJsonDecoderError): filemanager.get_queries(queries_file=str( TestDefaultFilemanagerValues.TEST_DATA_QUERIES_PATH.joinpath( TestDefaultFilemanagerValues.TEST_MALFORMED_FILE)))
def test_csv_dict_to_fix() -> None: """ Test additional filemanager convertor :return: None """ filemanager = GrinderFileManager() results_to_write = {"one": 1, "two": 2, "three": 3} res = filemanager.csv_dict_fix(results_to_write, "test.csv") assert res == [ {"test": "one", "count": 1}, {"test": "two", "count": 2}, {"test": "three", "count": 3}, ]
def test_load_data_from_malformed_file_error() -> None: """ Check case when JSON is malformed but needed to be loaded :return: None """ filemanager = GrinderFileManager() with raises(GrinderFileManagerJsonDecoderError): filemanager.load_data_from_file( load_dir=str(TestDefaultFilemanagerValues.TEST_DATA_PATH), load_json_dir="test_queries", load_file=TestDefaultFilemanagerValues.TEST_MALFORMED_FILE, )
def test_load_data_from_file_success() -> None: """ Originally this function is used to load results file, but in case of functionality we can check that this function can load any other json file (because results - is just dumped dicts) :return: None """ filemanager = GrinderFileManager() queries = filemanager.load_data_from_file( load_dir=str(TestDefaultFilemanagerValues.TEST_DATA_PATH), load_json_dir="test_queries", load_file=TestDefaultFilemanagerValues.TEST_QUERIES_FILE, ) for query_obj in queries: assert sorted(query_obj.keys()) == sorted( [ "vendor", "product", "shodan_queries", "censys_queries", "scripts", "vendor_confidence", ] ) assert ( isinstance(query_obj.get("vendor"), str) and query_obj.get("vendor") != "" ) assert ( isinstance(query_obj.get("product"), str) and query_obj.get("product") != "" ) assert ( isinstance(query_obj.get("shodan_queries"), list) and query_obj.get("shodan_queries") != [] ) for query in query_obj.get("shodan_queries"): assert sorted(query.keys()) == sorted(["query", "query_confidence"]) assert ( isinstance(query_obj.get("censys_queries"), list) and query_obj.get("censys_queries") != [] ) for query in query_obj.get("censys_queries"): assert sorted(query.keys()) == sorted(["query", "query_confidence"]) assert sorted(query_obj.get("scripts").keys()) == sorted( ["py_script", "nse_script"] ) assert ( isinstance(query_obj.get("vendor_confidence"), str) and query_obj.get("vendor_confidence") != "" )
def test_filemanager_get_queries_success() -> None: """ Check if we can successfully get query file from queries/* directory :return: None """ filemanager = GrinderFileManager() queries = filemanager.get_queries( queries_file=str( TestDefaultFilemanagerValues.TEST_DATA_QUERIES_PATH.joinpath( TestDefaultFilemanagerValues.TEST_QUERIES_FILE ) ) ) for query_obj in queries: assert sorted(query_obj.keys()) == sorted( [ "vendor", "product", "shodan_queries", "censys_queries", "scripts", "vendor_confidence", ] ) assert ( isinstance(query_obj.get("vendor"), str) and query_obj.get("vendor") != "" ) assert ( isinstance(query_obj.get("product"), str) and query_obj.get("product") != "" ) assert ( isinstance(query_obj.get("shodan_queries"), list) and query_obj.get("shodan_queries") != [] ) for query in query_obj.get("shodan_queries"): assert sorted(query.keys()) == sorted(["query", "query_confidence"]) assert ( isinstance(query_obj.get("censys_queries"), list) and query_obj.get("censys_queries") != [] ) for query in query_obj.get("censys_queries"): assert sorted(query.keys()) == sorted(["query", "query_confidence"]) assert sorted(query_obj.get("scripts").keys()) == sorted( ["py_script", "nse_script"] ) assert ( isinstance(query_obj.get("vendor_confidence"), str) and query_obj.get("vendor_confidence") != "" )
def test_write_results_csv_exploits_to_cve_error() -> None: """ Test how we can handle errors when operating with exploits and CVEs in file cases :return: None """ filemanager = GrinderFileManager() with raises(GrinderFileManagerOpenError): filemanager.write_results_csv_exploits_to_cve( hosts_results={"test": "test"}, results_to_write=["test", "test"], dest_dir=str(TestDefaultFilemanagerValues.TEST_RESULTS_PATH), csv_dir=str(Path("filemanager").joinpath("csv")), csv_file="malformed_exploits.csv")
def save_pie_chart(self, relative_path: str, filename: str) -> None: """ Save pie chart with filemanager in png :param relative_path: subdirectory to save :param filename: filename to save :return: None """ filemanager = GrinderFileManager() if self.plot: filemanager.write_results_png( self.plot, png_file=filename, dest_dir=DefaultValues.RESULTS_DIRECTORY, sub_dir=relative_path, ) self.plot.close()
def test_write_results_json() -> None: """ Test if we can successfully save our results somewhere in JSON :param get_results: results to save :return: None """ filemanager = GrinderFileManager() filemanager.write_results_json( results_to_write=get_results, dest_dir=str(TestDefaultFilemanagerValues.TEST_RESULTS_PATH), json_dir=str(Path("filemanager").joinpath("json")), json_file=TestDefaultFilemanagerValues.TEST_RESULTS_JSON_FILE, ) assert TestDefaultFilemanagerValues.TEST_RESULTS_JSON_FILE in listdir( TestDefaultFilemanagerValues.TEST_RESULTS_PATH_JSON) with open( TestDefaultFilemanagerValues.TEST_RESULTS_PATH_JSON.joinpath( TestDefaultFilemanagerValues.TEST_RESULTS_JSON_FILE), mode="r", ) as result_file: assert load(result_file) == get_results