time_test.c 6.31 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2016, 2018  Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * 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/.
7
8
9
 */

#include <config.h>
10
#include <stdlib.h>
11
#include <string.h>
Evan Hunt's avatar
Evan Hunt committed
12
#include <stdio.h>
13
14
15
16
17
18
19
20
21
22
23
24
25

#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;
Evan Hunt's avatar
Evan Hunt committed
26
	char buf[ISC_FORMATHTTPTIMESTAMP_SIZE];
27

28
	setenv("TZ", "PST8PDT", 1);
29
30
31
32
33
34
35
36
37
	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));
}

38
39
40
41
42
43
44
45
46
47
48
49
50
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);

Evan Hunt's avatar
Evan Hunt committed
51
	/* check formatting: yyyy-mm-ddThh:mm:ssZ */
52
53
54
55
56
57
58
59
60
	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');
Evan Hunt's avatar
Evan Hunt committed
61
62
63
64
65
66
67
68
69
70
71

	/* 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");
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

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);

Evan Hunt's avatar
Evan Hunt committed
88
	/* check formatting: yyyy-mm-ddThh:mm:ss.sssZ */
89
90
91
92
93
94
95
96
97
98
	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');
Evan Hunt's avatar
Evan Hunt committed
99
100
101
102
103
104
105
106
107
108
109

	/* 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");
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}

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);

Evan Hunt's avatar
Evan Hunt committed
126
	/* check formatting: yyyy-mm-ddThh:mm:ss */
127
128
129
130
131
132
133
134
	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], ':');
Evan Hunt's avatar
Evan Hunt committed
135
136
137
138
139
140
141
142
143
144
145

	/* 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");
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
}

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);

Evan Hunt's avatar
Evan Hunt committed
162
	/* check formatting: yyyy-mm-ddThh:mm:ss.sss */
163
164
165
166
167
168
169
170
171
	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], '.');
Evan Hunt's avatar
Evan Hunt committed
172
173
174
175
176
177
178
179
180
181
182

	/* 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");
183
184
}

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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) {
	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);

	/* check formatting: yyyymmddhhmmsssss */
	memset(buf, 'X', sizeof(buf));
	isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
	ATF_CHECK_EQ(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");

	memset(buf, 'X', sizeof(buf));
	isc_time_set(&t, 1450000000, 123000000);
	isc_time_formatshorttimestamp(&t, buf, sizeof(buf));
	ATF_CHECK_STREQ(buf, "20151213094640123");
}

216
217
218
219
220
/*
 * Main
 */
ATF_TP_ADD_TCS(tp) {
	ATF_TP_ADD_TC(tp, isc_time_parsehttptimestamp);
221
222
223
224
	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);
225
	ATF_TP_ADD_TC(tp, isc_time_formatshorttimestamp);
226
227
	return (atf_no_error());
}