1 /* $NetBSD: time_test.c,v 1.1.1.4 2014/12/10 03:34:43 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* Id */ 20 21 /*! \file */ 22 23 #include <config.h> 24 25 #include <atf-c.h> 26 27 #include <unistd.h> 28 29 #include <dns/time.h> 30 31 #include "dnstest.h" 32 33 #define TEST_ORIGIN "test" 34 35 /* 36 * Individual unit tests 37 */ 38 39 /* value = 0xfffffffff <-> 19691231235959 */ 40 ATF_TC(epoch_minus_one); 41 ATF_TC_HEAD(epoch_minus_one, tc) { 42 atf_tc_set_md_var(tc, "descr", "0xffffffff <-> 19691231235959"); 43 } 44 ATF_TC_BODY(epoch_minus_one, tc) { 45 const char *test_text = "19691231235959"; 46 const isc_uint32_t test_time = 0xffffffff; 47 isc_result_t result; 48 isc_buffer_t target; 49 isc_uint32_t when; 50 char buf[128]; 51 52 UNUSED(tc); 53 54 result = dns_test_begin(NULL, ISC_FALSE); 55 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 56 memset(buf, 0, sizeof(buf)); 57 isc_buffer_init(&target, buf, sizeof(buf)); 58 result = dns_time32_totext(test_time, &target); 59 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 60 ATF_REQUIRE_STREQ(buf, test_text); 61 result = dns_time32_fromtext(test_text, &when); 62 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 63 ATF_REQUIRE_EQ(when, test_time); 64 dns_test_end(); 65 } 66 67 /* value = 0x000000000 <-> 19700101000000*/ 68 ATF_TC(epoch); 69 ATF_TC_HEAD(epoch, tc) { 70 atf_tc_set_md_var(tc, "descr", "0x00000000 <-> 19700101000000"); 71 } 72 ATF_TC_BODY(epoch, tc) { 73 const char *test_text = "19700101000000"; 74 const isc_uint32_t test_time = 0x00000000; 75 isc_result_t result; 76 isc_buffer_t target; 77 isc_uint32_t when; 78 char buf[128]; 79 80 UNUSED(tc); 81 82 result = dns_test_begin(NULL, ISC_FALSE); 83 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 84 memset(buf, 0, sizeof(buf)); 85 isc_buffer_init(&target, buf, sizeof(buf)); 86 result = dns_time32_totext(test_time, &target); 87 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 88 ATF_REQUIRE_STREQ(buf, test_text); 89 result = dns_time32_fromtext(test_text, &when); 90 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 91 ATF_REQUIRE_EQ(when, test_time); 92 dns_test_end(); 93 } 94 95 /* value = 0x7fffffff <-> 20380119031407 */ 96 ATF_TC(half_maxint); 97 ATF_TC_HEAD(half_maxint, tc) { 98 atf_tc_set_md_var(tc, "descr", "0x7fffffff <-> 20380119031407"); 99 } 100 ATF_TC_BODY(half_maxint, tc) { 101 const char *test_text = "20380119031407"; 102 const isc_uint32_t test_time = 0x7fffffff; 103 isc_result_t result; 104 isc_buffer_t target; 105 isc_uint32_t when; 106 char buf[128]; 107 108 UNUSED(tc); 109 110 result = dns_test_begin(NULL, ISC_FALSE); 111 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 112 memset(buf, 0, sizeof(buf)); 113 isc_buffer_init(&target, buf, sizeof(buf)); 114 result = dns_time32_totext(test_time, &target); 115 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 116 ATF_REQUIRE_STREQ(buf, test_text); 117 result = dns_time32_fromtext(test_text, &when); 118 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 119 ATF_REQUIRE_EQ(when, test_time); 120 dns_test_end(); 121 } 122 123 /* value = 0x80000000 <-> 20380119031408 */ 124 ATF_TC(half_plus_one); 125 ATF_TC_HEAD(half_plus_one, tc) { 126 atf_tc_set_md_var(tc, "descr", "0x80000000 <-> 20380119031408"); 127 } 128 ATF_TC_BODY(half_plus_one, tc) { 129 const char *test_text = "20380119031408"; 130 const isc_uint32_t test_time = 0x80000000; 131 isc_result_t result; 132 isc_buffer_t target; 133 isc_uint32_t when; 134 char buf[128]; 135 136 UNUSED(tc); 137 138 result = dns_test_begin(NULL, ISC_FALSE); 139 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 140 memset(buf, 0, sizeof(buf)); 141 isc_buffer_init(&target, buf, sizeof(buf)); 142 result = dns_time32_totext(test_time, &target); 143 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 144 ATF_REQUIRE_STREQ(buf, test_text); 145 result = dns_time32_fromtext(test_text, &when); 146 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 147 ATF_REQUIRE_EQ(when, test_time); 148 dns_test_end(); 149 } 150 151 /* value = 0xef68f5d0 <-> 19610307130000 */ 152 ATF_TC(fifty_before); 153 ATF_TC_HEAD(fifty_before, tc) { 154 atf_tc_set_md_var(tc, "descr", "0xef68f5d0 <-> 19610307130000"); 155 } 156 ATF_TC_BODY(fifty_before, tc) { 157 isc_result_t result; 158 const char *test_text = "19610307130000"; 159 const isc_uint32_t test_time = 0xef68f5d0; 160 isc_buffer_t target; 161 isc_uint32_t when; 162 char buf[128]; 163 164 UNUSED(tc); 165 166 result = dns_test_begin(NULL, ISC_FALSE); 167 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 168 memset(buf, 0, sizeof(buf)); 169 isc_buffer_init(&target, buf, sizeof(buf)); 170 result = dns_time32_totext(test_time, &target); 171 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 172 ATF_REQUIRE_STREQ(buf, test_text); 173 result = dns_time32_fromtext(test_text, &when); 174 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 175 ATF_REQUIRE_EQ(when, test_time); 176 dns_test_end(); 177 } 178 179 /* value = 0x4d74d6d0 <-> 20110307130000 */ 180 ATF_TC(some_ago); 181 ATF_TC_HEAD(some_ago, tc) { 182 atf_tc_set_md_var(tc, "descr", "0x4d74d6d0 <-> 20110307130000"); 183 } 184 ATF_TC_BODY(some_ago, tc) { 185 const char *test_text = "20110307130000"; 186 const isc_uint32_t test_time = 0x4d74d6d0; 187 isc_result_t result; 188 isc_buffer_t target; 189 isc_uint32_t when; 190 char buf[128]; 191 192 UNUSED(tc); 193 194 result = dns_test_begin(NULL, ISC_FALSE); 195 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 196 memset(buf, 0, sizeof(buf)); 197 isc_buffer_init(&target, buf, sizeof(buf)); 198 result = dns_time32_totext(test_time, &target); 199 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 200 ATF_REQUIRE_STREQ(buf, test_text); 201 result = dns_time32_fromtext(test_text, &when); 202 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 203 ATF_REQUIRE_EQ(when, test_time); 204 dns_test_end(); 205 } 206 207 /* 208 * Main 209 */ 210 ATF_TP_ADD_TCS(tp) { 211 ATF_TP_ADD_TC(tp, epoch_minus_one); 212 ATF_TP_ADD_TC(tp, epoch); 213 ATF_TP_ADD_TC(tp, half_maxint); 214 ATF_TP_ADD_TC(tp, half_plus_one); 215 ATF_TP_ADD_TC(tp, fifty_before); 216 ATF_TP_ADD_TC(tp, some_ago); 217 218 return (atf_no_error()); 219 } 220 221