Greenbone Vulnerability Management Libraries 22.32.0
vtparser_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "../base/nvti.h"
7#include "vtparser.c"
8
9#include <cgreen/assertions.h>
10#include <cgreen/cgreen.h>
11#include <cgreen/constraint_syntax_helpers.h>
12#include <cgreen/internal/c_assertions.h>
13#include <cgreen/mocks.h>
14
15#define VT_NAME "Example: Security Advisory for eg (EXAMPLE-2025-cb7b)"
16
17#define VT \
18 "{" \
19 " \"oid\": \"1.3.6.1.4.1.25623.1.0.877440\"," \
20 " \"name\": \"" VT_NAME "\"," \
21 " \"filename\": \"2020/fedora/gb_fedora_2020_cb7b7181a0_sox_fc30.nasl\"," \
22 " \"tag\": {" \
23 " \"affected\": \"'sox' package(s) on Fedora 30.\"," \
24 " \"creation_date\": 1581134661," \
25 " \"cvss_base_vector\": \"AV:N/AC:L/Au:N/C:N/I:N/A:P\"," \
26 " \"insight\": \"SoX (Sound eXchange) is a sound file format...\"," \
27 " \"last_modification\": 1626919250," \
28 " \"qod_type\": \"package\"," \
29 " \"severity_date\": 1624547760," \
30 " \"severity_origin\": \"NVD\"," \
31 " \"severity_vector\": \"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H\"," \
32 " \"solution\": \"Please install the updated package(s).\"," \
33 " \"solution_type\": \"VendorFix\"," \
34 " \"summary\": \"The remote host is missing an update for ...\"" \
35 " }," \
36 " \"dependencies\": [" \
37 " \"gather-package-list.nasl\"" \
38 " ]," \
39 " \"required_keys\": []," \
40 " \"mandatory_keys\": [" \
41 " \"ssh/login/fedora\"," \
42 " \"ssh/login/rpms\"," \
43 " \"ssh/login/release=FC30\"" \
44 " ]," \
45 " \"excluded_keys\": []," \
46 " \"required_ports\": []," \
47 " \"required_udp_ports\": []," \
48 " \"references\": [" \
49 " {" \
50 " \"class\": \"cve\"," \
51 " \"id\": \"CVE-2017-18189\"" \
52 " }," \
53 " {" \
54 " \"class\": \"2020-cb7b7181a0\"," \
55 " \"id\": \"FEDORA\"" \
56 " }," \
57 " {" \
58 " \"class\": \"https://example.org/ann/EG-IZ3CX\"," \
59 " \"id\": \"URL\"" \
60 " }" \
61 " ]," \
62 " \"preferences\": []," \
63 " \"category\": \"gather_info\"," \
64 " \"family\": \"Fedora Local Security Checks\"" \
65 " }"
66
67Describe (vtparser);
68BeforeEach (vtparser)
69{
70}
71
72AfterEach (vtparser)
73{
74}
75
76static FILE *
77memopen (const gchar *str)
78{
79 return fmemopen ((void *) str, strlen (str), "r");
80}
81
82/* parse_vt_json */
83
84Ensure (vtparser, parse_vt_json_parses_a_vt)
85{
88 FILE *file;
89 nvti_t *nvt;
90 gchar *refs;
91
92 file = memopen ("[" VT "]");
93 assert_that (file, is_not_null);
94
96
97 gvm_json_pull_parser_init (&parser, file);
98
99 gvm_json_pull_parser_next (&parser, &event);
100
101 parse_vt_json (&parser, &event, &nvt);
102 assert_that (nvt, is_not_null);
103 assert_that (nvti_name (nvt), is_equal_to_string (VT_NAME));
104 refs = nvti_refs (nvt, NULL, NULL, 1);
105 assert_that (refs,
106 is_equal_to_string ("cve:CVE-2017-18189,"
107 " 2020-cb7b7181a0:FEDORA,"
108 " https://example.org/ann/EG-IZ3CX:URL"));
109 g_free (refs);
110 nvti_free (nvt);
111
114 fclose (file);
115}
116
117/* Test suite. */
118
119int
120main (int argc, char **argv)
121{
122 int ret;
123 TestSuite *suite;
124
125 suite = create_test_suite ();
126
127 add_test_with_context (suite, vtparser, parse_vt_json_parses_a_vt);
128
129 if (argc > 1)
130 ret = run_single_test (suite, argv[1], create_text_reporter ());
131 else
132 ret = run_test_suite (suite, create_text_reporter ());
133
134 destroy_test_suite (suite);
135
136 return ret;
137}
void gvm_json_pull_parser_cleanup(gvm_json_pull_parser_t *parser)
Frees the data of a JSON pull parser.
Definition jsonpull.c:120
void gvm_json_pull_event_cleanup(gvm_json_pull_event_t *event)
Frees all data of JSON pull event data structure.
Definition jsonpull.c:61
void gvm_json_pull_parser_next(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Get the next event from a JSON pull parser.
Definition jsonpull.c:669
void gvm_json_pull_parser_init(gvm_json_pull_parser_t *parser, FILE *input_stream)
Initializes a JSON pull parser with default buffer sizes.
Definition jsonpull.c:109
void gvm_json_pull_event_init(gvm_json_pull_event_t *event)
Initializes a JSON pull event data structure.
Definition jsonpull.c:50
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:766
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:625
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:570
Protos and data structures for NVT Information data sets.
Event generated by the JSON pull parser.
Definition jsonpull.h:59
A json pull parser.
Definition jsonpull.h:86
Simple JSON reader.
int parse_vt_json(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, nvti_t **nvt)
Parse a VT element given in json format.
Definition vtparser.c:239
#define VT
AfterEach(vtparser)
int main(int argc, char **argv)
static FILE * memopen(const gchar *str)
Ensure(vtparser, parse_vt_json_parses_a_vt)
BeforeEach(vtparser)
#define VT_NAME
Describe(vtparser)