Greenbone Vulnerability Management Libraries 22.32.0
json_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 "json.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10#include <stdio.h>
11
12Describe (json);
14{
15}
17{
18}
19
20/* gvm_json_string_escape */
21
22Ensure (json, can_json_escape_strings)
23{
24 const char *unescaped_string = "\"'Abc\\\b\f\n\r\t\001Äöü'\"";
25 const char *escaped_string_dq = "\\\"'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü'\\\"";
26 const char *escaped_string_sq = "\"\\'Abc\\\\\\b\\f\\n\\r\\t\\u0001Äöü\\'\"";
27
28 gchar *escaped_string = NULL;
29 escaped_string = gvm_json_string_escape (NULL, FALSE);
30 assert_that (escaped_string, is_null);
31
32 escaped_string = gvm_json_string_escape (unescaped_string, FALSE);
33 assert_that (escaped_string, is_equal_to_string (escaped_string_dq));
34 g_free (escaped_string);
35
36 escaped_string = gvm_json_string_escape (unescaped_string, TRUE);
37 assert_that (escaped_string, is_equal_to_string (escaped_string_sq));
38 g_free (escaped_string);
39}
40
41/* gvm_json_obj_double */
42
43Ensure (json, gvm_json_obj_double_gets_value)
44{
45 cJSON *json;
46 double d;
47
48 json = cJSON_Parse ("{ \"eg\": 2.3 }");
49 assert_that (json, is_not_null);
50 d = gvm_json_obj_double (json, "eg");
51 assert_that_double (d, is_equal_to_double (2.3));
52 cJSON_Delete (json);
53}
54
55Ensure (json, gvm_json_obj_double_0_when_missing)
56{
57 cJSON *json;
58 double d;
59
60 json = cJSON_Parse ("{ \"eg\": 2.3 }");
61 assert_that (json, is_not_null);
62 d = gvm_json_obj_double (json, "err");
63 assert_that_double (d, is_equal_to_double (0));
64 cJSON_Delete (json);
65}
66
67/* gvm_json_obj_check_str */
68
69Ensure (json, gvm_json_obj_check_str_0_when_has)
70{
71 cJSON *json;
72
73 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
74 assert_that (json, is_not_null);
75 assert_that (gvm_json_obj_check_str (json, "eg", NULL), is_equal_to (0));
76 cJSON_Delete (json);
77}
78
79Ensure (json, gvm_json_obj_check_str_1_when_missing)
80{
81 cJSON *json;
82
83 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
84 assert_that (json, is_not_null);
85 assert_that (gvm_json_obj_check_str (json, "err", NULL), is_equal_to (1));
86 cJSON_Delete (json);
87}
88
89Ensure (json, gvm_json_obj_check_str_1_when_int)
90{
91 cJSON *json;
92
93 json = cJSON_Parse ("{ \"eg\": 29 }");
94 assert_that (json, is_not_null);
95 assert_that (gvm_json_obj_check_str (json, "eg", NULL), is_equal_to (1));
96 cJSON_Delete (json);
97}
98
99Ensure (json, gvm_json_obj_check_str_0_and_val_when_has)
100{
101 cJSON *json;
102 gchar *ret = NULL;
103
104 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
105 assert_that (json, is_not_null);
106 assert_that (gvm_json_obj_check_str (json, "eg", &ret), is_equal_to (0));
107 assert_that (ret, is_equal_to_string ("abc"));
108 cJSON_Delete (json);
109}
110
111/* gvm_json_obj_str */
112
113Ensure (json, gvm_json_obj_str_gets_value)
114{
115 cJSON *json;
116 const gchar *s;
117
118 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
119 assert_that (json, is_not_null);
120 s = gvm_json_obj_str (json, "eg");
121 assert_that (s, is_equal_to_string ("abc"));
122 cJSON_Delete (json);
123}
124
125Ensure (json, gvm_json_obj_str_null_when_missing)
126{
127 cJSON *json;
128 const gchar *s;
129
130 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
131 assert_that (json, is_not_null);
132 s = gvm_json_obj_str (json, "err");
133 assert_that (s, is_null);
134 cJSON_Delete (json);
135}
136
137/* gvm_json_obj_int */
138
139Ensure (json, gvm_json_obj_int_gets_value)
140{
141 cJSON *json;
142 int i;
143
144 json = cJSON_Parse ("{ \"eg\": 33 }");
145 assert_that (json, is_not_null);
146 i = gvm_json_obj_int (json, "eg");
147 assert_that (i, is_equal_to (33));
148 cJSON_Delete (json);
149}
150
151Ensure (json, gvm_json_obj_int_0_when_missing)
152{
153 cJSON *json;
154 int i;
155
156 json = cJSON_Parse ("{ \"eg\": 33 }");
157 assert_that (json, is_not_null);
158 i = gvm_json_obj_int (json, "err");
159 assert_that (i, is_equal_to (0));
160 cJSON_Delete (json);
161}
162
163Ensure (json, gvm_json_obj_int_0_when_str)
164{
165 cJSON *json;
166 int i;
167
168 json = cJSON_Parse ("{ \"eg\": \"abc\" }");
169 assert_that (json, is_not_null);
170 i = gvm_json_obj_int (json, "eg");
171 assert_that (i, is_equal_to (0));
172 cJSON_Delete (json);
173}
174
175/* gvm_json_obj_check_int */
176
177Ensure (json, gvm_json_obj_check_int_0_when_has)
178{
179 cJSON *json;
180
181 json = cJSON_Parse ("{ \"eg\": 33 }");
182 assert_that (json, is_not_null);
183 assert_that (gvm_json_obj_check_int (json, "eg", NULL), is_equal_to (0));
184 cJSON_Delete (json);
185}
186
187Ensure (json, gvm_json_obj_check_int_1_when_missing)
188{
189 cJSON *json;
190
191 json = cJSON_Parse ("{ \"eg\": 33 }");
192 assert_that (json, is_not_null);
193 assert_that (gvm_json_obj_check_int (json, "err", NULL), is_equal_to (1));
194 cJSON_Delete (json);
195}
196
197Ensure (json, gvm_json_obj_check_int_1_when_str)
198{
199 cJSON *json;
200
201 json = cJSON_Parse ("{ \"eg\": \"33\" }");
202 assert_that (json, is_not_null);
203 assert_that (gvm_json_obj_check_int (json, "eg", NULL), is_equal_to (1));
204 cJSON_Delete (json);
205}
206
207Ensure (json, gvm_json_obj_check_int_0_and_val_when_has)
208{
209 cJSON *json;
210 int ret = -1;
211
212 json = cJSON_Parse ("{ \"eg\": 33 }");
213 assert_that (json, is_not_null);
214 assert_that (gvm_json_obj_check_int (json, "eg", &ret), is_equal_to (0));
215 assert_that (ret, is_equal_to (33));
216 cJSON_Delete (json);
217}
218
219int
220main (int argc, char **argv)
221{
222 int ret;
223 TestSuite *suite;
224
225 suite = create_test_suite ();
226
227 add_test_with_context (suite, json, can_json_escape_strings);
228
229 add_test_with_context (suite, json, gvm_json_obj_double_gets_value);
230 add_test_with_context (suite, json, gvm_json_obj_double_0_when_missing);
231
232 add_test_with_context (suite, json, gvm_json_obj_str_gets_value);
233 add_test_with_context (suite, json, gvm_json_obj_str_null_when_missing);
234
235 add_test_with_context (suite, json, gvm_json_obj_int_gets_value);
236 add_test_with_context (suite, json, gvm_json_obj_int_0_when_missing);
237 add_test_with_context (suite, json, gvm_json_obj_int_0_when_str);
238
239 add_test_with_context (suite, json, gvm_json_obj_check_int_0_when_has);
240 add_test_with_context (suite, json, gvm_json_obj_check_int_1_when_missing);
241 add_test_with_context (suite, json, gvm_json_obj_check_int_1_when_str);
242 add_test_with_context (suite, json,
243 gvm_json_obj_check_int_0_and_val_when_has);
244
245 add_test_with_context (suite, json, gvm_json_obj_check_str_0_when_has);
246 add_test_with_context (suite, json, gvm_json_obj_check_str_1_when_missing);
247 add_test_with_context (suite, json, gvm_json_obj_check_str_1_when_int);
248 add_test_with_context (suite, json,
249 gvm_json_obj_check_str_0_and_val_when_has);
250
251 if (argc > 1)
252 ret = run_single_test (suite, argv[1], create_text_reporter ());
253 else
254 ret = run_test_suite (suite, create_text_reporter ());
255
256 destroy_test_suite (suite);
257
258 return ret;
259}
double gvm_json_obj_double(cJSON *obj, const gchar *key)
Get a double field from a JSON object.
Definition json.c:75
int gvm_json_obj_check_int(cJSON *obj, const gchar *key, int *val)
Get an int field from a JSON object.
Definition json.c:97
gchar * gvm_json_string_escape(const char *string, gboolean single_quote)
Escapes a string according to the JSON or JSONPath standard.
Definition json.c:17
int gvm_json_obj_int(cJSON *obj, const gchar *key)
Get an int field from a JSON object.
Definition json.c:120
gchar * gvm_json_obj_str(cJSON *obj, const gchar *key)
Get a string field from a JSON object.
Definition json.c:165
int gvm_json_obj_check_str(cJSON *obj, const gchar *key, gchar **val)
Get a string field from a JSON object.
Definition json.c:142
Describe(json)
AfterEach(json)
Definition json_tests.c:16
int main(int argc, char **argv)
Definition json_tests.c:220
BeforeEach(json)
Definition json_tests.c:13
Ensure(json, can_json_escape_strings)
Definition json_tests.c:22