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>
35 #include <modest-utils.h>
37 #include <modest-init.h>
39 /* ----------------------- Defines ---------------------- */
41 #define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-àccount"
43 /* ------------------ Global variables ------------------ */
45 static ModestAccountMgr *account_mgr = NULL;
47 /* ---------------------- Fixtures --------------------- */
50 fx_setup_default_account_mgr ()
52 fail_unless (gtk_init_check (NULL, NULL));
54 fail_unless (g_setenv (MODEST_DIR_ENV, ".modesttest", TRUE));
55 fail_unless (g_setenv (MODEST_NAMESPACE_ENV, "/apps/modesttest", TRUE));
56 fail_unless (modest_init (0, NULL), "Failed running modest_init");
58 account_mgr = modest_runtime_get_account_mgr ();
59 fail_unless (MODEST_IS_ACCOUNT_MGR (account_mgr),
60 "modest_account_mgr_new failed");
62 /* cleanup old garbage (from previous runs)*/
63 if (modest_account_mgr_account_exists(account_mgr,
64 TEST_MODEST_ACCOUNT_NAME,
66 modest_account_mgr_remove_account (account_mgr,
67 TEST_MODEST_ACCOUNT_NAME);
68 if (modest_account_mgr_account_exists(account_mgr,
69 TEST_MODEST_ACCOUNT_NAME,
71 modest_account_mgr_remove_server_account (account_mgr,
72 TEST_MODEST_ACCOUNT_NAME);
76 /* ---------- add/exists/remove account tests ---------- */
79 * Test regular usage of
80 * modest_account_mgr_add_account
81 * modest_account_mgr_add_server_account
82 * modest_account_mgr_account_exists
83 * modest_account_mgr_remove_account
84 * - Test 1: Create anaccount
85 * - Test 2: Check account exists
86 * - Test 3: Remove account
87 * - Test 4: Create a server account
88 * - Test 5: Check server account exists
89 * - Test 6: Remove server account
90 * - Test 7: Check if a non-existing account exists
91 * - Test 8: Check if a non-existing server account exists
93 START_TEST (test_add_exists_remove_account_regular)
96 gchar *store_account = NULL;
97 gchar *transport_account = NULL;
98 gchar *hostname = NULL;
99 gchar *username = NULL;
100 gchar *password = NULL;
102 ModestProtocolType proto;
105 name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
107 store_account = g_strdup ("imap://me@myserver");
108 transport_account = g_strdup ("local-smtp");
109 result = modest_account_mgr_add_account (account_mgr,
114 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
116 transport_account, TRUE);
118 "modest_account_mgr_add_account failed:\n" \
119 "name: %s\nstore: %s\ntransport: %s\n",
120 name, store_account, transport_account, TRUE);
122 g_free (store_account);
123 g_free (transport_account);
126 result = modest_account_mgr_account_exists (account_mgr,
130 "modest_account_mgr_account_exists failed: " \
131 "Account with name \"%s\" should exist.\n", name);
134 gchar *account_name_from_recipient;
135 account_name_from_recipient = modest_utils_get_account_name_from_recipient ("user@email.com");
136 fail_unless (account_name_from_recipient != NULL,
137 "modest_utils_get_account_name_from_recipient failed: "\
138 "From user@email.com should match account");
141 account_name_from_recipient = modest_utils_get_account_name_from_recipient ("egg@egg.com");
142 fail_unless (account_name_from_recipient == NULL,
143 "modest_utils_get_account_name_from_recipient failed: "\
144 "From egg@egg.com shouldn't match account");
148 result = modest_account_mgr_remove_account (account_mgr,
151 "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
156 hostname = g_strdup ("myhostname.mydomain.com");
157 username = g_strdup ("myusername");
158 password = g_strdup ("mypassword");
159 proto = MODEST_PROTOCOLS_TRANSPORT_SMTP;
161 result = modest_account_mgr_add_server_account (account_mgr,
168 MODEST_PROTOCOLS_CONNECTION_NONE,
169 MODEST_PROTOCOLS_AUTH_NONE);
171 "modest_account_mgr_add_server_account failed:\n" \
172 "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %d",
173 name, hostname, username, password, proto);
180 result = modest_account_mgr_account_exists (account_mgr,name,TRUE);
182 "modest_account_mgr_account_exists failed: " \
183 "Server account with name \"%s\" should exist. Error: %s", name);
186 result = modest_account_mgr_remove_server_account (account_mgr,
189 "modest_account_mgr_remove_server_account failed:\nname: %s\nerror: %s",
194 result = modest_account_mgr_account_exists (account_mgr,
195 "a_name_that_does_not_exist",
197 fail_unless (!result,
198 "modest_account_mgr_exists_account does not return " \
199 "FALSE when passing an account that does not exist");
202 result = modest_account_mgr_account_exists (account_mgr,
203 "a_name_that_does_not_exist",
205 fail_unless (!result,
206 "modest_account_mgr_exists_account does not return " \
207 "FALSE when passing a server account that does not exist");
214 * Test regular usage of
215 * modest_account_mgr_add_account,
216 * modest_account_mgr_add_server_account
217 * modest_account_mgr_account_exists
218 * modest_account_mgr_remove_account
219 * - Test 1: Create account with NULL account_mgr
220 * - Test 2: Create account with NULL name
221 * - Test 3: Create account with invalid name string
222 * - Test 4: Create server account with NULL account_mgr
223 * - Test 5: Create server account with NULL name
224 * - Test 6: Create server account with invalid name string
225 * - Test 7: Remove a non-existing account
226 * - Test 8: Remove a non-existing server account (REMOVED)
227 * - Test 9: Remove with NULL acount manager
228 * - Test 10: Remove with NULL name
229 * - Test 11: Check if an account exists with NULL account_mgr
230 * - Test 12: Check if a server account exists with a NULL account_mgr
231 * - Test 13: Check if a NULL account exists
232 * - Test 14: Check if a NULL server account exists
234 START_TEST (test_add_exists_remove_account_invalid)
239 result = modest_account_mgr_add_account (NULL,
240 TEST_MODEST_ACCOUNT_NAME,
244 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
246 "transport_account", TRUE);
247 fail_unless (!result,
248 "modest_account_mgr_add_account does not return FALSE when" \
249 "passing a NULL ModestAccountMgr");
252 result = modest_account_mgr_add_account (account_mgr,
257 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
259 "transport_account", TRUE);
260 fail_unless (!result,
261 "modest_account_mgr_add_account does not return FALSE when" \
262 "passing a NULL account name");
265 result = modest_account_mgr_add_account (account_mgr,
266 "ïnválid//accountñ//nÄméç",
270 MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
272 "transport_account", TRUE);
273 fail_unless (!result,
274 "modest_account_mgr_add_account does not return FALSE when" \
275 "passing an invalid account name");
278 result = modest_account_mgr_add_server_account (NULL,
279 TEST_MODEST_ACCOUNT_NAME,
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 ModestAccountMgr");
292 result = modest_account_mgr_add_server_account (account_mgr,
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 a NULL account name");
306 result = modest_account_mgr_add_server_account (account_mgr,
307 "ïnválid//accountñ//nÄméç",
312 MODEST_PROTOCOLS_STORE_IMAP,
313 MODEST_PROTOCOLS_CONNECTION_NONE,
314 MODEST_PROTOCOLS_AUTH_NONE);
315 fail_unless (!result,
316 "modest_account_mgr_add_server_account does not return " \
317 "FALSE when passing an invalid account name");
320 result = modest_account_mgr_remove_account (account_mgr,
321 "a_name_that_does_not_exist");
322 fail_unless (!result,
323 "modest_account_mgr_remove_acccount does not return FALSE " \
324 "when trying to remove an account that does not exist");
327 result = modest_account_mgr_remove_account (NULL,
328 TEST_MODEST_ACCOUNT_NAME);
329 fail_unless (!result,
330 "modest_account_mgr_remove_acccount does not return " \
331 "FALSE when passing a NULL ModestAccountMgr");
334 result = modest_account_mgr_remove_account (account_mgr,
336 fail_unless (!result,
337 "modest_account_mgr_remove_acccount does not return " \
338 "FALSE when passing a NULL account name");
341 result = modest_account_mgr_account_exists (NULL,
342 TEST_MODEST_ACCOUNT_NAME,
344 fail_unless (!result,
345 "modest_account_mgr_exists_account does not return " \
346 "FALSE when passing a NULL ModestAccountMgr");
349 result = modest_account_mgr_account_exists (NULL,
350 TEST_MODEST_ACCOUNT_NAME,
352 fail_unless (!result,
353 "modest_account_mgr_exists_account does not return " \
354 "FALSE when passing a NULL ModestAccountMgr");
357 result = modest_account_mgr_account_exists (account_mgr,
360 fail_unless (!result,
361 "modest_account_mgr_exists_acccount does not return " \
362 "FALSE when passing a NULL account name");
365 result = modest_account_mgr_account_exists (account_mgr,
368 fail_unless (!result,
369 "modest_account_mgr_exists_account does not return " \
370 "FALSE when passing a NULL server account name");
374 /* ------------------- Suite creation ------------------- */
377 account_mgr_suite (void)
379 Suite *suite = suite_create ("ModestAccountMgr");
382 /* Tests case for "add/exists/remove account" */
383 tc = tcase_create ("add_exists_remove_account");
384 tcase_add_checked_fixture (tc,
385 fx_setup_default_account_mgr, NULL);
386 tcase_add_test (tc, test_add_exists_remove_account_regular);
387 tcase_add_test (tc, test_add_exists_remove_account_invalid);
388 suite_add_tcase (suite, tc);
393 /* --------------------- Main program ------------------- */
402 suite = account_mgr_suite ();
403 srunner = srunner_create (suite);
405 srunner_run_all (srunner, CK_ENV);
406 failures = srunner_ntests_failed (srunner);
407 srunner_free (srunner);