Home | History | Annotate | Line # | Download | only in dns
      1 /*	$NetBSD: time_test.c,v 1.3 2025/01/26 16:25:48 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     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 <stdio.h>
     22 #include <stdlib.h>
     23 #include <unistd.h>
     24 
     25 #define UNIT_TESTING
     26 #include <cmocka.h>
     27 
     28 #include <isc/util.h>
     29 
     30 #include <dns/time.h>
     31 
     32 #include <tests/dns.h>
     33 
     34 #define TEST_ORIGIN "test"
     35 
     36 /* value = 0xfffffffff <-> 19691231235959 */
     37 ISC_RUN_TEST_IMPL(epoch_minus_one) {
     38 	const char *test_text = "19691231235959";
     39 	const uint32_t test_time = 0xffffffff;
     40 	isc_result_t result;
     41 	isc_buffer_t target;
     42 	uint32_t when;
     43 	char buf[128];
     44 
     45 	UNUSED(state);
     46 
     47 	memset(buf, 0, sizeof(buf));
     48 	isc_buffer_init(&target, buf, sizeof(buf));
     49 	result = dns_time32_totext(test_time, &target);
     50 	assert_int_equal(result, ISC_R_SUCCESS);
     51 	assert_string_equal(buf, test_text);
     52 	result = dns_time32_fromtext(test_text, &when);
     53 	assert_int_equal(result, ISC_R_SUCCESS);
     54 	assert_int_equal(when, test_time);
     55 }
     56 
     57 /* value = 0x000000000 <-> 19700101000000*/
     58 ISC_RUN_TEST_IMPL(epoch) {
     59 	const char *test_text = "19700101000000";
     60 	const uint32_t test_time = 0x00000000;
     61 	isc_result_t result;
     62 	isc_buffer_t target;
     63 	uint32_t when;
     64 	char buf[128];
     65 
     66 	UNUSED(state);
     67 
     68 	memset(buf, 0, sizeof(buf));
     69 	isc_buffer_init(&target, buf, sizeof(buf));
     70 	result = dns_time32_totext(test_time, &target);
     71 	assert_int_equal(result, ISC_R_SUCCESS);
     72 	assert_string_equal(buf, test_text);
     73 	result = dns_time32_fromtext(test_text, &when);
     74 	assert_int_equal(result, ISC_R_SUCCESS);
     75 	assert_int_equal(when, test_time);
     76 }
     77 
     78 /* value = 0x7fffffff <-> 20380119031407 */
     79 ISC_RUN_TEST_IMPL(half_maxint) {
     80 	const char *test_text = "20380119031407";
     81 	const uint32_t test_time = 0x7fffffff;
     82 	isc_result_t result;
     83 	isc_buffer_t target;
     84 	uint32_t when;
     85 	char buf[128];
     86 
     87 	UNUSED(state);
     88 
     89 	memset(buf, 0, sizeof(buf));
     90 	isc_buffer_init(&target, buf, sizeof(buf));
     91 	result = dns_time32_totext(test_time, &target);
     92 	assert_int_equal(result, ISC_R_SUCCESS);
     93 	assert_string_equal(buf, test_text);
     94 	result = dns_time32_fromtext(test_text, &when);
     95 	assert_int_equal(result, ISC_R_SUCCESS);
     96 	assert_int_equal(when, test_time);
     97 }
     98 
     99 /* value = 0x80000000 <-> 20380119031408 */
    100 ISC_RUN_TEST_IMPL(half_plus_one) {
    101 	const char *test_text = "20380119031408";
    102 	const uint32_t test_time = 0x80000000;
    103 	isc_result_t result;
    104 	isc_buffer_t target;
    105 	uint32_t when;
    106 	char buf[128];
    107 
    108 	UNUSED(state);
    109 
    110 	memset(buf, 0, sizeof(buf));
    111 	isc_buffer_init(&target, buf, sizeof(buf));
    112 	result = dns_time32_totext(test_time, &target);
    113 	assert_int_equal(result, ISC_R_SUCCESS);
    114 	assert_string_equal(buf, test_text);
    115 	result = dns_time32_fromtext(test_text, &when);
    116 	assert_int_equal(result, ISC_R_SUCCESS);
    117 	assert_int_equal(when, test_time);
    118 }
    119 
    120 /* value = 0xef68f5d0 <-> 19610307130000 */
    121 ISC_RUN_TEST_IMPL(fifty_before) {
    122 	isc_result_t result;
    123 	const char *test_text = "19610307130000";
    124 	const uint32_t test_time = 0xef68f5d0;
    125 	isc_buffer_t target;
    126 	uint32_t when;
    127 	char buf[128];
    128 
    129 	UNUSED(state);
    130 
    131 	memset(buf, 0, sizeof(buf));
    132 	isc_buffer_init(&target, buf, sizeof(buf));
    133 	result = dns_time32_totext(test_time, &target);
    134 	assert_int_equal(result, ISC_R_SUCCESS);
    135 	assert_string_equal(buf, test_text);
    136 	result = dns_time32_fromtext(test_text, &when);
    137 	assert_int_equal(result, ISC_R_SUCCESS);
    138 	assert_int_equal(when, test_time);
    139 }
    140 
    141 /* value = 0x4d74d6d0 <-> 20110307130000 */
    142 ISC_RUN_TEST_IMPL(some_ago) {
    143 	const char *test_text = "20110307130000";
    144 	const uint32_t test_time = 0x4d74d6d0;
    145 	isc_result_t result;
    146 	isc_buffer_t target;
    147 	uint32_t when;
    148 	char buf[128];
    149 
    150 	UNUSED(state);
    151 
    152 	memset(buf, 0, sizeof(buf));
    153 	isc_buffer_init(&target, buf, sizeof(buf));
    154 	result = dns_time32_totext(test_time, &target);
    155 	assert_int_equal(result, ISC_R_SUCCESS);
    156 	assert_string_equal(buf, test_text);
    157 	result = dns_time32_fromtext(test_text, &when);
    158 	assert_int_equal(result, ISC_R_SUCCESS);
    159 	assert_int_equal(when, test_time);
    160 }
    161 
    162 ISC_TEST_LIST_START
    163 ISC_TEST_ENTRY(epoch_minus_one)
    164 ISC_TEST_ENTRY(epoch)
    165 ISC_TEST_ENTRY(half_maxint)
    166 ISC_TEST_ENTRY(half_plus_one)
    167 ISC_TEST_ENTRY(fifty_before)
    168 ISC_TEST_ENTRY(some_ago)
    169 ISC_TEST_LIST_END
    170 
    171 ISC_TEST_MAIN
    172