1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005 Novell, Inc.
9 #include <libsoup/soup-date.h>
12 #include "test-utils.h"
14 static gboolean check_ok (const char *strdate, SoupDate *date);
17 make_date (const char *strdate)
22 /* We do it this way so that if soup_date_new_from_string()
23 * reads off the end of the string, it will trigger an error
24 * when valgrinding, rather than just reading the start of the
27 dup = g_strdup (strdate);
28 date = soup_date_new_from_string (dup);
34 SoupDateFormat format;
37 { SOUP_DATE_HTTP, "Sat, 06 Nov 2004 08:09:07 GMT" },
38 { SOUP_DATE_COOKIE, "Sat, 06-Nov-2004 08:09:07 GMT" },
39 { SOUP_DATE_RFC2822, "Sat, 6 Nov 2004 08:09:07 -0430" },
40 { SOUP_DATE_ISO8601_COMPACT, "20041106T080907" },
41 { SOUP_DATE_ISO8601_FULL, "2004-11-06T08:09:07" },
42 { SOUP_DATE_ISO8601_XMLRPC, "20041106T08:09:07" }
46 check_good (SoupDateFormat format, const char *strdate)
51 date = make_date (strdate);
53 strdate2 = soup_date_to_string (date, format);
54 if (!check_ok (strdate, date))
57 if (strcmp (strdate, strdate2) != 0) {
58 debug_printf (1, " restringification failed: '%s' -> '%s'\n",
65 static const char *ok_dates[] = {
66 /* rfc1123-date, and broken variants */
67 "Sat, 06 Nov 2004 08:09:07 GMT",
68 "Sat, 6 Nov 2004 08:09:07 GMT",
69 "Sat, 6 Nov 2004 08:09:07 GMT",
70 "Sat, 06 Nov 2004 08:09:07",
71 "06 Nov 2004 08:09:07 GMT",
72 "SAT, 06 NOV 2004 08:09:07 +1000",
74 /* rfc850-date, and broken variants */
75 "Saturday, 06-Nov-04 08:09:07 GMT",
76 "Saturday, 6-Nov-04 08:09:07 GMT",
77 "Saturday, 6-Nov-04 08:09:07 GMT",
78 "Saturday, 06-Nov-104 08:09:07 GMT",
79 "Saturday, 06-Nov-04 08:09:07",
80 "06-Nov-04 08:09:07 GMT",
82 /* asctime-date, and broken variants */
83 "Sat Nov 6 08:09:07 2004",
84 "Sat Nov 06 08:09:07 2004",
85 "Sat Nov 6 08:09:07 2004",
86 "Sat Nov 6 08:09:07 2004 GMT",
89 "2004-11-06T08:09:07Z",
91 "20041106T08:09:07+00:00",
92 "20041106T080907+00:00",
94 /* Netscape cookie spec date, and broken variants */
95 "Sat, 06-Nov-2004 08:09:07 GMT",
96 "Sat, 6-Nov-2004 08:09:07 GMT",
97 "Sat, 6-Nov-2004 08:09:07 GMT",
98 "Sat, 06-Nov-2004 08:09:07",
100 /* Original version of Netscape cookie spec, and broken variants */
101 "Sat, 06-Nov-04 08:09:07 GMT",
102 "Sat, 6-Nov-04 08:09:07 GMT",
103 "Sat, 6-Nov-04 08:09:07 GMT",
104 "Sat, 06-Nov-104 08:09:07 GMT",
105 "Sat, 06-Nov-04 08:09:07",
107 /* Netscape cookie spec example syntax, and broken variants */
108 "Saturday, 06-Nov-04 08:09:07 GMT",
109 "Saturday, 6-Nov-04 08:09:07 GMT",
110 "Saturday, 6-Nov-04 08:09:07 GMT",
111 "Saturday, 06-Nov-104 08:09:07 GMT",
112 "Saturday, 06-Nov-2004 08:09:07 GMT",
113 "Saturday, 6-Nov-2004 08:09:07 GMT",
114 "Saturday, 6-Nov-2004 08:09:07 GMT",
115 "Saturday, 06-Nov-04 08:09:07",
117 /* Miscellaneous broken formats seen on the web */
118 "Sat 06-Nov-2004 08:9:07",
119 "Saturday, 06-Nov-04 8:9:07 GMT",
120 "Sat, 06 Nov 2004 08:09:7 GMT"
123 #define TIME_T 1099728547L
124 #define TIME_T_STRING "1099728547"
127 check_ok (const char *strdate, SoupDate *date)
129 debug_printf (2, "%s\n", strdate);
132 date->year == 2004 && date->month == 11 && date->day == 6 &&
133 date->hour == 8 && date->minute == 9 && date->second == 7) {
134 soup_date_free (date);
138 debug_printf (1, " date parsing failed for '%s'.\n", strdate);
140 debug_printf (1, " got: %d %d %d - %d %d %d\n\n",
141 date->year, date->month, date->day,
142 date->hour, date->minute, date->second);
143 soup_date_free (date);
149 static const char *bad_dates[] = {
150 /* broken rfc1123-date */
151 ", 06 Nov 2004 08:09:07 GMT",
152 "Sat, Nov 2004 08:09:07 GMT",
153 "Sat, 06 2004 08:09:07 GMT",
154 "Sat, 06 Nov 08:09:07 GMT",
155 "Sat, 06 Nov 2004 :09:07 GMT",
156 "Sat, 06 Nov 2004 09:07 GMT",
157 "Sat, 06 Nov 2004 08::07 GMT",
158 "Sat, 06 Nov 2004 08:09: GMT",
160 /* broken rfc850-date */
161 ", 06-Nov-04 08:09:07 GMT",
162 "Saturday, -Nov-04 08:09:07 GMT",
163 "Saturday, Nov-04 08:09:07 GMT",
164 "Saturday, 06-04 08:09:07 GMT",
165 "Saturday, 06--04 08:09:07 GMT",
166 "Saturday, 06-Nov- 08:09:07 GMT",
167 "Saturday, 06-Nov 08:09:07 GMT",
168 "Saturday, 06-Nov-04 :09:07 GMT",
169 "Saturday, 06-Nov-04 09:07 GMT",
170 "Saturday, 06-Nov-04 08::07 GMT",
171 "Saturday, 06-Nov-04 08:09: GMT",
173 /* broken asctime-date */
174 "Nov 6 08:09:07 2004",
175 "Sat 6 08:09:07 2004",
176 "Sat Nov 08:09:07 2004",
177 "Sat Nov 6 :09:07 2004",
178 "Sat Nov 6 09:07 2004",
179 "Sat Nov 6 08::07 2004",
180 "Sat Nov 6 08:09: 2004",
181 "Sat Nov 6 08:09:07",
182 "Sat Nov 6 08:09:07 GMT 2004"
186 check_bad (const char *strdate, SoupDate *date)
188 debug_printf (2, "%s\n", strdate);
194 debug_printf (1, " date parsing succeeded for '%s'!\n", strdate);
195 debug_printf (1, " got: %d %d %d - %d %d %d\n\n",
196 date->year, date->month, date->day,
197 date->hour, date->minute, date->second);
198 soup_date_free (date);
201 static const struct conversion {
203 const char *http, *cookie, *rfc2822, *compact, *full, *xmlrpc;
206 { "Sat, 06 Nov 2004 08:09:07 GMT",
208 "Sat, 06 Nov 2004 08:09:07 GMT",
209 "Sat, 06-Nov-2004 08:09:07 GMT",
210 "Sat, 6 Nov 2004 08:09:07 +0000",
212 "2004-11-06T08:09:07Z",
213 "20041106T08:09:07" },
216 { "Sat, 6 Nov 2004 08:09:07 +0000",
218 "Sat, 06 Nov 2004 08:09:07 GMT",
219 "Sat, 06-Nov-2004 08:09:07 GMT",
220 "Sat, 6 Nov 2004 08:09:07 +0000",
222 "2004-11-06T08:09:07Z",
223 "20041106T08:09:07" },
225 /* RFC2822 with positive offset */
226 { "Sat, 6 Nov 2004 08:09:07 +0430",
228 "Sat, 06 Nov 2004 04:39:07 GMT",
229 "Sat, 06-Nov-2004 04:39:07 GMT",
230 "Sat, 6 Nov 2004 08:09:07 +0430",
231 "20041106T080907+0430",
232 "2004-11-06T08:09:07+04:30",
233 "20041106T08:09:07" },
235 /* RFC2822 with negative offset */
236 { "Sat, 6 Nov 2004 08:09:07 -0430",
238 "Sat, 06 Nov 2004 12:39:07 GMT",
239 "Sat, 06-Nov-2004 12:39:07 GMT",
240 "Sat, 6 Nov 2004 08:09:07 -0430",
241 "20041106T080907-0430",
242 "2004-11-06T08:09:07-04:30",
243 "20041106T08:09:07" },
245 /* RFC2822 floating */
246 { "Sat, 6 Nov 2004 08:09:07 -0000",
248 "Sat, 06 Nov 2004 08:09:07 GMT",
249 "Sat, 06-Nov-2004 08:09:07 GMT",
250 "Sat, 6 Nov 2004 08:09:07 -0000",
252 "2004-11-06T08:09:07",
253 "20041106T08:09:07" },
256 { "2004-11-06T08:09:07Z",
258 "Sat, 06 Nov 2004 08:09:07 GMT",
259 "Sat, 06-Nov-2004 08:09:07 GMT",
260 "Sat, 6 Nov 2004 08:09:07 +0000",
262 "2004-11-06T08:09:07Z",
263 "20041106T08:09:07" },
265 /* ISO with positive offset */
266 { "2004-11-06T08:09:07+04:30",
268 "Sat, 06 Nov 2004 04:39:07 GMT",
269 "Sat, 06-Nov-2004 04:39:07 GMT",
270 "Sat, 6 Nov 2004 08:09:07 +0430",
271 "20041106T080907+0430",
272 "2004-11-06T08:09:07+04:30",
273 "20041106T08:09:07" },
275 /* ISO with negative offset */
276 { "2004-11-06T08:09:07-04:30",
278 "Sat, 06 Nov 2004 12:39:07 GMT",
279 "Sat, 06-Nov-2004 12:39:07 GMT",
280 "Sat, 6 Nov 2004 08:09:07 -0430",
281 "20041106T080907-0430",
282 "2004-11-06T08:09:07-04:30",
283 "20041106T08:09:07" },
286 { "2004-11-06T08:09:07",
288 "Sat, 06 Nov 2004 08:09:07 GMT",
289 "Sat, 06-Nov-2004 08:09:07 GMT",
290 "Sat, 6 Nov 2004 08:09:07 -0000",
292 "2004-11-06T08:09:07",
293 "20041106T08:09:07" }
297 check_conversion (const struct conversion *conv)
302 debug_printf (2, "%s\n", conv->source);
303 date = make_date (conv->source);
305 debug_printf (1, " date parsing failed for '%s'.\n", conv->source);
310 str = soup_date_to_string (date, SOUP_DATE_HTTP);
311 if (!str || strcmp (str, conv->http) != 0) {
312 debug_printf (1, " conversion of '%s' to HTTP failed:\n"
313 " wanted: %s\n got: %s\n",
314 conv->source, conv->http, str ? str : "(null)");
319 str = soup_date_to_string (date, SOUP_DATE_COOKIE);
320 if (!str || strcmp (str, conv->cookie) != 0) {
321 debug_printf (1, " conversion of '%s' to COOKIE failed:\n"
322 " wanted: %s\n got: %s\n",
323 conv->source, conv->cookie, str ? str : "(null)");
328 str = soup_date_to_string (date, SOUP_DATE_RFC2822);
329 if (!str || strcmp (str, conv->rfc2822) != 0) {
330 debug_printf (1, " conversion of '%s' to RFC2822 failed:\n"
331 " wanted: %s\n got: %s\n",
332 conv->source, conv->rfc2822, str ? str : "(null)");
337 str = soup_date_to_string (date, SOUP_DATE_ISO8601_COMPACT);
338 if (!str || strcmp (str, conv->compact) != 0) {
339 debug_printf (1, " conversion of '%s' to COMPACT failed:\n"
340 " wanted: %s\n got: %s\n",
341 conv->source, conv->compact, str ? str : "(null)");
346 str = soup_date_to_string (date, SOUP_DATE_ISO8601_FULL);
347 if (!str || strcmp (str, conv->full) != 0) {
348 debug_printf (1, " conversion of '%s' to FULL failed:\n"
349 " wanted: %s\n got: %s\n",
350 conv->source, conv->full, str ? str : "(null)");
355 str = soup_date_to_string (date, SOUP_DATE_ISO8601_XMLRPC);
356 if (!str || strcmp (str, conv->xmlrpc) != 0) {
357 debug_printf (1, " conversion of '%s' to XMLRPC failed:\n"
358 " wanted: %s\n got: %s\n",
359 conv->source, conv->xmlrpc, str ? str : "(null)");
364 soup_date_free (date);
368 main (int argc, char **argv)
372 test_init (argc, argv, NULL);
374 debug_printf (1, "Good dates:\n");
375 for (i = 0; i < G_N_ELEMENTS (good_dates); i++)
376 check_good (good_dates[i].format, good_dates[i].date);
378 debug_printf (1, "\nOK dates:\n");
379 for (i = 0; i < G_N_ELEMENTS (ok_dates); i++)
380 check_ok (ok_dates[i], make_date (ok_dates[i]));
381 check_ok (TIME_T_STRING, soup_date_new_from_time_t (TIME_T));
383 debug_printf (1, "\nBad dates:\n");
384 for (i = 0; i < G_N_ELEMENTS (bad_dates); i++)
385 check_bad (bad_dates[i], make_date (bad_dates[i]));
387 debug_printf (1, "\nConversions:\n");
388 for (i = 0; i < G_N_ELEMENTS (conversions); i++)
389 check_conversion (&conversions[i] );