* Fixes NB#85880, show the proper server name if a SMTP send fails
[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         ModestTransportStoreProtocol 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, TRUE);
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, TRUE);
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                                                         MODEST_PROTOCOL_SECURITY_NONE,
156                                                         MODEST_PROTOCOL_AUTH_NONE); 
157         fail_unless (result,
158                      "modest_account_mgr_add_server_account failed:\n" \
159                      "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %s",
160                      name, hostname, username, password, proto);
161
162         g_free (hostname);
163         g_free (username);
164         g_free (password);
165         
166         /* Test 5 */
167         result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
168         fail_unless (result,
169                      "modest_account_mgr_account_exists failed: " \
170                      "Server account with name \"%s\" should exist. Error: %s", name);
171
172         /* Test 6 */
173         result = modest_account_mgr_remove_account (account_mgr,
174                                                     name,
175                                                     TRUE);
176         fail_unless (result,
177                      "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
178                      name);
179
180
181         /* Test 7 */
182         result = modest_account_mgr_account_exists (account_mgr,
183                                                     "a_name_that_does_not_exist",
184                                                     FALSE);
185         fail_unless (result,
186                      "modest_account_mgr_exists_account does not return " \
187                      "FALSE when passing an account that does not exist");
188
189         /* Test 8 */
190         result = modest_account_mgr_account_exists (account_mgr,
191                                                     "a_name_that_does_not_exist",
192                                                     TRUE);
193         fail_unless (result,
194                      "modest_account_mgr_exists_account does not return " \
195                      "FALSE when passing a server account that does not exist");
196         
197         g_free (name);
198 }
199 END_TEST
200
201 /**
202  * Test regular usage of 
203  * modest_account_mgr_add_account,
204  * modest_account_mgr_add_server_account
205  * modest_account_mgr_account_exists
206  * modest_account_mgr_remove_account
207  *  - Test 1: Create account with NULL account_mgr
208  *  - Test 2: Create account with NULL name
209  *  - Test 3: Create account with invalid name string
210  *  - Test 4: Create server account with NULL account_mgr
211  *  - Test 5: Create server account with NULL name
212  *  - Test 6: Create server account with invalid name string
213  *  - Test 7: Remove a non-existing account
214  *  - Test 8: Remove a non-existing server account
215  *  - Test 9: Remove with NULL acount manager
216  *  - Test 10: Remove with NULL name
217  *  - Test 11: Check if an  account exists with NULL account_mgr
218  *  - Test 12: Check if a server account exists with a NULL account_mgr
219  *  - Test 13: Check if a NULL account exists
220  *  - Test 14: Check if a NULL server account exists
221  */
222 START_TEST (test_add_exists_remove_account_invalid)
223 {
224         gboolean result;
225
226         /* Test 1 */
227         result = modest_account_mgr_add_account (NULL,
228                                                  TEST_MODEST_ACCOUNT_NAME,
229                                                  "store_account",
230                                                  "transport_account", TRUE);
231         fail_unless (!result,
232                      "modest_account_mgr_add_account does not return FALSE when" \
233                      "passing a NULL ModestAccountMgr");
234
235         /* Test 2 */
236         result = modest_account_mgr_add_account (account_mgr,
237                                                  NULL,
238                                                  "store_account",
239                                                  "transport_account", TRUE);
240         fail_unless (!result,
241                      "modest_account_mgr_add_account does not return FALSE when" \
242                      "passing a NULL account name");
243
244         /* Test 3*/
245         result = modest_account_mgr_add_account (account_mgr,
246                                                  "ïnválid//accountñ//nÄméç",
247                                                  "store_account",
248                                                  "transport_account", TRUE);
249         fail_unless (!result,
250                      "modest_account_mgr_add_account does not return FALSE when" \
251                      "passing an invalid account name");
252
253         /* Test 4 */
254         result = modest_account_mgr_add_server_account (NULL,
255                                                         TEST_MODEST_ACCOUNT_NAME,
256                                                         "hostname",
257                                                         "username",
258                                                         "password",
259                                                         MODEST_PROTOCOL_STORE_IMAP,
260                                                        MODEST_PROTOCOL_SECURITY_NONE,
261                                                        MODEST_PROTOCOL_AUTH_NONE);
262         fail_unless (!result,
263                      "modest_account_mgr_add_server_account does not return " \
264                      "FALSE when passing a NULL ModestAccountMgr");
265
266         /* Test 5 */
267         result = modest_account_mgr_add_server_account (account_mgr,
268                                                         NULL,
269                                                         "hostname",
270                                                         "username",
271                                                         "password",
272                                                         MODEST_PROTOCOL_STORE_IMAP,
273                                                         MODEST_PROTOCOL_SECURITY_NONE,
274                                                         MODEST_PROTOCOL_AUTH_NONE); 
275         fail_unless (!result,
276                      "modest_account_mgr_add_server_account does not return " \
277                      "FALSE when passing a NULL account name");
278
279         /* Test 6 */
280         result = modest_account_mgr_add_server_account (account_mgr, 
281                                                         "ïnválid//accountñ//nÄméç",
282                                                         "hostname", 
283                                                         "username", 
284                                                         "password", 
285                                                         MODEST_PROTOCOL_STORE_IMAP,
286                                                         MODEST_PROTOCOL_SECURITY_NONE,
287                                                         MODEST_PROTOCOL_AUTH_NONE); 
288         fail_unless (!result, 
289                      "modest_account_mgr_add_server_account does not return " \
290                      "FALSE when passing an invalid account name"); 
291
292         /* Test 7 */
293         result = modest_account_mgr_remove_account (account_mgr,
294                                                     "a_name_that_does_not_exist",
295                                                     FALSE);
296         fail_unless (!result,
297                      "modest_account_mgr_remove_acccount does not return FALSE " \
298                      "when trying to remove an account that does not exist");
299
300         /* Test 8 */
301         result = modest_account_mgr_remove_account (account_mgr,
302                                                     "a_name_that_does_not_exist",
303                                                     TRUE);
304         fail_unless (!result,
305                      "modest_account_mgr_remove_acccount does not return FALSE " \
306                      "when trying to remove a server account that does not exist");
307
308         /* Test 9 */
309         result = modest_account_mgr_remove_account (NULL,
310                                                     TEST_MODEST_ACCOUNT_NAME,
311                                                     FALSE);
312         fail_unless (!result,
313                      "modest_account_mgr_remove_acccount does not return " \
314                      "FALSE when passing a NULL ModestAccountMgr");
315
316         /* Test 10 */
317         result = modest_account_mgr_remove_account (account_mgr,
318                                                     NULL,
319                                                     FALSE);
320         fail_unless (!result,
321                      "modest_account_mgr_remove_acccount does not return " \
322                      "FALSE when passing a NULL account name");
323
324         /* Test 11 */
325         result = modest_account_mgr_account_exists (NULL,
326                                                     TEST_MODEST_ACCOUNT_NAME,
327                                                     TRUE);
328         fail_unless (!result,
329                      "modest_account_mgr_exists_account does not return " \
330                      "FALSE when passing a NULL ModestAccountMgr");
331
332         /* Test 12 */
333         result = modest_account_mgr_account_exists (NULL,
334                                                     TEST_MODEST_ACCOUNT_NAME,
335                                                     FALSE);
336         fail_unless (!result,
337                      "modest_account_mgr_exists_account does not return " \
338                      "FALSE when passing a NULL ModestAccountMgr");
339
340         /* Test 13 */
341         result = modest_account_mgr_account_exists (account_mgr,
342                                                     NULL,
343                                                     FALSE);
344         fail_unless (!result,
345                      "modest_account_mgr_exists_acccount does not return " \
346                      "FALSE when passing a NULL account name");
347
348         /* Test 14 */
349         result = modest_account_mgr_account_exists (account_mgr,
350                                                     NULL,
351                                                     TRUE);
352         fail_unless (!result,
353                      "modest_account_mgr_exists_account does not return " \
354                      "FALSE when passing a NULL server account name");
355 }
356 END_TEST
357
358 /* ------------------- Suite creation ------------------- */
359
360 static Suite*
361 account_mgr_suite (void)
362 {
363         Suite *suite = suite_create ("ModestAccountMgr");
364         TCase *tc = NULL;
365
366         /* Tests case for "add/exists/remove account" */
367         tc = tcase_create ("add_exists_remove_account");
368         tcase_add_checked_fixture (tc, 
369                                    fx_setup_default_account_mgr, 
370                                    fx_teardown_default_account_mgr);
371         tcase_add_test (tc, test_add_exists_remove_account_regular);
372         tcase_add_test (tc, test_add_exists_remove_account_invalid);
373         suite_add_tcase (suite, tc);
374
375         return suite;
376 }
377
378 /* --------------------- Main program ------------------- */
379
380 gint
381 main ()
382 {
383         SRunner *srunner;
384         Suite   *suite;
385         int     failures;
386
387         suite   = account_mgr_suite ();
388         srunner = srunner_create (suite);
389         
390         srunner_run_all (srunner, CK_ENV);
391         failures = srunner_ntests_failed (srunner);
392         srunner_free (srunner);
393         
394         return failures;
395 }