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