Added new user api unit test for the new fields.
[libgcal.git] / utests / utest_gcal.c
blobda02245d05c1eb5011a85accb83a38be4197f64d
1 /*
2 * @file utest_gcal.h
3 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
4 * @date Started on Mar 03 2008
6 * @brief Module for libgcal utests.
7 */
9 #include "utest_gcal.h"
10 #include "gcal.h"
11 #include "gcal_parser.h"
12 #include "utils.h"
13 #include <string.h>
15 struct gcal_resource *ptr_gcal = NULL;
17 static void setup(void)
19 /* here goes any common data allocation */
20 ptr_gcal = gcal_construct(GCALENDAR);
23 static void teardown(void)
25 /* and here we clean up */
26 gcal_destroy(ptr_gcal);
29 START_TEST (test_gcal_authenticate)
32 int result;
33 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "66libgcal");
34 fail_if(result != 0, "Authentication should work");
36 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "fail_fail");
37 fail_if(result == 0, "Authentication must fail");
39 /* Calling again, it must free internal pointer with previous
40 * authentication.
42 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "66libgcal");
43 fail_if(result == -1, "Authentication should work");
46 END_TEST
48 START_TEST (test_url_parse)
50 char value_url[] = "http://www.google.com/calendar/feeds/default"
51 "/owncalendars/full?gsessionid=3ymuQGgqKY1Qz8mk5qUJrw";
52 char *url;
54 char raw_data[] = "<HTML>\n"
55 "<HEAD>\n"
56 "<TITLE>Moved Temporarily</TITLE>\n"
57 "</HEAD>\n"
58 "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n"
59 "<H1>Moved Temporarily</H1>\n"
60 "The document has moved"
61 "<A HREF=\"http://www.google.com/calendar/feeds/default/owncalendars/full?gsessionid=3ymuQGgqKY1Qz8mk5qUJrw\">here</A>.\n"
62 "</BODY>\n"
63 "</HTML>\n";
65 get_the_url(raw_data, sizeof(raw_data), &url);
66 fail_if(url == NULL, "Function failed to get the URL");
67 fail_if(strncmp(value_url, url, sizeof(value_url)) != 0,
68 "Returned url is wrong");
71 END_TEST
73 START_TEST (test_editurl_parse)
75 char *super_contact = NULL;
76 char *edit_url = NULL;
77 int result;
78 char *tmp;
80 if (find_load_file("/utests/fullcontact.xml", &super_contact))
81 fail_if(1, "Cannot load contact XML file!");
83 result = get_edit_url(super_contact, strlen(super_contact), &edit_url);
84 fail_if(result == -1, "Failed extracting edit URL from raw XML entry!");
85 fail_if(edit_url == NULL, "Failed extracting edit URL from raw XML entry!");
87 result = strcmp(edit_url, "http://www.google.com/m8/feeds/contacts/gcalntester%40gmail.com/base/a1fa2ca095c082e/1216490120006000");
88 fail_if(result != 0, "Extracted URL differs from sample file!");
89 free(super_contact);
90 free(edit_url);
92 if (find_load_file("/utests/gcalendar.xml", &super_contact))
93 fail_if(1, "Cannot load calendar XML file!");
95 result = get_edit_url(super_contact, strlen(super_contact), &edit_url);
96 fail_if(result == -1, "Failed extracting edit URL from raw XML entry!");
97 fail_if(edit_url == NULL, "Failed extracting edit URL from raw XML entry!");
98 tmp = strstr(edit_url, "http://www.google.com/calendar/feeds/default/private/full/oserr36hrabj8l184qkddocmo8/63352172611");
99 fail_if(tmp == NULL, "Cannot find address, check if URL is correct!");
100 free(super_contact);
101 free(edit_url);
103 if (find_load_file("/utests/supercontact.xml", &super_contact))
104 fail_if(1, "Cannot load contact XML file!");
105 result = get_edit_url(super_contact, strlen(super_contact), &edit_url);
106 fail_if(edit_url != NULL, "This file has no edit URL. Failed!");
107 free(super_contact);
108 free(edit_url);
111 END_TEST
114 START_TEST (test_gcal_dump)
116 int result;
117 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "66libgcal");
118 if (result)
119 fail_if(1, "Authentication should work");
121 result = gcal_dump(ptr_gcal, "GData-Version: 2");
122 fail_if(result != 0, "Failed dumping events");
125 END_TEST
128 START_TEST (test_gcal_event)
130 int i;
131 size_t result;
132 struct gcal_event *entries;
133 char *entries_update[] = { "2008-03-26T20:20:51.000Z",
134 "2008-03-26T12:30:06.000Z",
135 "2008-03-10T12:56:43.000Z",
136 "2008-03-06T15:32:25.000Z" };
138 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "66libgcal");
139 if (result)
140 fail_if(1, "Authentication should work");
142 result = gcal_dump(ptr_gcal, "GData-Version: 2");
143 fail_if(result != 0, "Failed dumping events");
145 result = gcal_entry_number(ptr_gcal);
146 fail_if(result != 4, "Got wrong number of entries");
148 entries = gcal_get_entries(ptr_gcal, &result);
149 fail_if(entries == NULL, "Failed extracting the entries vector");
151 if (entries != NULL)
152 for (i = 0; i < (int)result; ++i)
153 fail_if(strcmp(entries[i].common.updated, entries_update[i]),
154 "extracted data differs from expected");
155 /* Cleanup */
156 gcal_destroy_entries(entries, result);
158 END_TEST
162 START_TEST (test_gcal_naive)
164 /* This test uses a user/password invalid. The purpose is to check
165 * if the library will behave correctly.
167 size_t result, i;
168 struct gcal_resource *local_gcal;
169 struct gcal_event *entries;
171 local_gcal = gcal_construct(GCALENDAR);
172 result = gcal_get_authentication(local_gcal, "username", "a_password");
173 fail_if((signed)result != -1, "Authentication must fail!");
175 result = gcal_dump(local_gcal, "GData-Version: 2");
176 fail_if((signed)result != -1, "Dump must fail!");
178 entries = gcal_get_entries(local_gcal, &result);
179 fail_if(entries, "Getting the calendar field data must fail!");
181 if (entries)
182 for (i = 0; i < result; ++i)
183 printf("%s\t%s\n", entries[i].common.title,
184 entries[i].common.updated);
185 /* Cleanup */
186 gcal_destroy_entries(entries, result);
187 gcal_destroy(local_gcal);
190 END_TEST
194 TCase *gcal_tcase_create(void)
196 TCase *tc = NULL;
197 int timeout_seconds = 90;
198 tc = tcase_create("gcal");
199 tcase_add_checked_fixture(tc, setup, teardown);
200 tcase_set_timeout (tc, timeout_seconds);
201 tcase_add_test(tc, test_gcal_authenticate);
202 tcase_add_test(tc, test_url_parse);
203 tcase_add_test(tc, test_gcal_dump);
204 tcase_add_test(tc, test_gcal_event);
205 tcase_add_test(tc, test_gcal_naive);
206 tcase_add_test(tc, test_editurl_parse);
207 return tc;