Line data Source code
1 : /* t-gettime.c - Module test for gettime.c
2 : * Copyright (C) 2007, 2011 Free Software Foundation, Inc.
3 : *
4 : * This file is part of GnuPG.
5 : *
6 : * GnuPG is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * GnuPG is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <config.h>
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 :
24 : #include "util.h"
25 :
26 : #define pass() do { ; } while(0)
27 : #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
28 : __FILE__,__LINE__, (a)); \
29 : errcount++; \
30 : } while(0)
31 :
32 : static int verbose;
33 : static int errcount;
34 : #define INVALID ((time_t)(-1))
35 :
36 :
37 : static void
38 1 : test_isotime2epoch (void)
39 : {
40 1 : struct { const char *string; time_t expected; } array [] = {
41 : { "19700101T000001", 1 },
42 : { "19700101T235959", 86399 },
43 : { "19980815T143712", 903191832 },
44 : { "19700101T000000", 0 },
45 : { "19691231T235959", INVALID },
46 : { "19000101T000000", INVALID },
47 : { "", INVALID },
48 : { "19000101T00000", INVALID },
49 : { "20010101t123456", INVALID },
50 : { "20010101T123456", 978352496 },
51 : { "20070629T160000", 1183132800 },
52 : { "20070629T160000:", 1183132800 },
53 : { "20070629T160000,", 1183132800 },
54 : { "20070629T160000 ", 1183132800 },
55 : { "20070629T160000\n", 1183132800 },
56 : { "20070629T160000.", INVALID },
57 : #if SIZEOF_TIME_T > 4
58 : { "21060207T062815", (time_t)0x0ffffffff },
59 : { "21060207T062816", (time_t)0x100000000 },
60 : { "21060207T062817", (time_t)0x100000001 },
61 : { "21060711T120001", (time_t)4308292801 },
62 : #endif /*SIZEOF_TIME_T > 4*/
63 : { NULL, 0 }
64 : };
65 : int idx;
66 : time_t val;
67 : gnupg_isotime_t tbuf;
68 :
69 21 : for (idx=0; array[idx].string; idx++)
70 : {
71 20 : val = isotime2epoch (array[idx].string);
72 20 : if (val != array[idx].expected )
73 : {
74 0 : fail (idx);
75 0 : if (verbose)
76 0 : fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
77 : array[idx].string, (long)array[idx].expected,
78 : (long)val);
79 : }
80 20 : if (array[idx].expected != INVALID)
81 : {
82 14 : epoch2isotime (tbuf, val);
83 14 : if (strlen (tbuf) != 15)
84 : {
85 0 : if (verbose)
86 0 : fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
87 : array[idx].string, (long)val, tbuf);
88 0 : fail (idx);
89 : }
90 14 : if (strncmp (array[idx].string, tbuf, 15))
91 0 : fail (idx);
92 : }
93 : }
94 1 : }
95 :
96 :
97 :
98 : static void
99 1 : test_string2isotime (void)
100 : {
101 : struct {
102 : const char *string;
103 : size_t result;
104 : const char *expected;
105 1 : } array [] = {
106 : { "19700101T000001", 15, "19700101T000001" },
107 : { "19700101T235959", 15, "19700101T235959" },
108 : { "19980815T143712", 15, "19980815T143712" },
109 : { "19700101T000000", 15, "19700101T000000" },
110 : { "19691231T235959", 15, "19691231T235959" },
111 : { "19000101T000000", 15, "19000101T000000" },
112 : { "", 0, "" },
113 : { "19000101T00000", 0, "" },
114 : { "20010101t123456", 0, "" },
115 : { "20010101T123456", 15, "20010101T123456" },
116 : { "20070629T160000", 15, "20070629T160000" },
117 : { "20070629T160000:", 15, "20070629T160000" },
118 : { "20070629T160000,", 15, "20070629T160000" },
119 : { "20070629T160000 ", 15, "20070629T160000" },
120 : { "20070629T160000\n", 15,"20070629T160000" },
121 : { "20070629T160000.", 0, "" },
122 : { "1066-03-20", 10, "10660320T000000" },
123 : { "1066-03-20,", 10, "10660320T000000" },
124 : { "1066-03-20:", 0, "" },
125 : { "1066-03-20 00", 13, "10660320T000000" },
126 : { "1066-03-20 01", 13, "10660320T010000" },
127 : { "1066-03-20 23", 13, "10660320T230000" },
128 : { "1066-03-20 24", 0, "" },
129 : { "1066-03-20 00:", 0, "" },
130 : { "1066-03-20 00:3", 0, "" },
131 : { "1066-03-20 00:31", 16, "10660320T003100" },
132 : { "1066-03-20 00:31:47", 19, "10660320T003147" },
133 : { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
134 : { "1066-03-20 00:31:47,", 19, "10660320T003147" },
135 : { "1066-03-20 00:31:47:", 0, "" },
136 : { "1-03-20 00:31:47:", 0, "" },
137 : { "10-03-20 00:31:47:", 0, "" },
138 : { "106-03-20 00:31:47:", 0, "" },
139 : { "1066-23-20 00:31:47:", 0, "" },
140 : { "1066-00-20 00:31:47:", 0, "" },
141 : { "1066-0-20 00:31:47:", 0, "" },
142 : { "1066-01-2 00:31:47:", 0, "" },
143 : { "1066-01-2 00:31:47:", 0, "" },
144 : { "1066-01-32 00:31:47:", 0, "" },
145 : { "1066-01-00 00:31:47:", 0, "" },
146 : { "1066-03-20 00:31:47:",11, "10660320T000000" },
147 : { "1066-03-2000:31:47:", 0, "" },
148 : { "10666-03-20 00:31:47:", 0, "" },
149 : { NULL, 0 }
150 : };
151 : int idx;
152 : size_t result;
153 : gnupg_isotime_t tbuf;
154 :
155 44 : for (idx=0; array[idx].string; idx++)
156 : {
157 43 : result = string2isotime (tbuf, array[idx].string);
158 43 : if (result != array[idx].result)
159 : {
160 0 : fail (idx);
161 0 : if (verbose)
162 0 : fprintf (stderr, "string '%s' expected: %d, got: %d\n",
163 0 : array[idx].string, (int)array[idx].result, (int)result);
164 : }
165 43 : else if (result && strlen (tbuf) != 15)
166 : {
167 0 : fail (idx);
168 0 : if (verbose)
169 0 : fprintf (stderr, "string '%s' invalid isotime returned\n",
170 : array[idx].string);
171 : }
172 43 : else if (result && strcmp (array[idx].expected, tbuf))
173 : {
174 0 : fail (idx);
175 0 : if (verbose)
176 0 : fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
177 : array[idx].string, tbuf);
178 : }
179 : }
180 1 : }
181 :
182 :
183 : static void
184 1 : test_isodate_human_to_tm (void)
185 : {
186 : struct {
187 : const char *string;
188 : int okay;
189 : int year, mon, mday;
190 1 : } array [] = {
191 : { "1970-01-01", 1, 1970, 1, 1 },
192 : { "1970-02-01", 1, 1970, 2, 1 },
193 : { "1970-12-31", 1, 1970, 12, 31 },
194 : { "1971-01-01", 1, 1971, 1, 1 },
195 : { "1998-08-15", 1, 1998, 8, 15 },
196 : { "2015-04-10", 1, 2015, 4, 10 },
197 : { "2015-04-10 11:30",1, 2015, 4, 10 },
198 : { "1969-12-31", 0, 0, 0, 0 },
199 : { "1900-01-01", 0, 0, 0, 0 },
200 : { "", 0, 0, 0, 0 },
201 : { "1970-12-32", 0, 0, 0, 0 },
202 : { "1970-13-01", 0, 0, 0, 0 },
203 : { "1970-01-00", 0, 0, 0, 0 },
204 : { "1970-00-01", 0, 0, 0, 0 },
205 : { "1970-00-01", 0, 0, 0, 0 },
206 : { "1970", 0, 0, 0, 0 },
207 : { "1970-01", 0, 0, 0, 0 },
208 : { "1970-01-1", 0, 0, 0, 0 },
209 : { "1970-1--01", 0, 0, 0, 0 },
210 : { "1970-01-01,", 1, 1970, 1, 1 },
211 : { "1970-01-01 ", 1, 1970, 1, 1 },
212 : { "1970-01-01\t", 1, 1970, 1, 1 },
213 : { "1970-01-01;", 0, 0, 0, 0 },
214 : { "1970-01-01:", 0, 0, 0, 0 },
215 : { "1970_01-01", 0, 0, 0, 0 },
216 : { "1970-01_01", 0, 0, 0, 0 },
217 : { NULL, 0 }
218 : };
219 : int idx;
220 : int okay;
221 : struct tm tmbuf;
222 :
223 27 : for (idx=0; array[idx].string; idx++)
224 : {
225 26 : okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
226 26 : if (okay != array[idx].okay)
227 : {
228 0 : fail (idx);
229 0 : if (verbose)
230 0 : fprintf (stderr, "string '%s' expected: %d, got: %d\n",
231 : array[idx].string, (int)array[idx].okay, okay);
232 : }
233 26 : else if (!okay)
234 : ;
235 10 : else if (tmbuf.tm_year + 1900 != array[idx].year
236 10 : || tmbuf.tm_mon +1 != array[idx].mon
237 10 : || tmbuf.tm_mday != array[idx].mday)
238 : {
239 0 : fail (idx);
240 0 : if (verbose)
241 0 : fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
242 : array[idx].string,
243 0 : tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
244 : }
245 10 : else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
246 10 : || tmbuf.tm_isdst != -1)
247 : {
248 0 : fail (idx);
249 0 : if (verbose)
250 0 : fprintf (stderr, "string '%s' returned bad time part\n",
251 : array[idx].string);
252 : }
253 : }
254 1 : }
255 :
256 :
257 : int
258 1 : main (int argc, char **argv)
259 : {
260 1 : if (argc > 1 && !strcmp (argv[1], "--verbose"))
261 0 : verbose = 1;
262 :
263 1 : test_isotime2epoch ();
264 1 : test_string2isotime ();
265 1 : test_isodate_human_to_tm ();
266 :
267 1 : return !!errcount;
268 : }
|