1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
4 *
5 * This library is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdlib.h>
19 #include <locale.h>
20 #include <libecal/libecal.h>
21
22 #include "e-test-server-utils.h"
23 #include "test-cal-cache-utils.h"
24
25 static ECalComponentId *
extract_id_from_component(ECalComponent * component)26 extract_id_from_component (ECalComponent *component)
27 {
28 ECalComponentId *id;
29
30 g_assert (component != NULL);
31
32 id = e_cal_component_get_id (component);
33 g_assert (id != NULL);
34 g_assert (e_cal_component_id_get_uid (id) != NULL);
35
36 return id;
37 }
38
39 static ECalComponentId *
extract_id_from_string(const gchar * icalstring)40 extract_id_from_string (const gchar *icalstring)
41 {
42 ECalComponent *component;
43 ECalComponentId *id;
44
45 g_assert (icalstring != NULL);
46
47 component = e_cal_component_new_from_string (icalstring);
48 g_assert (component != NULL);
49
50 id = extract_id_from_component (component);
51
52 g_object_unref (component);
53
54 return id;
55 }
56
57 static void
test_get_one(ECalCache * cal_cache,const gchar * uid,const gchar * rid,gboolean expect_failure)58 test_get_one (ECalCache *cal_cache,
59 const gchar *uid,
60 const gchar *rid,
61 gboolean expect_failure)
62 {
63 ECalComponent *component = NULL;
64 ECalComponentId *id;
65 gchar *icalstring = NULL;
66 gboolean success;
67 GError *error = NULL;
68
69 success = e_cal_cache_get_component (cal_cache, uid, rid, &component, NULL, &error);
70 if (expect_failure) {
71 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
72 g_assert (!success);
73 g_assert (!component);
74
75 g_clear_error (&error);
76 } else {
77 g_assert_no_error (error);
78 g_assert (success);
79 g_assert_nonnull (component);
80
81 id = extract_id_from_component (component);
82
83 g_assert_cmpstr (e_cal_component_id_get_uid (id), ==, uid);
84 g_assert_cmpstr (e_cal_component_id_get_rid (id), ==, rid && *rid ? rid : NULL);
85
86 e_cal_component_id_free (id);
87 g_object_unref (component);
88 }
89
90 success = e_cal_cache_get_component_as_string (cal_cache, uid, rid, &icalstring, NULL, &error);
91 if (expect_failure) {
92 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
93 g_assert (!success);
94 g_assert (!icalstring);
95
96 g_clear_error (&error);
97 } else {
98 g_assert_no_error (error);
99 g_assert (success);
100 g_assert_nonnull (icalstring);
101
102 id = extract_id_from_string (icalstring);
103
104 g_assert_cmpstr (e_cal_component_id_get_uid (id), ==, uid);
105 g_assert_cmpstr (e_cal_component_id_get_rid (id), ==, rid && *rid ? rid : NULL);
106
107 e_cal_component_id_free (id);
108 g_free (icalstring);
109 }
110 }
111
112 static void
test_getters_one(TCUFixture * fixture,gconstpointer user_data)113 test_getters_one (TCUFixture *fixture,
114 gconstpointer user_data)
115 {
116 test_get_one (fixture->cal_cache, "unexistent-event", NULL, TRUE);
117 test_get_one (fixture->cal_cache, "unexistent-event", "", TRUE);
118 test_get_one (fixture->cal_cache, "event-2", NULL, FALSE);
119 test_get_one (fixture->cal_cache, "event-2", "", FALSE);
120 test_get_one (fixture->cal_cache, "event-5", NULL, FALSE);
121 test_get_one (fixture->cal_cache, "event-5", "", FALSE);
122 test_get_one (fixture->cal_cache, "event-5", "20131231T000000Z", TRUE);
123 test_get_one (fixture->cal_cache, "event-6", NULL, FALSE);
124 test_get_one (fixture->cal_cache, "event-6", "", FALSE);
125 test_get_one (fixture->cal_cache, "event-6", "20170225T134900", FALSE);
126 }
127
128 /* NULL-terminated list of pairs <uid, rid>, what to expect */
129 static void
test_get_all(ECalCache * cal_cache,const gchar * uid,...)130 test_get_all (ECalCache *cal_cache,
131 const gchar *uid,
132 ...)
133 {
134 ECalComponentId *id;
135 GSList *items, *link;
136 va_list va;
137 const gchar *tmp;
138 GHashTable *expects;
139 gboolean success;
140 GError *error = NULL;
141
142 expects = g_hash_table_new_full ((GHashFunc) e_cal_component_id_hash, (GEqualFunc) e_cal_component_id_equal,
143 (GDestroyNotify) e_cal_component_id_free, NULL);
144
145 va_start (va, uid);
146 tmp = va_arg (va, const gchar *);
147 while (tmp) {
148 const gchar *rid = va_arg (va, const gchar *);
149 id = e_cal_component_id_new (tmp, rid);
150
151 g_hash_table_insert (expects, id, NULL);
152
153 tmp = va_arg (va, const gchar *);
154 }
155 va_end (va);
156
157 items = NULL;
158
159 success = e_cal_cache_get_components_by_uid (cal_cache, uid, &items, NULL, &error);
160 if (!g_hash_table_size (expects)) {
161 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
162 g_assert (!success);
163 g_assert (!items);
164
165 g_clear_error (&error);
166 } else {
167 g_assert_no_error (error);
168 g_assert (success);
169 g_assert_nonnull (items);
170
171 g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items));
172
173 for (link = items; link; link = g_slist_next (link)) {
174 id = extract_id_from_component (link->data);
175
176 g_assert_cmpstr (e_cal_component_id_get_uid (id), ==, uid);
177 g_assert (g_hash_table_contains (expects, id));
178
179 e_cal_component_id_free (id);
180 }
181
182 g_slist_free_full (items, g_object_unref);
183 }
184
185 items = NULL;
186
187 success = e_cal_cache_get_components_by_uid_as_string (cal_cache, uid, &items, NULL, &error);
188 if (!g_hash_table_size (expects)) {
189 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
190 g_assert (!success);
191 g_assert (!items);
192
193 g_clear_error (&error);
194 } else {
195 g_assert_no_error (error);
196 g_assert (success);
197 g_assert_nonnull (items);
198
199 g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items));
200
201 for (link = items; link; link = g_slist_next (link)) {
202 id = extract_id_from_string (link->data);
203
204 g_assert_cmpstr (e_cal_component_id_get_uid (id), ==, uid);
205 g_assert (g_hash_table_contains (expects, id));
206
207 e_cal_component_id_free (id);
208 }
209
210 g_slist_free_full (items, g_free);
211 }
212
213 g_hash_table_destroy (expects);
214 }
215
216 static void
test_getters_all(TCUFixture * fixture,gconstpointer user_data)217 test_getters_all (TCUFixture *fixture,
218 gconstpointer user_data)
219 {
220 test_get_all (fixture->cal_cache, "unexistent-event", NULL);
221 test_get_all (fixture->cal_cache, "unexistent-event", NULL);
222 test_get_all (fixture->cal_cache, "event-2", "event-2", NULL, NULL);
223 test_get_all (fixture->cal_cache, "event-5", "event-5", NULL, NULL);
224 test_get_all (fixture->cal_cache, "event-6", "event-6", NULL, "event-6", "20170225T134900", NULL);
225 }
226
227 gint
main(gint argc,gchar ** argv)228 main (gint argc,
229 gchar **argv)
230 {
231 TCUClosure closure_events = { TCU_LOAD_COMPONENT_SET_EVENTS };
232
233 #if !GLIB_CHECK_VERSION (2, 35, 1)
234 g_type_init ();
235 #endif
236 g_test_init (&argc, &argv, NULL);
237 g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");
238
239 tcu_read_args (argc, argv);
240
241 /* Ensure that the client and server get the same locale */
242 g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE));
243 setlocale (LC_ALL, "");
244
245 g_test_add ("/ECalCache/Getters/One", TCUFixture, &closure_events,
246 tcu_fixture_setup, test_getters_one, tcu_fixture_teardown);
247 g_test_add ("/ECalCache/Getters/All", TCUFixture, &closure_events,
248 tcu_fixture_setup, test_getters_all, tcu_fixture_teardown);
249
250 return e_test_server_utils_run_full (argc, argv, 0);
251 }
252