Debian Codegraph @

src/common/test-jsonprint.c

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitjsonprint.h"
9
10 #include "testlib/cockpittest.h"
11
12 #include <glib.h>
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
18
19 /* --- testing of printing --- */
20
21 typedef struct
22 {
23 char buffer[1024];
24 FILE *stream;
25
26 char expected_buffer[1024];
27 gchar *expected_end;
28
29 size_t pagesize;
30 char *accessible;
31 char *inaccessible;
32 } TestFixture;
33
34 static void
35 test_fixture_setup (TestFixture *fixture,
36 gconstpointer user_data)
37 {
38 /* set up the stream */
39 fixture->stream = fmemopen (fixture->buffer, sizeof fixture->buffer, "w");
40 setbuf (fixture->stream, NULL);
41 rewind (fixture->stream);
42
43 /* create a range of accessible bytes surrounded by memory that will
44 * cause a crash if accessed.
45 */
46 fixture->pagesize = sysconf(_SC_PAGE_SIZE);
47
48 /* allocate 3 pages of memory that will crash when accessed (PROT_NONE) */
49 char *region = mmap (NULL, 3 * fixture->pagesize, PROT_NONE,
50 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
51 g_assert (region != MAP_FAILED);
52
53 /* punch a read/writable hole in the middle of the 3 pages */
54 int r = mprotect (region + fixture->pagesize, fixture->pagesize, PROT_READ | PROT_WRITE);
55 g_assert (r == 0);
56
57 /* record the first and last(+1) accessible bytes. accessing the
58 * bytes immediately outside of this range is guaranteed to crash.
59 * this allows us to ensure proper memory behaviour of the code we're
60 * testing.
61 */
62 fixture->accessible = region + fixture->pagesize;
63 fixture->inaccessible = fixture->accessible + fixture->pagesize;
64
65 /* setup the expected buffer, empty to start */
66 fixture->expected_end = fixture->expected_buffer;
67 }
68
69 static void
70 test_fixture_teardown (TestFixture *fixture,
71 gconstpointer user_data)
72 {
73 char *region = fixture->accessible - fixture->pagesize;
74 munmap (region, 3 * fixture->pagesize);
75 fixture->accessible = NULL;
76 fixture->inaccessible = NULL;
77
78 fixture->expected_end = NULL;
79
80 fclose (fixture->stream);
81 fixture->stream = NULL;
82 }
83
84 static void
85 test_fixture_expect (TestFixture *fixture,
86 const gchar *expected)
87 {
88 fixture->expected_end = stpcpy (fixture->expected_end, expected);
89 }
90
91 static void
92 test_fixture_compare_expected (TestFixture *fixture)
93 {
94 /* Ensure that both strings are nul terminated */
95 fixture->buffer[ftell (fixture->stream)] = '\0';
96 *fixture->expected_end = '\0';
97
98 /* Ensure that neither string has embedded nuls */
99 g_assert_cmpint (strlen (fixture->buffer), ==, ftell (fixture->stream));
100 g_assert (strchr (fixture->expected_buffer, '\0') == fixture->expected_end);
101
102 /* Compare! */
103 g_assert_cmpstr (fixture->buffer, ==, fixture->expected_buffer);
104 }
105
106 static void
107 test_print_string (TestFixture *fixture,
108 gconstpointer user_data)
109 {
110 /* create a string with every possible byte in it and check that
111 * everything is correctly escaped
112 */
113 char buffer[256];
114 int offset = 0;
115 char tmp[10];
116
117 test_fixture_expect (fixture, ", \"key\": \"");
118
119 for (int c = 1; c < 32; c++) /* control characters (before space) */
120 {
121 /* ascii control characters printed as unicode escapes */
122 snprintf (tmp, sizeof tmp, "\\u%04x", c);
123 test_fixture_expect (fixture, tmp);
124
125 buffer[offset++] = c;
126 }
127
128 test_fixture_expect (fixture, " !");
129 buffer[offset++] = 32; /* space */
130 buffer[offset++] = 33; /* ! */
131
132 test_fixture_expect (fixture, "\\\"");
133 buffer[offset++] = 34; /* " */
134
135 for (int c = 35; c < 92; c++) /* # through [ */
136 {
137 snprintf (tmp, sizeof tmp, "%c", c);
138 test_fixture_expect (fixture, tmp);
139 buffer[offset++] = c;
140 }
141
142 test_fixture_expect (fixture, "\\\\");
143 buffer[offset++] = 92; /* \ */
144
145 for (int c = 93; c < 127; c++) /* ] through ~ */
146 {
147 snprintf (tmp, sizeof tmp, "%c", c);
148 test_fixture_expect (fixture, tmp);
149 buffer[offset++] = c;
150 }
151
152 test_fixture_expect (fixture, "\\u007f");
153 buffer[offset++] = 127; /* DEL */
154
155 for (int c = 128; c < 256; c++) /* non-ascii */
156 {
157 test_fixture_expect (fixture, "?");
158 buffer[offset++] = c;
159 }
160
161 buffer[offset++] = '\0';
162 g_assert_cmpint (offset, ==, sizeof buffer);
163 test_fixture_expect (fixture, "\"");
164
165 /* print it with -1, correct length, and "too big" length */
166 cockpit_json_print_string_property (fixture->stream, "key", buffer, -1);
167 test_fixture_compare_expected (fixture);
168 rewind (fixture->stream);
169
170 cockpit_json_print_string_property (fixture->stream, "key", buffer, 255);
171 test_fixture_compare_expected (fixture);
172 rewind (fixture->stream);
173
174 cockpit_json_print_string_property (fixture->stream, "key", buffer, 256);
175 test_fixture_compare_expected (fixture);
176 rewind (fixture->stream);
177 }
178
179 /* The following test tries to catch bad behaviour from the scanner that
180 * finds the groups of unescaped characters for fast printing. It
181 * ensures that:
182 *
183 * 1) the groups are always scanned correctly and correct escaped
184 * output is produced
185 *
186 * 2) a nul is always honoured, regardless of a larger given
187 * max_length parameter
188 *
189 * 3) max_length is always honoured, regardless of if a nul is present
190 * or not.
191 *
192 * In particular, we use our fixture to ensure that we never touch
193 * memory past either the final nul character, or past the specified
194 * max_length.
195 *
196 * We test strings composed of a given number 'groups'. Each group
197 * consists of a certain number of repetitions ('reps') of a given
198 * character.
199 *
200 * We use a sequence counter to determine the character and the number
201 * of reps for each group. At each step we take the modulus of a
202 * division to make a decision about the given variable, using the whole
203 * result as a residual for future decisions. This effectively
204 * implements a counter with an arbitrary radix at each position. Once
205 * we see a non-zero resitual, we know that we've surely exhausted all
206 * possible combinations.
207 *
208 * These constants could easily be a bit higher, but the running time
209 * explodes pretty quickly, and this test benefits from being run under
210 * valgrind.
211 */
212 #define MIN_GROUPS 1
213 #define MAX_GROUPS 3
214 #define MAX_REPS 5
215
216 static guint
217 divmod (guint *residual,
218 guint divisor)
219 {
220 guint result = *residual % divisor;
221
222 *residual /= divisor;
223
224 return result;
225 }
226
227 static void
228 test_print_string_memory_safety (TestFixture *fixture,
229 gconstpointer user_data)
230 {
231 gchar characters[] = { '\n', ' ', 'a', '\\', '\"', 0xcc };
232 const gchar *escaped[] = { "\\u000a", " ", "a", "\\\\", "\\\"", "?" };
233 gchar buffer[MAX_GROUPS * MAX_REPS];
234
235 for (gint n_groups = MIN_GROUPS; n_groups <= MAX_GROUPS; n_groups++)
236 {
237 for (guint seq = 0;; seq++)
238 {
239 guint residual = seq;
240 gint length = 0;
241 int reps = 0;
242
243 fixture->expected_end = stpcpy (fixture->expected_buffer, ", \"key\": \"");
244
245 for (gint group = 0; group < n_groups; group++)
246 {
247 gint c = divmod (&residual, sizeof characters);
248 reps = divmod (&residual, MAX_REPS) + 1;
249
250 memset (buffer + length, characters[c], reps);
251 length += reps;
252
253 for (gint i = 0; i < reps; i++)
254 test_fixture_expect (fixture, escaped[c]);
255 }
256
257 g_assert_cmpint (length, <=, sizeof buffer);
258
259 if (residual)
260 /* non-zero residual → we've already tried all cases */
261 break;
262
263 test_fixture_expect (fixture, "\"");
264
265 /* Test various cases of the string not being nul terminated.
266 * We avoid starting from 0 each time in order to avoid
267 * effectively testing fewer groups. `reps` is leftover from
268 * the last iteration of the loop above. Starting at:
269 *
270 * length - reps + 1
271 *
272 * makes sure that we see at least one character from this
273 * final group.
274 *
275 * We position the subset of the string at `region` (at a
276 * negative offset to the inaccessible area in the fixture) to
277 * ensure that we don't read more than the requested `i`
278 * characters.
279 *
280 * This test doesn't ensure that the correct output is
281 * produced. It's difficult to cut the expected string to the
282 * correct length, given the different lengths of escaped
283 * characters.
284 */
285 for (gint i = length - reps + 1; i <= length; i++)
286 {
287 gchar *region = fixture->inaccessible - i;
288
289 memcpy (region, buffer, i);
290 cockpit_json_print_string_property (fixture->stream, "key", region, i);
291 rewind (fixture->stream);
292 }
293
294 /* These ones test a complete nul-terminated string. As such,
295 * we configure the region to be exactly large enough to hold
296 * the nul-terminated string. Then we try giving different
297 * lengths.
298 */
299 gchar *region = fixture->inaccessible - (length + 1);
300 memcpy (region, buffer, length);
301 region[length] = '\0';
302
303 /* First with -1 */
304 cockpit_json_print_string_property (fixture->stream, "key", region, -1);
305 test_fixture_compare_expected (fixture);
306 rewind (fixture->stream);
307
308 /* Then the exact length */
309 cockpit_json_print_string_property (fixture->stream, "key", region, length);
310 test_fixture_compare_expected (fixture);
311 rewind (fixture->stream);
312
313 /* Then lengths bigger than the string (keeping in mind that
314 * this is a *max* length parameter).
315 */
316 for (gint i = length + 1; i <= length + 3; i++)
317 {
318 cockpit_json_print_string_property (fixture->stream, "key", region, i);
319 test_fixture_compare_expected (fixture);
320 rewind (fixture->stream);
321 }
322 }
323 }
324 }
325
326 static void
327 test_print_numeric (TestFixture *fixture,
328 gconstpointer user_data)
329 {
330 cockpit_json_print_integer_property (fixture->stream, "zero", 0);
331 test_fixture_expect (fixture, ", \"zero\": 0");
332
333 cockpit_json_print_integer_property (fixture->stream, "one", 1);
334 test_fixture_expect (fixture, ", \"one\": 1");
335
336 cockpit_json_print_integer_property (fixture->stream, "million", 1000000);
337 test_fixture_expect (fixture, ", \"million\": 1000000");
338
339 /* check that numbers that can't be encoded in double still work */
340 guint64 extra_big = 9007199254740993ull; /* 2^53 + 1 */
341 g_assert_cmpint (extra_big, !=, (guint64) (double) extra_big);
342 cockpit_json_print_integer_property (fixture->stream, "extrabig", extra_big);
343 test_fixture_expect (fixture, ", \"extrabig\": 9007199254740993");
344
345 /* check these special values to make sure they're being handled as uint64 */
346 cockpit_json_print_integer_property (fixture->stream, "intmax", INT64_MAX);
347 test_fixture_expect (fixture, ", \"intmax\": 9223372036854775807");
348 cockpit_json_print_integer_property (fixture->stream, "intmaxplusone", INT64_MAX + 1ull);
349 test_fixture_expect (fixture, ", \"intmaxplusone\": 9223372036854775808");
350 cockpit_json_print_integer_property (fixture->stream, "uintmax", UINT64_MAX);
351 test_fixture_expect (fixture, ", \"uintmax\": 18446744073709551615");
352 cockpit_json_print_integer_property (fixture->stream, "minus1", -1);
353 test_fixture_expect (fixture, ", \"minus1\": 18446744073709551615");
354
355 /* make sure it all worked out */
356 test_fixture_compare_expected (fixture);
357 }
358
359 static void
360 test_print_boolean (TestFixture *fixture,
361 gconstpointer user_data)
362 {
363 test_fixture_expect (fixture, ", \"true\": true, \"false\": false, \"alsotrue\": true");
364
365 cockpit_json_print_bool_property (fixture->stream, "true", true);
366 cockpit_json_print_bool_property (fixture->stream, "false", false);
367 cockpit_json_print_bool_property (fixture->stream, "alsotrue", 123456);
368
369 test_fixture_compare_expected (fixture);
370 }
371
372 int
373 main (int argc,
374 char **argv)
375 {
376 cockpit_test_init (&argc, &argv);
377
378 g_test_add ("/jsonprint/string", TestFixture, NULL,
379 test_fixture_setup, test_print_string, test_fixture_teardown);
380 g_test_add ("/jsonprint/string/memory-safety", TestFixture, NULL,
381 test_fixture_setup, test_print_string_memory_safety, test_fixture_teardown);
382 g_test_add ("/jsonprint/numeric", TestFixture, NULL,
383 test_fixture_setup, test_print_numeric, test_fixture_teardown);
384 g_test_add ("/jsonprint/boolean", TestFixture, NULL,
385 test_fixture_setup, test_print_boolean, test_fixture_teardown);
386
387 return g_test_run ();
388 }