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