Commit 3bf9a1e5 authored by Evan Hunt's avatar Evan Hunt
Browse files

convert time_test

parent c9ff174d
......@@ -26,5 +26,5 @@ atf_test_program{name='socket_test'}
tap_test_program{name='symtab_test'}
atf_test_program{name='task_test'}
tap_test_program{name='taskpool_test'}
atf_test_program{name='time_test'}
tap_test_program{name='time_test'}
atf_test_program{name='timer_test'}
......@@ -179,8 +179,9 @@ taskpool_test@EXEEXT@: taskpool_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
time_test@EXEEXT@: time_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
time_test.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ time_test.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
timer_test@EXEEXT@: timer_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
......
......@@ -10,221 +10,239 @@
*/
#include <config.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <atf-c.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/time.h>
#include <isc/result.h>
#include <isc/util.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) {
/* parse http time stamp */
static void
isc_time_parsehttptimestamp_test(void **state) {
isc_result_t result;
isc_time_t t, x;
char buf[ISC_FORMATHTTPTIMESTAMP_SIZE];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(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));
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(isc_time_seconds(&t), isc_time_seconds(&x));
}
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) {
/* print UTC in ISO8601 */
static void
isc_time_formatISO8601_test(void **state) {
isc_result_t result;
isc_time_t t;
char buf[64];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/* check formatting: yyyy-mm-ddThh:mm:ssZ */
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');
assert_int_equal(strlen(buf), 20);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
assert_int_equal(buf[13], ':');
assert_int_equal(buf[16], ':');
assert_int_equal(buf[19], 'Z');
/* 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");
assert_string_equal(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");
assert_string_equal(buf, "2015-12-13T09:46:40Z");
}
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) {
/* print UTC in ISO8601 with milliseconds */
static void
isc_time_formatISO8601ms_test(void **state) {
isc_result_t result;
isc_time_t t;
char buf[64];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
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');
assert_int_equal(strlen(buf), 24);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
assert_int_equal(buf[13], ':');
assert_int_equal(buf[16], ':');
assert_int_equal(buf[19], '.');
assert_int_equal(buf[23], 'Z');
/* 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");
assert_string_equal(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");
assert_string_equal(buf, "2015-12-13T09:46:40.123Z");
}
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) {
/* print local time in ISO8601 */
static void
isc_time_formatISO8601L_test(void **state) {
isc_result_t result;
isc_time_t t;
char buf[64];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/* check formatting: yyyy-mm-ddThh:mm:ss */
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], ':');
assert_int_equal(strlen(buf), 19);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
assert_int_equal(buf[13], ':');
assert_int_equal(buf[16], ':');
/* 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");
assert_string_equal(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");
assert_string_equal(buf, "2015-12-13T01:46:40");
}
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) {
/* print local time in ISO8601 with milliseconds */
static void
isc_time_formatISO8601Lms_test(void **state) {
isc_result_t result;
isc_time_t t;
char buf[64];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
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], '.');
assert_int_equal(strlen(buf), 23);
assert_int_equal(buf[4], '-');
assert_int_equal(buf[7], '-');
assert_int_equal(buf[10], 'T');
assert_int_equal(buf[13], ':');
assert_int_equal(buf[16], ':');
assert_int_equal(buf[19], '.');
/* 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");
assert_string_equal(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");
assert_string_equal(buf, "2015-12-13T01:46:40.123");
}
ATF_TC(isc_time_formatshorttimestamp);
ATF_TC_HEAD(isc_time_formatshorttimestamp, tc) {
atf_tc_set_md_var(tc, "descr",
"print UTC time as yyyymmddhhmmsssss");
}
ATF_TC_BODY(isc_time_formatshorttimestamp, tc) {
/* print UTC time as yyyymmddhhmmsssss */
static void
isc_time_formatshorttimestamp_test(void **state) {
isc_result_t result;
isc_time_t t;
char buf[64];
UNUSED(state);
setenv("TZ", "PST8PDT", 1);
result = isc_time_now(&t);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/* check formatting: yyyymmddhhmmsssss */
memset(buf, 'X', sizeof(buf));
isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
ATF_CHECK_EQ(strlen(buf), 17);
assert_int_equal(strlen(buf), 17);
/* check time conversion correctness */
memset(buf, 'X', sizeof(buf));
isc_time_settoepoch(&t);
isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
ATF_CHECK_STREQ(buf, "19700101000000000");
assert_string_equal(buf, "19700101000000000");
memset(buf, 'X', sizeof(buf));
isc_time_set(&t, 1450000000, 123000000);
isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
ATF_CHECK_STREQ(buf, "20151213094640123");
assert_string_equal(buf, "20151213094640123");
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, isc_time_parsehttptimestamp);
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);
ATF_TP_ADD_TC(tp, isc_time_formatshorttimestamp);
return (atf_no_error());
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(isc_time_parsehttptimestamp_test),
cmocka_unit_test(isc_time_formatISO8601_test),
cmocka_unit_test(isc_time_formatISO8601ms_test),
cmocka_unit_test(isc_time_formatISO8601L_test),
cmocka_unit_test(isc_time_formatISO8601Lms_test),
cmocka_unit_test(isc_time_formatshorttimestamp_test),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment