Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: first test of vtparser.c #888

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions openvasd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ endif (BUILD_SHARED)
if (BUILD_TESTS)
add_unit_test (openvasd-test openvasd_tests.c gvm_base_shared gvm_util_shared
${GLIB_LDFLAGS} ${CJSON_LDFLAGS} ${CURL_LDFLAGS} ${LINKER_HARDENING_FLAGS})

add_unit_test (vtparser-test vtparser_tests.c gvm_base_shared gvm_util_shared
${GLIB_LDFLAGS} ${CJSON_LDFLAGS} ${CURL_LDFLAGS} ${LINKER_HARDENING_FLAGS})
endif (BUILD_TESTS)

## Install
Expand Down
126 changes: 126 additions & 0 deletions openvasd/vtparser_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "vtparser.c"
#include "../base/nvti.h"

#include <cgreen/assertions.h>
#include <cgreen/cgreen.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/internal/c_assertions.h>
#include <cgreen/mocks.h>

#define VT_NAME "Example: Security Advisory for eg (EXAMPLE-2025-cb7b)"

#define VT "{" \
" \"oid\": \"1.3.6.1.4.1.25623.1.0.877440\"," \
" \"name\": \"" VT_NAME "\"," \
" \"filename\": \"2020/fedora/gb_fedora_2020_cb7b7181a0_sox_fc30.nasl\"," \
" \"tag\": {" \
" \"affected\": \"'sox' package(s) on Fedora 30.\"," \
" \"creation_date\": 1581134661," \
" \"cvss_base_vector\": \"AV:N/AC:L/Au:N/C:N/I:N/A:P\"," \
" \"insight\": \"SoX (Sound eXchange) is a sound file format...\"," \
" \"last_modification\": 1626919250," \
" \"qod_type\": \"package\"," \
" \"severity_date\": 1624547760," \
" \"severity_origin\": \"NVD\"," \
" \"severity_vector\": \"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H\"," \
" \"solution\": \"Please install the updated package(s).\"," \
" \"solution_type\": \"VendorFix\"," \
" \"summary\": \"The remote host is missing an update for ...\"" \
" }," \
" \"dependencies\": [" \
" \"gather-package-list.nasl\"" \
" ]," \
" \"required_keys\": []," \
" \"mandatory_keys\": [" \
" \"ssh/login/fedora\"," \
" \"ssh/login/rpms\"," \
" \"ssh/login/release=FC30\"" \
" ]," \
" \"excluded_keys\": []," \
" \"required_ports\": []," \
" \"required_udp_ports\": []," \
" \"references\": [" \
" {" \
" \"class\": \"cve\"," \
" \"id\": \"CVE-2017-18189\"" \
" }," \
" {" \
" \"class\": \"2020-cb7b7181a0\"," \
" \"id\": \"FEDORA\"" \
" }," \
" {" \
" \"class\": \"https://example.org/ann/EG-IZ3CX\"," \
" \"id\": \"URL\"" \
" }" \
" ]," \
" \"preferences\": []," \
" \"category\": \"gather_info\"," \
" \"family\": \"Fedora Local Security Checks\"" \
" }"

Describe (vtparser);
BeforeEach (vtparser)
{
}

AfterEach (vtparser)
{
}

static FILE *
memopen (const gchar *str) {
return fmemopen ((void *) str, strlen (str), "r");
}

/* openvasd_parse_vt */

Ensure (vtparser, openvasd_parse_vt_parses_a_vt)
{
gvm_json_pull_parser_t parser;
gvm_json_pull_event_t event;
FILE *file;
nvti_t *nvt;

file = memopen ("[" VT "]");
assert_that (file, is_not_null);

gvm_json_pull_event_init (&event);

gvm_json_pull_parser_init (&parser, file);

gvm_json_pull_parser_next (&parser, &event);

nvt = openvasd_parse_vt (&parser, &event);
assert_that (nvt, is_not_null);
assert_that (nvti_name (nvt), is_equal_to_string (VT_NAME));
assert_that (nvti_refs (nvt, NULL, NULL, 1),
is_equal_to_string ("cve:CVE-2017-18189,"
" 2020-cb7b7181a0:FEDORA,"
" https://example.org/ann/EG-IZ3CX:URL"));

gvm_json_pull_event_cleanup (&event);
gvm_json_pull_parser_cleanup (&parser);
fclose (file);
}

/* Test suite. */

int
main (int argc, char **argv)
{
TestSuite *suite;

suite = create_test_suite ();

add_test_with_context (suite, vtparser, openvasd_parse_vt_parses_a_vt);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());

Check warning on line 123 in openvasd/vtparser_tests.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser_tests.c#L123

Added line #L123 was not covered by tests

return run_test_suite (suite, create_text_reporter ());
}