Line data Source code
1 : /*
2 : * Unit tests for the audit_logging library.
3 : *
4 : * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : *
19 : */
20 :
21 : /*
22 : * from cmocka.c:
23 : * These headers or their equivalents should be included prior to
24 : * including
25 : * this header file.
26 : *
27 : * #include <stdarg.h>
28 : * #include <stddef.h>
29 : * #include <setjmp.h>
30 : *
31 : * This allows test applications to use custom definitions of C standard
32 : * library functions and types.
33 : *
34 : */
35 :
36 : /*
37 : * Note that the messaging routines (audit_message_send and get_event_server)
38 : * are not tested by these unit tests. Currently they are for integration
39 : * test support, and as such are exercised by the integration tests.
40 : */
41 : #include <stdarg.h>
42 : #include <stddef.h>
43 : #include <setjmp.h>
44 : #include <cmocka.h>
45 :
46 : #include <string.h>
47 : #include <time.h>
48 : #include <tevent.h>
49 : #include <config.h>
50 : #include <talloc.h>
51 : #include "lib/util/talloc_stack.h"
52 :
53 : #include "lib/util/data_blob.h"
54 : #include "lib/util/time.h"
55 : #include "libcli/util/werror.h"
56 : #include "lib/param/loadparm.h"
57 : #include "libcli/security/dom_sid.h"
58 : #include "librpc/ndr/libndr.h"
59 :
60 : #include "lib/audit_logging/audit_logging.h"
61 :
62 1 : static void test_json_add_int(_UNUSED_ void **state)
63 : {
64 : struct json_object object;
65 1 : struct json_t *value = NULL;
66 : double n;
67 1 : int rc = 0;
68 :
69 1 : object = json_new_object();
70 1 : rc = json_add_int(&object, "positive_one", 1);
71 1 : assert_int_equal(0, rc);
72 1 : rc = json_add_int(&object, "zero", 0);
73 1 : assert_int_equal(0, rc);
74 1 : rc = json_add_int(&object, "negative_one", -1);
75 1 : assert_int_equal(0, rc);
76 :
77 1 : assert_int_equal(3, json_object_size(object.root));
78 :
79 1 : value = json_object_get(object.root, "positive_one");
80 1 : assert_true(json_is_integer(value));
81 1 : n = json_number_value(value);
82 1 : assert_true(n == 1.0);
83 :
84 1 : value = json_object_get(object.root, "zero");
85 1 : assert_true(json_is_integer(value));
86 1 : n = json_number_value(value);
87 1 : assert_true(n == 0.0);
88 :
89 1 : value = json_object_get(object.root, "negative_one");
90 1 : assert_true(json_is_integer(value));
91 1 : n = json_number_value(value);
92 1 : assert_true(n == -1.0);
93 :
94 1 : object.valid = false;
95 1 : rc = json_add_int(&object, "should fail 1", 0xf1);
96 1 : assert_int_equal(JSON_ERROR, rc);
97 :
98 1 : json_free(&object);
99 :
100 1 : rc = json_add_int(&object, "should fail 2", 0xf2);
101 1 : assert_int_equal(JSON_ERROR, rc);
102 1 : }
103 :
104 1 : static void test_json_add_bool(_UNUSED_ void **state)
105 : {
106 : struct json_object object;
107 1 : struct json_t *value = NULL;
108 1 : int rc = 0;
109 :
110 1 : object = json_new_object();
111 1 : rc = json_add_bool(&object, "true", true);
112 1 : assert_int_equal(0, rc);
113 1 : rc = json_add_bool(&object, "false", false);
114 1 : assert_int_equal(0, rc);
115 :
116 1 : assert_int_equal(2, json_object_size(object.root));
117 :
118 1 : value = json_object_get(object.root, "true");
119 1 : assert_true(json_is_boolean(value));
120 1 : assert_true(value == json_true());
121 :
122 1 : value = json_object_get(object.root, "false");
123 1 : assert_true(json_is_boolean(value));
124 1 : assert_true(value == json_false());
125 :
126 1 : object.valid = false;
127 1 : rc = json_add_bool(&object, "should fail 1", true);
128 1 : assert_int_equal(JSON_ERROR, rc);
129 :
130 1 : json_free(&object);
131 :
132 1 : rc = json_add_bool(&object, "should fail 2", false);
133 1 : assert_int_equal(JSON_ERROR, rc);
134 1 : }
135 :
136 1 : static void test_json_add_string(_UNUSED_ void **state)
137 : {
138 : struct json_object object;
139 1 : struct json_t *value = NULL;
140 1 : const char *s = NULL;
141 1 : int rc = 0;
142 :
143 1 : object = json_new_object();
144 1 : rc = json_add_string(&object, "null", NULL);
145 1 : assert_int_equal(0, rc);
146 1 : rc = json_add_string(&object, "empty", "");
147 1 : assert_int_equal(0, rc);
148 1 : rc = json_add_string(&object, "name", "value");
149 1 : assert_int_equal(0, rc);
150 :
151 1 : assert_int_equal(3, json_object_size(object.root));
152 :
153 1 : value = json_object_get(object.root, "null");
154 1 : assert_true(json_is_null(value));
155 :
156 1 : value = json_object_get(object.root, "empty");
157 1 : assert_true(json_is_string(value));
158 1 : s = json_string_value(value);
159 1 : assert_string_equal("", s);
160 :
161 1 : value = json_object_get(object.root, "name");
162 1 : assert_true(json_is_string(value));
163 1 : s = json_string_value(value);
164 1 : assert_string_equal("value", s);
165 :
166 1 : object.valid = false;
167 1 : rc = json_add_string(&object, "should fail 1", "A value");
168 1 : assert_int_equal(JSON_ERROR, rc);
169 :
170 1 : json_free(&object);
171 :
172 1 : rc = json_add_string(&object, "should fail 2", "Another value");
173 1 : assert_int_equal(JSON_ERROR, rc);
174 1 : }
175 :
176 1 : static void test_json_add_object(_UNUSED_ void **state)
177 : {
178 : struct json_object object;
179 : struct json_object other;
180 : struct json_object after;
181 1 : struct json_object invalid = json_empty_object;
182 1 : struct json_t *value = NULL;
183 1 : int rc = 0;
184 :
185 1 : object = json_new_object();
186 1 : other = json_new_object();
187 1 : rc = json_add_object(&object, "null", NULL);
188 1 : assert_int_equal(0, rc);
189 1 : rc = json_add_object(&object, "other", &other);
190 1 : assert_int_equal(0, rc);
191 :
192 1 : assert_int_equal(2, json_object_size(object.root));
193 :
194 1 : value = json_object_get(object.root, "null");
195 1 : assert_true(json_is_null(value));
196 :
197 1 : value = json_object_get(object.root, "other");
198 1 : assert_true(json_is_object(value));
199 1 : assert_ptr_equal(other.root, value);
200 :
201 1 : rc = json_add_object(&object, "invalid", &invalid);
202 1 : assert_int_equal(JSON_ERROR, rc);
203 :
204 1 : object.valid = false;
205 1 : after = json_new_object();
206 1 : rc = json_add_object(&object, "after", &after);
207 1 : assert_int_equal(JSON_ERROR, rc);
208 :
209 1 : json_free(&object);
210 :
211 1 : rc = json_add_object(&object, "after", &after);
212 1 : assert_int_equal(JSON_ERROR, rc);
213 :
214 1 : json_free(&after);
215 1 : }
216 :
217 1 : static void test_json_add_to_array(_UNUSED_ void **state)
218 : {
219 : struct json_object array;
220 : struct json_object o1;
221 : struct json_object o2;
222 : struct json_object o3;
223 : struct json_object after;
224 1 : struct json_object invalid = json_empty_object;
225 1 : struct json_t *value = NULL;
226 1 : int rc = 0;
227 :
228 1 : array = json_new_array();
229 1 : assert_true(json_is_array(array.root));
230 :
231 1 : o1 = json_new_object();
232 1 : o2 = json_new_object();
233 1 : o3 = json_new_object();
234 :
235 1 : rc = json_add_object(&array, NULL, &o3);
236 1 : assert_int_equal(0, rc);
237 1 : rc = json_add_object(&array, "", &o2);
238 1 : assert_int_equal(0, rc);
239 1 : rc = json_add_object(&array, "will-be-ignored", &o1);
240 1 : assert_int_equal(0, rc);
241 1 : rc = json_add_object(&array, NULL, NULL);
242 1 : assert_int_equal(0, rc);
243 :
244 1 : assert_int_equal(4, json_array_size(array.root));
245 :
246 1 : value = json_array_get(array.root, 0);
247 1 : assert_ptr_equal(o3.root, value);
248 :
249 1 : value = json_array_get(array.root, 1);
250 1 : assert_ptr_equal(o2.root, value);
251 :
252 1 : value = json_array_get(array.root, 2);
253 1 : assert_ptr_equal(o1.root, value);
254 :
255 1 : value = json_array_get(array.root, 3);
256 1 : assert_true(json_is_null(value));
257 :
258 1 : rc = json_add_object(&array, "invalid", &invalid);
259 1 : assert_int_equal(JSON_ERROR, rc);
260 :
261 1 : array.valid = false;
262 1 : after = json_new_object();
263 1 : rc = json_add_object(&array, "after", &after);
264 1 : assert_int_equal(JSON_ERROR, rc);
265 :
266 1 : json_free(&array);
267 :
268 1 : rc = json_add_object(&array, "after", &after);
269 1 : assert_int_equal(JSON_ERROR, rc);
270 :
271 1 : json_free(&after);
272 1 : }
273 :
274 1 : static void test_json_add_timestamp(_UNUSED_ void **state)
275 : {
276 : struct json_object object;
277 1 : struct json_t *ts = NULL;
278 1 : const char *t = NULL;
279 : int rc;
280 : int usec, tz;
281 : char c[2];
282 : struct tm tm;
283 : time_t before;
284 : time_t after;
285 : time_t actual;
286 : struct timeval tv;
287 : int ret;
288 :
289 1 : object = json_new_object();
290 :
291 1 : ret = gettimeofday(&tv, NULL);
292 1 : assert_int_equal(0, ret);
293 1 : before = tv.tv_sec;
294 :
295 1 : rc = json_add_timestamp(&object);
296 1 : assert_int_equal(0, rc);
297 :
298 1 : ret = gettimeofday(&tv, NULL);
299 1 : assert_int_equal(0, ret);
300 1 : after = tv.tv_sec;
301 :
302 1 : ts = json_object_get(object.root, "timestamp");
303 1 : assert_true(json_is_string(ts));
304 :
305 : /*
306 : * Convert the returned ISO 8601 timestamp into a time_t
307 : * Note for convenience we ignore the value of the microsecond
308 : * part of the time stamp.
309 : */
310 1 : t = json_string_value(ts);
311 1 : rc = sscanf(
312 : t,
313 : "%4d-%2d-%2dT%2d:%2d:%2d.%6d%1c%4d",
314 : &tm.tm_year,
315 : &tm.tm_mon,
316 : &tm.tm_mday,
317 : &tm.tm_hour,
318 : &tm.tm_min,
319 : &tm.tm_sec,
320 : &usec,
321 : c,
322 : &tz);
323 1 : assert_int_equal(9, rc);
324 1 : tm.tm_year = tm.tm_year - 1900;
325 1 : tm.tm_mon = tm.tm_mon - 1;
326 1 : tm.tm_isdst = -1;
327 1 : actual = mktime(&tm);
328 :
329 : /*
330 : * The timestamp should be before <= actual <= after
331 : */
332 1 : assert_true(difftime(actual, before) >= 0);
333 1 : assert_true(difftime(after, actual) >= 0);
334 :
335 1 : object.valid = false;
336 1 : rc = json_add_timestamp(&object);
337 1 : assert_int_equal(JSON_ERROR, rc);
338 :
339 1 : json_free(&object);
340 :
341 1 : rc = json_add_timestamp(&object);
342 1 : assert_int_equal(JSON_ERROR, rc);
343 1 : }
344 :
345 1 : static void test_json_add_stringn(_UNUSED_ void **state)
346 : {
347 : struct json_object object;
348 1 : struct json_t *value = NULL;
349 1 : const char *s = NULL;
350 1 : int rc = 0;
351 :
352 1 : object = json_new_object();
353 1 : rc = json_add_stringn(&object, "null", NULL, 10);
354 1 : assert_int_equal(0, rc);
355 1 : rc = json_add_stringn(&object, "null-zero-len", NULL, 0);
356 1 : assert_int_equal(0, rc);
357 1 : rc = json_add_stringn(&object, "empty", "", 1);
358 1 : assert_int_equal(0, rc);
359 1 : rc = json_add_stringn(&object, "empty-zero-len", "", 0);
360 1 : assert_int_equal(0, rc);
361 1 : rc = json_add_stringn(&object, "value-less-than-len", "123456", 7);
362 1 : assert_int_equal(0, rc);
363 1 : rc = json_add_stringn(&object, "value-greater-than-len", "abcd", 3);
364 1 : assert_int_equal(0, rc);
365 1 : rc = json_add_stringn(&object, "value-equal-len", "ZYX", 3);
366 1 : assert_int_equal(0, rc);
367 1 : rc = json_add_stringn(
368 : &object, "value-len-is-zero", "this will be null", 0);
369 1 : assert_int_equal(0, rc);
370 :
371 1 : assert_int_equal(8, json_object_size(object.root));
372 :
373 1 : value = json_object_get(object.root, "null");
374 1 : assert_true(json_is_null(value));
375 :
376 1 : value = json_object_get(object.root, "null-zero-len");
377 1 : assert_true(json_is_null(value));
378 :
379 1 : value = json_object_get(object.root, "empty");
380 1 : assert_true(json_is_string(value));
381 1 : s = json_string_value(value);
382 1 : assert_string_equal("", s);
383 :
384 1 : value = json_object_get(object.root, "empty-zero-len");
385 1 : assert_true(json_is_null(value));
386 :
387 1 : value = json_object_get(object.root, "value-greater-than-len");
388 1 : assert_true(json_is_string(value));
389 1 : s = json_string_value(value);
390 1 : assert_string_equal("abc", s);
391 1 : assert_int_equal(3, strlen(s));
392 :
393 1 : value = json_object_get(object.root, "value-equal-len");
394 1 : assert_true(json_is_string(value));
395 1 : s = json_string_value(value);
396 1 : assert_string_equal("ZYX", s);
397 1 : assert_int_equal(3, strlen(s));
398 :
399 1 : value = json_object_get(object.root, "value-len-is-zero");
400 1 : assert_true(json_is_null(value));
401 :
402 1 : object.valid = false;
403 1 : rc = json_add_stringn(&object, "fail-01", "xxxxxxx", 1);
404 1 : assert_int_equal(JSON_ERROR, rc);
405 :
406 1 : json_free(&object);
407 :
408 1 : rc = json_add_stringn(&object, "fail-02", "xxxxxxx", 1);
409 1 : assert_int_equal(JSON_ERROR, rc);
410 1 : }
411 :
412 1 : static void test_json_add_version(_UNUSED_ void **state)
413 : {
414 : struct json_object object;
415 1 : struct json_t *version = NULL;
416 1 : struct json_t *v = NULL;
417 : double n;
418 : int rc;
419 :
420 1 : object = json_new_object();
421 1 : rc = json_add_version(&object, 3, 1);
422 1 : assert_int_equal(0, rc);
423 :
424 1 : assert_int_equal(1, json_object_size(object.root));
425 :
426 1 : version = json_object_get(object.root, "version");
427 1 : assert_true(json_is_object(version));
428 1 : assert_int_equal(2, json_object_size(version));
429 :
430 1 : v = json_object_get(version, "major");
431 1 : assert_true(json_is_integer(v));
432 1 : n = json_number_value(v);
433 1 : assert_true(n == 3.0);
434 :
435 1 : v = json_object_get(version, "minor");
436 1 : assert_true(json_is_integer(v));
437 1 : n = json_number_value(v);
438 1 : assert_true(n == 1.0);
439 :
440 1 : object.valid = false;
441 1 : rc = json_add_version(&object, 3, 1);
442 1 : assert_int_equal(JSON_ERROR, rc);
443 :
444 1 : json_free(&object);
445 :
446 1 : rc = json_add_version(&object, 3, 1);
447 1 : assert_int_equal(JSON_ERROR, rc);
448 1 : }
449 :
450 1 : static void test_json_add_address(_UNUSED_ void **state)
451 : {
452 : struct json_object object;
453 1 : struct json_t *value = NULL;
454 1 : struct tsocket_address *ip4 = NULL;
455 1 : struct tsocket_address *ip6 = NULL;
456 1 : struct tsocket_address *pipe = NULL;
457 :
458 1 : struct tsocket_address *after = NULL;
459 1 : const char *s = NULL;
460 : int rc;
461 :
462 1 : TALLOC_CTX *ctx = talloc_new(NULL);
463 :
464 1 : object = json_new_object();
465 :
466 1 : rc = json_add_address(&object, "null", NULL);
467 1 : assert_int_equal(0, rc);
468 :
469 1 : rc = tsocket_address_inet_from_strings(
470 : ctx,
471 : "ip",
472 : "127.0.0.1",
473 : 21,
474 : &ip4);
475 1 : assert_int_equal(0, rc);
476 1 : rc = json_add_address(&object, "ip4", ip4);
477 1 : assert_int_equal(0, rc);
478 :
479 1 : rc = tsocket_address_inet_from_strings(
480 : ctx,
481 : "ip",
482 : "2001:db8:0:0:1:0:0:1",
483 : 42,
484 : &ip6);
485 1 : assert_int_equal(0, rc);
486 1 : rc = json_add_address(&object, "ip6", ip6);
487 1 : assert_int_equal(0, rc);
488 :
489 1 : rc = tsocket_address_unix_from_path(ctx, "/samba/pipe", &pipe);
490 1 : assert_int_equal(0, rc);
491 1 : rc = json_add_address(&object, "pipe", pipe);
492 1 : assert_int_equal(0, rc);
493 :
494 1 : assert_int_equal(4, json_object_size(object.root));
495 :
496 1 : value = json_object_get(object.root, "null");
497 1 : assert_true(json_is_null(value));
498 :
499 1 : value = json_object_get(object.root, "ip4");
500 1 : assert_true(json_is_string(value));
501 1 : s = json_string_value(value);
502 1 : assert_string_equal("ipv4:127.0.0.1:21", s);
503 :
504 1 : value = json_object_get(object.root, "ip6");
505 1 : assert_true(json_is_string(value));
506 1 : s = json_string_value(value);
507 1 : assert_string_equal("ipv6:2001:db8::1:0:0:1:42", s);
508 :
509 1 : value = json_object_get(object.root, "pipe");
510 1 : assert_true(json_is_string(value));
511 1 : s = json_string_value(value);
512 1 : assert_string_equal("unix:/samba/pipe", s);
513 :
514 1 : object.valid = false;
515 1 : rc = tsocket_address_inet_from_strings(
516 : ctx, "ip", "127.0.0.11", 23, &after);
517 1 : assert_int_equal(0, rc);
518 1 : rc = json_add_address(&object, "invalid_object", after);
519 1 : assert_int_equal(JSON_ERROR, rc);
520 :
521 1 : json_free(&object);
522 :
523 1 : rc = json_add_address(&object, "freed object", after);
524 1 : assert_int_equal(JSON_ERROR, rc);
525 :
526 1 : TALLOC_FREE(ctx);
527 1 : }
528 :
529 1 : static void test_json_add_sid(_UNUSED_ void **state)
530 : {
531 : struct json_object object;
532 1 : struct json_t *value = NULL;
533 1 : const char *SID = "S-1-5-21-2470180966-3899876309-2637894779";
534 : struct dom_sid sid;
535 1 : const char *s = NULL;
536 : int rc;
537 :
538 1 : object = json_new_object();
539 :
540 1 : rc = json_add_sid(&object, "null", NULL);
541 1 : assert_int_equal(0, rc);
542 :
543 1 : assert_true(string_to_sid(&sid, SID));
544 1 : rc = json_add_sid(&object, "sid", &sid);
545 1 : assert_int_equal(0, rc);
546 :
547 1 : assert_int_equal(2, json_object_size(object.root));
548 :
549 1 : value = json_object_get(object.root, "null");
550 1 : assert_true(json_is_null(value));
551 :
552 1 : value = json_object_get(object.root, "sid");
553 1 : assert_true(json_is_string(value));
554 1 : s = json_string_value(value);
555 1 : assert_string_equal(SID, s);
556 :
557 1 : object.valid = false;
558 1 : rc = json_add_sid(&object, "invalid_object", &sid);
559 1 : assert_int_equal(JSON_ERROR, rc);
560 :
561 1 : json_free(&object);
562 :
563 1 : rc = json_add_sid(&object, "freed_object", &sid);
564 1 : assert_int_equal(JSON_ERROR, rc);
565 1 : }
566 :
567 1 : static void test_json_add_guid(_UNUSED_ void **state)
568 : {
569 : struct json_object object;
570 1 : struct json_t *value = NULL;
571 1 : const char *GUID = "3ab88633-1e57-4c1a-856c-d1bc4b15bbb1";
572 : struct GUID guid;
573 1 : const char *s = NULL;
574 : NTSTATUS status;
575 : int rc;
576 :
577 1 : object = json_new_object();
578 :
579 1 : rc = json_add_guid(&object, "null", NULL);
580 1 : assert_int_equal(0, rc);
581 :
582 1 : status = GUID_from_string(GUID, &guid);
583 1 : assert_true(NT_STATUS_IS_OK(status));
584 1 : rc = json_add_guid(&object, "guid", &guid);
585 1 : assert_int_equal(0, rc);
586 :
587 1 : assert_int_equal(2, json_object_size(object.root));
588 :
589 1 : value = json_object_get(object.root, "null");
590 1 : assert_true(json_is_null(value));
591 :
592 1 : value = json_object_get(object.root, "guid");
593 1 : assert_true(json_is_string(value));
594 1 : s = json_string_value(value);
595 1 : assert_string_equal(GUID, s);
596 :
597 1 : object.valid = false;
598 1 : rc = json_add_guid(&object, "invalid_object", &guid);
599 1 : assert_int_equal(JSON_ERROR, rc);
600 :
601 1 : json_free(&object);
602 :
603 1 : rc = json_add_guid(&object, "freed_object", &guid);
604 1 : assert_int_equal(JSON_ERROR, rc);
605 1 : }
606 :
607 1 : static void test_json_to_string(_UNUSED_ void **state)
608 : {
609 : struct json_object object;
610 1 : char *s = NULL;
611 : int rc;
612 :
613 1 : TALLOC_CTX *ctx = talloc_new(NULL);
614 :
615 1 : object = json_new_object();
616 :
617 1 : s = json_to_string(ctx, &object);
618 1 : assert_string_equal("{}", s);
619 1 : TALLOC_FREE(s);
620 :
621 1 : rc = json_add_string(&object, "name", "value");
622 1 : assert_int_equal(0, rc);
623 1 : s = json_to_string(ctx, &object);
624 1 : assert_string_equal("{\"name\": \"value\"}", s);
625 1 : TALLOC_FREE(s);
626 :
627 1 : object.valid = false;
628 1 : s = json_to_string(ctx, &object);
629 1 : assert_null(s);
630 :
631 1 : json_free(&object);
632 :
633 1 : object.valid = true;
634 1 : object.root = NULL;
635 :
636 1 : s = json_to_string(ctx, &object);
637 1 : assert_null(s);
638 1 : TALLOC_FREE(ctx);
639 1 : }
640 :
641 1 : static void test_json_get_array(_UNUSED_ void **state)
642 : {
643 : struct json_object object;
644 : struct json_object array;
645 1 : struct json_object stored_array = json_new_array();
646 1 : json_t *value = NULL;
647 1 : json_t *o = NULL;
648 : struct json_object o1;
649 : struct json_object o2;
650 : int rc;
651 :
652 1 : object = json_new_object();
653 :
654 1 : array = json_get_array(&object, "not-there");
655 1 : assert_true(array.valid);
656 1 : assert_non_null(array.root);
657 1 : assert_true(json_is_array(array.root));
658 1 : json_free(&array);
659 :
660 1 : o1 = json_new_object();
661 1 : rc = json_add_string(&o1, "value", "value-one");
662 1 : assert_int_equal(0, rc);
663 1 : rc = json_add_object(&stored_array, NULL, &o1);
664 1 : assert_int_equal(0, rc);
665 1 : rc = json_add_object(&object, "stored_array", &stored_array);
666 1 : assert_int_equal(0, rc);
667 :
668 1 : array = json_get_array(&object, "stored_array");
669 1 : assert_true(array.valid);
670 1 : assert_non_null(array.root);
671 1 : assert_true(json_is_array(array.root));
672 :
673 1 : assert_int_equal(1, json_array_size(array.root));
674 :
675 1 : o = json_array_get(array.root, 0);
676 1 : assert_non_null(o);
677 1 : assert_true(json_is_object(o));
678 :
679 1 : value = json_object_get(o, "value");
680 1 : assert_non_null(value);
681 1 : assert_true(json_is_string(value));
682 :
683 1 : assert_string_equal("value-one", json_string_value(value));
684 1 : json_free(&array);
685 :
686 : /*
687 : * Now update the array and add it back to the object
688 : */
689 1 : array = json_get_array(&object, "stored_array");
690 1 : assert_true(json_is_array(array.root));
691 1 : o2 = json_new_object();
692 1 : rc = json_add_string(&o2, "value", "value-two");
693 1 : assert_int_equal(0, rc);
694 1 : assert_true(o2.valid);
695 1 : rc = json_add_object(&array, NULL, &o2);
696 1 : assert_int_equal(0, rc);
697 1 : assert_true(json_is_array(array.root));
698 1 : rc = json_add_object(&object, "stored_array", &array);
699 1 : assert_int_equal(0, rc);
700 1 : assert_true(json_is_array(array.root));
701 :
702 1 : array = json_get_array(&object, "stored_array");
703 1 : assert_non_null(array.root);
704 1 : assert_true(json_is_array(array.root));
705 1 : assert_true(array.valid);
706 1 : assert_true(json_is_array(array.root));
707 :
708 1 : assert_int_equal(2, json_array_size(array.root));
709 :
710 1 : o = json_array_get(array.root, 0);
711 1 : assert_non_null(o);
712 1 : assert_true(json_is_object(o));
713 :
714 1 : assert_non_null(value);
715 1 : assert_true(json_is_string(value));
716 :
717 1 : assert_string_equal("value-one", json_string_value(value));
718 :
719 1 : o = json_array_get(array.root, 1);
720 1 : assert_non_null(o);
721 1 : assert_true(json_is_object(o));
722 :
723 1 : value = json_object_get(o, "value");
724 1 : assert_non_null(value);
725 1 : assert_true(json_is_string(value));
726 :
727 1 : assert_string_equal("value-two", json_string_value(value));
728 :
729 1 : json_free(&array);
730 1 : json_free(&object);
731 :
732 1 : array = json_get_array(&object, "stored_array");
733 1 : assert_false(array.valid);
734 1 : json_free(&array);
735 1 : }
736 :
737 1 : static void test_json_get_object(_UNUSED_ void **state)
738 : {
739 : struct json_object object;
740 : struct json_object o1;
741 : struct json_object o2;
742 : struct json_object o3;
743 1 : json_t *value = NULL;
744 : int rc;
745 :
746 1 : object = json_new_object();
747 :
748 1 : o1 = json_get_object(&object, "not-there");
749 1 : assert_true(o1.valid);
750 1 : assert_non_null(o1.root);
751 1 : assert_true(json_is_object(o1.root));
752 1 : json_free(&o1);
753 :
754 1 : o1 = json_new_object();
755 1 : rc = json_add_string(&o1, "value", "value-one");
756 1 : assert_int_equal(0, rc);
757 1 : rc = json_add_object(&object, "stored_object", &o1);
758 1 : assert_int_equal(0, rc);
759 :
760 1 : o2 = json_get_object(&object, "stored_object");
761 1 : assert_true(o2.valid);
762 1 : assert_non_null(o2.root);
763 1 : assert_true(json_is_object(o2.root));
764 :
765 1 : value = json_object_get(o2.root, "value");
766 1 : assert_non_null(value);
767 1 : assert_true(json_is_string(value));
768 :
769 1 : assert_string_equal("value-one", json_string_value(value));
770 :
771 1 : rc = json_add_string(&o2, "value", "value-two");
772 1 : assert_int_equal(0, rc);
773 1 : rc = json_add_object(&object, "stored_object", &o2);
774 1 : assert_int_equal(0, rc);
775 :
776 1 : o3 = json_get_object(&object, "stored_object");
777 1 : assert_true(o3.valid);
778 1 : assert_non_null(o3.root);
779 1 : assert_true(json_is_object(o3.root));
780 :
781 1 : value = json_object_get(o3.root, "value");
782 1 : assert_non_null(value);
783 1 : assert_true(json_is_string(value));
784 :
785 1 : assert_string_equal("value-two", json_string_value(value));
786 :
787 1 : json_free(&o3);
788 1 : json_free(&object);
789 :
790 1 : o3 = json_get_object(&object, "stored_object");
791 1 : assert_false(o3.valid);
792 1 : json_free(&o3);
793 1 : }
794 :
795 1 : static void test_audit_get_timestamp(_UNUSED_ void **state)
796 : {
797 1 : const char *t = NULL;
798 : char *c;
799 : struct tm tm;
800 : time_t before;
801 : time_t after;
802 : time_t actual;
803 : struct timeval tv;
804 : int ret;
805 1 : char *env_tz = NULL;
806 1 : char *orig_tz = NULL;
807 :
808 1 : TALLOC_CTX *ctx = talloc_new(NULL);
809 :
810 : /*
811 : * Explicitly set the time zone to UTC to make the test easier
812 : */
813 1 : env_tz = getenv("TZ");
814 1 : if (env_tz != NULL) {
815 1 : orig_tz = talloc_strdup(ctx, env_tz);
816 : }
817 1 : setenv("TZ", "UTC", 1);
818 :
819 1 : ret = gettimeofday(&tv, NULL);
820 1 : assert_int_equal(0, ret);
821 1 : before = tv.tv_sec;
822 :
823 1 : t = audit_get_timestamp(ctx);
824 :
825 1 : ret = gettimeofday(&tv, NULL);
826 1 : assert_int_equal(0, ret);
827 1 : after = tv.tv_sec;
828 :
829 1 : c = strptime(t, "%a, %d %b %Y %H:%M:%S", &tm);
830 :
831 : /*
832 : * Restore the time zone if we changed it
833 : */
834 1 : if (orig_tz != NULL) {
835 1 : setenv("TZ", orig_tz, 1);
836 1 : TALLOC_FREE(orig_tz);
837 : }
838 :
839 1 : assert_non_null(c);
840 1 : tm.tm_isdst = -1;
841 1 : if (c != NULL && *c == '.') {
842 : char *e;
843 1 : strtod(c, &e);
844 1 : c = e;
845 : }
846 1 : if (c != NULL && *c == ' ') {
847 1 : assert_string_equal(" UTC", c);
848 1 : c += 4;
849 : }
850 1 : assert_int_equal(0, strlen(c));
851 :
852 1 : actual = mktime(&tm);
853 :
854 : /*
855 : * The timestamp should be before <= actual <= after
856 : */
857 1 : assert_true(difftime(actual, before) >= 0);
858 1 : assert_true(difftime(after, actual) >= 0);
859 :
860 1 : TALLOC_FREE(ctx);
861 1 : }
862 :
863 1 : int main(_UNUSED_ int argc, _UNUSED_ const char **argv)
864 : {
865 1 : const struct CMUnitTest tests[] = {
866 : cmocka_unit_test(test_json_add_int),
867 : cmocka_unit_test(test_json_add_bool),
868 : cmocka_unit_test(test_json_add_string),
869 : cmocka_unit_test(test_json_add_object),
870 : cmocka_unit_test(test_json_add_to_array),
871 : cmocka_unit_test(test_json_add_timestamp),
872 : cmocka_unit_test(test_json_add_stringn),
873 : cmocka_unit_test(test_json_add_version),
874 : cmocka_unit_test(test_json_add_address),
875 : cmocka_unit_test(test_json_add_sid),
876 : cmocka_unit_test(test_json_add_guid),
877 : cmocka_unit_test(test_json_to_string),
878 : cmocka_unit_test(test_json_get_array),
879 : cmocka_unit_test(test_json_get_object),
880 : cmocka_unit_test(test_audit_get_timestamp),
881 : };
882 :
883 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
884 1 : return cmocka_run_group_tests(tests, NULL, NULL);
885 : }
|