Compare commits

...

2 Commits

Author SHA1 Message Date
Ondřej Surý
0dddbbefbd Add more unit tests for isc_test API
While working on internal 64-bit time for BIND 9, the unit test suite
for isc_time API has been extended.

Backport the unit tests from 64-bit time branch to the main branch.
This should make the unit tests for isc_time API (mostly) complete.
2024-12-12 17:35:20 -08:00
Ondřej Surý
2392827a43 Add more validity checks to the isc_time API
This commits backport few things from the 64-bit time branch:

* Add VALID_TIME() macro that checks the valid range for seconds and
  nanoseconds in one go

* Add internal TIME_S_MAX defined to UINT_MAX, so we can change the
  maximum range for seconds in one place.

* Add more specific buffer sizes for formatting the dates and require
  the buffers passed to the formatting functions to be at least that
  big.

* When error happens when formatting the string, always return a valid
  buffer with formatted mock datetime string instead of garbage.
  Because the formatting functions doesn't return status, the caller
  could print garbage (unterminated string).
2024-12-12 17:35:20 -08:00
3 changed files with 396 additions and 133 deletions

View File

@@ -32,6 +32,15 @@ ISC_CONSTEXPR unsigned int US_PER_SEC = 1000 * 1000;
ISC_CONSTEXPR unsigned int NS_PER_MS = 1000 * 1000;
ISC_CONSTEXPR unsigned int NS_PER_SEC = 1000 * 1000 * 1000;
#define ISC_FORMATTIMESTAMP_SIZE sizeof("99-Bad-9999 99:99:99.999")
#define ISC_FORMATISO8601L_SIZE sizeof("9999-99-99T99:99:99")
#define ISC_FORMATISO8601LMS_SIZE sizeof("9999-99-99T99:99:99.999")
#define ISC_FORMATISO8601_SIZE sizeof("9999-99-99T99:99:99Z")
#define ISC_FORMATISO8601MS_SIZE sizeof("9999-99-99T99:99:99.999Z")
#define ISC_FORMATISO8601US_SIZE sizeof("9999-99-99T99:99:99.999999Z")
#define ISC_FORMATISO8601TZMS_SIZE sizeof("9999-99-99T99:99:99.999+99:99")
#define ISC_FORMATSHORTTIMESTAMP_SIZE sizeof("99999999999999999")
/*
* ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially
* more for other locales to handle longer national abbreviations when

View File

@@ -30,6 +30,9 @@
#include <isc/tm.h>
#include <isc/util.h>
#define VALID_TIME(t) \
(t != NULL && t->seconds <= UINT_MAX && t->nanoseconds < NS_PER_SEC)
#if defined(CLOCK_REALTIME)
#define CLOCKSOURCE_HIRES CLOCK_REALTIME
#endif /* #if defined(CLOCK_REALTIME) */
@@ -54,6 +57,7 @@ const isc_time_t *const isc_time_epoch = &epoch;
void
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
REQUIRE(t != NULL);
REQUIRE(seconds <= UINT_MAX);
REQUIRE(nanoseconds < NS_PER_SEC);
t->seconds = seconds;
@@ -70,8 +74,7 @@ isc_time_settoepoch(isc_time_t *t) {
bool
isc_time_isepoch(const isc_time_t *t) {
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
if (t->seconds == 0 && t->nanoseconds == 0) {
return true;
@@ -130,27 +133,26 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
struct timespec ts;
REQUIRE(t != NULL);
REQUIRE(i != NULL);
INSIST(i->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(i));
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
UNEXPECTED_SYSERROR(errno, "clock_gettime()");
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR("%s", strbuf);
return ISC_R_UNEXPECTED;
}
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= (long)NS_PER_SEC) {
if ((ts.tv_sec < 0 || ts.tv_sec > UINT_MAX) ||
(ts.tv_nsec < 0 || ts.tv_nsec >= (long)NS_PER_SEC))
{
return ISC_R_UNEXPECTED;
}
/*
* Ensure the resulting seconds value fits in the size of an
* unsigned int. (It is written this way as a slight optimization;
* note that even if both values == INT_MAX, then when added
* and getting another 1 added below the result is UINT_MAX.)
* unsigned int.
*/
if ((ts.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
((long long)ts.tv_sec + i->seconds > UINT_MAX))
{
if ((unsigned int)ts.tv_sec > UINT_MAX - i->seconds) {
return ISC_R_RANGE;
}
@@ -166,8 +168,8 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
int
isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
REQUIRE(t1 != NULL && t2 != NULL);
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t1));
REQUIRE(VALID_TIME(t2));
if (t1->seconds < t2->seconds) {
return -1;
@@ -186,8 +188,9 @@ isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
isc_result_t
isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) {
REQUIRE(t != NULL && i != NULL && result != NULL);
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(VALID_TIME(i));
REQUIRE(result != NULL);
/* Seconds */
if (ISC_OVERFLOW_ADD(t->seconds, i->seconds, &result->seconds)) {
@@ -210,8 +213,9 @@ isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) {
isc_result_t
isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
isc_time_t *result) {
REQUIRE(t != NULL && i != NULL && result != NULL);
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(VALID_TIME(i));
REQUIRE(result != NULL);
/* Seconds */
if (ISC_OVERFLOW_SUB(t->seconds, i->seconds, &result->seconds)) {
@@ -237,8 +241,8 @@ uint64_t
isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
uint64_t i1, i2, i3;
REQUIRE(t1 != NULL && t2 != NULL);
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t1));
REQUIRE(VALID_TIME(t2));
i1 = (uint64_t)t1->seconds * NS_PER_SEC + t1->nanoseconds;
i2 = (uint64_t)t2->seconds * NS_PER_SEC + t2->nanoseconds;
@@ -259,8 +263,7 @@ isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
uint32_t
isc_time_seconds(const isc_time_t *t) {
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
return (uint32_t)t->seconds;
}
@@ -269,8 +272,7 @@ isc_result_t
isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
time_t seconds;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
/*
* Ensure that the number of seconds represented by t->seconds
@@ -305,9 +307,7 @@ isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
uint32_t
isc_time_nanoseconds(const isc_time_t *t) {
REQUIRE(t != NULL);
ENSURE(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
return (uint32_t)t->nanoseconds;
}
@@ -326,19 +326,17 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATTIMESTAMP_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
INSIST(flen < len);
if (flen != 0) {
if (flen == 0) {
strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
} else {
snprintf(buf + flen, len - flen, ".%03u",
t->nanoseconds / NS_PER_MS);
} else {
strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
}
}
@@ -348,10 +346,9 @@ isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATHTTPTIMESTAMP_SIZE);
/*
* 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+
@@ -360,7 +357,9 @@ isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
now = (time_t)t->seconds;
flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT",
gmtime_r(&now, &tm));
INSIST(flen < len);
if (flen == 0) {
strlcpy(buf, "Bad, 99 Bad 9999 99:99:99 GMT", len);
}
}
isc_result_t
@@ -390,15 +389,15 @@ isc_time_formatISO8601Lms(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATISO8601LMS_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
INSIST(flen < len);
if (flen > 0U && len - flen >= 6) {
if (flen == 0) {
strlcpy(buf, "9999-99-99T99:99:99.999", len);
} else {
snprintf(buf + flen, len - flen, ".%03u",
t->nanoseconds / NS_PER_MS);
}
@@ -412,10 +411,9 @@ isc_time_formatISO8601TZms(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATISO8601TZMS_SIZE);
now = (time_t)t->seconds;
flen = strftime(strftime_buf, len, "%Y-%m-%dT%H:%M:%S.xxx%z",
@@ -438,14 +436,15 @@ isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATISO8601_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
INSIST(flen < len);
if (flen == 0) {
strlcpy(buf, "9999-99-99T99:99:99Z", len);
}
}
void
@@ -454,16 +453,15 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATISO8601MS_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
INSIST(flen < len);
if (flen > 0U && len - flen >= 5) {
flen -= 1; /* rewind one character (Z) */
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", gmtime_r(&now, &tm));
if (flen == 0) {
strlcpy(buf, "9999-99-99T99:99:99.999Z", len);
} else {
snprintf(buf + flen, len - flen, ".%03uZ",
t->nanoseconds / NS_PER_MS);
}
@@ -475,16 +473,15 @@ isc_time_formatISO8601us(const isc_time_t *t, char *buf, unsigned int len) {
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATISO8601US_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
INSIST(flen < len);
if (flen > 0U && len - flen >= 5) {
flen -= 1; /* rewind one character (Z) */
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", gmtime_r(&now, &tm));
if (flen == 0) {
strlcpy(buf, "9999-99-99T99:99:99.999999Z", len);
} else {
snprintf(buf + flen, len - flen, ".%06uZ",
t->nanoseconds / NS_PER_US);
}
@@ -497,15 +494,15 @@ isc_time_formatshorttimestamp(const isc_time_t *t, char *buf,
unsigned int flen;
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(VALID_TIME(t));
REQUIRE(buf != NULL);
REQUIRE(len > 0);
REQUIRE(len >= ISC_FORMATSHORTTIMESTAMP_SIZE);
now = (time_t)t->seconds;
flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime_r(&now, &tm));
INSIST(flen < len);
if (flen > 0U && len - flen >= 5) {
if (flen == 0) {
strlcpy(buf, "99999999999999999", len);
} else {
snprintf(buf + flen, len - flen, "%03u",
t->nanoseconds / NS_PER_MS);
}

View File

@@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define UNIT_TESTING
#include <cmocka.h>
@@ -31,8 +32,88 @@
#include <tests/isc.h>
#define MAX_S UINT_MAX
#define MAX_NS (NS_PER_SEC - 1)
ISC_RUN_TEST_IMPL(isc_interval_basic_test) {
isc_interval_t i = { 0, 0 };
assert_true(isc_interval_iszero(&i));
isc_interval_set(&i, MAX_S, MAX_NS);
assert_int_equal(i.seconds, MAX_S);
assert_int_equal(i.nanoseconds, MAX_NS);
isc_interval_set(&i, 1, NS_PER_MS * 2);
assert_int_equal(isc_interval_ms(&i), MS_PER_SEC + 2);
expect_assert_failure(isc_interval_set(NULL, 0, 0));
expect_assert_failure(isc_interval_set(&i, 0, MAX_NS + 1));
expect_assert_failure(isc_interval_iszero(NULL));
}
ISC_RUN_TEST_IMPL(isc_time_basic_test) {
isc_time_t t = { 0, 0 };
assert_true(isc_time_isepoch(&t));
isc_time_set(&t, MAX_S, MAX_NS);
assert_int_equal(t.seconds, MAX_S);
assert_int_equal(t.nanoseconds, MAX_NS);
assert_int_equal(isc_time_seconds(&t), t.seconds);
assert_int_equal(isc_time_nanoseconds(&t), t.nanoseconds);
isc_time_settoepoch(&t);
assert_int_equal(t.seconds, 0);
assert_int_equal(t.nanoseconds, 0);
assert_true(isc_time_isepoch(&t));
expect_assert_failure(isc_time_set(NULL, 0, 0));
expect_assert_failure(isc_time_set(&t, 0, MAX_NS + 1));
expect_assert_failure(isc_time_settoepoch(NULL));
expect_assert_failure(isc_time_isepoch(NULL));
expect_assert_failure(isc_time_isepoch(&(isc_time_t){ 0, MAX_NS + 1 }));
expect_assert_failure(isc_time_seconds(NULL));
expect_assert_failure(isc_time_seconds(&(isc_time_t){ 0, MAX_NS + 1 }));
expect_assert_failure(isc_time_nanoseconds(NULL));
expect_assert_failure(
isc_time_nanoseconds(&(isc_time_t){ 0, MAX_NS + 1 }));
}
ISC_RUN_TEST_IMPL(isc_time_now_test) {
isc_time_t t1 = { 0, 0 };
isc_time_t t2 = { 0, 0 };
time_t tm;
tm = time(NULL);
t1 = isc_time_now();
nanosleep(&(struct timespec){ 1, 0 }, NULL);
t2 = isc_time_now();
assert_true(t1.seconds >= tm);
assert_true(t2.seconds >= tm);
assert_int_not_equal(t1.seconds, 0);
assert_int_not_equal(t2.seconds, 0);
assert_int_equal(isc_time_compare(&t2, &t1), 1);
assert_int_equal(t2.seconds - t1.seconds, 1);
tm = time(NULL);
t1 = isc_time_now_hires();
nanosleep(&(struct timespec){ 0, NS_PER_US }, NULL);
t2 = isc_time_now_hires();
assert_true(t1.seconds >= tm);
assert_true(t2.seconds >= tm);
assert_true(isc_time_microdiff(&t2, &t1) >= 1);
assert_true(isc_time_microdiff(&t2, &t1) < US_PER_SEC);
}
struct time_vectors {
isc_time_t a;
isc_interval_t b;
@@ -64,16 +145,14 @@ const struct time_vectors vectors_sub[7] = {
{ 0, MAX_NS },
{ 0, NS_PER_SEC / 2 + 1 },
ISC_R_SUCCESS },
{ { UINT_MAX, MAX_NS }, { UINT_MAX, 0 }, { 0, MAX_NS }, ISC_R_SUCCESS },
{ { MAX_S, MAX_NS }, { MAX_S, 0 }, { 0, MAX_NS }, ISC_R_SUCCESS },
{ { 0, 0 }, { 1, 0 }, { 0, 0 }, ISC_R_RANGE },
{ { 0, 0 }, { 0, MAX_NS }, { 0, 0 }, ISC_R_RANGE },
};
ISC_RUN_TEST_IMPL(isc_time_add_test) {
UNUSED(state);
for (size_t i = 0; i < ARRAY_SIZE(vectors_add); i++) {
isc_time_t r = { UINT_MAX, UINT_MAX };
isc_time_t r = { MAX_S, MAX_S };
isc_result_t result = isc_time_add(&(vectors_add[i].a),
&(vectors_add[i].b), &r);
assert_int_equal(result, vectors_add[i].result);
@@ -103,8 +182,6 @@ ISC_RUN_TEST_IMPL(isc_time_add_test) {
}
ISC_RUN_TEST_IMPL(isc_time_sub_test) {
UNUSED(state);
for (size_t i = 0; i < ARRAY_SIZE(vectors_sub); i++) {
isc_time_t r = { UINT_MAX, UINT_MAX };
isc_result_t result = isc_time_subtract(
@@ -134,39 +211,185 @@ ISC_RUN_TEST_IMPL(isc_time_sub_test) {
&(isc_time_t){ 0, 0 }, &(isc_interval_t){ 0, 0 }, NULL));
}
/* parse http time stamp */
struct compare_vectors {
isc_time_t a;
isc_time_t b;
int64_t r;
};
const struct compare_vectors vectors_compare[] = {
{ { 0, 0 }, { 0, 0 }, 0 },
{ { 1, 0 }, { 0, 0 }, 1 },
{ { 0, 0 }, { 1, 0 }, -1 },
{ { 0, 1 }, { 0, 1 }, 0 },
{ { 0, 1 }, { 0, 0 }, 1 },
{ { 0, 0 }, { 0, 1 }, -1 },
{ { 0, 0 }, { MAX_S, MAX_NS }, -1 },
{ { MAX_S, MAX_NS }, { 0, 0 }, 1 },
{ { MAX_S, MAX_NS }, { MAX_S, MAX_NS }, 0 },
{ { 1, 0 }, { 0, MAX_NS }, 1 },
{ { 0, MAX_NS }, { 1, 0 }, -1 }
};
ISC_RUN_TEST_IMPL(isc_time_compare_test) {
for (size_t i = 0; i < ARRAY_SIZE(vectors_compare); i++) {
int r = isc_time_compare(&(vectors_compare[i].a),
&(vectors_compare[i].b));
assert_int_equal(vectors_compare[i].r, r);
}
/* Invalid first argument */
expect_assert_failure((void)isc_time_compare((isc_time_t *)NULL,
&(isc_time_t){ 0, 0 }));
expect_assert_failure((void)isc_time_compare(
&(isc_time_t){ 0, MAX_NS + 1 }, &(isc_time_t){ 0, 0 }));
/* Invalid second argument */
expect_assert_failure((void)isc_time_compare(&(isc_time_t){ 0, 0 },
(isc_time_t *)NULL));
expect_assert_failure((void)isc_time_compare(
&(isc_time_t){ 0, 0 }, &(isc_time_t){ 0, MAX_NS + 1 }));
}
#define MAX_NS_PER_US (MAX_NS / NS_PER_US)
#define MAX_MICRODIFF_N (MAX_S + 0LL)
#define MAX_MICRODIFF_US (MAX_S + 0LL) * US_PER_SEC
const struct compare_vectors vectors_microdiff[] = {
{ { 0, 0 }, { 0, 0 }, 0 },
{ { 1, 0 }, { 0, 0 }, 1 * US_PER_SEC },
{ { 0, 0 }, { 1, 0 }, 0 },
{ { 0, 1 }, { 0, 1 }, 0 },
{ { 0, 1 }, { 0, 0 }, 0 },
{ { 0, 0 }, { 0, 1 }, 0 },
{ { 0, NS_PER_US }, { 0, NS_PER_US }, 0 },
{ { 0, NS_PER_US }, { 0, 0 }, 1 },
{ { 0, 0 }, { 0, NS_PER_US }, 0 },
{ { 0, 0 }, { MAX_MICRODIFF_N, MAX_NS }, 0 },
{ { MAX_MICRODIFF_N, MAX_NS },
{ 0, 0 },
MAX_MICRODIFF_US + MAX_NS_PER_US },
{ { MAX_MICRODIFF_N, MAX_NS }, { MAX_MICRODIFF_N, MAX_NS }, 0 },
{ { 1, 0 }, { 0, MAX_NS }, 0 },
{ { 0, MAX_NS }, { 1, 0 }, 0 }
};
ISC_RUN_TEST_IMPL(isc_time_microdiff_test) {
for (size_t i = 0; i < ARRAY_SIZE(vectors_microdiff); i++) {
int64_t r = isc_time_microdiff(&(vectors_microdiff[i].a),
&(vectors_microdiff[i].b));
assert_int_equal(vectors_microdiff[i].r, r);
}
/* Invalid first argument */
expect_assert_failure((void)isc_time_microdiff((isc_time_t *)NULL,
&(isc_time_t){ 0, 0 }));
expect_assert_failure((void)isc_time_microdiff(
&(isc_time_t){ 0, MAX_NS + 1 }, &(isc_time_t){ 0, 0 }));
/* Invalid second argument */
expect_assert_failure((void)isc_time_microdiff(&(isc_time_t){ 0, 0 },
(isc_time_t *)NULL));
expect_assert_failure((void)isc_time_microdiff(
&(isc_time_t){ 0, 0 }, &(isc_time_t){ 0, MAX_NS + 1 }));
}
ISC_RUN_TEST_IMPL(isc_time_formattimestamp_test) {
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATTIMESTAMP_SIZE];
/*
* The second least population is at UTC+08:45 that covers village of
* Eucla and other villages in Australia with a population of around
* 200. The area is surrounded by signs to remind you to turn your
* clocks, but it is unofficial so it might not comply either.
*/
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
memset(buf, 'X', sizeof(buf));
isc_time_formattimestamp(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), sizeof(buf) - 1);
memset(buf, 'X', sizeof(buf));
isc_time_settoepoch(&t);
isc_time_formattimestamp(&t, buf, sizeof(buf));
assert_string_equal(buf, "01-Jan-1970 08:45:00.000");
memset(buf, 'X', sizeof(buf));
isc_time_set(&t, 1450000000, 123000000);
isc_time_formattimestamp(&t, buf, sizeof(buf));
assert_string_equal(buf, "13-Dec-2015 18:31:40.123");
expect_assert_failure(isc_time_formattimestamp(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formattimestamp(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formattimestamp(&t, NULL, 0));
expect_assert_failure(
isc_time_formattimestamp(&t, buf, sizeof(buf) - 1));
}
/* parse http time stamp */
ISC_RUN_TEST_IMPL(isc_time_parsehttptimestamp_test) {
isc_result_t result;
isc_time_t t, x;
char buf[ISC_FORMATHTTPTIMESTAMP_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
isc_time_formathttptimestamp(&t, buf, sizeof(buf));
result = isc_time_parsehttptimestamp(buf, &x);
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(isc_time_parsehttptimestamp(buf, &x), ISC_R_SUCCESS);
assert_int_equal(isc_time_seconds(&t), isc_time_seconds(&x));
memset(buf, 'X', sizeof(buf));
isc_time_settoepoch(&t);
isc_time_formathttptimestamp(&t, buf, sizeof(buf));
assert_string_equal(buf, "Thu, 01 Jan 1970 00:00:00 GMT");
assert_int_equal(isc_time_parsehttptimestamp(buf, &x), ISC_R_SUCCESS);
assert_int_equal(isc_time_seconds(&t), isc_time_seconds(&x));
memset(buf, 'X', sizeof(buf));
isc_time_set(&t, 1450000000, 123000000);
isc_time_formathttptimestamp(&t, buf, sizeof(buf));
assert_string_equal(buf, "Sun, 13 Dec 2015 09:46:40 GMT");
assert_int_equal(isc_time_parsehttptimestamp(buf, &x), ISC_R_SUCCESS);
assert_int_equal(isc_time_seconds(&t), isc_time_seconds(&x));
expect_assert_failure(
isc_time_formathttptimestamp(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formathttptimestamp(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formathttptimestamp(&t, NULL, 0));
expect_assert_failure(
isc_time_formathttptimestamp(&t, buf, sizeof(buf) - 1));
expect_assert_failure(isc_time_parsehttptimestamp(buf, NULL));
expect_assert_failure(isc_time_parsehttptimestamp(NULL, &t));
}
/* print UTC in ISO8601 */
ISC_RUN_TEST_IMPL(isc_time_formatISO8601_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATISO8601_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ssZ */
memset(buf, 'X', sizeof(buf));
isc_time_formatISO8601(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), 20);
assert_int_equal(strlen(buf), sizeof(buf) - 1);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
@@ -184,23 +407,26 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601_test) {
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatISO8601(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T09:46:40Z");
expect_assert_failure(isc_time_formatISO8601(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601(&t, NULL, 0));
expect_assert_failure(isc_time_formatISO8601(&t, buf, sizeof(buf) - 1));
}
/* print UTC in ISO8601 with milliseconds */
ISC_RUN_TEST_IMPL(isc_time_formatISO8601ms_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATISO8601MS_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
memset(buf, 'X', sizeof(buf));
isc_time_formatISO8601ms(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), 24);
assert_int_equal(strlen(buf), sizeof(buf) - 1);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
@@ -219,23 +445,27 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601ms_test) {
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatISO8601ms(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T09:46:40.123Z");
expect_assert_failure(isc_time_formatISO8601ms(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601ms(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601ms(&t, NULL, 0));
expect_assert_failure(
isc_time_formatISO8601ms(&t, buf, sizeof(buf) - 1));
}
/* print UTC in ISO8601 with microseconds */
ISC_RUN_TEST_IMPL(isc_time_formatISO8601us_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATISO8601US_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now_hires();
/* check formatting: yyyy-mm-ddThh:mm:ss.ssssssZ */
memset(buf, 'X', sizeof(buf));
isc_time_formatISO8601us(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), 27);
assert_int_equal(strlen(buf), sizeof(buf) - 1);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
@@ -254,24 +484,27 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601us_test) {
isc_time_set(&t, 1450000000, 123456000);
isc_time_formatISO8601us(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T09:46:40.123456Z");
expect_assert_failure(isc_time_formatISO8601us(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601us(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601us(&t, NULL, 0));
expect_assert_failure(
isc_time_formatISO8601us(&t, buf, sizeof(buf) - 1));
}
/* print local time in ISO8601 with milliseconds and timezone */
ISC_RUN_TEST_IMPL(isc_time_formatISO8601TZms_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATISO8601TZMS_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
/* check formatting: yyyy-mm-ddThh:mm:ss.sss+hh:mm */
memset(buf, 'X', sizeof(buf));
buf[63] = '\0';
isc_time_formatISO8601TZms(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), strlen("yyyy-mm-ddThh:mm:ss.sss+hh:mm"));
assert_int_equal(strlen(buf), sizeof(buf) - 1);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
@@ -283,32 +516,37 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601TZms_test) {
assert_true((buf[23] == '+') || (buf[23] == '-'));
assert_int_equal(buf[26], ':');
/* check time conversion correctness */
memset(buf, 'X', sizeof(buf));
isc_time_settoepoch(&t);
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
assert_string_equal(buf, "1969-12-31T16:00:00.000");
isc_time_formatISO8601TZms(&t, buf, sizeof(buf));
assert_string_equal(buf, "1970-01-01T08:45:00.000+08:45");
memset(buf, 'X', sizeof(buf));
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T01:46:40.123");
isc_time_formatISO8601TZms(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T18:31:40.123+08:45");
expect_assert_failure(
isc_time_formatISO8601TZms(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601TZms(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601TZms(&t, NULL, 0));
expect_assert_failure(
isc_time_formatISO8601TZms(&t, buf, sizeof(buf) - 1));
}
/* print local time in ISO8601 with milliseconds */
ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lms_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATISO8601LMS_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
memset(buf, 'X', sizeof(buf));
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), 23);
assert_int_equal(strlen(buf), sizeof(buf) - 1);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
@@ -320,29 +558,34 @@ ISC_RUN_TEST_IMPL(isc_time_formatISO8601Lms_test) {
memset(buf, 'X', sizeof(buf));
isc_time_settoepoch(&t);
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
assert_string_equal(buf, "1969-12-31T16:00:00.000");
assert_string_equal(buf, "1970-01-01T08:45:00.000");
memset(buf, 'X', sizeof(buf));
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
assert_string_equal(buf, "2015-12-13T01:46:40.123");
assert_string_equal(buf, "2015-12-13T18:31:40.123");
expect_assert_failure(
isc_time_formatISO8601Lms(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601Lms(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatISO8601Lms(&t, NULL, 0));
expect_assert_failure(
isc_time_formatISO8601Lms(&t, buf, sizeof(buf) - 1));
}
/* print UTC time as yyyymmddhhmmsssss */
ISC_RUN_TEST_IMPL(isc_time_formatshorttimestamp_test) {
isc_time_t t;
char buf[64];
isc_time_t t = { 0, 0 };
char buf[ISC_FORMATSHORTTIMESTAMP_SIZE];
UNUSED(state);
setenv("TZ", "America/Los_Angeles", 1);
setenv("TZ", "Australia/Eucla", 1);
t = isc_time_now();
/* check formatting: yyyymmddhhmmsssss */
memset(buf, 'X', sizeof(buf));
isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
assert_int_equal(strlen(buf), 17);
assert_int_equal(strlen(buf), sizeof(buf) - 1);
/* check time conversion correctness */
memset(buf, 'X', sizeof(buf));
@@ -354,10 +597,21 @@ ISC_RUN_TEST_IMPL(isc_time_formatshorttimestamp_test) {
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
assert_string_equal(buf, "20151213094640123");
expect_assert_failure(
isc_time_formatshorttimestamp(NULL, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatshorttimestamp(
&(isc_time_t){ 0, MAX_NS + 1 }, buf, sizeof(buf)));
expect_assert_failure(isc_time_formatshorttimestamp(&t, NULL, 0));
expect_assert_failure(
isc_time_formatshorttimestamp(&t, buf, sizeof(buf) - 1));
}
ISC_TEST_LIST_START
ISC_TEST_ENTRY(isc_interval_basic_test)
ISC_TEST_ENTRY(isc_time_basic_test)
ISC_TEST_ENTRY(isc_time_now_test)
ISC_TEST_ENTRY(isc_time_add_test)
ISC_TEST_ENTRY(isc_time_sub_test)
ISC_TEST_ENTRY(isc_time_parsehttptimestamp_test)
@@ -367,6 +621,9 @@ ISC_TEST_ENTRY(isc_time_formatISO8601us_test)
ISC_TEST_ENTRY(isc_time_formatISO8601Lms_test)
ISC_TEST_ENTRY(isc_time_formatISO8601TZms_test)
ISC_TEST_ENTRY(isc_time_formatshorttimestamp_test)
ISC_TEST_ENTRY(isc_time_formattimestamp_test)
ISC_TEST_ENTRY(isc_time_microdiff_test)
ISC_TEST_ENTRY(isc_time_compare_test)
ISC_TEST_LIST_END