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