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"
36 static void check_plugin(GSignondPlugin* plugin)
41 fail_if(plugin == NULL);
43 g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
45 fail_unless(g_strcmp0(type, "password") == 0);
46 fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
47 fail_unless(mechanisms[1] == NULL);
50 g_strfreev(mechanisms);
53 START_TEST (test_passwordplugin_create)
57 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
59 g_object_unref(plugin);
63 static void response_callback(GSignondPlugin* plugin, GSignondSessionData* result,
66 GSignondSessionData** user_data_p = user_data;
67 *user_data_p = gsignond_dictionary_copy(result);
70 static void user_action_required_callback(GSignondPlugin* plugin,
71 GSignondSignonuiData* ui_request,
74 GSignondSignonuiData** user_data_p = user_data;
75 *user_data_p = gsignond_dictionary_copy(ui_request);
78 static void error_callback(GSignondPlugin* plugin, GError* error,
81 GError** user_data_p = user_data;
82 *user_data_p = g_error_copy(error);
86 START_TEST (test_passwordplugin_request)
90 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
91 fail_if(plugin == NULL);
93 GSignondSessionData* result = NULL;
94 GSignondSignonuiData* ui_action = NULL;
98 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback), &result);
99 g_signal_connect(plugin, "user-action-required",
100 G_CALLBACK(user_action_required_callback), &ui_action);
101 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
103 GSignondSessionData* data = gsignond_dictionary_new();
105 // username empty, password not empty
106 gsignond_session_data_set_secret(data, "megapassword");
107 gsignond_plugin_request_initial(plugin, data, NULL, "password");
108 fail_if(result == NULL);
109 fail_if(ui_action != NULL);
110 fail_if(error != NULL);
111 fail_if(gsignond_session_data_get_username(result) != NULL);
113 gsignond_session_data_get_secret(result), "megapassword") != 0);
114 gsignond_dictionary_unref(result);
117 // username and password not empty
118 gsignond_session_data_set_username(data, "megauser");
119 gsignond_plugin_request_initial(plugin, data, NULL, "password");
120 fail_if(result == NULL);
121 fail_if(ui_action != NULL);
122 fail_if(error != NULL);
124 gsignond_session_data_get_username(result), "megauser") != 0);
126 gsignond_session_data_get_secret(result), "megapassword") != 0);
127 gsignond_dictionary_unref(result);
130 //username and password empty
131 gsignond_dictionary_unref(data);
132 data = gsignond_dictionary_new();
133 gsignond_plugin_request_initial(plugin, data, NULL, "password");
134 fail_if(result != NULL);
135 fail_if(ui_action == NULL);
136 fail_if(error != NULL);
137 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res) == FALSE);
138 fail_if(bool_res == FALSE);
139 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res) == FALSE);
140 fail_if(bool_res == FALSE);
141 gsignond_dictionary_unref(ui_action);
144 //username not empty, password empty
145 gsignond_session_data_set_username(data, "megauser");
146 gsignond_plugin_request_initial(plugin, data, NULL, "password");
147 fail_if(result != NULL);
148 fail_if(ui_action == NULL);
149 fail_if(error != NULL);
150 fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res) == FALSE);
151 fail_if(bool_res == TRUE);
152 fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res) == FALSE);
153 fail_if(bool_res == FALSE);
154 gsignond_dictionary_unref(ui_action);
157 gsignond_dictionary_unref(data);
158 g_object_unref(plugin);
162 START_TEST (test_passwordplugin_user_action_finished)
166 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
167 fail_if(plugin == NULL);
169 GSignondSessionData* result = NULL;
170 GSignondSignonuiData* ui_action = NULL;
171 GError* error = NULL;
173 g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback), &result);
174 g_signal_connect(plugin, "user-action-required",
175 G_CALLBACK(user_action_required_callback), &ui_action);
176 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
178 GSignondSignonuiData* data = gsignond_dictionary_new();
181 gsignond_plugin_user_action_finished(plugin, data);
182 fail_if(result != NULL);
183 fail_if(ui_action != NULL);
184 fail_if(error == NULL);
185 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
186 GSIGNOND_ERROR_USER_INTERACTION));
191 gsignond_signonui_data_set_username(data, "megauser");
192 gsignond_signonui_data_set_password(data, "megapassword");
193 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_NONE);
194 gsignond_plugin_user_action_finished(plugin, data);
195 fail_if(result == NULL);
196 fail_if(ui_action != NULL);
197 fail_if(error != NULL);
199 gsignond_session_data_get_username(result), "megauser") != 0);
201 gsignond_session_data_get_secret(result), "megapassword") != 0);
202 gsignond_dictionary_unref(result);
206 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_CANCELED);
207 gsignond_plugin_user_action_finished(plugin, data);
208 fail_if(result != NULL);
209 fail_if(ui_action != NULL);
210 fail_if(error == NULL);
211 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
212 GSIGNOND_ERROR_SESSION_CANCELED));
216 // error in ui request
217 gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_GENERAL);
218 gsignond_plugin_user_action_finished(plugin, data);
219 fail_if(result != NULL);
220 fail_if(ui_action != NULL);
221 fail_if(error == NULL);
222 fail_unless(g_error_matches(error, GSIGNOND_ERROR,
223 GSIGNOND_ERROR_USER_INTERACTION));
227 gsignond_dictionary_unref(data);
228 g_object_unref(plugin);
232 START_TEST (test_passwordplugin_refresh)
236 plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
237 fail_if(plugin == NULL);
239 GSignondSessionData* result = NULL;
240 GError* error = NULL;
242 g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback), &result);
243 g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
245 GSignondSessionData* data = gsignond_dictionary_new();
246 gsignond_plugin_refresh(plugin, data);
247 fail_if(result == NULL);
248 fail_if(error != NULL);
249 gsignond_dictionary_unref(result);
252 gsignond_dictionary_unref(data);
253 g_object_unref(plugin);
257 Suite* passwordplugin_suite (void)
259 Suite *s = suite_create ("Password plugin");
262 TCase *tc_core = tcase_create ("Tests");
263 tcase_add_test (tc_core, test_passwordplugin_create);
264 tcase_add_test (tc_core, test_passwordplugin_request);
265 tcase_add_test (tc_core, test_passwordplugin_user_action_finished);
266 tcase_add_test (tc_core, test_passwordplugin_refresh);
267 suite_add_tcase (s, tc_core);
275 #if !GLIB_CHECK_VERSION (2, 36, 0)
279 Suite *s = passwordplugin_suite();
280 SRunner *sr = srunner_create(s);
281 srunner_run_all(sr, CK_NORMAL);
282 number_failed = srunner_ntests_failed(sr);
284 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;