summaryrefslogtreecommitdiff
path: root/tests/date-test.c
blob: 7eefd7c00c6249896b78b2c2bfbf20e40c314e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2005 Novell, Inc.
 */

#include "test-utils.h"

static void check_ok (gconstpointer data);

static GDateTime *
make_date (const char *strdate)
{
	char *dup;
	GDateTime *date;

	/* We do it this way so that if soup_date_time_new_from_http_string()
	 * reads off the end of the string, it will trigger an error
	 * when valgrinding, rather than just reading the start of the
	 * next const string.
	 */
	dup = g_strdup (strdate);
	date = soup_date_time_new_from_http_string (dup);
	g_free (dup);
	return date;
}

static GDateTime *
check_correct_date (const char *strdate)
{
	GDateTime *date;

	date = make_date (strdate);
	if (!date) {
		g_assert_nonnull (date);
		return NULL;
	}

	g_assert_cmpint (g_date_time_get_year (date),   ==, 2004);
	g_assert_cmpint (g_date_time_get_month (date),  ==, 11);
	g_assert_cmpint (g_date_time_get_day_of_month (date), ==, 6);
	g_assert_cmpint (g_date_time_get_hour (date),   ==, 8);
	g_assert_cmpint (g_date_time_get_minute (date), ==, 9);
	g_assert_cmpint (g_date_time_get_second (date), ==, 7);

	return date;
}

typedef struct {
	SoupDateFormat format;
	const char *date;
	const char *bugref;
} GoodDate;

static const GoodDate good_dates[] = {
	{ SOUP_DATE_HTTP,            "Sat, 06 Nov 2004 08:09:07 GMT", NULL },
	{ SOUP_DATE_COOKIE,          "Sat, 06-Nov-2004 08:09:07 GMT", NULL },
};

static void
check_good (gconstpointer data)
{
	GoodDate *good = (GoodDate *)data;
	GDateTime *date;
	char *strdate2;

	if (good->bugref)
		g_test_bug (good->bugref);

	date = check_correct_date (good->date);
	if (!date)
		return;

	strdate2 = soup_date_time_to_string (date, good->format);
	g_date_time_unref (date);

	soup_test_assert (strcmp (good->date, strdate2) == 0,
			  "restringification failed: '%s' -> '%s'\n",
			  good->date, strdate2);
	g_free (strdate2);
}

typedef struct {
	const char *date;
	const char *bugref;
} OkDate;

static const OkDate ok_dates[] = {
	/* rfc1123-date, and broken variants */
	{ "Sat, 06 Nov 2004 08:09:07 GMT", NULL },
	{ "Sat, 6 Nov 2004 08:09:07 GMT", NULL },
	{ "Sat,  6 Nov 2004 08:09:07 GMT", NULL },
	{ "Sat, 06 Nov 2004 08:09:07", NULL },
	{ "06 Nov 2004 08:09:07 GMT", NULL },
	{ "SAT, 06 NOV 2004 08:09:07 +1000", "644048" },

	/* rfc850-date, and broken variants */
	{ "Saturday, 06-Nov-04 08:09:07 GMT", NULL },
	{ "Saturday, 6-Nov-04 08:09:07 GMT", NULL },
	{ "Saturday,  6-Nov-04 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov-104 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov-2004 08:09:07 GMT", NULL },
	{ "Saturday, 6-Nov-2004 08:09:07 GMT", NULL },
	{ "Saturday,  6-Nov-2004 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov-04 08:09:07", NULL },
	{ "06-Nov-04 08:09:07 GMT", NULL },

	/* asctime-date, and broken variants */
	{ "Sat Nov  6 08:09:07 2004", NULL },
	{ "Sat Nov 06 08:09:07 2004", NULL },
	{ "Sat Nov 6 08:09:07 2004", NULL },
	{ "Sat Nov  6 08:09:07 2004 GMT", NULL },

	/* Netscape cookie spec date, and broken variants */
	{ "Sat, 06-Nov-2004 08:09:07 GMT", NULL },
	{ "Sat, 6-Nov-2004 08:09:07 GMT", NULL },
	{ "Sat,  6-Nov-2004 08:09:07 GMT", NULL },
	{ "Sat, 06-Nov-2004 08:09:07", NULL },

	/* Original version of Netscape cookie spec, and broken variants */
	{ "Sat, 06-Nov-04 08:09:07 GMT", NULL },
	{ "Sat, 6-Nov-04 08:09:07 GMT", NULL },
	{ "Sat,  6-Nov-04 08:09:07 GMT", NULL },
	{ "Sat, 06-Nov-104 08:09:07 GMT", NULL },
	{ "Sat, 06-Nov-04 08:09:07", NULL },

	/* Miscellaneous broken formats seen on the web */
	{ "Sat 06-Nov-2004  08:9:07", NULL },
	{ "Saturday, 06-Nov-04 8:9:07 GMT", NULL },
	{ "Sat, 06 Nov 2004 08:09:7 GMT", NULL }
};

static void
check_ok (gconstpointer data)
{
	OkDate *ok = (OkDate *)data;
	GDateTime *date;

	if (ok->bugref)
		g_test_bug (ok->bugref);

	date = check_correct_date (ok->date);
	if (!date)
		return;
	g_date_time_unref (date);
}

typedef struct {
	const char *date;
	const char *bugref;
} BadDate;

