From 2b06d202cec10714c6cfd56be5d35dfce045817f Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Sat, 11 Jan 2025 19:06:06 +0200 Subject: [PATCH] Add test of openvasd_parsed_results with host detail --- CMakeLists.txt | 3 +- openvasd/CMakeLists.txt | 19 ++++++++++ openvasd/openvasd.c | 36 +++++++++++------- openvasd/openvasd_tests.c | 78 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 openvasd/openvasd_tests.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e3078b25..c3eb98f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,7 +233,8 @@ if (BUILD_TESTS AND NOT SKIP_SRC) DEPENDS array-test alivedetection-test boreas_error-test boreas_io-test cli-test cpeutils-test cvss-test ping-test sniffer-test util-test networking-test passwordbasedauthentication-test xmlutils-test version-test versionutils-test - osp-test nvti-test hosts-test jsonpull-test compressutils-test) + osp-test nvti-test hosts-test jsonpull-test compressutils-test + openvasd-test) endif (BUILD_TESTS AND NOT SKIP_SRC) diff --git a/openvasd/CMakeLists.txt b/openvasd/CMakeLists.txt index 88e9bd27..8011ad7d 100644 --- a/openvasd/CMakeLists.txt +++ b/openvasd/CMakeLists.txt @@ -44,6 +44,25 @@ if (BUILD_SHARED) ${CURL_LDFLAGS} ${LINKER_HARDENING_FLAGS}) endif (BUILD_SHARED) +## Tests + +if (BUILD_TESTS) + add_executable (openvasd-test + EXCLUDE_FROM_ALL + openvasd_tests.c ../base/array.c ../base/networking.c) + + add_test (openvasd-test openvasd-test) + + target_include_directories (openvasd-test PRIVATE ${CGREEN_INCLUDE_DIRS}) + + target_link_libraries (openvasd-test ${CGREEN_LIBRARIES} + ${GLIB_LDFLAGS} ${CJSON_LDFLAGS} ${CURL_LDFLAGS} + ${LINKER_HARDENING_FLAGS}) + + add_custom_target (tests-openvasd + DEPENDS openvasd-test) +endif (BUILD_TESTS) + ## Install configure_file (libgvm_openvasd.pc.in ${CMAKE_BINARY_DIR}/libgvm_openvasd.pc @ONLY) diff --git a/openvasd/openvasd.c b/openvasd/openvasd.c index 63a7cbb9..b461de52 100644 --- a/openvasd/openvasd.c +++ b/openvasd/openvasd.c @@ -1086,14 +1086,12 @@ openvasd_result_free (openvasd_result_t result) result = NULL; } -int -openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, - unsigned long last, GSList **results) +static int +parse_results (const gchar *body, GSList **results) { cJSON *parser = NULL; cJSON *result_obj = NULL; const gchar *err = NULL; - openvasd_resp_t resp = NULL; openvasd_result_t result = NULL; unsigned long id = 0; gchar *type = NULL; @@ -1110,12 +1108,7 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, gchar *detail_source_description = NULL; int ret = -1; - resp = openvasd_get_scan_results (conn, first, last); - - if (resp->code != 200) - return resp->code; - - if ((parser = cJSON_Parse (resp->body)) == NULL) + if ((parser = cJSON_Parse (body)) == NULL) { err = cJSON_GetErrorPtr (); goto res_cleanup; @@ -1203,11 +1196,10 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, detail_source_description); *results = g_slist_append (*results, result); - ret = resp->code; + ret = 200; } -res_cleanup: - openvasd_response_cleanup (resp); + res_cleanup: if (err != NULL) { g_warning ("%s: Unable to parse scan results. Reason: %s", __func__, err); @@ -1217,6 +1209,24 @@ openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, return ret; } +int +openvasd_parsed_results (openvasd_connector_t conn, unsigned long first, + unsigned long last, GSList **results) +{ + int ret; + openvasd_resp_t resp; + + resp = openvasd_get_scan_results (conn, first, last); + if (resp->code == 200) + ret = parse_results (resp->body, results); + else + ret = resp->code; + + openvasd_response_cleanup (resp); + + return ret; +} + openvasd_resp_t openvasd_get_scan_status (openvasd_connector_t conn) { diff --git a/openvasd/openvasd_tests.c b/openvasd/openvasd_tests.c new file mode 100644 index 00000000..5a5c1190 --- /dev/null +++ b/openvasd/openvasd_tests.c @@ -0,0 +1,78 @@ +/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "openvasd.c" + +#include +#include + +Describe (openvasd); +BeforeEach (openvasd) +{ +} + +AfterEach (openvasd) +{ +} + +/* parse_results */ + +Ensure (openvasd, parse_results_handles_details) +{ + const gchar *str; + GSList *results; + openvasd_result_t result; + + + results = NULL; + + str = "[ {" + " \"id\": 16," + " \"type\": \"host_detail\"," + " \"ip_address\": \"192.168.0.101\"," + " \"hostname\": \"g\"," + " \"oid\": \"1.3.6.1.4.1.25623.1.0.103997\"," + " \"message\": \"MAC94:E6:F7:67:4B:C0nvt1.3.6.1.4.1.25623.1.0.103585Nmap MAC Scan\"," + " \"detail\": {" + " \"name\": \"MAC\"," + " \"value\": \"00:1A:2B:3C:4D:5E\"," + " \"source\": {" + " \"type\": \"nvt\"," + " \"name\": \"1.3.6.1.4.1.25623.1.0.103585\"," + " \"description\": \"Nmap MAC Scan\"" + " }" + " }" + "} ]"; + + parse_results (str, &results); + + assert_that (g_slist_length (results), is_equal_to (1)); + + result = results->data; + assert_that (result->detail_name, is_equal_to_string ("MAC")); + assert_that (result->detail_value, is_equal_to_string ("00:1A:2B:3C:4D:5E")); + assert_that (result->detail_source_type, is_equal_to_string ("nvt")); + assert_that (result->detail_source_name, is_equal_to_string ("1.3.6.1.4.1.25623.1.0.103585")); + assert_that (result->detail_source_description, is_equal_to_string ("Nmap MAC Scan")); + + if (g_slist_length (results)) + g_slist_free_full (results, (GDestroyNotify) openvasd_result_free); +} + +/* Test suite. */ +int +main (int argc, char **argv) +{ + TestSuite *suite; + + suite = create_test_suite (); + + add_test_with_context (suite, openvasd, parse_results_handles_details); + + if (argc > 1) + return run_single_test (suite, argv[1], create_text_reporter ()); + + return run_test_suite (suite, create_text_reporter ()); +}