1 /* $NetBSD: update_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 <time.h> 24 #include <unistd.h> 25 26 #define UNIT_TESTING 27 #include <cmocka.h> 28 29 #include <isc/serial.h> 30 #include <isc/stdtime.h> 31 #include <isc/util.h> 32 33 #include <dns/update.h> 34 #define KEEP_BEFORE 35 36 /* 37 * Fix the linking order problem for overridden isc_stdtime_now() by making 38 * everything local. This also allows static functions from update.c to be 39 * tested. 40 */ 41 #pragma GCC diagnostic push 42 #pragma GCC diagnostic ignored "-Wshadow" 43 #undef CHECK 44 #include "update.c" 45 #pragma GCC diagnostic pop 46 47 #undef CHECK 48 #include <tests/dns.h> 49 50 static int 51 setup_test(void **state) { 52 UNUSED(state); 53 54 setenv("TZ", "", 1); 55 56 return 0; 57 } 58 59 static uint32_t mystdtime; 60 61 static void 62 set_mystdtime(int year, int month, int day) { 63 struct tm tm; 64 65 memset(&tm, 0, sizeof(tm)); 66 tm.tm_year = year - 1900; 67 tm.tm_mon = month - 1; 68 tm.tm_mday = day; 69 mystdtime = timegm(&tm); 70 } 71 72 isc_stdtime_t 73 isc_stdtime_now(void) { 74 return mystdtime; 75 } 76 77 /* simple increment by 1 */ 78 ISC_RUN_TEST_IMPL(increment) { 79 uint32_t old = 50; 80 uint32_t serial; 81 82 UNUSED(state); 83 84 serial = dns_update_soaserial(old, dns_updatemethod_increment, NULL); 85 assert_true(isc_serial_lt(old, serial)); 86 assert_int_not_equal(serial, 0); 87 assert_int_equal(serial, 51); 88 } 89 90 /* increment past zero, 0xfffffffff -> 1 */ 91 ISC_RUN_TEST_IMPL(increment_past_zero) { 92 uint32_t old = 0xffffffffu; 93 uint32_t serial; 94 95 UNUSED(state); 96 97 serial = dns_update_soaserial(old, dns_updatemethod_increment, NULL); 98 assert_true(isc_serial_lt(old, serial)); 99 assert_int_not_equal(serial, 0); 100 assert_int_equal(serial, 1u); 101 } 102 103 /* past to unixtime */ 104 ISC_RUN_TEST_IMPL(past_to_unix) { 105 uint32_t old; 106 uint32_t serial; 107 108 UNUSED(state); 109 110 set_mystdtime(2011, 6, 22); 111 old = mystdtime - 1; 112 113 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 114 assert_true(isc_serial_lt(old, serial)); 115 assert_int_not_equal(serial, 0); 116 assert_int_equal(serial, mystdtime); 117 } 118 119 /* now to unixtime */ 120 ISC_RUN_TEST_IMPL(now_to_unix) { 121 uint32_t old; 122 uint32_t serial; 123 124 UNUSED(state); 125 126 set_mystdtime(2011, 6, 22); 127 old = mystdtime; 128 129 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 130 assert_true(isc_serial_lt(old, serial)); 131 assert_int_not_equal(serial, 0); 132 assert_int_equal(serial, old + 1); 133 } 134 135 /* future to unixtime */ 136 ISC_RUN_TEST_IMPL(future_to_unix) { 137 uint32_t old; 138 uint32_t serial; 139 140 UNUSED(state); 141 142 set_mystdtime(2011, 6, 22); 143 old = mystdtime + 1; 144 145 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 146 assert_true(isc_serial_lt(old, serial)); 147 assert_int_not_equal(serial, 0); 148 assert_int_equal(serial, old + 1); 149 } 150 151 /* undefined plus 1 to unixtime */ 152 ISC_RUN_TEST_IMPL(undefined_plus1_to_unix) { 153 uint32_t old; 154 uint32_t serial; 155 156 UNUSED(state); 157 158 set_mystdtime(2011, 6, 22); 159 old = mystdtime ^ 0x80000000u; 160 old += 1; 161 162 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 163 assert_true(isc_serial_lt(old, serial)); 164 assert_int_not_equal(serial, 0); 165 assert_int_equal(serial, mystdtime); 166 } 167 168 /* undefined minus 1 to unixtime */ 169 ISC_RUN_TEST_IMPL(undefined_minus1_to_unix) { 170 uint32_t old; 171 uint32_t serial; 172 173 UNUSED(state); 174 175 set_mystdtime(2011, 6, 22); 176 old = mystdtime ^ 0x80000000u; 177 old -= 1; 178 179 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 180 assert_true(isc_serial_lt(old, serial)); 181 assert_int_not_equal(serial, 0); 182 assert_int_equal(serial, old + 1); 183 } 184 185 /* undefined to unixtime */ 186 ISC_RUN_TEST_IMPL(undefined_to_unix) { 187 uint32_t old; 188 uint32_t serial; 189 190 UNUSED(state); 191 192 set_mystdtime(2011, 6, 22); 193 old = mystdtime ^ 0x80000000u; 194 195 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 196 assert_true(isc_serial_lt(old, serial)); 197 assert_int_not_equal(serial, 0); 198 assert_int_equal(serial, old + 1); 199 } 200 201 /* handle unixtime being zero */ 202 ISC_RUN_TEST_IMPL(unixtime_zero) { 203 uint32_t old; 204 uint32_t serial; 205 206 UNUSED(state); 207 208 mystdtime = 0; 209 old = 0xfffffff0; 210 211 serial = dns_update_soaserial(old, dns_updatemethod_unixtime, NULL); 212 assert_true(isc_serial_lt(old, serial)); 213 assert_int_not_equal(serial, 0); 214 assert_int_equal(serial, old + 1); 215 } 216 217 /* past to date */ 218 ISC_RUN_TEST_IMPL(past_to_date) { 219 uint32_t old, serial; 220 dns_updatemethod_t used = dns_updatemethod_none; 221 222 UNUSED(state); 223 224 set_mystdtime(2014, 3, 31); 225 old = dns_update_soaserial(0, dns_updatemethod_date, NULL); 226 set_mystdtime(2014, 4, 1); 227 228 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 229 assert_true(isc_serial_lt(old, serial)); 230 assert_int_not_equal(serial, 0); 231 assert_int_equal(serial, 2014040100); 232 assert_int_equal(dns_updatemethod_date, used); 233 } 234 235 /* now to date */ 236 ISC_RUN_TEST_IMPL(now_to_date) { 237 uint32_t old; 238 uint32_t serial; 239 dns_updatemethod_t used = dns_updatemethod_none; 240 241 UNUSED(state); 242 243 set_mystdtime(2014, 4, 1); 244 old = dns_update_soaserial(0, dns_updatemethod_date, NULL); 245 246 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 247 assert_true(isc_serial_lt(old, serial)); 248 assert_int_not_equal(serial, 0); 249 assert_int_equal(serial, 2014040101); 250 assert_int_equal(dns_updatemethod_date, used); 251 252 old = 2014040198; 253 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 254 assert_true(isc_serial_lt(old, serial)); 255 assert_int_not_equal(serial, 0); 256 assert_int_equal(serial, 2014040199); 257 assert_int_equal(dns_updatemethod_date, used); 258 259 /* 260 * Stealing from "tomorrow". 261 */ 262 old = 2014040199; 263 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 264 assert_true(isc_serial_lt(old, serial)); 265 assert_int_not_equal(serial, 0); 266 assert_int_equal(serial, 2014040200); 267 assert_int_equal(dns_updatemethod_increment, used); 268 } 269 270 /* future to date */ 271 ISC_RUN_TEST_IMPL(future_to_date) { 272 uint32_t old; 273 uint32_t serial; 274 dns_updatemethod_t used = dns_updatemethod_none; 275 276 UNUSED(state); 277 278 set_mystdtime(2014, 4, 1); 279 old = dns_update_soaserial(0, dns_updatemethod_date, NULL); 280 set_mystdtime(2014, 3, 31); 281 282 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 283 assert_true(isc_serial_lt(old, serial)); 284 assert_int_not_equal(serial, 0); 285 assert_int_equal(serial, 2014040101); 286 assert_int_equal(dns_updatemethod_increment, used); 287 288 old = serial; 289 serial = dns_update_soaserial(old, dns_updatemethod_date, &used); 290 assert_true(isc_serial_lt(old, serial)); 291 assert_int_not_equal(serial, 0); 292 assert_int_equal(serial, 2014040102); 293 assert_int_equal(dns_updatemethod_increment, used); 294 } 295 296 ISC_TEST_LIST_START 297 ISC_TEST_ENTRY_CUSTOM(increment, setup_test, NULL) 298 ISC_TEST_ENTRY_CUSTOM(increment_past_zero, setup_test, NULL) 299 ISC_TEST_ENTRY_CUSTOM(past_to_unix, setup_test, NULL) 300 ISC_TEST_ENTRY_CUSTOM(now_to_unix, setup_test, NULL) 301 ISC_TEST_ENTRY_CUSTOM(future_to_unix, setup_test, NULL) 302 ISC_TEST_ENTRY_CUSTOM(undefined_to_unix, setup_test, NULL) 303 ISC_TEST_ENTRY_CUSTOM(undefined_plus1_to_unix, setup_test, NULL) 304 ISC_TEST_ENTRY_CUSTOM(undefined_minus1_to_unix, setup_test, NULL) 305 ISC_TEST_ENTRY_CUSTOM(unixtime_zero, setup_test, NULL) 306 ISC_TEST_ENTRY_CUSTOM(past_to_date, setup_test, NULL) 307 ISC_TEST_ENTRY_CUSTOM(now_to_date, setup_test, NULL) 308 ISC_TEST_ENTRY_CUSTOM(future_to_date, setup_test, NULL) 309 ISC_TEST_LIST_END 310 311 ISC_TEST_MAIN 312