* Fixes invalid unrefs when comparing merge folders
[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 <gtk/gtk.h>
36 #include <modest-init.h>
37
38 /* ----------------------- Defines ---------------------- */
39
40 #define TEST_MODEST_ACCOUNT_NAME  "modest-unit-tests-àccount"
41
42 /* ------------------ Global variables ------------------ */
43
44 static ModestAccountMgr *account_mgr = NULL;
45
46 /* ---------------------- Fixtures --------------------- */
47
48 static void
49 fx_setup_default_account_mgr ()
50 {
51         fail_unless (gtk_init_check (NULL, NULL));
52
53         fail_unless (g_setenv (MODEST_DIR_ENV, ".modesttest", TRUE));
54         fail_unless (g_setenv (MODEST_NAMESPACE_ENV, "/apps/modesttest", TRUE));
55         fail_unless (modest_init (0, NULL), "Failed running modest_init");
56
57         account_mgr = modest_runtime_get_account_mgr ();
58         fail_unless (MODEST_IS_ACCOUNT_MGR (account_mgr),
59                      "modest_account_mgr_new failed");
60
61         /* cleanup old garbage (from previous runs)*/
62         if (modest_account_mgr_account_exists(account_mgr,
63                                               TEST_MODEST_ACCOUNT_NAME,
64                                               FALSE))
65                 modest_account_mgr_remove_account (account_mgr,
66                                                    TEST_MODEST_ACCOUNT_NAME);
67         if (modest_account_mgr_account_exists(account_mgr,
68                                               TEST_MODEST_ACCOUNT_NAME,
69                                               TRUE)) {
70                 modest_account_mgr_remove_server_account (account_mgr,
71                                                           TEST_MODEST_ACCOUNT_NAME);
72         }
73 }
74
75 /* ---------- add/exists/remove account tests  ---------- */
76
77 /**
78  * Test regular usage of 
79  * modest_account_mgr_add_account
80  * modest_account_mgr_add_server_account
81  * modest_account_mgr_account_exists
82  * modest_account_mgr_remove_account
83  *  - Test 1: Create anaccount
84  *  - Test 2: Check account exists
85  *  - Test 3: Remove account
86  *  - Test 4: Create a server account
87  *  - Test 5: Check server account exists
88  *  - Test 6: Remove server account
89  *  - Test 7: Check if a non-existing account exists
90  *  - Test 8: Check if a non-existing server account exists
91  */
92 START_TEST (test_add_exists_remove_account_regular)
93 {
94         gchar *name = NULL;
95         gchar *store_account = NULL;
96         gchar *transport_account = NULL;
97         gchar *hostname = NULL;
98         gchar *username = NULL;
99         gchar *password = NULL;
100         gint portnum;
101         ModestProtocolType proto;
102         gboolean result;
103         
104         name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
105         /* Test 1 */
106         store_account = g_strdup ("imap://me@myserver");
107         transport_account = g_strdup ("local-smtp");
108         result = modest_account_mgr_add_account (account_mgr,
109                                                  name,
110                                                  "test display name",
111                                                  "user fullname",
112                                                  "user@email.com",
113                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
114                                                  store_account,
115                                                  transport_account, TRUE);
116         fail_unless (result,
117                      "modest_account_mgr_add_account failed:\n" \
118                      "name: %s\nstore: %s\ntransport: %s\n",
119                      name, store_account, transport_account, TRUE);
120         
121         g_free (store_account);
122         g_free (transport_account);
123
124         /* Test 2 */
125         result = modest_account_mgr_account_exists (account_mgr,
126                                                     name,
127                                                     FALSE);
128         fail_unless (result,
129                      "modest_account_mgr_account_exists failed: " \
130                      "Account with name \"%s\" should exist.\n", name);
131         
132
133         /* Test 3 */
134         result = modest_account_mgr_remove_account (account_mgr,
135                                                     name);
136         fail_unless (result,
137                      "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
138                      name);
139
140
141         /* Test 4 */
142         hostname = g_strdup ("myhostname.mydomain.com");
143         username = g_strdup ("myusername");
144         password = g_strdup ("mypassword");
145         proto = MODEST_PROTOCOLS_TRANSPORT_SMTP;
146         portnum = 25;
147         result = modest_account_mgr_add_server_account (account_mgr,
148                                                         name,
149                                                         hostname,
150                                                         portnum,
151                                                         username,
152                                                         password,
153                                                         proto,
154                                                         MODEST_PROTOCOLS_CONNECTION_NONE,
155                                                         MODEST_PROTOCOLS_AUTH_NONE); 
156         fail_unless (result,
157                      "modest_account_mgr_add_server_account failed:\n" \
158                      "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %d",
159                      name, hostname, username, password, proto);
160
161         g_free (hostname);
162         g_free (username);
163         g_free (password);
164         
165         /* Test 5 */
166         result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
167         fail_unless (result,
168                      "modest_account_mgr_account_exists failed: " \
169                      "Server account with name \"%s\" should exist. Error: %s", name);
170
171         /* Test 6 */
172         result = modest_account_mgr_remove_server_account (account_mgr,
173                                                            name);
174         fail_unless (result,
175                      "modest_account_mgr_remove_server_account failed:\nname: %s\nerror: %s",
176                      name);
177
178
179         /* Test 7 */
180         result = modest_account_mgr_account_exists (account_mgr,
181                                                     "a_name_that_does_not_exist",
182                                                     FALSE);
183         fail_unless (!result,
184                      "modest_account_mgr_exists_account does not return " \
185                      "FALSE when passing an account that does not exist");
186
187         /* Test 8 */
188         result = modest_account_mgr_account_exists (account_mgr,
189                                                     "a_name_that_does_not_exist",
190                                                     TRUE);
191         fail_unless (!result,
192                      "modest_account_mgr_exists_account does not return " \
193                      "FALSE when passing a server account that does not exist");
194         
195         g_free (name);
196 }
197 END_TEST
198
199 /**
200  * Test regular usage of 
201  * modest_account_mgr_add_account,
202  * modest_account_mgr_add_server_account
203  * modest_account_mgr_account_exists
204  * modest_account_mgr_remove_account
205  *  - Test 1: Create account with NULL account_mgr
206  *  - Test 2: Create account with NULL name
207  *  - Test 3: Create account with invalid name string
208  *  - Test 4: Create server account with NULL account_mgr
209  *  - Test 5: Create server account with NULL name
210  *  - Test 6: Create server account with invalid name string
211  *  - Test 7: Remove a non-existing account
212  *  - Test 8: Remove a non-existing server account (REMOVED)
213  *  - Test 9: Remove with NULL acount manager
214  *  - Test 10: Remove with NULL name
215  *  - Test 11: Check if an  account exists with NULL account_mgr
216  *  - Test 12: Check if a server account exists with a NULL account_mgr
217  *  - Test 13: Check if a NULL account exists
218  *  - Test 14: Check if a NULL server account exists
219  */
220 START_TEST (test_add_exists_remove_account_invalid)
221 {
222         gboolean result;
223
224         /* Test 1 */
225         result = modest_account_mgr_add_account (NULL,
226                                                  TEST_MODEST_ACCOUNT_NAME,
227                                                  "test display name",
228                                                  "user fullname",
229                                                  "user@email.com",
230                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
231                                                  "store_account",
232                                                  "transport_account", TRUE);
233         fail_unless (!result,
234                      "modest_account_mgr_add_account does not return FALSE when" \
235                      "passing a NULL ModestAccountMgr");
236
237         /* Test 2 */
238         result = modest_account_mgr_add_account (account_mgr,
239                                                  NULL,
240                                                  "test display name",
241                                                  "user fullname",
242                                                  "user@email.com",
243                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
244                                                  "store_account",
245                                                  "transport_account", TRUE);
246         fail_unless (!result,
247                      "modest_account_mgr_add_account does not return FALSE when" \
248                      "passing a NULL account name");
249
250         /* Test 3*/
251         result = modest_account_mgr_add_account (account_mgr,
252                                                  "ïnválid//accountñ//nÄméç",
253                                                  "test display name",
254                                                  "user fullname",
255                                                  "user@email.com",
256                                                  MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
257                                                  "store_account",
258                                                  "transport_account", TRUE);
259         fail_unless (!result,
260                      "modest_account_mgr_add_account does not return FALSE when" \
261                      "passing an invalid account name");
262
263         /* Test 4 */
264         result = modest_account_mgr_add_server_account (NULL,
265                                                         TEST_MODEST_ACCOUNT_NAME,
266                                                         "hostname",
267                                                         143,
268                                                         "username",
269                                                         "password",
270                                                         MODEST_PROTOCOLS_STORE_IMAP,
271                                                        MODEST_PROTOCOLS_CONNECTION_NONE,
272                                                        MODEST_PROTOCOLS_AUTH_NONE);
273         fail_unless (!result,
274                      "modest_account_mgr_add_server_account does not return " \
275                      "FALSE when passing a NULL ModestAccountMgr");
276
277         /* Test 5 */
278         result = modest_account_mgr_add_server_account (account_mgr,
279                                                         NULL,
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 account name");
290
291         /* Test 6 */
292         result = modest_account_mgr_add_server_account (account_mgr, 
293                                                         "ïnválid//accountñ//nÄméç",
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 an invalid account name"); 
304
305         /* Test 7 */
306         result = modest_account_mgr_remove_account (account_mgr,
307                                                     "a_name_that_does_not_exist");
308         fail_unless (!result,
309                      "modest_account_mgr_remove_acccount does not return FALSE " \
310                      "when trying to remove an account that does not exist");
311
312         /* Test 9 */
313         result = modest_account_mgr_remove_account (NULL,
314                                                     TEST_MODEST_ACCOUNT_NAME);
315         fail_unless (!result,
316                      "modest_account_mgr_remove_acccount does not return " \
317                      "FALSE when passing a NULL ModestAccountMgr");
318
319         /* Test 10 */
320         result = modest_account_mgr_remove_account (account_mgr,
321                                                     NULL);
322         fail_unless (!result,
323                      "modest_account_mgr_remove_acccount does not return " \
324                      "FALSE when passing a NULL account name");
325
326         /* Test 11 */
327         result = modest_account_mgr_account_exists (NULL,
328                                                     TEST_MODEST_ACCOUNT_NAME,
329                                                     TRUE);
330         fail_unless (!result,
331                      "modest_account_mgr_exists_account does not return " \
332                      "FALSE when passing a NULL ModestAccountMgr");
333
334         /* Test 12 */
335         result = modest_account_mgr_account_exists (NULL,
336                                                     TEST_MODEST_ACCOUNT_NAME,
337                                                     FALSE);
338         fail_unless (!result,
339                      "modest_account_mgr_exists_account does not return " \
340                      "FALSE when passing a NULL ModestAccountMgr");
341
342         /* Test 13 */
343         result = modest_account_mgr_account_exists (account_mgr,
344                                                     NULL,
345                                                     FALSE);
346         fail_unless (!result,
347                      "modest_account_mgr_exists_acccount does not return " \
348                      "FALSE when passing a NULL account name");
349
350         /* Test 14 */
351         result = modest_account_mgr_account_exists (account_mgr,
352                                                     NULL,
353                                                     TRUE);
354         fail_unless (!result,
355                      "modest_account_mgr_exists_account does not return " \
356                      "FALSE when passing a NULL server account name");
357 }
358 END_TEST
359
360 /* ------------------- Suite creation ------------------- */
361
362 static Suite*
363 account_mgr_suite (void)
364 {
365         Suite *suite = suite_create ("ModestAccountMgr");
366         TCase *tc = NULL;
367
368         /* Tests case for "add/exists/remove account" */
369         tc = tcase_create ("add_exists_remove_account");
370         tcase_add_unchecked_fixture (tc, 
371                                      fx_setup_default_account_mgr, NULL);
372         tcase_add_test (tc, test_add_exists_remove_account_regular);
373         tcase_add_test (tc, test_add_exists_remove_account_invalid);
374         suite_add_tcase (suite, tc);
375
376         return suite;
377 }
378
379 /* --------------------- Main program ------------------- */
380
381 gint
382 main ()
383 {
384         SRunner *srunner;
385         Suite   *suite;
386         int     failures;
387
388         suite   = account_mgr_suite ();
389         srunner = srunner_create (suite);
390         
391         srunner_run_all (srunner, CK_ENV);
392         failures = srunner_ntests_failed (srunner);
393         srunner_free (srunner);
394         
395         return failures;
396 }