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-conf.h>
33 #include <modest-account-mgr.h>
34 #include <modest-protocol-info.h>
36 /* ----------------------- Defines ---------------------- */
38 #define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-àccount"
40 /* ------------------ Global variables ------------------ */
42 static ModestAccountMgr *account_mgr = NULL;
44 /* ---------------------- Fixtures --------------------- */
47 fx_setup_default_account_mgr ()
49 ModestConf *conf = NULL;
53 conf = modest_conf_new ();
54 fail_unless (MODEST_IS_CONF (conf),
55 "modest_conf_new failed");
57 account_mgr = modest_account_mgr_new (conf);
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,
69 if (modest_account_mgr_account_exists(account_mgr,
70 TEST_MODEST_ACCOUNT_NAME,
72 modest_account_mgr_remove_account (account_mgr,
73 TEST_MODEST_ACCOUNT_NAME,
79 fx_teardown_default_account_mgr ()
81 g_object_unref (account_mgr);
84 /* ---------- add/exists/remove account tests ---------- */
87 * Test regular usage of
88 * modest_account_mgr_add_account
89 * modest_account_mgr_add_server_account
90 * modest_account_mgr_account_exists
91 * modest_account_mgr_remove_account
92 * - Test 1: Create anaccount
93 * - Test 2: Check account exists
94 * - Test 3: Remove account
95 * - Test 4: Create a server account
96 * - Test 5: Check server account exists
97 * - Test 6: Remove server account
98 * - Test 7: Check if a non-existing account exists
99 * - Test 8: Check if a non-existing server account exists
101 START_TEST (test_add_exists_remove_account_regular)
104 gchar *store_account = NULL;
105 gchar *transport_account = NULL;
106 gchar *hostname = NULL;
107 gchar *username = NULL;
108 gchar *password = NULL;
109 ModestProtocol proto;
110 GError *error = NULL;
113 name = g_strdup (TEST_MODEST_ACCOUNT_NAME);
115 store_account = g_strdup ("imap://me@myserver");
116 transport_account = g_strdup ("local-smtp");
117 result = modest_account_mgr_add_account (account_mgr,
122 fail_unless (result && !error,
123 "modest_account_mgr_add_account failed:\n" \
124 "name: %s\nstore: %s\ntransport: %s\nerror: %s",
125 name, store_account, transport_account,
126 error ? error->message : "");
128 g_free (store_account);
129 g_free (transport_account);
132 result = modest_account_mgr_account_exists (account_mgr,
136 fail_unless (result && !error,
137 "modest_account_mgr_account_exists failed: " \
138 "Account with name \"%s\" should exist. Error: %s",
139 name, error ? error->message : "");
143 result = modest_account_mgr_remove_account (account_mgr,
147 fail_unless (result && !error,
148 "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
149 name, error ? error->message : "");
153 hostname = g_strdup ("myhostname.mydomain.com");
154 username = g_strdup ("myusername");
155 password = g_strdup ("mypassword");
156 proto = MODEST_PROTOCOL_TRANSPORT_SMTP;
157 result = modest_account_mgr_add_server_account (account_mgr,
164 "modest_account_mgr_add_server_account failed:\n" \
165 "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %s",
166 name, hostname, username, password, proto);
173 result = modest_account_mgr_account_exists (account_mgr,
177 fail_unless (result && !error,
178 "modest_account_mgr_account_exists failed: " \
179 "Server account with name \"%s\" should exist. Error: %s",
180 name, error ? error->message : "");
184 result = modest_account_mgr_remove_account (account_mgr,
188 fail_unless (result && !error,
189 "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s",
190 name, error ? error->message : "");
194 result = modest_account_mgr_account_exists (account_mgr,
195 "a_name_that_does_not_exist",
198 fail_unless (!result,
199 "modest_account_mgr_exists_account does not return " \
200 "FALSE when passing an account that does not exist");
203 result = modest_account_mgr_account_exists (account_mgr,
204 "a_name_that_does_not_exist",
207 fail_unless (!result,
208 "modest_account_mgr_exists_account does not return " \
209 "FALSE when passing a server account that does not exist");
216 * Test regular usage of
217 * modest_account_mgr_add_account,
218 * modest_account_mgr_add_server_account
219 * modest_account_mgr_account_exists
220 * modest_account_mgr_remove_account
221 * - Test 1: Create account with NULL account_mgr
222 * - Test 2: Create account with NULL name
223 * - Test 3: Create account with invalid name string
224 * - Test 4: Create server account with NULL account_mgr
225 * - Test 5: Create server account with NULL name
226 * - Test 6: Create server account with invalid name string
227 * - Test 7: Remove a non-existing account
228 * - Test 8: Remove a non-existing server account
229 * - Test 9: Remove with NULL acount manager
230 * - Test 10: Remove with NULL name
231 * - Test 11: Check if an account exists with NULL account_mgr
232 * - Test 12: Check if a server account exists with a NULL account_mgr
233 * - Test 13: Check if a NULL account exists
234 * - Test 14: Check if a NULL server account exists
236 START_TEST (test_add_exists_remove_account_invalid)
241 result = modest_account_mgr_add_account (NULL,
242 TEST_MODEST_ACCOUNT_NAME,
246 fail_unless (!result,
247 "modest_account_mgr_add_account does not return FALSE when" \
248 "passing a NULL ModestAccountMgr");
251 result = modest_account_mgr_add_account (account_mgr,
256 fail_unless (!result,
257 "modest_account_mgr_add_account does not return FALSE when" \
258 "passing a NULL account name");
261 result = modest_account_mgr_add_account (account_mgr,
262 "ïnválid//accountñ//nÄméç",
266 fail_unless (!result,
267 "modest_account_mgr_add_account does not return FALSE when" \
268 "passing an invalid account name");
271 result = modest_account_mgr_add_server_account (NULL,
272 TEST_MODEST_ACCOUNT_NAME,
276 MODEST_PROTOCOL_STORE_IMAP);
277 fail_unless (!result,
278 "modest_account_mgr_add_server_account does not return " \
279 "FALSE when passing a NULL ModestAccountMgr");
282 result = modest_account_mgr_add_server_account (account_mgr,
287 MODEST_PROTOCOL_STORE_IMAP);
288 fail_unless (!result,
289 "modest_account_mgr_add_server_account does not return " \
290 "FALSE when passing a NULL account name");
293 result = modest_account_mgr_add_server_account (account_mgr,
294 "ïnválid//accountñ//nÄméç",
298 MODEST_PROTOCOL_STORE_IMAP);
299 fail_unless (!result,
300 "modest_account_mgr_add_server_account does not return " \
301 "FALSE when passing an invalid account name");
304 result = modest_account_mgr_remove_account (account_mgr,
305 "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 (account_mgr,
314 "a_name_that_does_not_exist",
317 fail_unless (!result,
318 "modest_account_mgr_remove_acccount does not return FALSE " \
319 "when trying to remove a server account that does not exist");
322 result = modest_account_mgr_remove_account (NULL,
323 TEST_MODEST_ACCOUNT_NAME,
326 fail_unless (!result,
327 "modest_account_mgr_remove_acccount does not return " \
328 "FALSE when passing a NULL ModestAccountMgr");
331 result = modest_account_mgr_remove_account (account_mgr,
335 fail_unless (!result,
336 "modest_account_mgr_remove_acccount does not return " \
337 "FALSE when passing a NULL account name");
340 result = modest_account_mgr_account_exists (NULL,
341 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,
353 fail_unless (!result,
354 "modest_account_mgr_exists_account does not return " \
355 "FALSE when passing a NULL ModestAccountMgr");
358 result = modest_account_mgr_account_exists (account_mgr,
362 fail_unless (!result,
363 "modest_account_mgr_exists_acccount does not return " \
364 "FALSE when passing a NULL account name");
367 result = modest_account_mgr_account_exists (account_mgr,
371 fail_unless (!result,
372 "modest_account_mgr_exists_account does not return " \
373 "FALSE when passing a NULL server account name");
377 /* ------------------- Suite creation ------------------- */
380 account_mgr_suite (void)
382 Suite *suite = suite_create ("ModestAccountMgr");
385 /* Tests case for "add/exists/remove account" */
386 tc = tcase_create ("add_exists_remove_account");
387 tcase_add_checked_fixture (tc,
388 fx_setup_default_account_mgr,
389 fx_teardown_default_account_mgr);
390 tcase_add_test (tc, test_add_exists_remove_account_regular);
391 tcase_add_test (tc, test_add_exists_remove_account_invalid);
392 suite_add_tcase (suite, tc);
397 /* --------------------- Main program ------------------- */
406 suite = account_mgr_suite ();
407 srunner = srunner_create (suite);
409 srunner_run_all (srunner, CK_ENV);
410 failures = srunner_ntests_failed (srunner);
411 srunner_free (srunner);