2014-01-11 00:30:41 +11:00
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
|
2014-01-11 00:30:41 +11:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-01-11 00:30:41 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2014-01-10 18:49:51 -08:00
|
|
|
#include <stdlib.h>
|
2016-11-22 23:34:47 -08:00
|
|
|
#include <string.h>
|
2016-12-02 12:32:34 -08:00
|
|
|
#include <stdio.h>
|
2014-01-11 00:30:41 +11:00
|
|
|
|
|
|
|
#include <atf-c.h>
|
|
|
|
|
|
|
|
#include <isc/time.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
|
|
|
|
ATF_TC(isc_time_parsehttptimestamp);
|
|
|
|
ATF_TC_HEAD(isc_time_parsehttptimestamp, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "parse http time stamp");
|
|
|
|
}
|
|
|
|
ATF_TC_BODY(isc_time_parsehttptimestamp, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t t, x;
|
2015-02-05 17:18:15 -08:00
|
|
|
char buf[ISC_FORMATHTTPTIMESTAMP_SIZE];
|
2014-01-11 00:30:41 +11:00
|
|
|
|
2014-01-10 18:49:51 -08:00
|
|
|
setenv("TZ", "PST8PDT", 1);
|
2014-01-11 00:30:41 +11:00
|
|
|
result = isc_time_now(&t);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
isc_time_formathttptimestamp(&t, buf, sizeof(buf));
|
|
|
|
result = isc_time_parsehttptimestamp(buf, &x);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_REQUIRE_EQ(isc_time_seconds(&t), isc_time_seconds(&x));
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:34:47 -08:00
|
|
|
ATF_TC(isc_time_formatISO8601);
|
|
|
|
ATF_TC_HEAD(isc_time_formatISO8601, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "print UTC in ISO8601");
|
|
|
|
}
|
|
|
|
ATF_TC_BODY(isc_time_formatISO8601, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t t;
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
setenv("TZ", "PST8PDT", 1);
|
|
|
|
result = isc_time_now(&t);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2016-12-02 12:32:34 -08:00
|
|
|
/* check formatting: yyyy-mm-ddThh:mm:ssZ */
|
2016-11-22 23:34:47 -08:00
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_formatISO8601(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_EQ(strlen(buf), 20);
|
|
|
|
ATF_CHECK_EQ(buf[4], '-');
|
|
|
|
ATF_CHECK_EQ(buf[7], '-');
|
|
|
|
ATF_CHECK_EQ(buf[10], 'T');
|
|
|
|
ATF_CHECK_EQ(buf[13], ':');
|
|
|
|
ATF_CHECK_EQ(buf[16], ':');
|
|
|
|
ATF_CHECK_EQ(buf[19], 'Z');
|
2016-12-02 12:32:34 -08:00
|
|
|
|
|
|
|
/* check time conversion correctness */
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_settoepoch(&t);
|
|
|
|
isc_time_formatISO8601(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "1970-01-01T00:00:00Z");
|
|
|
|
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_set(&t, 1450000000, 123000000);
|
|
|
|
isc_time_formatISO8601(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "2015-12-13T09:46:40Z");
|
2016-11-22 23:34:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(isc_time_formatISO8601ms);
|
|
|
|
ATF_TC_HEAD(isc_time_formatISO8601ms, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr",
|
|
|
|
"print UTC in ISO8601 with milliseconds");
|
|
|
|
}
|
|
|
|
ATF_TC_BODY(isc_time_formatISO8601ms, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t t;
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
setenv("TZ", "PST8PDT", 1);
|
|
|
|
result = isc_time_now(&t);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2016-12-02 12:32:34 -08:00
|
|
|
/* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
|
2016-11-22 23:34:47 -08:00
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_formatISO8601ms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_EQ(strlen(buf), 24);
|
|
|
|
ATF_CHECK_EQ(buf[4], '-');
|
|
|
|
ATF_CHECK_EQ(buf[7], '-');
|
|
|
|
ATF_CHECK_EQ(buf[10], 'T');
|
|
|
|
ATF_CHECK_EQ(buf[13], ':');
|
|
|
|
ATF_CHECK_EQ(buf[16], ':');
|
|
|
|
ATF_CHECK_EQ(buf[19], '.');
|
|
|
|
ATF_CHECK_EQ(buf[23], 'Z');
|
2016-12-02 12:32:34 -08:00
|
|
|
|
|
|
|
/* check time conversion correctness */
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_settoepoch(&t);
|
|
|
|
isc_time_formatISO8601ms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "1970-01-01T00:00:00.000Z");
|
|
|
|
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_set(&t, 1450000000, 123000000);
|
|
|
|
isc_time_formatISO8601ms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "2015-12-13T09:46:40.123Z");
|
2016-11-22 23:34:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(isc_time_formatISO8601L);
|
|
|
|
ATF_TC_HEAD(isc_time_formatISO8601L, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr",
|
|
|
|
"print local time in ISO8601");
|
|
|
|
}
|
|
|
|
ATF_TC_BODY(isc_time_formatISO8601L, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t t;
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
setenv("TZ", "PST8PDT", 1);
|
|
|
|
result = isc_time_now(&t);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2016-12-02 12:32:34 -08:00
|
|
|
/* check formatting: yyyy-mm-ddThh:mm:ss */
|
2016-11-22 23:34:47 -08:00
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_formatISO8601L(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_EQ(strlen(buf), 19);
|
|
|
|
ATF_CHECK_EQ(buf[4], '-');
|
|
|
|
ATF_CHECK_EQ(buf[7], '-');
|
|
|
|
ATF_CHECK_EQ(buf[10], 'T');
|
|
|
|
ATF_CHECK_EQ(buf[13], ':');
|
|
|
|
ATF_CHECK_EQ(buf[16], ':');
|
2016-12-02 12:32:34 -08:00
|
|
|
|
|
|
|
/* check time conversion correctness */
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_settoepoch(&t);
|
|
|
|
isc_time_formatISO8601L(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "1969-12-31T16:00:00");
|
|
|
|
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_set(&t, 1450000000, 123000000);
|
|
|
|
isc_time_formatISO8601L(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "2015-12-13T01:46:40");
|
2016-11-22 23:34:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(isc_time_formatISO8601Lms);
|
|
|
|
ATF_TC_HEAD(isc_time_formatISO8601Lms, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr",
|
|
|
|
"print local time in ISO8601 with milliseconds");
|
|
|
|
}
|
|
|
|
ATF_TC_BODY(isc_time_formatISO8601Lms, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_time_t t;
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
setenv("TZ", "PST8PDT", 1);
|
|
|
|
result = isc_time_now(&t);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2016-12-02 12:32:34 -08:00
|
|
|
/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
|
2016-11-22 23:34:47 -08:00
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_EQ(strlen(buf), 23);
|
|
|
|
ATF_CHECK_EQ(buf[4], '-');
|
|
|
|
ATF_CHECK_EQ(buf[7], '-');
|
|
|
|
ATF_CHECK_EQ(buf[10], 'T');
|
|
|
|
ATF_CHECK_EQ(buf[13], ':');
|
|
|
|
ATF_CHECK_EQ(buf[16], ':');
|
|
|
|
ATF_CHECK_EQ(buf[19], '.');
|
2016-12-02 12:32:34 -08:00
|
|
|
|
|
|
|
/* check time conversion correctness */
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_settoepoch(&t);
|
|
|
|
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "1969-12-31T16:00:00.000");
|
|
|
|
|
|
|
|
memset(buf, 'X', sizeof(buf));
|
|
|
|
isc_time_set(&t, 1450000000, 123000000);
|
|
|
|
isc_time_formatISO8601Lms(&t, buf, sizeof(buf));
|
|
|
|
ATF_CHECK_STREQ(buf, "2015-12-13T01:46:40.123");
|
2016-11-22 23:34:47 -08:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:41 +11:00
|
|
|
/*
|
|
|
|
* Main
|
|
|
|
*/
|
|
|
|
ATF_TP_ADD_TCS(tp) {
|
|
|
|
ATF_TP_ADD_TC(tp, isc_time_parsehttptimestamp);
|
2016-11-22 23:34:47 -08:00
|
|
|
ATF_TP_ADD_TC(tp, isc_time_formatISO8601);
|
|
|
|
ATF_TP_ADD_TC(tp, isc_time_formatISO8601ms);
|
|
|
|
ATF_TP_ADD_TC(tp, isc_time_formatISO8601L);
|
|
|
|
ATF_TP_ADD_TC(tp, isc_time_formatISO8601Lms);
|
2014-01-11 00:30:41 +11:00
|
|
|
return (atf_no_error());
|
|
|
|
}
|
|
|
|
|