def test_service(sample): overwrite_results = False # Used temporarily to mass-correct tests cls = ViperMonkey() cls.start() task = Task(create_service_task(sample=sample)) service_request = ServiceRequest(task) cls.execute(service_request) # Get the result of execute() from the test method test_result = task.get_service_result() # Get the assumed "correct" result of the sample correct_path = os.path.join(SELF_LOCATION, "tests", "results", f"{sample}.json") with open(correct_path, "r") as f: correct_result = json.load(f) test_result = generalize_result(test_result) if overwrite_results: if test_result != correct_result: with open(correct_path, "w") as f: json.dump(test_result, f) else: assert test_result == correct_result
def test_execute(class_instance, sample): # Imports required to execute the sample from assemblyline_v4_service.common.task import Task from assemblyline.odm.messages.task import Task as ServiceTask from assemblyline_v4_service.common.request import ServiceRequest # Creating the required objects for execution service_task = ServiceTask(sample1) task = Task(service_task) class_instance._task = task service_request = ServiceRequest(task) # Actually executing the sample class_instance.execute(service_request) # Get the result of execute() from the test method test_result = task.get_service_result() # Get the assumed "correct" result of the sample correct_result_path = os.path.join(TEST_DIR, "results", task.file_name + ".json") with open(correct_result_path, "r") as f: correct_result = json.loads(f.read()) # Assert that the appropriate sections of the dict are equal # Avoiding date in the response test_result_response = test_result.pop("response") correct_result_response = correct_result.pop("response") assert test_result == correct_result # Comparing everything in the response except for the date test_result_response.pop("milestones") correct_result_response.pop("milestones") assert test_result_response == correct_result_response
def test_service(sample): config = helper.get_service_attributes().config cls = emlparser.emlparser.EmlParser(config=config) cls.start() task = Task(create_service_task(sample=sample)) service_request = ServiceRequest(task) cls.execute(service_request) # Get the result of execute() from the test method test_result = task.get_service_result() assert "0766" in test_result["temp_submission_data"]["email_body"]
def test_execute(sample, target): # TODO: Break down the execute method to make it easily testable from assemblyline_v4_service.common.task import Task from assemblyline.odm.messages.task import Task as ServiceTask from assemblyline_v4_service.common.request import ServiceRequest service_task = ServiceTask(sample) task = Task(service_task) target._task = task service_request = ServiceRequest(task) # Actually executing the sample target.execute(service_request) # Get the result of execute() from the test method test_result = task.get_service_result() # Get the assumed "correct" result of the sample correct_result_path = os.path.join(TEST_DIR, "results", task.file_name + ".json") with open(correct_result_path, "r") as f: correct_result = json.loads(f.read()) f.close() # Assert that the appropriate sections of the dict are equal # Avoiding unique items in the response test_result_response = test_result.pop("response") correct_result_response = correct_result.pop("response") assert test_result == correct_result # Comparing everything in the response except for the service_completed and the output.json supplementary test_result_response["milestones"].pop("service_completed") correct_result_response["milestones"].pop("service_completed") correct_result_response.pop("supplementary") test_result_response.pop("supplementary") assert test_result_response == correct_result_response
def test_execute(sample, intezer_static_class_instance, dummy_api_interface_class, dummy_get_response_class, mocker): from assemblyline_v4_service.common.task import Task from assemblyline.odm.messages.task import Task as ServiceTask from assemblyline_v4_service.common.request import ServiceRequest from json import loads from intezer_static import ALIntezerApi mocker.patch.object(intezer_static_class_instance, "get_api_interface", return_value=dummy_api_interface_class) intezer_static_class_instance.start() service_task = ServiceTask(sample) task = Task(service_task) task.service_config = { "analysis_id": "", } intezer_static_class_instance._task = task service_request = ServiceRequest(task) intezer_static_class_instance.config["private_only"] = False mocker.patch.object(ALIntezerApi, "get_latest_analysis", return_value={"analysis_id": "blah"}) mocker.patch.object(ALIntezerApi, "analyze_by_file", return_value="blah") mocker.patch.object(ALIntezerApi, "get_iocs", return_value={ "files": [], "network": [] }) mocker.patch.object(ALIntezerApi, "get_dynamic_ttps", return_value=[]) mocker.patch.object(ALIntezerApi, "get_sub_analyses_by_id", return_value=[]) # Actually executing the sample intezer_static_class_instance.execute(service_request) # Get the result of execute() from the test method test_result = task.get_service_result() # Get the assumed "correct" result of the sample correct_result_path = os.path.join(TEST_DIR, "results", task.file_name + ".json") with open(correct_result_path, "r") as f: correct_result = loads(f.read()) f.close() # Assert that the appropriate sections of the dict are equal # Avoiding unique items in the response test_result_response = test_result.pop("response") correct_result_response = correct_result.pop("response") assert test_result == correct_result # Comparing everything in the response except for the service_completed and the output.json supplementary test_result_response["milestones"].pop("service_completed") correct_result_response["milestones"].pop("service_completed") correct_result_response.pop("supplementary") test_result_response.pop("supplementary") correct_result_response.pop("service_context") test_result_response.pop("service_context") assert test_result_response == correct_result_response # Code coverage task.service_config = { "analysis_id": "blah", } intezer_static_class_instance._task = task service_request = ServiceRequest(task) intezer_static_class_instance.execute(service_request) task.service_config = {"analysis_id": ""} intezer_static_class_instance.config["is_on_premise"] = False mocker.patch.object(ALIntezerApi, "get_latest_analysis", return_value={"verdict": "not_supported"}) mocker.patch.object(ALIntezerApi, "get_dynamic_ttps", return_value=[]) intezer_static_class_instance.execute(service_request) mocker.patch.object(ALIntezerApi, "get_latest_analysis", return_value={"verdict": "failed"}) intezer_static_class_instance.execute(service_request)
def test_service(sample): overwrite_results = False # Used temporarily to mass-correct tests cls = Characterize() cls.start() task = Task(create_service_task(sample=sample)) service_request = ServiceRequest(task) cls.execute(service_request) result_dir_files = [ os.path.basename(x) for x in glob.glob( os.path.join(SELF_LOCATION, "tests", "results", sample, "*")) ] correct_path = os.path.join(SELF_LOCATION, "tests", "results", sample, "features.json") if os.path.exists(correct_path): result_dir_files.remove("features.json") with open(correct_path, "r") as f: correct_result = json.load(f) test_path = os.path.join(cls.working_directory, "features.json") with open(test_path, "r") as f: test_result = json.load(f) if overwrite_results: if test_result != correct_result: with open(correct_path, "w") as f: json.dump(test_result, f) else: assert test_result == correct_result # Get the result of execute() from the test method test_result = task.get_service_result() result_dir_files.remove("result.json") # Get the assumed "correct" result of the sample correct_path = os.path.join(SELF_LOCATION, "tests", "results", sample, "result.json") with open(correct_path, "r") as f: correct_result = json.load(f) test_result = generalize_result(test_result) if overwrite_results: if test_result != correct_result: with open(correct_path, "w") as f: json.dump(test_result, f) else: assert test_result == correct_result for extracted_file in test_result["response"]["extracted"]: if not overwrite_results or extracted_file[ "name"] in result_dir_files: result_dir_files.remove(extracted_file["name"]) correct_path = os.path.join(SELF_LOCATION, "tests", "results", sample, extracted_file["name"]) with open(correct_path, "rb") as f: correct_result = f.read() test_path = os.path.join(cls.working_directory, extracted_file["name"]) with open(test_path, "rb") as f: test_result = f.read() if overwrite_results: if test_result != correct_result: with open(correct_path, "wb") as f: f.write(test_result) else: assert test_result == correct_result assert not result_dir_files