Debian Codegraph @

tests/isc/safe_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 /* ! \file */
15
16 #include <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/safe.h>
28 #include <isc/util.h>
29
30 #include <tests/isc.h>
31
32 /* test isc_safe_memequal() */
33 ISC_RUN_TEST_IMPL(isc_safe_memequal) {
34 UNUSED(state);
35
36 assert_true(isc_safe_memequal("test", "test", 4));
37 assert_true(!isc_safe_memequal("test", "tesc", 4));
38 assert_true(
39 isc_safe_memequal("\x00\x00\x00\x00", "\x00\x00\x00\x00", 4));
40 assert_true(
41 !isc_safe_memequal("\x00\x00\x00\x00", "\x00\x00\x00\x01", 4));
42 assert_true(
43 !isc_safe_memequal("\x00\x00\x00\x02", "\x00\x00\x00\x00", 4));
44 }
45
46 /* test isc_safe_memwipe() */
47 ISC_RUN_TEST_IMPL(isc_safe_memwipe) {
48 UNUSED(state);
49
50 /* These should pass. */
51 isc_safe_memwipe(NULL, 0);
52 isc_safe_memwipe((void *)-1, 0);
53
54 /*
55 * isc_safe_memwipe(ptr, size) should function same as
56 * memset(ptr, 0, size);
57 */
58 {
59 char buf1[4] = { 1, 2, 3, 4 };
60 char buf2[4] = { 1, 2, 3, 4 };
61
62 isc_safe_memwipe(buf1, sizeof(buf1));
63 memset(buf2, 0, sizeof(buf2));
64
65 assert_int_equal(memcmp(buf1, buf2, sizeof(buf1)), 0);
66 }
67
68 /*
69 * Boundary test.
70 */
71 {
72 char buf1[4] = { 1, 2, 3, 4 };
73 char buf2[4] = { 1, 2, 3, 4 };
74
75 /*
76 * We wipe 3 elements on purpose, keeping the 4th in
77 * place.
78 */
79 isc_safe_memwipe(buf1, 3);
80 memset(buf2, 0, 3);
81
82 assert_int_equal(memcmp(buf1, buf2, sizeof(buf1)), 0);
83 }
84 }
85
86 ISC_TEST_LIST_START
87 ISC_TEST_ENTRY(isc_safe_memequal)
88 ISC_TEST_ENTRY(isc_safe_memwipe)
89
90 ISC_TEST_LIST_END
91
92 ISC_TEST_MAIN