1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of gsignond
6 * Copyright (C) 2012 Intel Corporation.
8 * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include "gsignond-password-plugin.h"
29 #include "gsignond/gsignond-session-data.h"
30 #include "gsignond/gsignond-signonui-data.h"
31 #include "gsignond/gsignond-plugin-interface.h"
32 #include "gsignond/gsignond-error.h"
33 #include "gsignond/gsignond-config.h"
34 #include "common/gsignond-plugin-loader.h"
37 static void check_plugin(GSignondPlugin* plugin)
42 fail_if(plugin == NULL);
44 g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
46 fail_unless(g_strcmp0(type, "password") == 0);
47 fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
48 fail_unless(mechanisms[1] == NULL);
51 g_strfreev(mechanisms);
54 START_TEST (test_passwordplugin_create)
58 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
60 g_object_unref(plugin);
64 static void response_callback(GSignondPlugin* plugin, GSignondSessionData* result,
67 GSignondSessionData** user_data_p = user_data;
68 *user_data_p = gsignond_dictionary_copy(result);
71 static void user_action_required_callback(GSignondPlugin* plugin,
72 GSignondSignonuiData* ui_request,
75 GSignondSignonuiData** user_data_p = user_data;
76 *user_data_p = gsignond_dictionary_copy(ui_request);
79 static void error_callback(GSignondPlugin* plugin, GError* error,
82 GError** user_data_p = user_data;
83 *user_data_p = g_error_copy(error);
87 START_TEST (test_passwordplugin_request)
91 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
92 fail_if(plugin == NULL);
94 GSignondSessionData* result = NULL;
95 GSignondSignonuiData* ui_action = NULL;
99 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback), &result);
100 g_signal_connect(plugin, "user-action-required",
101 G_CALLBACK(user_action_required_callback), &ui_action);
102 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
104 GSignondSessionData* data = gsignond_dictionary_new();
106 // username empty, password not empty
107 gsignond_session_data_set_secret(data, "megapassword");
108 gsignond_plugin_request_initial(plugin, data, NULL, "password");
109 fail_if(result == NULL);
110 fail_if(ui_action != NULL);
111 fail_if(error != NULL);
112 fail_if(gsignond_session_data_get_username(result) != NULL);
114 gsignond_session_data_get_secret(result), "megapassword") != 0);
115 gsignond_dictionary_unref(result);
118 // username and password not empty
119 gsignond_session_data_set_username(data, "megauser");
120 gsignond_plugin_request_initial(plugin, data, NULL, "password");
121 fail_if(result == NULL);
122 fail_if(ui_action != NULL);
123 fail_if(error != NULL);
125 gsignond_session_data_get_username(result), "megauser") != 0);
127 gsignond_session_data_get_secret(result), "megapassword") != 0);
128 gsignond_dictionary_unref(result);
131 //username and password empty
132 gsignond_dictionary_unref(data);
133 data = gsignond_dictionary_new();
134 gsignond_plugin_request_initial(plugin, data, NULL, "password");
135 fail_if(result != NULL);
136 fail_if(ui_action == NULL);
137 fail_if(error != NULL);
138 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res) == FALSE);
139 fail_if(bool_res == FALSE);
140 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res) == FALSE);
141 fail_if(bool_res == FALSE);
142 gsignond_dictionary_unref(ui_action);
145 //username not empty, password empty
146 gsignond_session_data_set_username(data, "megauser");
147 gsignond_plugin_request_initial(plugin, data, NULL, "password");
148 fail_if(result != NULL);
149 fail_if(ui_action == NULL);
150 fail_if(error != NULL);
151 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res) == FALSE);
152 fail_if(bool_res == TRUE);
153 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res) == FALSE);
154 fail_if(bool_res == FALSE);
155 gsignond_dictionary_unref(ui_action);
158 gsignond_dictionary_unref(data);
159 g_object_unref(plugin);
163 START_TEST (test_passwordplugin_user_action_finished)
167 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
168 fail_if(plugin == NULL);
170 GSignondSessionData* result = NULL;
171 GSignondSignonuiData* ui_action = NULL;
172 GError* error = NULL;
174 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback), &result);
175 g_signal_connect(plugin, "user-action-required",
176 G_CALLBACK(user_action_required_callback), &ui_action);
177 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
179 GSignondSignonuiData* data = gsignond_dictionary_new();
182 gsignond_plugin_user_action_finished(plugin, data);
183 fail_if(result != NULL);
184 fail_if(ui_action != NULL);
185 fail_if(error == NULL);
186 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
187 GSIGNOND_ERROR_USER_INTERACTION));
192 gsignond_signonui_data_set_username(data, "megauser");
193 gsignond_signonui_data_set_password(data, "megapassword");
194 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_NONE);
195 gsignond_plugin_user_action_finished(plugin, data);
196 fail_if(result == NULL);
197 fail_if(ui_action != NULL);
198 fail_if(error != NULL);
200 gsignond_session_data_get_username(result), "megauser") != 0);
202 gsignond_session_data_get_secret(result), "megapassword") != 0);
203 gsignond_dictionary_unref(result);
207 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_CANCELED);
208 gsignond_plugin_user_action_finished(plugin, data);
209 fail_if(result != NULL);
210 fail_if(ui_action != NULL);
211 fail_if(error == NULL);
212 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
213 GSIGNOND_ERROR_SESSION_CANCELED));
217 // error in ui request
218 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_GENERAL);
219 gsignond_plugin_user_action_finished(plugin, data);
220 fail_if(result != NULL);
221 fail_if(ui_action != NULL);
222 fail_if(error == NULL);
223 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
224 GSIGNOND_ERROR_USER_INTERACTION));
228 gsignond_dictionary_unref(data);
229 g_object_unref(plugin);
233 START_TEST (test_passwordplugin_refresh)
237 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
238 fail_if(plugin == NULL);
240 GSignondSessionData* result = NULL;
241 GError* error = NULL;
243 g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback), &result);
244 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
246 GSignondSessionData* data = gsignond_dictionary_new();
247 gsignond_plugin_refresh(plugin, data);
248 fail_if(result == NULL);
249 fail_if(error != NULL);
250 gsignond_dictionary_unref(result);
253 gsignond_dictionary_unref(data);
254 g_object_unref(plugin);
258 Suite* passwordplugin_suite (void)
260 Suite *s = suite_create ("Password plugin");
263 TCase *tc_core = tcase_create ("Tests");
264 tcase_add_test (tc_core, test_passwordplugin_create);
265 tcase_add_test (tc_core, test_passwordplugin_request);
266 tcase_add_test (tc_core, test_passwordplugin_user_action_finished);
267 tcase_add_test (tc_core, test_passwordplugin_refresh);
268 suite_add_tcase (s, tc_core);
276 #if !GLIB_CHECK_VERSION (2, 36, 0)
280 Suite *s = passwordplugin_suite();
281 SRunner *sr = srunner_create(s);
282 srunner_run_all(sr, CK_NORMAL);
283 number_failed = srunner_ntests_failed(sr);
285 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;