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