time_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 
00027 #include <dns/time.h>
00028 
00029 #include "dnstest.h"
00030 
00031 #define TEST_ORIGIN     "test"
00032 
00033 /*
00034  * Individual unit tests
00035  */
00036 
00037 /* value = 0xfffffffff <-> 19691231235959 */
00038 ATF_TC(epoch_minus_one);
00039 ATF_TC_HEAD(epoch_minus_one, tc) {
00040   atf_tc_set_md_var(tc, "descr", "0xffffffff <-> 19691231235959");
00041 }
00042 ATF_TC_BODY(epoch_minus_one, tc) {
00043         const char *test_text = "19691231235959";
00044         const isc_uint32_t test_time = 0xffffffff;
00045         isc_result_t result;
00046         isc_buffer_t target;
00047         isc_uint32_t when;
00048         char buf[128];
00049 
00050         UNUSED(tc);
00051 
00052         result = dns_test_begin(NULL, ISC_FALSE);
00053         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00054         memset(buf, 0, sizeof(buf));
00055         isc_buffer_init(&target, buf, sizeof(buf));
00056         result = dns_time32_totext(test_time, &target);
00057         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00058         ATF_REQUIRE_STREQ(buf, test_text);
00059         result = dns_time32_fromtext(test_text, &when);
00060         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00061         ATF_REQUIRE_EQ(when, test_time);
00062         dns_test_end();
00063 }
00064 
00065 /* value = 0x000000000 <-> 19700101000000*/
00066 ATF_TC(epoch);
00067 ATF_TC_HEAD(epoch, tc) {
00068   atf_tc_set_md_var(tc, "descr", "0x00000000 <-> 19700101000000");
00069 }
00070 ATF_TC_BODY(epoch, tc) {
00071         const char *test_text = "19700101000000";
00072         const isc_uint32_t test_time = 0x00000000;
00073         isc_result_t result;
00074         isc_buffer_t target;
00075         isc_uint32_t when;
00076         char buf[128];
00077 
00078         UNUSED(tc);
00079 
00080         result = dns_test_begin(NULL, ISC_FALSE);
00081         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00082         memset(buf, 0, sizeof(buf));
00083         isc_buffer_init(&target, buf, sizeof(buf));
00084         result = dns_time32_totext(test_time, &target);
00085         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00086         ATF_REQUIRE_STREQ(buf, test_text);
00087         result = dns_time32_fromtext(test_text, &when);
00088         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00089         ATF_REQUIRE_EQ(when, test_time);
00090         dns_test_end();
00091 }
00092 
00093 /* value = 0x7fffffff <-> 20380119031407 */
00094 ATF_TC(half_maxint);
00095 ATF_TC_HEAD(half_maxint, tc) {
00096   atf_tc_set_md_var(tc, "descr", "0x7fffffff <-> 20380119031407");
00097 }
00098 ATF_TC_BODY(half_maxint, tc) {
00099         const char *test_text = "20380119031407";
00100         const isc_uint32_t test_time = 0x7fffffff;
00101         isc_result_t result;
00102         isc_buffer_t target;
00103         isc_uint32_t when;
00104         char buf[128];
00105 
00106         UNUSED(tc);
00107 
00108         result = dns_test_begin(NULL, ISC_FALSE);
00109         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00110         memset(buf, 0, sizeof(buf));
00111         isc_buffer_init(&target, buf, sizeof(buf));
00112         result = dns_time32_totext(test_time, &target);
00113         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00114         ATF_REQUIRE_STREQ(buf, test_text);
00115         result = dns_time32_fromtext(test_text, &when);
00116         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00117         ATF_REQUIRE_EQ(when, test_time);
00118         dns_test_end();
00119 }
00120 
00121 /* value = 0x80000000 <-> 20380119031408 */
00122 ATF_TC(half_plus_one);
00123 ATF_TC_HEAD(half_plus_one, tc) {
00124   atf_tc_set_md_var(tc, "descr", "0x80000000 <-> 20380119031408");
00125 }
00126 ATF_TC_BODY(half_plus_one, tc) {
00127         const char *test_text = "20380119031408";
00128         const isc_uint32_t test_time = 0x80000000;
00129         isc_result_t result;
00130         isc_buffer_t target;
00131         isc_uint32_t when;
00132         char buf[128];
00133 
00134         UNUSED(tc);
00135 
00136         result = dns_test_begin(NULL, ISC_FALSE);
00137         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00138         memset(buf, 0, sizeof(buf));
00139         isc_buffer_init(&target, buf, sizeof(buf));
00140         result = dns_time32_totext(test_time, &target);
00141         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00142         ATF_REQUIRE_STREQ(buf, test_text);
00143         result = dns_time32_fromtext(test_text, &when);
00144         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00145         ATF_REQUIRE_EQ(when, test_time);
00146         dns_test_end();
00147 }
00148 
00149 /* value = 0xef68f5d0 <-> 19610307130000 */
00150 ATF_TC(fifty_before);
00151 ATF_TC_HEAD(fifty_before, tc) {
00152   atf_tc_set_md_var(tc, "descr", "0xef68f5d0 <-> 19610307130000");
00153 }
00154 ATF_TC_BODY(fifty_before, tc) {
00155         isc_result_t result;
00156         const char *test_text = "19610307130000";
00157         const isc_uint32_t test_time = 0xef68f5d0;
00158         isc_buffer_t target;
00159         isc_uint32_t when;
00160         char buf[128];
00161 
00162         UNUSED(tc);
00163 
00164         result = dns_test_begin(NULL, ISC_FALSE);
00165         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00166         memset(buf, 0, sizeof(buf));
00167         isc_buffer_init(&target, buf, sizeof(buf));
00168         result = dns_time32_totext(test_time, &target);
00169         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00170         ATF_REQUIRE_STREQ(buf, test_text);
00171         result = dns_time32_fromtext(test_text, &when);
00172         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00173         ATF_REQUIRE_EQ(when, test_time);
00174         dns_test_end();
00175 }
00176 
00177 /* value = 0x4d74d6d0 <-> 20110307130000 */
00178 ATF_TC(some_ago);
00179 ATF_TC_HEAD(some_ago, tc) {
00180   atf_tc_set_md_var(tc, "descr", "0x4d74d6d0 <-> 20110307130000");
00181 }
00182 ATF_TC_BODY(some_ago, tc) {
00183         const char *test_text = "20110307130000";
00184         const isc_uint32_t test_time = 0x4d74d6d0;
00185         isc_result_t result;
00186         isc_buffer_t target;
00187         isc_uint32_t when;
00188         char buf[128];
00189 
00190         UNUSED(tc);
00191 
00192         result = dns_test_begin(NULL, ISC_FALSE);
00193         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00194         memset(buf, 0, sizeof(buf));
00195         isc_buffer_init(&target, buf, sizeof(buf));
00196         result = dns_time32_totext(test_time, &target);
00197         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00198         ATF_REQUIRE_STREQ(buf, test_text);
00199         result = dns_time32_fromtext(test_text, &when);
00200         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00201         ATF_REQUIRE_EQ(when, test_time);
00202         dns_test_end();
00203 }
00204 
00205 /*
00206  * Main
00207  */
00208 ATF_TP_ADD_TCS(tp) {
00209         ATF_TP_ADD_TC(tp, epoch_minus_one);
00210         ATF_TP_ADD_TC(tp, epoch);
00211         ATF_TP_ADD_TC(tp, half_maxint);
00212         ATF_TP_ADD_TC(tp, half_plus_one);
00213         ATF_TP_ADD_TC(tp, fifty_before);
00214         ATF_TP_ADD_TC(tp, some_ago);
00215 
00216         return (atf_no_error());
00217 }
00218 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha