update_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011, 2012, 2014  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 #include <time.h>
00027 
00028 #include <isc/serial.h>
00029 #include <isc/stdtime.h>
00030 
00031 #include <dns/update.h>
00032 
00033 #include "dnstest.h"
00034 
00035 static isc_uint32_t mystdtime;
00036 
00037 static void set_mystdtime(int year, int month, int day) {
00038         struct tm tm;
00039 
00040         memset(&tm, 0, sizeof(tm));
00041         tm.tm_year = year - 1900;
00042         tm.tm_mon = month - 1;
00043         tm.tm_mday = day;
00044         mystdtime = timegm(&tm) ;
00045 }
00046 
00047 void isc_stdtime_get(isc_stdtime_t *now) {
00048         *now = mystdtime;
00049 }
00050 
00051 /*
00052  * Individual unit tests
00053  */
00054 
00055 ATF_TC(increment);
00056 ATF_TC_HEAD(increment, tc) {
00057   atf_tc_set_md_var(tc, "descr", "simple increment by 1");
00058 }
00059 ATF_TC_BODY(increment, tc) {
00060         isc_uint32_t old = 50;
00061         isc_uint32_t new;
00062         isc_result_t result;
00063 
00064         UNUSED(tc);
00065 
00066         result = dns_test_begin(NULL, ISC_FALSE);
00067         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00068         new = dns_update_soaserial(old, dns_updatemethod_increment);
00069         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00070         ATF_CHECK_MSG(new != 0, "new (%d) should not equal 0", new);
00071         ATF_REQUIRE_EQ(new, 51);
00072         dns_test_end();
00073 }
00074 
00075 /* 0xfffffffff -> 1 */
00076 ATF_TC(increment_past_zero);
00077 ATF_TC_HEAD(increment_past_zero, tc) {
00078   atf_tc_set_md_var(tc, "descr", "increment past zero, ffffffff -> 1");
00079 }
00080 ATF_TC_BODY(increment_past_zero, tc) {
00081         isc_uint32_t old = 0xffffffffu;
00082         isc_uint32_t new;
00083         isc_result_t result;
00084 
00085         UNUSED(tc);
00086 
00087         result = dns_test_begin(NULL, ISC_FALSE);
00088         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00089         new = dns_update_soaserial(old, dns_updatemethod_increment);
00090         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00091         ATF_CHECK(new != 0);
00092         ATF_REQUIRE_EQ(new, 1u);
00093         dns_test_end();
00094 }
00095 
00096 ATF_TC(past_to_unix);
00097 ATF_TC_HEAD(past_to_unix, tc) {
00098   atf_tc_set_md_var(tc, "descr", "past to unixtime");
00099 }
00100 ATF_TC_BODY(past_to_unix, tc) {
00101         isc_uint32_t old;
00102         isc_uint32_t new;
00103         isc_result_t result;
00104 
00105         UNUSED(tc);
00106 
00107         set_mystdtime(2011, 6, 22);
00108         old = mystdtime - 1;
00109 
00110         result = dns_test_begin(NULL, ISC_FALSE);
00111         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00112         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00113         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00114         ATF_CHECK(new != 0);
00115         ATF_REQUIRE_EQ(new, mystdtime);
00116         dns_test_end();
00117 }
00118 
00119 ATF_TC(now_to_unix);
00120 ATF_TC_HEAD(now_to_unix, tc) {
00121   atf_tc_set_md_var(tc, "descr", "now to unixtime");
00122 }
00123 ATF_TC_BODY(now_to_unix, tc) {
00124         isc_uint32_t old;
00125         isc_uint32_t new;
00126         isc_result_t result;
00127 
00128         UNUSED(tc);
00129 
00130         set_mystdtime(2011, 6, 22);
00131         old = mystdtime;
00132 
00133         result = dns_test_begin(NULL, ISC_FALSE);
00134         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00135         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00136         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00137         ATF_CHECK(new != 0);
00138         ATF_REQUIRE_EQ(new, old + 1);
00139         dns_test_end();
00140 }
00141 
00142 ATF_TC(future_to_unix);
00143 ATF_TC_HEAD(future_to_unix, tc) {
00144   atf_tc_set_md_var(tc, "descr", "future to unixtime");
00145 }
00146 ATF_TC_BODY(future_to_unix, tc) {
00147         isc_uint32_t old;
00148         isc_uint32_t new;
00149         isc_result_t result;
00150 
00151         UNUSED(tc);
00152 
00153         set_mystdtime(2011, 6, 22);
00154         old = mystdtime + 1;
00155 
00156         result = dns_test_begin(NULL, ISC_FALSE);
00157         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00158         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00159         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00160         ATF_CHECK(new != 0);
00161         ATF_REQUIRE_EQ(new, old + 1);
00162         dns_test_end();
00163 }
00164 
00165 ATF_TC(undefined_plus1_to_unix);
00166 ATF_TC_HEAD(undefined_plus1_to_unix, tc) {
00167   atf_tc_set_md_var(tc, "descr", "undefined plus 1 to unixtime");
00168 }
00169 ATF_TC_BODY(undefined_plus1_to_unix, tc) {
00170         isc_uint32_t old;
00171         isc_uint32_t new;
00172         isc_result_t result;
00173 
00174         UNUSED(tc);
00175 
00176         set_mystdtime(2011, 6, 22);
00177         old = mystdtime ^ 0x80000000u;
00178         old += 1;
00179 
00180         result = dns_test_begin(NULL, ISC_FALSE);
00181         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00182         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00183         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00184         ATF_CHECK(new != 0);
00185         ATF_REQUIRE_EQ(new, mystdtime);
00186         dns_test_end();
00187 }
00188 
00189 ATF_TC(undefined_minus1_to_unix);
00190 ATF_TC_HEAD(undefined_minus1_to_unix, tc) {
00191   atf_tc_set_md_var(tc, "descr", "undefined minus 1 to unixtime");
00192 }
00193 ATF_TC_BODY(undefined_minus1_to_unix, tc) {
00194         isc_uint32_t old;
00195         isc_uint32_t new;
00196         isc_result_t result;
00197 
00198         UNUSED(tc);
00199 
00200         set_mystdtime(2011, 6, 22);
00201         old = mystdtime ^ 0x80000000u;
00202         old -= 1;
00203 
00204         result = dns_test_begin(NULL, ISC_FALSE);
00205         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00206         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00207         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00208         ATF_CHECK(new != 0);
00209         ATF_REQUIRE_EQ(new, old + 1);
00210         dns_test_end();
00211 }
00212 
00213 ATF_TC(undefined_to_unix);
00214 ATF_TC_HEAD(undefined_to_unix, tc) {
00215   atf_tc_set_md_var(tc, "descr", "undefined to unixtime");
00216 }
00217 ATF_TC_BODY(undefined_to_unix, tc) {
00218         isc_uint32_t old;
00219         isc_uint32_t new;
00220         isc_result_t result;
00221 
00222         UNUSED(tc);
00223 
00224         set_mystdtime(2011, 6, 22);
00225         old = mystdtime ^ 0x80000000u;
00226 
00227         result = dns_test_begin(NULL, ISC_FALSE);
00228         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00229         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00230         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00231         ATF_CHECK(new != 0);
00232         ATF_REQUIRE_EQ(new, old + 1);
00233         dns_test_end();
00234 }
00235 
00236 ATF_TC(unixtime_zero);
00237 ATF_TC_HEAD(unixtime_zero, tc) {
00238   atf_tc_set_md_var(tc, "descr", "handle unixtime being zero");
00239 }
00240 ATF_TC_BODY(unixtime_zero, tc) {
00241         isc_uint32_t old;
00242         isc_uint32_t new;
00243         isc_result_t result;
00244 
00245         UNUSED(tc);
00246 
00247         mystdtime = 0;
00248         old = 0xfffffff0;
00249 
00250         result = dns_test_begin(NULL, ISC_FALSE);
00251         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00252         new = dns_update_soaserial(old, dns_updatemethod_unixtime);
00253         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00254         ATF_CHECK(new != 0);
00255         ATF_REQUIRE_EQ(new, old + 1);
00256         dns_test_end();
00257 }
00258 
00259 ATF_TC(past_to_date);
00260 ATF_TC_HEAD(past_to_date, tc) {
00261   atf_tc_set_md_var(tc, "descr", "past to date");
00262 }
00263 ATF_TC_BODY(past_to_date, tc) {
00264         isc_uint32_t old, new;
00265         isc_result_t result;
00266 
00267         UNUSED(tc);
00268 
00269         set_mystdtime(2014, 3, 31);
00270         old = dns_update_soaserial(0, dns_updatemethod_date);
00271         set_mystdtime(2014, 4, 1);
00272 
00273         result = dns_test_begin(NULL, ISC_FALSE);
00274         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00275         new = dns_update_soaserial(old, dns_updatemethod_date);
00276         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00277         ATF_CHECK(new != 0);
00278         ATF_REQUIRE_EQ(new, 2014040100);
00279         dns_test_end();
00280 }
00281 
00282 ATF_TC(now_to_date);
00283 ATF_TC_HEAD(now_to_date, tc) {
00284   atf_tc_set_md_var(tc, "descr", "now to date");
00285 }
00286 ATF_TC_BODY(now_to_date, tc) {
00287         isc_uint32_t old;
00288         isc_uint32_t new;
00289         isc_result_t result;
00290 
00291         UNUSED(tc);
00292 
00293         set_mystdtime(2014, 4, 1);
00294         old = dns_update_soaserial(0, dns_updatemethod_date);
00295 
00296         result = dns_test_begin(NULL, ISC_FALSE);
00297         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00298         new = dns_update_soaserial(old, dns_updatemethod_date);
00299         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00300         ATF_CHECK(new != 0);
00301         ATF_REQUIRE_EQ(new, 2014040101);
00302         dns_test_end();
00303 }
00304 
00305 ATF_TC(future_to_date);
00306 ATF_TC_HEAD(future_to_date, tc) {
00307   atf_tc_set_md_var(tc, "descr", "future to date");
00308 }
00309 ATF_TC_BODY(future_to_date, tc) {
00310         isc_uint32_t old;
00311         isc_uint32_t new;
00312         isc_result_t result;
00313 
00314         UNUSED(tc);
00315 
00316         set_mystdtime(2014, 4, 1);
00317         old = dns_update_soaserial(0, dns_updatemethod_date);
00318         set_mystdtime(2014, 3, 31);
00319 
00320         result = dns_test_begin(NULL, ISC_FALSE);
00321         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00322         new = dns_update_soaserial(old, dns_updatemethod_date);
00323         ATF_REQUIRE_EQ(isc_serial_lt(old, new), ISC_TRUE);
00324         ATF_CHECK(new != 0);
00325         ATF_REQUIRE_EQ(new, 2014040101);
00326         dns_test_end();
00327 }
00328 
00329 /*
00330  * Main
00331  */
00332 ATF_TP_ADD_TCS(tp) {
00333         ATF_TP_ADD_TC(tp, increment);
00334         ATF_TP_ADD_TC(tp, increment_past_zero);
00335         ATF_TP_ADD_TC(tp, past_to_unix);
00336         ATF_TP_ADD_TC(tp, now_to_unix);
00337         ATF_TP_ADD_TC(tp, future_to_unix);
00338         ATF_TP_ADD_TC(tp, undefined_to_unix);
00339         ATF_TP_ADD_TC(tp, undefined_plus1_to_unix);
00340         ATF_TP_ADD_TC(tp, undefined_minus1_to_unix);
00341         ATF_TP_ADD_TC(tp, unixtime_zero);
00342         ATF_TP_ADD_TC(tp, past_to_date);
00343         ATF_TP_ADD_TC(tp, now_to_date);
00344         ATF_TP_ADD_TC(tp, future_to_date);
00345 
00346         return (atf_no_error());
00347 }
00348 

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