Pointing where valgrind says that there is a remaining
[libgcal.git] / utests / utest_edit.c
bloba10882b114a00fccc05c2ddfd3d27c374d938ea7
1 /*
2 * @file utest_edit.h
3 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
4 * @date Tue Apr 8 09:09:25 2008
6 * @brief Module for tests for edit/add/delete
7 */
9 #include "utest_edit.h"
10 #include "gcal.h"
11 #include "xml_aux.h"
12 #include "gcal_parser.h"
13 #include <string.h>
14 #include <unistd.h>
16 static struct gcal_resource *ptr_gcal = NULL;
18 static void setup(void)
20 /* here goes any common data allocation */
21 ptr_gcal = gcal_construct(GCALENDAR);
25 static void teardown(void)
27 /* and here we clean up */
28 gcal_destroy(ptr_gcal);
31 START_TEST (test_edit_xmlres)
33 xmlTextWriter *writer;
34 xmlBuffer *buffer;
35 int result;
37 result = xmlentry_init_resources(&writer, &buffer);
38 fail_if(result == -1, "Failed creating XML resources");
40 xmlentry_destroy_resources(&writer, &buffer);
44 END_TEST
46 START_TEST (test_edit_xml)
48 struct gcal_event event;
49 char *xml = NULL;
50 int result, length;
52 gcal_init_event(&event);
54 event.common.title = "A new event";
55 event.content = "Here goes the description of my new event";
56 event.dt_start = "2008-04-08T08:00:00.000Z";
57 event.dt_end = "2008-04-08T09:00:00.000Z";
58 event.where = "someplace";
59 event.common.id = NULL;
60 event.common.edit_uri = event.common.etag = NULL;
62 /* TODO: think in a better way to describe the status, maybe use
63 * a set of strings.
65 event.status = "confirmed";
67 result = xmlentry_create(&event, &xml, &length);
68 fail_if(result == -1 || xml == NULL,
69 "Failed creating XML for a new calendar entry");
71 /* TODO: add a way to validate the generated XML. */
73 free(xml);
76 END_TEST
78 START_TEST (test_edit_add)
80 int result;
81 struct gcal_event event;
83 gcal_init_event(&event);
85 event.common.title = "A new event";
86 event.content = "Here goes the description of my new event";
87 event.dt_start = "2008-05-10T08:00:00.000Z";
88 event.dt_end = "2008-05-10T09:00:00.000Z";
89 event.where = "someplace";
90 event.common.id = NULL;
91 event.common.edit_uri = event.common.etag = NULL;
93 /* TODO: think in a better way to describe the status, maybe use
94 * a set of strings.
96 event.status = "confirmed";
98 result = gcal_get_authentication(ptr_gcal, "gcalntester", "77libgcal");
99 fail_if(result == -1, "Authentication should work.");
101 result = gcal_create_event(ptr_gcal, &event, NULL);
102 fail_if(result == -1, "Failed creating a new event!");
106 END_TEST
108 START_TEST (test_edit_delete)
111 int result, i, entry_index = -1;
112 struct gcal_event event;
113 struct gcal_event *entries;
115 gcal_init_event(&event);
117 event.common.title = "A soon to be deleted event";
118 event.content = "This event will be included and deleted soon";
119 event.dt_start = "2008-05-07T08:00:00.000Z";
120 event.dt_end = "2008-05-07T09:00:00.000Z";
121 event.where = "nevermind";
122 event.common.id = NULL;
123 event.common.edit_uri = event.common.etag = NULL;
125 /* TODO: think in a better way to describe the status, maybe use
126 * a set of strings.
128 event.status = "confirmed";
130 result = gcal_get_authentication(ptr_gcal, "gcalntester", "77libgcal");
131 fail_if(result == -1, "Authentication should work.");
133 result = gcal_create_event(ptr_gcal, &event, NULL);
134 fail_if(result == -1, "Failed creating a new event!");
136 result = gcal_dump(ptr_gcal, "GData-Version: 2");
137 fail_if(result == -1, "Failed dumping events");
139 entries = gcal_get_entries(ptr_gcal, &result);
140 fail_if(entries == NULL, "Failed extracting entries");
142 for (i = 0; i < result; ++i)
143 if (!strcmp(entries[i].common.title, event.common.title)) {
144 entry_index = i;
145 break;
147 fail_if(entry_index == -1, "Cannot locate the newly added event!");
149 result = gcal_delete_event(ptr_gcal, (entries + entry_index));
150 fail_if(result == -1, "Failed deleting event!");
152 /* Cleanup */
153 gcal_destroy_entries(entries, result);
155 END_TEST
159 START_TEST (test_edit_stress)
161 int result, i, j, total;
162 struct gcal_event event;
163 char *format_start, *format_end;
164 char start_buffer[256], end_buffer[256];
165 char number_buffer1[4], number_buffer2[4];
166 struct gcal_event *entries;
168 gcal_init_event(&event);
170 event.common.title = "A new event: stress test";
171 event.content = "Here goes the description of my new event";
172 format_start = "2008-05-%sT%s:00:00.000Z";
173 format_end = "2008-05-%sT%s:00:00.000Z";
174 event.where = "someplace";
175 event.common.id = NULL;
176 event.common.edit_uri = event.common.etag = NULL;
178 /* TODO: think in a better way to describe the status, maybe use
179 * a set of strings.
181 event.status = "confirmed";
183 result = gcal_get_authentication(ptr_gcal, "gcalntester", "77libgcal");
184 fail_if(result == -1, "Authentication should work.");
187 /* Whole month loop */
188 for (i = 1; i < 31; i += 2) {
189 /* whole day loop */
190 for (j = 4; j < 23; j += 3) {
191 snprintf(number_buffer1, sizeof(number_buffer1) - 1,
192 "%02d", i);
193 snprintf(number_buffer2, sizeof(number_buffer2) - 1,
194 "%02d", j);
195 snprintf(start_buffer, sizeof(start_buffer) - 1,
196 format_start, number_buffer1, number_buffer2);
198 snprintf(number_buffer2, sizeof(number_buffer2) - 1,
199 "%02d", j + 1);
200 snprintf(end_buffer, sizeof(end_buffer) - 1,
201 format_end, number_buffer1, number_buffer2);
203 event.dt_start = start_buffer;
204 event.dt_end = end_buffer;
205 result = gcal_create_event(ptr_gcal, &event, NULL);
206 fail_if(result == -1, "Failed creating a new event!"
207 " Loop is i = %d\tj = %d", i, j);
211 /* Give me some time to check if everything was added
212 * TODO: read an environment variable with the sleep time.
214 sleep(10);
216 /* Delete all test events */
217 result = gcal_dump(ptr_gcal, "GData-Version: 2");
218 fail_if(result == -1, "Failed dumping events");
220 entries = gcal_get_entries(ptr_gcal, &total);
221 fail_if(entries == NULL, "Failed extracting entries");
222 for (i = 0; i < total; ++i)
223 if (!strcmp(entries[i].common.title, event.common.title)) {
224 result = gcal_delete_event(ptr_gcal, &entries[i]);
225 fail_if(result == -1, "Failed deleting event!");
230 END_TEST
232 START_TEST (test_edit_edit)
235 int result;
236 struct gcal_event event, new_event, updated_event;
238 gcal_init_event(&event);
239 gcal_init_event(&new_event);
240 gcal_init_event(&updated_event);
242 event.common.title = "An editable event";
243 event.content = "This event will be included and edited";
244 event.dt_start = "2008-05-07T08:00:00.000Z";
245 event.dt_end = "2008-05-07T09:00:00.000Z";
246 event.where = "nevermind";
247 event.common.id = NULL;
248 event.common.edit_uri = event.common.etag = NULL;
249 /* TODO: think in a better way to describe the status, maybe use
250 * a set of strings.
252 event.status = "confirmed";
254 result = gcal_get_authentication(ptr_gcal, "gcalntester", "77libgcal");
255 fail_if(result == -1, "Authentication should work.");
257 result = gcal_create_event(ptr_gcal, &event, &new_event);
258 fail_if(result == -1, "Failed creating a new event!");
260 free(new_event.common.title);
261 new_event.common.title = strdup("An editable event: edited now!");
262 result = gcal_edit_event(ptr_gcal, &new_event, &updated_event);
263 fail_if(result == -1, "Failed editing event!");
265 /* TODO: delete the event from server */
267 /* Cleanup */
268 gcal_destroy_entry(&new_event);
269 gcal_destroy_entry(&updated_event);
271 END_TEST
273 TCase *edit_tcase_create(void)
275 TCase *tc = NULL;
276 int timeout_seconds = 1200;
277 tc = tcase_create("edit");
278 tcase_add_checked_fixture(tc, setup, teardown);
279 tcase_set_timeout (tc, timeout_seconds);
280 tcase_add_test(tc, test_edit_add);
281 tcase_add_test(tc, test_edit_xmlres);
282 tcase_add_test(tc, test_edit_xml);
283 tcase_add_test(tc, test_edit_delete);
284 // tcase_add_test(tc, test_edit_stress);
285 tcase_add_test(tc, test_edit_edit);
286 return tc;