* all:
[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-conf.h>
33 #include <modest-account-mgr.h>
34 #include <modest-protocol-info.h>
35
36 /* ----------------------- Defines ---------------------- */
37
38 #define TEST_MODEST_ACCOUNT_NAME  "modest-unit-tests-àccount"
39
40 /* ------------------ Global variables ------------------ */
41
42 static ModestAccountMgr *account_mgr = NULL;
43
44 /* ---------------------- Fixtures --------------------- */
45
46 static void
47 fx_setup_default_account_mgr ()
48 {
49         ModestConf *conf = NULL;
50
51         g_type_init ();
52
53         conf = modest_conf_new ();
54         fail_unless (MODEST_IS_CONF (conf), 
55                      "modest_conf_new failed");
56
57         account_mgr = modest_account_mgr_new (conf);
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                                                    FALSE);
68         if (modest_account_mgr_account_exists(account_mgr,
69                                               TEST_MODEST_ACCOUNT_NAME,
70                                               TRUE))
71                 modest_account_mgr_remove_account (account_mgr,
72                                                    TEST_MODEST_ACCOUNT_NAME,
73                                                    TRUE);
74 }
75
76 static void
77 fx_teardown_default_account_mgr ()
78 {
79         g_object_unref (account_mgr);
80 }
81
82 /* ---------- add/exists/remove account tests  ---------- */
83
84 /**
85  * Test regular usage of 
86  * modest_account_mgr_add_account
87  * modest_account_mgr_add_server_account
88  * modest_account_mgr_account_exists
89  * modest_account_mgr_remove_account
90  *  - Test 1: Create anaccount
91  *  - Test 2: Check account exists
92  *  - Test 3: Remove account
93  *  - Test 4: Create a server account
94  *  - Test 5: Check server account exists
95  *  - Test 6: Remove server account
96  *  - Test 7: Check if a non-existing account exists
97  *  - Test 8: Check if a non-existing server account exists
98  */
99 START_TEST (test_add_exists_remove_account_regular)
100 {
101         gchar *name = NULL;
102         gchar *store_account = NULL;
103         gchar *transport_account = NULL;
104         gchar *hostname = NULL;
105         gchar *username = NULL;
106         gchar *password = NULL;
107         ModestProtocol proto;
108         gboolean result;
109         
110         name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
111         /* Test 1 */
112         store_account = g_strdup ("imap://me@myserver");
113         transport_account = g_strdup ("local-smtp");
114         result = modest_account_mgr_add_account (account_mgr,
115                                                  name,
116                                                  store_account,
117                                                  transport_account);
118         fail_unless (result,
119                      "modest_account_mgr_add_account failed:\n" \
120                      "name: %s\nstore: %s\ntransport: %s\n",
121                      name, store_account, transport_account);
122         
123         g_free (store_account);
124         g_free (transport_account);
125
126         /* Test 2 */
127         result = modest_account_mgr_account_exists (account_mgr,
128                                                     name,
129                                                     FALSE);
130         fail_unless (result,
131                      "modest_account_mgr_account_exists failed: " \
132                      "Account with name \"%s\" should exist.\n", name);
133         
134
135         /* Test 3 */
136         result = modest_account_mgr_remove_account (account_mgr,
137                                                     name,
138                                                     FALSE);
139         fail_unless (result,
140                      "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
141                      name);
142
143
144         /* Test 4 */
145         hostname = g_strdup ("myhostname.mydomain.com");
146         username = g_strdup ("myusername");
147         password = g_strdup ("mypassword");
148         proto = MODEST_PROTOCOL_TRANSPORT_SMTP;
149         result = modest_account_mgr_add_server_account (account_mgr,
150                                                         name,
151                                                         hostname,
152                                                         username,
153                                                         password,
154                                                         proto);
155         fail_unless (result,
156                      "modest_account_mgr_add_server_account failed:\n" \
157                      "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %s",
158                      name, hostname, username, password, proto);
159
160         g_free (hostname);
161         g_free (username);
162         g_free (password);
163         
164         /* Test 5 */
165         result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
166         fail_unless (result,
167                      "modest_account_mgr_account_exists failed: " \
168                      "Server account with name \"%s\" should exist. Error: %s", name);
169
170         /* Test 6 */
171         result = modest_account_mgr_remove_account (account_mgr,
172                                                     name,
173                                                     TRUE);
174         fail_unless (result,
175                      "modest_account_mgr_remove_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
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                                                  "store_account",
228                                                  "transport_account");
229         fail_unless (!result,
230                      "modest_account_mgr_add_account does not return FALSE when" \
231                      "passing a NULL ModestAccountMgr");
232
233         /* Test 2 */
234         result = modest_account_mgr_add_account (account_mgr,
235                                                  NULL,
236                                                  "store_account",
237                                                  "transport_account");
238         fail_unless (!result,
239                      "modest_account_mgr_add_account does not return FALSE when" \
240                      "passing a NULL account name");
241
242         /* Test 3*/
243         result = modest_account_mgr_add_account (account_mgr,
244                                                  "ïnválid//accountñ//nÄméç",
245                                                  "store_account",
246                                                  "transport_account");
247         fail_unless (!result,
248                      "modest_account_mgr_add_account does not return FALSE when" \
249                      "passing an invalid account name");
250
251         /* Test 4 */
252         result = modest_account_mgr_add_server_account (NULL,
253                                                         TEST_MODEST_ACCOUNT_NAME,
254                                                         "hostname",
255                                                         "username",
256                                                         "password",
257                                                         MODEST_PROTOCOL_STORE_IMAP);
258         fail_unless (!result,
259                      "modest_account_mgr_add_server_account does not return " \
260                      "FALSE when passing a NULL ModestAccountMgr");
261
262         /* Test 5 */
263         result = modest_account_mgr_add_server_account (account_mgr,
264                                                         NULL,
265                                                         "hostname",
266                                                         "username",
267                                                         "password",
268                                                         MODEST_PROTOCOL_STORE_IMAP);
269         fail_unless (!result,
270                      "modest_account_mgr_add_server_account does not return " \
271                      "FALSE when passing a NULL account name");
272
273         /* Test 6 */
274         result = modest_account_mgr_add_server_account (account_mgr, 
275                                                         "ïnválid//accountñ//nÄméç",
276                                                         "hostname", 
277                                                         "username", 
278                                                         "password", 
279                                                         MODEST_PROTOCOL_STORE_IMAP); 
280         fail_unless (!result, 
281                      "modest_account_mgr_add_server_account does not return " \
282                      "FALSE when passing an invalid account name"); 
283
284         /* Test 7 */
285         result = modest_account_mgr_remove_account (account_mgr,
286                                                     "a_name_that_does_not_exist",
287                                                     FALSE);
288         fail_unless (!result,
289                      "modest_account_mgr_remove_acccount does not return FALSE " \
290                      "when trying to remove an account that does not exist");
291
292         /* Test 8 */
293         result = modest_account_mgr_remove_account (account_mgr,
294                                                     "a_name_that_does_not_exist",
295                                                     TRUE);
296         fail_unless (!result,
297                      "modest_account_mgr_remove_acccount does not return FALSE " \
298                      "when trying to remove a server account that does not exist");
299
300         /* Test 9 */
301         result = modest_account_mgr_remove_account (NULL,
302                                                     TEST_MODEST_ACCOUNT_NAME,
303                                                     FALSE);
304         fail_unless (!result,
305                      "modest_account_mgr_remove_acccount does not return " \
306                      "FALSE when passing a NULL ModestAccountMgr");
307
308         /* Test 10 */
309         result = modest_account_mgr_remove_account (account_mgr,
310                                                     NULL,
311                                                     FALSE);
312         fail_unless (!result,
313                      "modest_account_mgr_remove_acccount does not return " \
314                      "FALSE when passing a NULL account name");
315
316         /* Test 11 */
317         result = modest_account_mgr_account_exists (NULL,
318                                                     TEST_MODEST_ACCOUNT_NAME,
319                                                     TRUE);
320         fail_unless (!result,
321                      "modest_account_mgr_exists_account does not return " \
322                      "FALSE when passing a NULL ModestAccountMgr");
323
324         /* Test 12 */
325         result = modest_account_mgr_account_exists (NULL,
326                                                     TEST_MODEST_ACCOUNT_NAME,
327                                                     FALSE);
328         fail_unless (!result,
329                      "modest_account_mgr_exists_account does not return " \
330                      "FALSE when passing a NULL ModestAccountMgr");
331
332         /* Test 13 */
333         result = modest_account_mgr_account_exists (account_mgr,
334                                                     NULL,
335                                                     FALSE);
336         fail_unless (!result,
337                      "modest_account_mgr_exists_acccount does not return " \
338                      "FALSE when passing a NULL account name");
339
340         /* Test 14 */
341         result = modest_account_mgr_account_exists (account_mgr,
342                                                     NULL,
343                                                     TRUE);
344         fail_unless (!result,
345                      "modest_account_mgr_exists_account does not return " \
346                      "FALSE when passing a NULL server account name");
347 }
348 END_TEST
349
350 /* ------------------- Suite creation ------------------- */
351
352 static Suite*
353 account_mgr_suite (void)
354 {
355         Suite *suite = suite_create ("ModestAccountMgr");
356         TCase *tc = NULL;
357
358         /* Tests case for "add/exists/remove account" */
359         tc = tcase_create ("add_exists_remove_account");
360         tcase_add_checked_fixture (tc, 
361                                    fx_setup_default_account_mgr, 
362                                    fx_teardown_default_account_mgr);
363         tcase_add_test (tc, test_add_exists_remove_account_regular);
364         tcase_add_test (tc, test_add_exists_remove_account_invalid);
365         suite_add_tcase (suite, tc);
366
367         return suite;
368 }
369
370 /* --------------------- Main program ------------------- */
371
372 gint
373 main ()
374 {
375         SRunner *srunner;
376         Suite   *suite;
377         int     failures;
378
379         suite   = account_mgr_suite ();
380         srunner = srunner_create (suite);
381         
382         srunner_run_all (srunner, CK_ENV);
383         failures = srunner_ntests_failed (srunner);
384         srunner_free (srunner);
385         
386         return failures;
387 }