Greenbone Vulnerability Management Libraries 22.32.0
json_tests.c File Reference
#include "json.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <stdio.h>
Include dependency graph for json_tests.c:

Go to the source code of this file.

Functions

 Describe (json)
 BeforeEach (json)
 AfterEach (json)
 Ensure (json, can_json_escape_strings)
 Ensure (json, gvm_json_obj_double_gets_value)
 Ensure (json, gvm_json_obj_double_0_when_missing)
 Ensure (json, gvm_json_obj_check_str_0_when_has)
 Ensure (json, gvm_json_obj_check_str_1_when_missing)
 Ensure (json, gvm_json_obj_check_str_1_when_int)
 Ensure (json, gvm_json_obj_check_str_0_and_val_when_has)
 Ensure (json, gvm_json_obj_str_gets_value)
 Ensure (json, gvm_json_obj_str_null_when_missing)
 Ensure (json, gvm_json_obj_int_gets_value)
 Ensure (json, gvm_json_obj_int_0_when_missing)
 Ensure (json, gvm_json_obj_int_0_when_str)
 Ensure (json, gvm_json_obj_check_int_0_when_has)
 Ensure (json, gvm_json_obj_check_int_1_when_missing)
 Ensure (json, gvm_json_obj_check_int_1_when_str)
 Ensure (json, gvm_json_obj_check_int_0_and_val_when_has)
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( json )

Definition at line 16 of file json_tests.c.

17{
18}

◆ BeforeEach()

BeforeEach ( json )

Definition at line 13 of file json_tests.c.

14{
15}

◆ Describe()

Describe ( json )

◆ Ensure() [1/16]

Ensure ( json ,
can_json_escape_strings  )

Definition at line 22 of file json_tests.c.

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}
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

References gvm_json_string_escape().

Here is the call graph for this function:

◆ Ensure() [2/16]

Ensure ( json ,
gvm_json_obj_check_int_0_and_val_when_has  )

Definition at line 207 of file json_tests.c.

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}
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

References gvm_json_obj_check_int().

Here is the call graph for this function:

◆ Ensure() [3/16]

Ensure ( json ,
gvm_json_obj_check_int_0_when_has  )

Definition at line 177 of file json_tests.c.

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}

References gvm_json_obj_check_int().

Here is the call graph for this function:

◆ Ensure() [4/16]

Ensure ( json ,
gvm_json_obj_check_int_1_when_missing  )

Definition at line 187 of file json_tests.c.

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}

References gvm_json_obj_check_int().

Here is the call graph for this function:

◆ Ensure() [5/16]

Ensure ( json ,
gvm_json_obj_check_int_1_when_str  )

Definition at line 197 of file json_tests.c.

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}

References gvm_json_obj_check_int().

Here is the call graph for this function:

◆ Ensure() [6/16]

Ensure ( json ,
gvm_json_obj_check_str_0_and_val_when_has  )

Definition at line 99 of file json_tests.c.

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}
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

References gvm_json_obj_check_str().

Here is the call graph for this function:

◆ Ensure() [7/16]

Ensure ( json ,
gvm_json_obj_check_str_0_when_has  )

Definition at line 69 of file json_tests.c.

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}

References gvm_json_obj_check_str().

Here is the call graph for this function:

◆ Ensure() [8/16]

Ensure ( json ,
gvm_json_obj_check_str_1_when_int  )

Definition at line 89 of file json_tests.c.

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}

References gvm_json_obj_check_str().

Here is the call graph for this function:

◆ Ensure() [9/16]

Ensure ( json ,
gvm_json_obj_check_str_1_when_missing  )

Definition at line 79 of file json_tests.c.

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}

References gvm_json_obj_check_str().

Here is the call graph for this function:

◆ Ensure() [10/16]

Ensure ( json ,
gvm_json_obj_double_0_when_missing  )

Definition at line 55 of file json_tests.c.

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}
double gvm_json_obj_double(cJSON *obj, const gchar *key)
Get a double field from a JSON object.
Definition json.c:75

References gvm_json_obj_double().

Here is the call graph for this function:

◆ Ensure() [11/16]

Ensure ( json ,
gvm_json_obj_double_gets_value  )

Definition at line 43 of file json_tests.c.

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}

References gvm_json_obj_double().

Here is the call graph for this function:

◆ Ensure() [12/16]

Ensure ( json ,
gvm_json_obj_int_0_when_missing  )

Definition at line 151 of file json_tests.c.

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}
int gvm_json_obj_int(cJSON *obj, const gchar *key)
Get an int field from a JSON object.
Definition json.c:120

References gvm_json_obj_int().

Here is the call graph for this function:

◆ Ensure() [13/16]

Ensure ( json ,
gvm_json_obj_int_0_when_str  )

Definition at line 163 of file json_tests.c.

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}

References gvm_json_obj_int().

Here is the call graph for this function:

◆ Ensure() [14/16]

Ensure ( json ,
gvm_json_obj_int_gets_value  )

Definition at line 139 of file json_tests.c.

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}

References gvm_json_obj_int().

Here is the call graph for this function:

◆ Ensure() [15/16]

Ensure ( json ,
gvm_json_obj_str_gets_value  )

Definition at line 113 of file json_tests.c.

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}
gchar * gvm_json_obj_str(cJSON *obj, const gchar *key)
Get a string field from a JSON object.
Definition json.c:165

References gvm_json_obj_str().

Here is the call graph for this function:

◆ Ensure() [16/16]

Ensure ( json ,
gvm_json_obj_str_null_when_missing  )

Definition at line 125 of file json_tests.c.

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}

References gvm_json_obj_str().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 220 of file json_tests.c.

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}