* 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-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, NULL))
65                 modest_account_mgr_remove_account (account_mgr,
66                                                    TEST_MODEST_ACCOUNT_NAME,
67                                                    FALSE,
68                                                    NULL);
69         if (modest_account_mgr_account_exists(account_mgr,
70                                               TEST_MODEST_ACCOUNT_NAME,
71                                               TRUE, NULL))
72                 modest_account_mgr_remove_account (account_mgr,
73                                                    TEST_MODEST_ACCOUNT_NAME,
74                                                    TRUE,
75                                                    NULL);
76 }
77
78 static void
79 fx_teardown_default_account_mgr ()
80 {
81         g_object_unref (account_mgr);
82 }
83
84 /* ---------- add/exists/remove account tests  ---------- */
85
86 /**
87  * Test regular usage of 
88  * modest_account_mgr_add_account
89  * modest_account_mgr_add_server_account
90  * modest_account_mgr_account_exists
91  * modest_account_mgr_remove_account
92  *  - Test 1: Create anaccount
93  *  - Test 2: Check account exists
94  *  - Test 3: Remove account
95  *  - Test 4: Create a server account
96  *  - Test 5: Check server account exists
97  *  - Test 6: Remove server account
98  *  - Test 7: Check if a non-existing account exists
99  *  - Test 8: Check if a non-existing server account exists
100  */
101 START_TEST (test_add_exists_remove_account_regular)
102 {
103         gchar *name = NULL;
104         gchar *store_account = NULL;
105         gchar *transport_account = NULL;
106         gchar *hostname = NULL;
107         gchar *username = NULL;
108         gchar *password = NULL;
109         ModestProtocol proto;
110         GError *error = NULL;
111         gboolean result;
112         
113         name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
114         /* Test 1 */
115         store_account = g_strdup ("imap://me@myserver");
116         transport_account = g_strdup ("local-smtp");
117         result = modest_account_mgr_add_account (account_mgr,
118                                                  name,
119                                                  store_account,
120                                                  transport_account,
121                                                  &error);
122         fail_unless (result && !error,
123                      "modest_account_mgr_add_account failed:\n" \
124                      "name: %s\nstore: %s\ntransport: %s\nerror: %s",
125                      name, store_account, transport_account,
126                      error ? error->message : "");
127         
128         g_free (store_account);
129         g_free (transport_account);
130
131         /* Test 2 */
132         result = modest_account_mgr_account_exists (account_mgr,
133                                                     name,
134                                                     FALSE,
135                                                     &error);
136         fail_unless (result && !error,
137                      "modest_account_mgr_account_exists failed: " \
138                      "Account with name \"%s\" should exist. Error: %s",
139                      name, error ? error->message : "");
140         
141
142         /* Test 3 */
143         result = modest_account_mgr_remove_account (account_mgr,
144                                                     name,
145                                                     FALSE,
146                                                     &error);
147         fail_unless (result && !error,
148                      "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
149                      name,  error ? error->message : "");
150
151
152         /* Test 4 */
153         hostname = g_strdup ("myhostname.mydomain.com");
154         username = g_strdup ("myusername");
155         password = g_strdup ("mypassword");
156         proto = MODEST_PROTOCOL_TRANSPORT_SMTP;
157         result = modest_account_mgr_add_server_account (account_mgr,
158                                                         name,
159                                                         hostname,
160                                                         username,
161                                                         password,
162                                                         proto);
163         fail_unless (result,
164                      "modest_account_mgr_add_server_account failed:\n" \
165                      "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %s",
166                      name, hostname, username, password, proto);
167
168         g_free (hostname);
169         g_free (username);
170         g_free (password);
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                                                         MODEST_PROTOCOL_STORE_IMAP);
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                                                         MODEST_PROTOCOL_STORE_IMAP);
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                                                         MODEST_PROTOCOL_STORE_IMAP); 
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 }