Debian Codegraph @

tests/dns/time_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 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/util.h>
27
28 #include <dns/time.h>
29
30 #include <tests/dns.h>
31
32 #define TEST_ORIGIN "test"
33
34 /* value = 0xfffffffff <-> 19691231235959 */
35 ISC_RUN_TEST_IMPL(epoch_minus_one) {
36 const char *test_text = "19691231235959";
37 const uint32_t test_time = 0xffffffff;
38 isc_result_t result;
39 isc_buffer_t target;
40 uint32_t when;
41 char buf[128];
42
43 UNUSED(state);
44
45 memset(buf, 0, sizeof(buf));
46 isc_buffer_init(&target, buf, sizeof(buf));
47 result = dns_time32_totext(test_time, &target);
48 assert_int_equal(result, ISC_R_SUCCESS);
49 assert_string_equal(buf, test_text);
50 result = dns_time32_fromtext(test_text, &when);
51 assert_int_equal(result, ISC_R_SUCCESS);
52 assert_int_equal(when, test_time);
53 }
54
55 /* value = 0x000000000 <-> 19700101000000*/
56 ISC_RUN_TEST_IMPL(epoch) {
57 const char *test_text = "19700101000000";
58 const uint32_t test_time = 0x00000000;
59 isc_result_t result;
60 isc_buffer_t target;
61 uint32_t when;
62 char buf[128];
63
64 UNUSED(state);
65
66 memset(buf, 0, sizeof(buf));
67 isc_buffer_init(&target, buf, sizeof(buf));
68 result = dns_time32_totext(test_time, &target);
69 assert_int_equal(result, ISC_R_SUCCESS);
70 assert_string_equal(buf, test_text);
71 result = dns_time32_fromtext(test_text, &when);
72 assert_int_equal(result, ISC_R_SUCCESS);
73 assert_int_equal(when, test_time);
74 }
75
76 /* value = 0x7fffffff <-> 20380119031407 */
77 ISC_RUN_TEST_IMPL(half_maxint) {
78 const char *test_text = "20380119031407";
79 const uint32_t test_time = 0x7fffffff;
80 isc_result_t result;
81 isc_buffer_t target;
82 uint32_t when;
83 char buf[128];
84
85 UNUSED(state);
86
87 memset(buf, 0, sizeof(buf));
88 isc_buffer_init(&target, buf, sizeof(buf));
89 result = dns_time32_totext(test_time, &target);
90 assert_int_equal(result, ISC_R_SUCCESS);
91 assert_string_equal(buf, test_text);
92 result = dns_time32_fromtext(test_text, &when);
93 assert_int_equal(result, ISC_R_SUCCESS);
94 assert_int_equal(when, test_time);
95 }
96
97 /* value = 0x80000000 <-> 20380119031408 */
98 ISC_RUN_TEST_IMPL(half_plus_one) {
99 const char *test_text = "20380119031408";
100 const uint32_t test_time = 0x80000000;
101 isc_result_t result;
102 isc_buffer_t target;
103 uint32_t when;
104 char buf[128];
105
106 UNUSED(state);
107
108 memset(buf, 0, sizeof(buf));
109 isc_buffer_init(&target, buf, sizeof(buf));
110 result = dns_time32_totext(test_time, &target);
111 assert_int_equal(result, ISC_R_SUCCESS);
112 assert_string_equal(buf, test_text);
113 result = dns_time32_fromtext(test_text, &when);
114 assert_int_equal(result, ISC_R_SUCCESS);
115 assert_int_equal(when, test_time);
116 }
117
118 /* value = 0xef68f5d0 <-> 19610307130000 */
119 ISC_RUN_TEST_IMPL(fifty_before) {
120 isc_result_t result;
121 const char *test_text = "19610307130000";
122 const uint32_t test_time = 0xef68f5d0;
123 isc_buffer_t target;
124 uint32_t when;
125 char buf[128];
126
127 UNUSED(state);
128
129 memset(buf, 0, sizeof(buf));
130 isc_buffer_init(&target, buf, sizeof(buf));
131 result = dns_time32_totext(test_time, &target);
132 assert_int_equal(result, ISC_R_SUCCESS);
133 assert_string_equal(buf, test_text);
134 result = dns_time32_fromtext(test_text, &when);
135 assert_int_equal(result, ISC_R_SUCCESS);
136 assert_int_equal(when, test_time);
137 }
138
139 /* value = 0x4d74d6d0 <-> 20110307130000 */
140 ISC_RUN_TEST_IMPL(some_ago) {
141 const char *test_text = "20110307130000";
142 const uint32_t test_time = 0x4d74d6d0;
143 isc_result_t result;
144 isc_buffer_t target;
145 uint32_t when;
146 char buf[128];
147
148 UNUSED(state);
149
150 memset(buf, 0, sizeof(buf));
151 isc_buffer_init(&target, buf, sizeof(buf));
152 result = dns_time32_totext(test_time, &target);
153 assert_int_equal(result, ISC_R_SUCCESS);
154 assert_string_equal(buf, test_text);
155 result = dns_time32_fromtext(test_text, &when);
156 assert_int_equal(result, ISC_R_SUCCESS);
157 assert_int_equal(when, test_time);
158 }
159
160 ISC_TEST_LIST_START
161 ISC_TEST_ENTRY(epoch_minus_one)
162 ISC_TEST_ENTRY(epoch)
163 ISC_TEST_ENTRY(half_maxint)
164 ISC_TEST_ENTRY(half_plus_one)
165 ISC_TEST_ENTRY(fifty_before)
166 ISC_TEST_ENTRY(some_ago)
167 ISC_TEST_LIST_END
168
169 ISC_TEST_MAIN