static const BadDate bad_dates[] = {
	/* broken rfc1123-date */
	{ ", 06 Nov 2004 08:09:07 GMT", NULL },
	{ "Sat, Nov 2004 08:09:07 GMT", NULL },
	{ "Sat, 06 2004 08:09:07 GMT", NULL },
	{ "Sat, 06 Nov 08:09:07 GMT", NULL },
	{ "Sat, 06 Nov 2004 :09:07 GMT", NULL },
	{ "Sat, 06 Nov 2004 09:07 GMT", NULL },
	{ "Sat, 06 Nov 2004 08::07 GMT", NULL },
	{ "Sat, 06 Nov 2004 08:09: GMT", NULL },

	/* broken rfc850-date */
	{ ", 06-Nov-04 08:09:07 GMT", NULL },
	{ "Saturday, -Nov-04 08:09:07 GMT", NULL },
	{ "Saturday, Nov-04 08:09:07 GMT", NULL },
	{ "Saturday, 06-04 08:09:07 GMT", NULL },
	{ "Saturday, 06--04 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov- 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov 08:09:07 GMT", NULL },
	{ "Saturday, 06-Nov-04 :09:07 GMT", NULL },
	{ "Saturday, 06-Nov-04 09:07 GMT", NULL },
	{ "Saturday, 06-Nov-04 08::07 GMT", NULL },
	{ "Saturday, 06-Nov-04 08:09: GMT", NULL },

	/* broken asctime-date */
	{ "Nov  6 08:09:07 2004", NULL },
	{ "Sat  6 08:09:07 2004", NULL },
	{ "Sat Nov 08:09:07 2004", NULL },
	{ "Sat Nov  6 :09:07 2004", NULL },
	{ "Sat Nov  6 09:07 2004", NULL },
	{ "Sat Nov  6 08::07 2004", NULL },
	{ "Sat Nov  6 08:09: 2004", NULL },
	{ "Sat Nov  6 08:09:07", NULL },
	{ "Sat Nov  6 08:09:07 GMT 2004", NULL }
};

static void
check_bad (gconstpointer data)
{
	BadDate *bad = (BadDate *)data;
	GDateTime *date;

	if (bad->bugref)
		g_test_bug (bad->bugref);

	date = make_date (bad->date);
	soup_test_assert (date == NULL,
			  "date parsing succeeded for '%s': %d %d %d - %d %d %d",
			  bad->date,
                          g_date_time_get_year (date),
                          g_date_time_get_month (date),
                          g_date_time_get_day_of_month (date),
                          g_date_time_get_hour (date),
                          g_date_time_get_minute (date),
                          g_date_time_get_second (date));
	g_clear_pointer (&date, g_date_time_unref);
}

typedef struct {
	const char *source;
	const char *http, *cookie;
} DateConversion;

static const DateConversion conversions[] = {
	/* SOUP_DATE_HTTP */
	{ "Sat, 06 Nov 2004 08:09:07 GMT",

	  "Sat, 06 Nov 2004 08:09:07 GMT",
	  "Sat, 06-Nov-2004 08:09:07 GMT" },

	/* RFC2822 GMT */
	{ "Sat, 6 Nov 2004 08:09:07 +0000",

	  "Sat, 06 Nov 2004 08:09:07 GMT",
	  "Sat, 06-Nov-2004 08:09:07 GMT" },

	/* RFC2822 with positive offset */
	{ "Sat, 6 Nov 2004 08:09:07 +0430",

	  "Sat, 06 Nov 2004 03:39:07 GMT",
	  "Sat, 06-Nov-2004 03:39:07 GMT" },

	/* RFC2822 with negative offset */
	{ "Sat, 6 Nov 2004 08:09:07 -0430",

	  "Sat, 06 Nov 2004 12:39:07 GMT",
	  "Sat, 06-Nov-2004 12:39:07 GMT" },

	/* RFC2822 floating */
	{ "Sat, 6 Nov 2004 08:09:07 -0000",

	  "Sat, 06 Nov 2004 08:09:07 GMT",
	  "Sat, 06-Nov-2004 08:09:07 GMT" }
};

static void
check_conversion (gconstpointer data)
{
	const DateConversion *conv = data;
	GDateTime *date;
	char *str;

	date = make_date (conv->source);
	if (!date) {
		soup_test_assert (FALSE, "date parsing failed for '%s'.", conv->source);
		return;
	}

	str = soup_date_time_to_string (date, SOUP_DATE_HTTP);
	g_assert_cmpstr (str, ==, conv->http);
	g_free (str);

	str = soup_date_time_to_string (date, SOUP_DATE_COOKIE);
	g_assert_cmpstr (str, ==, conv->cookie);
	g_free (str);

	g_date_time_unref (date);
}

int
main (int argc, char **argv)
{
	int i, ret;
	char *path;

	test_init (argc, argv, NULL);

	for (i = 0; i < G_N_ELEMENTS (good_dates); i++) {
		path = g_strdup_printf ("/date/good/%s", good_dates[i].date);
		g_test_add_data_func (path, &good_dates[i], check_good);
		g_free (path);
	}

	for (i = 0; i < G_N_ELEMENTS (ok_dates); i++) {
		path = g_strdup_printf ("/date/ok/%s", ok_dates[i].date);
		g_test_add_data_func (path, &ok_dates[i], check_ok);
		g_free (path);
	}

	for (i = 0; i < G_N_ELEMENTS (bad_dates); i++) {
		path = g_strdup_printf ("/date/bad/%s", bad_dates[i].date);
		g_test_add_data_func (path, &bad_dates[i], check_bad);
		g_free (path);
	}

	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
		path = g_strdup_printf ("/date/conversions/%s", conversions[i].source);
		g_test_add_data_func (path, &conversions[i], check_conversion);
		g_free (path);
	}

	ret = g_test_run ();

	test_cleanup ();
	return ret;
}