1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
32 #include <modest-defs.h>
33 #include <modest-conf.h>
34 #include <modest-account-mgr.h>
36 #include <modest-init.h>
38 /* ----------------------- Defines ---------------------- */
40 #define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-àccount"
42 /* ------------------ Global variables ------------------ */
44 static ModestAccountMgr *account_mgr = NULL;
46 /* ---------------------- Fixtures --------------------- */
49 fx_setup_default_account_mgr ()
51 fail_unless (gtk_init_check (NULL, NULL));
53 fail_unless (g_setenv (MODEST_DIR_ENV, ".modesttest", TRUE));
54 fail_unless (g_setenv (MODEST_NAMESPACE_ENV, "/apps/modesttest", TRUE));
55 fail_unless (modest_init (0, NULL), "Failed running modest_init");
57 account_mgr = modest_runtime_get_account_mgr ();
58 fail_unless (MODEST_IS_ACCOUNT_MGR (account_mgr),
59 "modest_account_mgr_new failed");
61 /* cleanup old garbage (from previous runs)*/
62 if (modest_account_mgr_account_exists(account_mgr,
63 TEST_MODEST_ACCOUNT_NAME,
65 modest_account_mgr_remove_account (account_mgr,
66 TEST_MODEST_ACCOUNT_NAME);
67 if (modest_account_mgr_account_exists(account_mgr,
68 TEST_MODEST_ACCOUNT_NAME,
70 modest_account_mgr_remove_server_account (account_mgr,
71 TEST_MODEST_ACCOUNT_NAME);
75 /* ---------- add/exists/remove account tests ---------- */
78 * Test regular usage of
79 * modest_account_mgr_add_account
80 * modest_account_mgr_add_server_account
81 * modest_account_mgr_account_exists
82 * modest_account_mgr_remove_account
83 * - Test 1: Create anaccount
84 * - Test 2: Check account exists
85 * - Test 3: Remove account
86 * - Test 4: Create a server account
87 * - Test 5: Check server account exists
88 * - Test 6: Remove server account
89 * - Test 7: Check if a non-existing account exists
90 * - Test 8: Check if a non-existing server account exists
92 START_TEST (test_add_exists_remove_account_regular)
95 gchar *store_account = NULL;
96 gchar *transport_account = NULL;
97 gchar *hostname = NULL;
98 gchar *username = NULL;
99 gchar *password = NULL;
101 ModestProtocolType proto;
104 name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
106 store_account = g_strdup ("imap://me@myserver");
107 transport_account = g_strdup ("local-smtp");
108 result = modest_account_mgr_add_account (account_mgr,
113 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
115 transport_account, TRUE);
117 "modest_account_mgr_add_account failed:\n" \
118 "name: %s\nstore: %s\ntransport: %s\n",
119 name, store_account, transport_account, TRUE);
121 g_free (store_account);
122 g_free (transport_account);
125 result = modest_account_mgr_account_exists (account_mgr,
129 "modest_account_mgr_account_exists failed: " \
130 "Account with name \"%s\" should exist.\n", name);
134 result = modest_account_mgr_remove_account (account_mgr,
137 "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
142 hostname = g_strdup ("myhostname.mydomain.com");
143 username = g_strdup ("myusername");
144 password = g_strdup ("mypassword");
145 proto = MODEST_PROTOCOLS_TRANSPORT_SMTP;
147 result = modest_account_mgr_add_server_account (account_mgr,
154 MODEST_PROTOCOLS_CONNECTION_NONE,
155 MODEST_PROTOCOLS_AUTH_NONE);
157 "modest_account_mgr_add_server_account failed:\n" \
158 "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %d",
159 name, hostname, username, password, proto);
166 result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
168 "modest_account_mgr_account_exists failed: " \
169 "Server account with name \"%s\" should exist. Error: %s", name);
172 result = modest_account_mgr_remove_server_account (account_mgr,
175 "modest_account_mgr_remove_server_account failed:\nname: %s\nerror: %s",
180 result = modest_account_mgr_account_exists (account_mgr,
181 "a_name_that_does_not_exist",
183 fail_unless (!result,
184 "modest_account_mgr_exists_account does not return " \
185 "FALSE when passing an account that does not exist");
188 result = modest_account_mgr_account_exists (account_mgr,
189 "a_name_that_does_not_exist",
191 fail_unless (!result,
192 "modest_account_mgr_exists_account does not return " \
193 "FALSE when passing a server account that does not exist");
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 (REMOVED)
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
220 START_TEST (test_add_exists_remove_account_invalid)
225 result = modest_account_mgr_add_account (NULL,
226 TEST_MODEST_ACCOUNT_NAME,
230 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
232 "transport_account", TRUE);
233 fail_unless (!result,
234 "modest_account_mgr_add_account does not return FALSE when" \
235 "passing a NULL ModestAccountMgr");
238 result = modest_account_mgr_add_account (account_mgr,
243 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
245 "transport_account", TRUE);
246 fail_unless (!result,
247 "modest_account_mgr_add_account does not return FALSE when" \
248 "passing a NULL account name");
251 result = modest_account_mgr_add_account (account_mgr,
252 "ïnválid//accountñ//nÄméç",
256 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
258 "transport_account", TRUE);
259 fail_unless (!result,
260 "modest_account_mgr_add_account does not return FALSE when" \
261 "passing an invalid account name");
264 result = modest_account_mgr_add_server_account (NULL,
265 TEST_MODEST_ACCOUNT_NAME,
270 MODEST_PROTOCOLS_STORE_IMAP,
271 MODEST_PROTOCOLS_CONNECTION_NONE,
272 MODEST_PROTOCOLS_AUTH_NONE);
273 fail_unless (!result,
274 "modest_account_mgr_add_server_account does not return " \
275 "FALSE when passing a NULL ModestAccountMgr");
278 result = modest_account_mgr_add_server_account (account_mgr,
284 MODEST_PROTOCOLS_STORE_IMAP,
285 MODEST_PROTOCOLS_CONNECTION_NONE,
286 MODEST_PROTOCOLS_AUTH_NONE);
287 fail_unless (!result,
288 "modest_account_mgr_add_server_account does not return " \
289 "FALSE when passing a NULL account name");
292 result = modest_account_mgr_add_server_account (account_mgr,
293 "ïnválid//accountñ//nÄméç",
298 MODEST_PROTOCOLS_STORE_IMAP,
299 MODEST_PROTOCOLS_CONNECTION_NONE,
300 MODEST_PROTOCOLS_AUTH_NONE);
301 fail_unless (!result,
302 "modest_account_mgr_add_server_account does not return " \
303 "FALSE when passing an invalid account name");
306 result = modest_account_mgr_remove_account (account_mgr,
307 "a_name_that_does_not_exist");
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");
313 result = modest_account_mgr_remove_account (NULL,
314 TEST_MODEST_ACCOUNT_NAME);
315 fail_unless (!result,
316 "modest_account_mgr_remove_acccount does not return " \
317 "FALSE when passing a NULL ModestAccountMgr");
320 result = modest_account_mgr_remove_account (account_mgr,
322 fail_unless (!result,
323 "modest_account_mgr_remove_acccount does not return " \
324 "FALSE when passing a NULL account name");
327 result = modest_account_mgr_account_exists (NULL,
328 TEST_MODEST_ACCOUNT_NAME,
330 fail_unless (!result,
331 "modest_account_mgr_exists_account does not return " \
332 "FALSE when passing a NULL ModestAccountMgr");
335 result = modest_account_mgr_account_exists (NULL,
336 TEST_MODEST_ACCOUNT_NAME,
338 fail_unless (!result,
339 "modest_account_mgr_exists_account does not return " \
340 "FALSE when passing a NULL ModestAccountMgr");
343 result = modest_account_mgr_account_exists (account_mgr,
346 fail_unless (!result,
347 "modest_account_mgr_exists_acccount does not return " \
348 "FALSE when passing a NULL account name");
351 result = modest_account_mgr_account_exists (account_mgr,
354 fail_unless (!result,
355 "modest_account_mgr_exists_account does not return " \
356 "FALSE when passing a NULL server account name");
360 /* ------------------- Suite creation ------------------- */
363 account_mgr_suite (void)
365 Suite *suite = suite_create ("ModestAccountMgr");
368 /* Tests case for "add/exists/remove account" */
369 tc = tcase_create ("add_exists_remove_account");
370 tcase_add_unchecked_fixture (tc,
371 fx_setup_default_account_mgr, NULL);
372 tcase_add_test (tc, test_add_exists_remove_account_regular);
373 tcase_add_test (tc, test_add_exists_remove_account_invalid);
374 suite_add_tcase (suite, tc);
379 /* --------------------- Main program ------------------- */
388 suite = account_mgr_suite ();
389 srunner = srunner_create (suite);
391 srunner_run_all (srunner, CK_ENV);
392 failures = srunner_ntests_failed (srunner);
393 srunner_free (srunner);