5c7670aa0fe1d151cb0a43c921e80c78b66e40ca
[profile/ivi/gsignond.git] / test / plugins / passwordplugintest.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
9  *
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.
14  *
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.
19  *
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
23  * 02110-1301 USA
24  */
25
26 #include <check.h>
27 #include <stdlib.h>
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"
35
36
37 static void check_plugin(GSignondPlugin* plugin)
38 {
39     gchar* type;
40     gchar** mechanisms;
41
42     fail_if(plugin == NULL);
43     
44     g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
45     
46     fail_unless(g_strcmp0(type, "password") == 0);
47     fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
48     fail_unless(mechanisms[1] == NULL);
49     
50     g_free(type);
51     g_strfreev(mechanisms);
52 }
53
54 START_TEST (test_passwordplugin_create)
55 {
56     gpointer plugin;
57     
58     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
59     check_plugin(plugin);
60     g_object_unref(plugin);
61 }
62 END_TEST
63
64 static void response_callback(GSignondPlugin* plugin, GSignondSessionData* result,
65                      gpointer user_data)
66 {
67     GSignondSessionData** user_data_p = user_data;
68     *user_data_p = gsignond_dictionary_copy(result);
69 }
70
71 static void user_action_required_callback(GSignondPlugin* plugin, 
72                                           GSignondSignonuiData* ui_request, 
73                                           gpointer user_data)
74 {
75     GSignondSignonuiData** user_data_p = user_data;
76     *user_data_p = gsignond_dictionary_copy(ui_request);
77 }
78
79 static void error_callback(GSignondPlugin* plugin, GError* error,
80                      gpointer user_data)
81 {
82     GError** user_data_p = user_data;
83     *user_data_p = g_error_copy(error);
84 }
85
86
87 START_TEST (test_passwordplugin_request)
88 {
89     gpointer plugin;
90     
91     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
92     fail_if(plugin == NULL);
93
94     GSignondSessionData* result = NULL;
95     GSignondSignonuiData* ui_action = NULL;
96     GError* error = NULL;
97     gboolean bool_res;
98
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);
103
104     GSignondSessionData* data = gsignond_dictionary_new();
105
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);
113     fail_if(g_strcmp0(
114         gsignond_session_data_get_secret(result), "megapassword") != 0);
115     gsignond_dictionary_unref(result);
116     result = NULL;
117     
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);
124     fail_if(g_strcmp0(
125         gsignond_session_data_get_username(result), "megauser") != 0);
126     fail_if(g_strcmp0(
127         gsignond_session_data_get_secret(result), "megapassword") != 0);
128     gsignond_dictionary_unref(result);
129     result = NULL;
130     
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);
143     ui_action = NULL;
144     
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);
156     ui_action = NULL;
157     
158     gsignond_dictionary_unref(data);
159     g_object_unref(plugin);
160 }
161 END_TEST
162
163 START_TEST (test_passwordplugin_user_action_finished)
164 {
165     gpointer plugin;
166     
167     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
168     fail_if(plugin == NULL);
169
170     GSignondSessionData* result = NULL;
171     GSignondSignonuiData* ui_action = NULL;
172     GError* error = NULL;
173
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);
178
179     GSignondSignonuiData* data = gsignond_dictionary_new();
180     
181     //empty data
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));
188     g_error_free(error);
189     error = NULL;
190     
191     // correct values
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);
199     fail_if(g_strcmp0(
200         gsignond_session_data_get_username(result), "megauser") != 0);
201     fail_if(g_strcmp0(
202         gsignond_session_data_get_secret(result), "megapassword") != 0);
203     gsignond_dictionary_unref(result);
204     result = NULL;
205
206     // user canceled
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));
214     g_error_free(error);
215     error = NULL;
216
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));
225     g_error_free(error);
226     error = NULL;
227     
228     gsignond_dictionary_unref(data);
229     g_object_unref(plugin);
230 }
231 END_TEST
232
233 START_TEST (test_passwordplugin_refresh)
234 {
235     gpointer plugin;
236     
237     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
238     fail_if(plugin == NULL);
239
240     GSignondSessionData* result = NULL;
241     GError* error = NULL;
242
243     g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback), &result);
244     g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
245
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);
251     result = NULL;
252     
253     gsignond_dictionary_unref(data);
254     g_object_unref(plugin);
255 }
256 END_TEST
257
258 Suite* passwordplugin_suite (void)
259 {
260     Suite *s = suite_create ("Password plugin");
261     
262     /* Core test case */
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);
269     return s;
270 }
271
272 int main (void)
273 {
274     int number_failed;
275
276 #if !GLIB_CHECK_VERSION (2, 36, 0)
277     g_type_init ();
278 #endif
279
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);
284     srunner_free(sr);
285     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
286 }
287