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