Skip to content

Commit

Permalink
Add test of openvasd_parsed_results with host detail
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell committed Jan 11, 2025
1 parent 8be2ab9 commit 2b06d20
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions openvasd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 23 additions & 13 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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,

Check warning on line 1213 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1213

Added line #L1213 was not covered by tests
unsigned long last, GSList **results)
{
int ret;
openvasd_resp_t resp;

resp = openvasd_get_scan_results (conn, first, last);

Check warning on line 1219 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1219

Added line #L1219 was not covered by tests
if (resp->code == 200)
ret = parse_results (resp->body, results);

Check warning on line 1221 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1221

Added line #L1221 was not covered by tests
else
ret = resp->code;

Check warning on line 1223 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1223

Added line #L1223 was not covered by tests

openvasd_response_cleanup (resp);

Check warning on line 1225 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1225

Added line #L1225 was not covered by tests

return ret;

Check warning on line 1227 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1227

Added line #L1227 was not covered by tests
}

openvasd_resp_t
openvasd_get_scan_status (openvasd_connector_t conn)
{
Expand Down
78 changes: 78 additions & 0 deletions openvasd/openvasd_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "openvasd.c"

#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>

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\": \"<host><detail><name>MAC</name><value>94:E6:F7:67:4B:C0</value><source><type>nvt</type><name>1.3.6.1.4.1.25623.1.0.103585</name><description>Nmap MAC Scan</description></source></detail></host>\","
" \"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 ());

Check warning on line 75 in openvasd/openvasd_tests.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd_tests.c#L75

Added line #L75 was not covered by tests

return run_test_suite (suite, create_text_reporter ());
}

0 comments on commit 2b06d20

Please sign in to comment.