chiark / gitweb /
Import gnupg2_2.1.18.orig.tar.bz2
[gnupg2.git] / common / t-gettime.c
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 <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifdef HAVE_STDINT_H
24 # include <stdint.h>
25 #endif
26
27 #include "util.h"
28
29 /* In case we do not have stdint.h and no other version of that
30  * conversion macro provide shortcut it.  */
31 #ifndef UINTMAX_C
32 #define UINTMAX_C (c)  (c)
33 #endif
34
35 #define pass()  do { ; } while(0)
36 #define fail(a)  do { fprintf (stderr, "%s:%d: test %d failed\n",\
37                                __FILE__,__LINE__, (a));          \
38                      errcount++;                                 \
39                    } while(0)
40
41 static int verbose;
42 static int errcount;
43 #define INVALID ((time_t)(-1))
44
45
46 static void
47 test_isotime2epoch (void)
48 {
49   struct { const char *string; time_t expected; } array [] = {
50     { "19700101T000001",  1 },
51     { "19700101T235959",  86399 },
52     { "19980815T143712",  903191832 },
53     { "19700101T000000",  0 },
54     { "19691231T235959",  INVALID },
55     { "19000101T000000",  INVALID },
56     { "",                 INVALID },
57     { "19000101T00000",   INVALID },
58     { "20010101t123456",  INVALID },
59     { "20010101T123456",  978352496 },
60     { "20070629T160000",  1183132800 },
61     { "20070629T160000:",  1183132800 },
62     { "20070629T160000,",  1183132800 },
63     { "20070629T160000 ",  1183132800 },
64     { "20070629T160000\n", 1183132800 },
65     { "20070629T160000.",  INVALID },
66 #if SIZEOF_TIME_T > 4
67     { "21060207T062815", (time_t)UINTMAX_C(0x0ffffffff) },
68     { "21060207T062816", (time_t)UINTMAX_C(0x100000000) },
69     { "21060207T062817", (time_t)UINTMAX_C(0x100000001) },
70     { "21060711T120001", (time_t)UINTMAX_C(4308292801)  },
71 #endif /*SIZEOF_TIME_T > 4*/
72     { NULL, 0 }
73   };
74   int idx;
75   time_t val;
76   gnupg_isotime_t tbuf;
77
78   for (idx=0; array[idx].string; idx++)
79     {
80       val = isotime2epoch (array[idx].string);
81       if (val != array[idx].expected )
82         {
83           fail (idx);
84           if (verbose)
85             fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
86                      array[idx].string, (long)array[idx].expected,
87                      (long)val);
88         }
89       if (array[idx].expected != INVALID)
90         {
91           epoch2isotime (tbuf, val);
92           if (strlen (tbuf) != 15)
93             {
94               if (verbose)
95                 fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
96                          array[idx].string, (long)val, tbuf);
97               fail (idx);
98             }
99           if (strncmp (array[idx].string, tbuf, 15))
100             fail (idx);
101         }
102     }
103 }
104
105
106
107 static void
108 test_string2isotime (void)
109 {
110   struct {
111     const char *string;
112     size_t result;
113     const char *expected;
114   } array [] = {
115     { "19700101T000001",      15, "19700101T000001" },
116     { "19700101T235959",      15, "19700101T235959" },
117     { "19980815T143712",      15, "19980815T143712" },
118     { "19700101T000000",      15, "19700101T000000" },
119     { "19691231T235959",      15, "19691231T235959" },
120     { "19000101T000000",      15, "19000101T000000" },
121     { "",                      0, ""                },
122     { "19000101T00000",        0, ""                },
123     { "20010101t123456",       0, ""                },
124     { "20010101T123456",      15, "20010101T123456" },
125     { "20070629T160000",      15, "20070629T160000" },
126     { "20070629T160000:",     15, "20070629T160000" },
127     { "20070629T160000,",     15, "20070629T160000" },
128     { "20070629T160000 ",     15, "20070629T160000" },
129     { "20070629T160000\n",    15,"20070629T160000"  },
130     { "20070629T160000.",      0, ""                },
131     { "1066-03-20",           10, "10660320T000000" },
132     { "1066-03-20,",          10, "10660320T000000" },
133     { "1066-03-20:",           0, ""                },
134     { "1066-03-20 00",        13, "10660320T000000" },
135     { "1066-03-20 01",        13, "10660320T010000" },
136     { "1066-03-20 23",        13, "10660320T230000" },
137     { "1066-03-20 24",         0, ""                },
138     { "1066-03-20 00:",        0, ""                },
139     { "1066-03-20 00:3",       0, ""                },
140     { "1066-03-20 00:31",     16, "10660320T003100" },
141     { "1066-03-20 00:31:47",  19, "10660320T003147" },
142     { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
143     { "1066-03-20 00:31:47,", 19, "10660320T003147" },
144     { "1066-03-20 00:31:47:",  0, ""                },
145     { "1-03-20 00:31:47:",     0, ""                },
146     { "10-03-20 00:31:47:",    0, ""                },
147     { "106-03-20 00:31:47:",   0, ""                },
148     { "1066-23-20 00:31:47:",  0, ""                },
149     { "1066-00-20 00:31:47:",  0, ""                },
150     { "1066-0-20 00:31:47:",   0, ""                },
151     { "1066-01-2 00:31:47:",   0, ""                },
152     { "1066-01-2  00:31:47:",  0, ""                },
153     { "1066-01-32 00:31:47:",  0, ""                },
154     { "1066-01-00 00:31:47:",  0, ""                },
155     { "1066-03-20  00:31:47:",11, "10660320T000000" },
156     { "1066-03-2000:31:47:",   0, ""                },
157     { "10666-03-20 00:31:47:", 0, ""                },
158     { NULL, 0 }
159   };
160   int idx;
161   size_t result;
162   gnupg_isotime_t tbuf;
163
164   for (idx=0; array[idx].string; idx++)
165     {
166       result = string2isotime (tbuf, array[idx].string);
167       if (result != array[idx].result)
168         {
169           fail (idx);
170           if (verbose)
171             fprintf (stderr, "string '%s' expected: %d, got: %d\n",
172                      array[idx].string, (int)array[idx].result, (int)result);
173         }
174       else if (result && strlen (tbuf) != 15)
175         {
176           fail (idx);
177           if (verbose)
178             fprintf (stderr, "string '%s' invalid isotime returned\n",
179                      array[idx].string);
180         }
181       else if (result && strcmp (array[idx].expected, tbuf))
182         {
183           fail (idx);
184           if (verbose)
185             fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
186                      array[idx].string, tbuf);
187         }
188     }
189 }
190
191
192 static void
193 test_isodate_human_to_tm (void)
194 {
195   struct {
196     const char *string;
197     int okay;
198     int year, mon, mday;
199   } array [] = {
200     { "1970-01-01",      1, 1970,  1,  1 },
201     { "1970-02-01",      1, 1970,  2,  1 },
202     { "1970-12-31",      1, 1970, 12, 31 },
203     { "1971-01-01",      1, 1971,  1,  1 },
204     { "1998-08-15",      1, 1998,  8, 15 },
205     { "2015-04-10",      1, 2015,  4, 10 },
206     { "2015-04-10 11:30",1, 2015,  4, 10 },
207     { "1969-12-31",      0,    0,  0,  0 },
208     { "1900-01-01",      0,    0,  0,  0 },
209     { "",                0,    0,  0,  0 },
210     { "1970-12-32",      0,    0,  0,  0 },
211     { "1970-13-01",      0,    0,  0,  0 },
212     { "1970-01-00",      0,    0,  0,  0 },
213     { "1970-00-01",      0,    0,  0,  0 },
214     { "1970-00-01",      0,    0,  0,  0 },
215     { "1970",            0,    0,  0,  0 },
216     { "1970-01",         0,    0,  0,  0 },
217     { "1970-01-1",       0,    0,  0,  0 },
218     { "1970-1--01",      0,    0,  0,  0 },
219     { "1970-01-01,",     1, 1970,  1,  1 },
220     { "1970-01-01 ",     1, 1970,  1,  1 },
221     { "1970-01-01\t",    1, 1970,  1,  1 },
222     { "1970-01-01;",     0,    0,  0,  0 },
223     { "1970-01-01:",     0,    0,  0,  0 },
224     { "1970_01-01",      0,    0,  0,  0 },
225     { "1970-01_01",      0,    0,  0,  0 },
226     { NULL, 0 }
227   };
228   int idx;
229   int okay;
230   struct tm tmbuf;
231
232   for (idx=0; array[idx].string; idx++)
233     {
234       okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
235       if (okay != array[idx].okay)
236         {
237           fail (idx);
238           if (verbose)
239             fprintf (stderr, "string '%s' expected: %d, got: %d\n",
240                      array[idx].string, (int)array[idx].okay, okay);
241         }
242       else if (!okay)
243         ;
244       else if (tmbuf.tm_year + 1900 != array[idx].year
245                || tmbuf.tm_mon +1   != array[idx].mon
246                || tmbuf.tm_mday     != array[idx].mday)
247         {
248           fail (idx);
249           if (verbose)
250             fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
251                      array[idx].string,
252                      tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
253         }
254       else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
255                || tmbuf.tm_isdst != -1)
256         {
257           fail (idx);
258           if (verbose)
259             fprintf (stderr, "string '%s' returned bad time part\n",
260                      array[idx].string);
261         }
262     }
263 }
264
265
266 int
267 main (int argc, char **argv)
268 {
269   if (argc > 1 && !strcmp (argv[1], "--verbose"))
270     verbose = 1;
271
272   test_isotime2epoch ();
273   test_string2isotime ();
274   test_isodate_human_to_tm ();
275
276   return !!errcount;
277 }