Debian Codegraph @

tests/ews-test-timezones.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2013 Red Hat, Inc. (www.redhat.com)
3 * SPDX-FileContributor: Fabiano FidĂȘncio <[email protected]>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "calendar/e-cal-backend-ews-utils.h"
8 #include "common/e-ews-connection.h"
9 #include "common/e-ews-debug.h"
10
11 #include "ews-test-common.h"
12
13 void (* populate_windows_zones) (void);
14 const gchar * (* ical_to_msdn_equivalent) (const gchar *);
15 gboolean (* convert_calcomp_to_xml) (ESoapRequest *request,
16 gpointer user_data,
17 GError **error);
18 GType (* cal_backend_ews_get_type) (void);
19
20 const gchar *str_comp =
21 "BEGIN:VEVENT\n"
22 "UID:20140114T172626Z-2238-1000-5564-0@srv\n"
23 "DTSTAMP:20140114T172620Z\n"
24 "DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/ICAL_TIMEZONE:\n"
25 " 20140114T160000\n"
26 "DTEND;TZID=/freeassociation.sourceforge.net/Tzfile/ICAL_TIMEZONE:\n"
27 " 20140114T163000\n"
28 "TRANSP:OPAQUE\n"
29 "SEQUENCE:2\n"
30 "SUMMARY:Test\n"
31 "CLASS:PUBLIC\n"
32 "ORGANIZER;CN=Someone:mailto:[email protected]\n"
33 "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;\n"
34 " RSVP=TRUE;CN=Someone;LANGUAGE=en:mailto:[email protected]\n"
35 "END:VEVENT";
36
37 /*
38 * These timezones, present in tzdata, don't have a correspondent "Windows timezone".
39 * In runtime, we fallback and set the server version as 2007_SP1 in the request, which
40 * doesn't treat the transitions on the server side.
41 * For tests purposes, this list of known unknown timezones is enough.
42 */
43 static const gchar *unknown_timezones[] = {
44 "Australia/Lord_Howe",
45 "Australia/Eucla",
46 "America/Atikokan",
47 "Pacific/Easter",
48 "Africa/Asmara",
49 "Pacific/Chuuk",
50 "Pacific/Pohnpei",
51 "Atlantic/Faroe",
52 "Pacific/Kiritimati",
53 "Pacific/Norfolk",
54 "Pacific/Chatham",
55 "Pacific/Marquesas",
56 "Pacific/Gambier",
57 "America/Miquelon",
58 "Pacific/Pitcairn",
59 "Asia/Gaza",
60 "Asia/Hebron",
61 "America/Adak",
62 "America/Metlakatla",
63 "Asia/Ho_Chi_Minh",
64 "Antarctica/Troll",
65 "America/Argentina/Buenos_Aires",
66 "America/Argentina/Cordoba",
67 "America/Argentina/Jujuy",
68 "America/Argentina/Catamarca",
69 "America/Argentina/Mendoza",
70 "America/Fort_Nelson",
71 "America/Punta_Arenas",
72 "Asia/Famagusta",
73 "Asia/Atyrau",
74 "Asia/Yangon",
75 "Asia/Kathmandu",
76 "Pacific/Bougainville",
77 "Europe/Kirov",
78 "Europe/Astrakhan",
79 "Europe/Saratov",
80 "Europe/Ulyanovsk",
81 "Asia/Barnaul",
82 "Asia/Tomsk",
83 "Asia/Chita",
84 "Asia/Srednekolymsk",
85 "America/Kentucky/Louisville",
86 "America/Indiana/Indianapolis",
87 "Asia/Qostanay",
88 "Asia/Istanbul",
89 "Atlantic/Jan_Mayen",
90 "CET",
91 "EET",
92 "EST",
93 "Etc/GMT+8",
94 "Etc/GMT+9",
95 "Etc/GMT-14",
96 "Etc/UCT",
97 "Etc/UTC",
98 "Europe/Nicosia",
99 "HST",
100 "MET",
101 "MST",
102 "WET",
103 NULL
104 };
105
106 static ICalArray *builtin_timezones = NULL;
107
108 static gboolean
109 is_a_known_unknown_timezone (const gchar *zone)
110 {
111 gint i;
112
113 for (i = 0; unknown_timezones[i] != NULL; i++) {
114 if (g_strcmp0 (zone, unknown_timezones[i]) == 0)
115 return TRUE;
116 }
117
118 return FALSE;
119 }
120
121 static void
122 server_notify_resolver_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
123 {
124 UhmServer *local_server;
125 UhmResolver *resolver;
126 EwsTestData *etd;
127 const gchar *hostname;
128
129 local_server = UHM_SERVER (object);
130 etd = user_data;
131 hostname = etd->hostname;
132
133 /* Set up the expected domain names here. This should technically be split up between
134 * the different unit test suites, but that's too much effort. */
135 resolver = uhm_server_get_resolver (local_server);
136
137 if (resolver != NULL) {
138 const gchar *ip_address = uhm_server_get_address (local_server);
139
140 uhm_resolver_add_A (resolver, hostname, ip_address);
141 }
142 }
143
144 static void
145 test_libical_timezones_compatibility (gconstpointer user_data)
146 {
147 guint ii, nelems;
148
149 nelems = i_cal_array_size (builtin_timezones);
150 for (ii = 0; ii < nelems; ii++) {
151 ICalTimezone *zone;
152 const gchar *zone_location;
153
154 zone = i_cal_timezone_array_element_at (builtin_timezones, ii);
155 zone_location = i_cal_timezone_get_location (zone);
156
157 if (ical_to_msdn_equivalent (zone_location) == NULL) {
158 if (!is_a_known_unknown_timezone (zone_location)) {
159 g_printerr ("\nMissing ical_tz_location: %s\n", zone_location);
160 }
161 }
162 g_object_unref (zone);
163 }
164 }
165
166 static void
167 test_time_zones_sync (gconstpointer user_data)
168 {
169 gboolean retval = FALSE;
170 guint ii, nelems;
171 GError *error = NULL;
172 UhmServer *local_server;
173 EwsTestData *etd = (gpointer) user_data;
174 EwsCalendarConvertData convert_data = { 0, };
175 EwsFolderId *calendar_fid = NULL;
176 ESourceRegistry *registry;
177 ESource *dummy_source;
178 gboolean includes_last_folder = FALSE;
179 gchar *old_sync_state = NULL;
180 gchar **tokens;
181 GSList *zone_location_errors = NULL;
182
183 local_server = ews_test_get_mock_server ();
184
185 ews_test_server_set_trace_directory (local_server, etd->version, "calendar/timezones");
186 ews_test_server_start_trace (local_server, etd, "get_server_time_zones_sync", &error);
187 if (error != NULL) {
188 g_printerr ("\n%s\n", error->message);
189 goto exit;
190 }
191
192 while (!includes_last_folder) {
193 GSList *folders_created = NULL;
194 GSList *folders_updated = NULL;
195 GSList *folders_deleted = NULL;
196 GSList *l;
197 gchar *new_sync_state = NULL;
198
199 e_ews_connection_sync_folder_hierarchy_sync (
200 etd->connection,
201 EWS_PRIORITY_MEDIUM,
202 old_sync_state,
203 &new_sync_state,
204 &includes_last_folder,
205 &folders_created,
206 &folders_updated,
207 &folders_deleted,
208 NULL,
209 &error);
210
211 if (error != NULL) {
212 g_free (old_sync_state);
213 g_printerr ("\n%s\n", error->message);
214 goto exit;
215 }
216
217 for (l = folders_created; l != NULL; l = l->next) {
218 EEwsFolder *folder = l->data;
219
220 if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_CALENDAR) {
221 const EwsFolderId *fid;
222
223 fid = e_ews_folder_get_id (folder);
224
225 calendar_fid = e_ews_folder_id_new (fid->id, fid->change_key, fid->is_distinguished_id);
226 break;
227 }
228 }
229
230 g_slist_free_full (folders_created, g_object_unref);
231 g_slist_free_full (folders_updated, g_object_unref);
232 g_slist_free_full (folders_deleted, g_free);
233
234 g_free (old_sync_state);
235 old_sync_state = new_sync_state;
236
237 if (calendar_fid) {
238 g_free (new_sync_state);
239 break;
240 }
241 }
242
243 if (!calendar_fid) {
244 g_printerr ("No calendar folder found\n");
245 goto exit;
246 }
247
248 registry = e_source_registry_new_sync (NULL, NULL);
249 dummy_source = e_source_new (NULL, NULL, NULL);
250
251 convert_data.connection = etd->connection;
252 convert_data.default_zone = i_cal_timezone_get_utc_timezone ();
253 convert_data.timezone_cache = g_object_new (cal_backend_ews_get_type (),
254 "registry", registry,
255 "source", dummy_source,
256 "kind", I_CAL_VEVENT_COMPONENT,
257 NULL);
258
259 g_clear_object (&dummy_source);
260 g_clear_object (&registry);
261
262 tokens = g_strsplit (str_comp, "ICAL_TIMEZONE", 0);
263
264 nelems = i_cal_array_size (builtin_timezones);
265 for (ii = 0; ii < nelems; ii++) {
266 GSList *ll;
267 GSList *ids = NULL;
268 ICalTimezone *zone;
269 ECalComponent *comp;
270 const gchar *zone_location;
271 gchar *str;
272
273 zone = i_cal_timezone_array_element_at (builtin_timezones, ii);
274 zone_location = i_cal_timezone_get_location (zone);
275
276 if (is_a_known_unknown_timezone (zone_location)) {
277 g_object_unref (zone);
278 continue;
279 }
280
281 str = g_strdup_printf ("%s%s%s%s%s", tokens[0], zone_location, tokens[1], zone_location, tokens[2]);
282 comp = e_cal_component_new_from_string (str);
283 g_free (str);
284
285 convert_data.icomp = e_cal_component_get_icalcomponent (comp);
286
287 e_ews_connection_create_items_sync (
288 etd->connection,
289 EWS_PRIORITY_MEDIUM,
290 "SaveOnly",
291 "SendToNone",
292 calendar_fid,
293 convert_calcomp_to_xml,
294 &convert_data,
295 &ids,
296 NULL,
297 &error);
298
299 g_object_unref (comp);
300
301 if (error != NULL) {
302 g_printerr ("\n%s\n", error->message);
303 g_clear_error (&error);
304
305 zone_location_errors = g_slist_append (zone_location_errors, g_strdup (zone_location));
306 g_object_unref (zone);
307 continue;
308 }
309
310 for (ll = ids; ll != NULL; ll = ll->next) {
311 EEwsItem *item = ll->data;
312
313 if (e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR) {
314 const GError *item_error = e_ews_item_get_error (item);
315 g_printerr ("\n%s\n", item_error->message);
316 g_clear_error (&error);
317
318 zone_location_errors = g_slist_append (zone_location_errors, g_strdup (zone_location));
319 g_object_unref (zone);
320 continue;
321 }
322 }
323
324 g_slist_free_full (ids, g_object_unref);
325 g_object_unref (zone);
326 }
327
328 g_clear_object (&convert_data.timezone_cache);
329 g_strfreev (tokens);
330
331 retval = zone_location_errors == NULL;
332
333 exit:
334 if (zone_location_errors != NULL) {
335 GSList *l;
336
337 g_printerr ("Errors found in: \n");
338 for (l = zone_location_errors; l != NULL; l = l->next)
339 g_printerr (" - %s\n", (gchar *) l->data);
340
341 g_slist_free_full (zone_location_errors, g_free);
342 }
343
344 uhm_server_end_trace (local_server);
345 e_ews_folder_id_free (calendar_fid);
346 g_clear_error (&error);
347
348 g_assert_true (retval == TRUE);
349 }
350
351 int main (int argc,
352 char **argv)
353 {
354 gint retval;
355 GList *etds, *l;
356 UhmServer *server;
357 const gchar *module_path;
358 GModule *module = NULL;
359 gpointer symbol = NULL;
360
361 retval = ews_test_init (argc, argv);
362
363 if (retval < 0) {
364 g_printerr ("Failed to initialize test\n");
365 goto exit;
366 }
367
368 if (!g_module_supported ()) {
369 g_printerr ("GModule not supported\n");
370 retval = 1;
371 goto exit;
372 }
373
374 module_path = CALENDAR_MODULE_DIR "libecalbackendews.so";
375 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
376
377 if (module == NULL) {
378 g_printerr ("Failed to load module '%s': %s\n", module_path, g_module_error ());
379 retval = 2;
380 goto exit;
381 }
382
383 if (!g_module_symbol (module, "e_cal_backend_ews_populate_windows_zones", &symbol)) {
384 g_printerr ("\n%s\n", g_module_error ());
385 retval = 3;
386 goto exit;
387 }
388
389 populate_windows_zones = symbol;
390
391 if (!g_module_symbol (module, "e_cal_backend_ews_tz_util_get_msdn_equivalent", &symbol)) {
392 g_printerr ("\n%s\n", g_module_error ());
393 retval = 4;
394 goto exit;
395 }
396
397 ical_to_msdn_equivalent = symbol;
398
399 if (!g_module_symbol (module, "e_cal_backend_ews_convert_calcomp_to_xml", &symbol)) {
400 g_printerr ("\n%s\n", g_module_error ());
401 retval = 5;
402 goto exit;
403 }
404
405 convert_calcomp_to_xml = symbol;
406
407 if (!g_module_symbol (module, "e_cal_backend_ews_get_type_for_testing_sources", &symbol)) {
408 g_printerr ("\n%s\n", g_module_error ());
409 retval = 6;
410 goto exit;
411 }
412
413 cal_backend_ews_get_type = symbol;
414
415 server = ews_test_get_mock_server ();
416 etds = ews_test_get_test_data_list ();
417
418 /* Set handler of debug information */
419 populate_windows_zones ();
420 builtin_timezones = i_cal_timezone_get_builtin_timezones ();
421
422 for (l = etds; l != NULL; l = l->next) {
423 EwsTestData *etd = l->data;
424 gchar *message;
425
426 if (!uhm_server_get_enable_online (server))
427 g_signal_connect (server, "notify::resolver", (GCallback) server_notify_resolver_cb, etd);
428
429 if (e_ews_debug_get_server_version_from_string (etd->version) >= E_EWS_EXCHANGE_2010) {
430 message = g_strdup_printf ("/%s/calendar/timezones/ical_compatibility", etd->version);
431 g_test_add_data_func (message, etd, test_libical_timezones_compatibility);
432 g_free (message);
433
434 message = g_strdup_printf ("/%s/calendar/timezones/time_zones_sync", etd->version);
435 g_test_add_data_func (message, etd, test_time_zones_sync);
436 g_free (message);
437 }
438 }
439
440 retval = g_test_run ();
441
442 if (!uhm_server_get_enable_online (server))
443 for (l = etds; l != NULL; l = l->next)
444 g_signal_handlers_disconnect_by_func (server, server_notify_resolver_cb, l->data);
445
446 exit:
447 if (module != NULL)
448 g_module_close (module);
449 i_cal_timezone_free_builtin_timezones ();
450 ews_test_cleanup ();
451
452 return retval;
453 }