Some changes in unit tests.
[modest] / tests / check_account-mgr.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <check.h>
31 #include <string.h>
32 #include <modest-defs.h>
33 #include <modest-conf.h>
34 #include <modest-account-mgr.h>
35 #include <modest-utils.h>
36 #include <gtk/gtk.h>
37 #include <modest-init.h>
38
39 /* ----------------------- Defines ---------------------- */
40
41 #define TEST_MODEST_ACCOUNT_NAME  "modest-unit-tests-àccount"
42
43 /* ------------------ Global variables ------------------ */
44
45 static ModestAccountMgr *account_mgr = NULL;
46
47 /* ---------------------- Fixtures --------------------- */
48
49 static void
50 fx_setup_default_account_mgr ()
51 {
52         fail_unless (gtk_init_check (NULL, NULL));
53
54         fail_unless (g_setenv (MODEST_DIR_ENV, ".modesttest", TRUE));
55         fail_unless (g_setenv (MODEST_NAMESPACE_ENV, "/apps/modesttest", TRUE));
56         fail_unless (modest_init (0, NULL), "Failed running modest_init");
57
58         account_mgr = modest_runtime_get_account_mgr ();
59         fail_unless (MODEST_IS_ACCOUNT_MGR (account_mgr),
60                      "modest_account_mgr_new failed");
61
62         /* cleanup old garbage (from previous runs)*/
63         if (modest_account_mgr_account_exists(account_mgr,
64                                               TEST_MODEST_ACCOUNT_NAME,
65                                               FALSE))
66                 modest_account_mgr_remove_account (account_mgr,
67                                                    TEST_MODEST_ACCOUNT_NAME);
68         if (modest_account_mgr_account_exists(account_mgr,
69                                               TEST_MODEST_ACCOUNT_NAME,
70                                               TRUE)) {
71                 modest_account_mgr_remove_server_account (account_mgr,
72                                                           TEST_MODEST_ACCOUNT_NAME);
73         }
74 }
75
76 /* ---------- add/exists/remove account tests  ---------- */
77
78 /**
79  * Test regular usage of 
80  * modest_account_mgr_add_account
81  * modest_account_mgr_add_server_account
82  * modest_account_mgr_account_exists
83  * modest_account_mgr_remove_account
84  *  - Test 1: Create anaccount
85  *  - Test 2: Check account exists
86  *  - Test 3: Remove account
87  *  - Test 4: Create a server account
88  *  - Test 5: Check server account exists
89  *  - Test 6: Remove server account
90  *  - Test 7: Check if a non-existing account exists
91  *  - Test 8: Check if a non-existing server account exists
92  */
93 START_TEST (test_add_exists_remove_account_regular)
94 {
95         gchar *name = NULL;
96         gchar *store_account = NULL;
97         gchar *transport_account = NULL;
98         gchar *hostname = NULL;
99         gchar *username = NULL;
100         gchar *password = NULL;
101         gint portnum;
102         ModestProtocolType proto;
103         gboolean result;
104         
105         name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
106         /* Test 1 */
107         store_account = g_strdup ("imap://me@myserver");
108         transport_account = g_strdup ("local-smtp");
109         result = modest_account_mgr_add_account (account_mgr,
110                                                  name,
111                                                  "test display name",
112                                                  "user fullname",
113                                                  "user@email.com",
114                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
115                                                  store_account,
116                                                  transport_account, TRUE);
117         fail_unless (result,
118                      "modest_account_mgr_add_account failed:\n" \
119                      "name: %s\nstore: %s\ntransport: %s\n",
120                      name, store_account, transport_account, TRUE);
121         
122         g_free (store_account);
123         g_free (transport_account);
124
125         /* Test 2 */
126         result = modest_account_mgr_account_exists (account_mgr,
127                                                     name,
128                                                     FALSE);
129         fail_unless (result,
130                      "modest_account_mgr_account_exists failed: " \
131                      "Account with name \"%s\" should exist.\n", name);
132
133         /* Test 2b */
134         gchar *account_name_from_recipient;
135         account_name_from_recipient = modest_utils_get_account_name_from_recipient ("user@email.com");
136         fail_unless (account_name_from_recipient != NULL,
137                      "modest_utils_get_account_name_from_recipient failed: "\
138                      "From user@email.com should match account");
139         
140         /* Test 2c */
141         account_name_from_recipient = modest_utils_get_account_name_from_recipient ("egg@egg.com");
142         fail_unless (account_name_from_recipient == NULL,
143                      "modest_utils_get_account_name_from_recipient failed: "\
144                      "From egg@egg.com shouldn't match account");
145         
146
147         /* Test 3 */
148         result = modest_account_mgr_remove_account (account_mgr,
149                                                     name);
150         fail_unless (result,
151                      "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
152                      name);
153
154
155         /* Test 4 */
156         hostname = g_strdup ("myhostname.mydomain.com");
157         username = g_strdup ("myusername");
158         password = g_strdup ("mypassword");
159         proto = MODEST_PROTOCOLS_TRANSPORT_SMTP;
160         portnum = 25;
161         result = modest_account_mgr_add_server_account (account_mgr,
162                                                         name,
163                                                         hostname,
164                                                         portnum,
165                                                         username,
166                                                         password,
167                                                         proto,
168                                                         MODEST_PROTOCOLS_CONNECTION_NONE,
169                                                         MODEST_PROTOCOLS_AUTH_NONE); 
170         fail_unless (result,
171                      "modest_account_mgr_add_server_account failed:\n" \
172                      "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %d",
173                      name, hostname, username, password, proto);
174
175         g_free (hostname);
176         g_free (username);
177         g_free (password);
178         
179         /* Test 5 */
180         result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
181         fail_unless (result,
182                      "modest_account_mgr_account_exists failed: " \
183                      "Server account with name \"%s\" should exist. Error: %s", name);
184
185         /* Test 6 */
186         result = modest_account_mgr_remove_server_account (account_mgr,
187                                                            name);
188         fail_unless (result,
189                      "modest_account_mgr_remove_server_account failed:\nname: %s\nerror: %s",
190                      name);
191
192
193         /* Test 7 */
194         result = modest_account_mgr_account_exists (account_mgr,
195                                                     "a_name_that_does_not_exist",
196                                                     FALSE);
197         fail_unless (!result,
198                      "modest_account_mgr_exists_account does not return " \
199                      "FALSE when passing an account that does not exist");
200
201         /* Test 8 */
202         result = modest_account_mgr_account_exists (account_mgr,
203                                                     "a_name_that_does_not_exist",
204                                                     TRUE);
205         fail_unless (!result,
206                      "modest_account_mgr_exists_account does not return " \
207                      "FALSE when passing a server account that does not exist");
208         
209         g_free (name);
210 }
211 END_TEST
212
213 /**
214  * Test regular usage of 
215  * modest_account_mgr_add_account,
216  * modest_account_mgr_add_server_account
217  * modest_account_mgr_account_exists
218  * modest_account_mgr_remove_account
219  *  - Test 1: Create account with NULL account_mgr
220  *  - Test 2: Create account with NULL name
221  *  - Test 3: Create account with invalid name string
222  *  - Test 4: Create server account with NULL account_mgr
223  *  - Test 5: Create server account with NULL name
224  *  - Test 6: Create server account with invalid name string
225  *  - Test 7: Remove a non-existing account
226  *  - Test 8: Remove a non-existing server account (REMOVED)
227  *  - Test 9: Remove with NULL acount manager
228  *  - Test 10: Remove with NULL name
229  *  - Test 11: Check if an  account exists with NULL account_mgr
230  *  - Test 12: Check if a server account exists with a NULL account_mgr
231  *  - Test 13: Check if a NULL account exists
232  *  - Test 14: Check if a NULL server account exists
233  */
234 START_TEST (test_add_exists_remove_account_invalid)
235 {
236         gboolean result;
237
238         /* Test 1 */
239         result = modest_account_mgr_add_account (NULL,
240                                                  TEST_MODEST_ACCOUNT_NAME,
241                                                  "test display name",
242                                                  "user fullname",
243                                                  "user@email.com",
244                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
245                                                  "store_account",
246                                                  "transport_account", TRUE);
247         fail_unless (!result,
248                      "modest_account_mgr_add_account does not return FALSE when" \
249                      "passing a NULL ModestAccountMgr");
250
251         /* Test 2 */
252         result = modest_account_mgr_add_account (account_mgr,
253                                                  NULL,
254                                                  "test display name",
255                                                  "user fullname",
256                                                  "user@email.com",
257                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
258                                                  "store_account",
259                                                  "transport_account", TRUE);
260         fail_unless (!result,
261                      "modest_account_mgr_add_account does not return FALSE when" \
262                      "passing a NULL account name");
263
264         /* Test 3*/
265         result = modest_account_mgr_add_account (account_mgr,
266                                                  "ïnválid//accountñ//nÄméç",
267                                                  "test display name",
268                                                  "user fullname",
269                                                  "user@email.com",
270                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
271                                                  "store_account",
272                                                  "transport_account", TRUE);
273         fail_unless (!result,
274                      "modest_account_mgr_add_account does not return FALSE when" \
275                      "passing an invalid account name");
276
277         /* Test 4 */
278         result = modest_account_mgr_add_server_account (NULL,
279                                                         TEST_MODEST_ACCOUNT_NAME,
280                                                         "hostname",
281                                                         143,
282                                                         "username",
283                                                         "password",
284                                                         MODEST_PROTOCOLS_STORE_IMAP,
285                                                        MODEST_PROTOCOLS_CONNECTION_NONE,
286                                                        MODEST_PROTOCOLS_AUTH_NONE);
287         fail_unless (!result,
288                      "modest_account_mgr_add_server_account does not return " \
289                      "FALSE when passing a NULL ModestAccountMgr");
290
291         /* Test 5 */
292         result = modest_account_mgr_add_server_account (account_mgr,
293                                                         NULL,
294                                                         "hostname",
295                                                         143,
296                                                         "username",
297                                                         "password",
298                                                         MODEST_PROTOCOLS_STORE_IMAP,
299                                                         MODEST_PROTOCOLS_CONNECTION_NONE,
300                                                         MODEST_PROTOCOLS_AUTH_NONE); 
301         fail_unless (!result,
302                      "modest_account_mgr_add_server_account does not return " \
303                      "FALSE when passing a NULL account name");
304
305         /* Test 6 */
306         result = modest_account_mgr_add_server_account (account_mgr, 
307                                                         "ïnválid//accountñ//nÄméç",
308                                                         "hostname", 
309                                                         143,
310                                                         "username", 
311                                                         "password", 
312                                                         MODEST_PROTOCOLS_STORE_IMAP,
313                                                         MODEST_PROTOCOLS_CONNECTION_NONE,
314                                                         MODEST_PROTOCOLS_AUTH_NONE); 
315         fail_unless (!result, 
316                      "modest_account_mgr_add_server_account does not return " \
317                      "FALSE when passing an invalid account name"); 
318
319         /* Test 7 */
320         result = modest_account_mgr_remove_account (account_mgr,
321                                                     "a_name_that_does_not_exist");
322         fail_unless (!result,
323                      "modest_account_mgr_remove_acccount does not return FALSE " \
324                      "when trying to remove an account that does not exist");
325
326         /* Test 9 */
327         result = modest_account_mgr_remove_account (NULL,
328                                                     TEST_MODEST_ACCOUNT_NAME);
329         fail_unless (!result,
330                      "modest_account_mgr_remove_acccount does not return " \
331                      "FALSE when passing a NULL ModestAccountMgr");
332
333         /* Test 10 */
334         result = modest_account_mgr_remove_account (account_mgr,
335                                                     NULL);
336         fail_unless (!result,
337                      "modest_account_mgr_remove_acccount does not return " \
338                      "FALSE when passing a NULL account name");
339
340         /* Test 11 */
341         result = modest_account_mgr_account_exists (NULL,
342                                                     TEST_MODEST_ACCOUNT_NAME,
343                                                     TRUE);
344         fail_unless (!result,
345                      "modest_account_mgr_exists_account does not return " \
346                      "FALSE when passing a NULL ModestAccountMgr");
347
348         /* Test 12 */
349         result = modest_account_mgr_account_exists (NULL,
350                                                     TEST_MODEST_ACCOUNT_NAME,
351                                                     FALSE);
352         fail_unless (!result,
353                      "modest_account_mgr_exists_account does not return " \
354                      "FALSE when passing a NULL ModestAccountMgr");
355
356         /* Test 13 */
357         result = modest_account_mgr_account_exists (account_mgr,
358                                                     NULL,
359                                                     FALSE);
360         fail_unless (!result,
361                      "modest_account_mgr_exists_acccount does not return " \
362                      "FALSE when passing a NULL account name");
363
364         /* Test 14 */
365         result = modest_account_mgr_account_exists (account_mgr,
366                                                     NULL,
367                                                     TRUE);
368         fail_unless (!result,
369                      "modest_account_mgr_exists_account does not return " \
370                      "FALSE when passing a NULL server account name");
371 }
372 END_TEST
373
374 /* ------------------- Suite creation ------------------- */
375
376 static Suite*
377 account_mgr_suite (void)
378 {
379         Suite *suite = suite_create ("ModestAccountMgr");
380         TCase *tc = NULL;
381
382         /* Tests case for "add/exists/remove account" */
383         tc = tcase_create ("add_exists_remove_account");
384         tcase_add_checked_fixture (tc, 
385                                      fx_setup_default_account_mgr, NULL);
386         tcase_add_test (tc, test_add_exists_remove_account_regular);
387         tcase_add_test (tc, test_add_exists_remove_account_invalid);
388         suite_add_tcase (suite, tc);
389
390         return suite;
391 }
392
393 /* --------------------- Main program ------------------- */
394
395 gint
396 main ()
397 {
398         SRunner *srunner;
399         Suite   *suite;
400         int     failures;
401
402         suite   = account_mgr_suite ();
403         srunner = srunner_create (suite);
404         
405         srunner_run_all (srunner, CK_ENV);
406         failures = srunner_ntests_failed (srunner);
407         srunner_free (srunner);
408         
409         return failures;
410 }