Update to upstream 1.0.1
[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
35
36 static void check_plugin(GSignondPlugin* plugin)
37 {
38     gchar* type;
39     gchar** mechanisms;
40
41     fail_if(plugin == NULL);
42     
43     g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
44     
45     fail_unless(g_strcmp0(type, "password") == 0);
46     fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
47     fail_unless(mechanisms[1] == NULL);
48     
49     g_free(type);
50     g_strfreev(mechanisms);
51 }
52
53 START_TEST (test_passwordplugin_create)
54 {
55     gpointer plugin;
56     
57     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
58     check_plugin(plugin);
59     g_object_unref(plugin);
60 }
61 END_TEST
62
63 static void response_callback(GSignondPlugin* plugin, GSignondSessionData* result,
64                      gpointer user_data)
65 {
66     GSignondSessionData** user_data_p = user_data;
67     *user_data_p = gsignond_dictionary_copy(result);
68 }
69
70 static void user_action_required_callback(GSignondPlugin* plugin, 
71                                           GSignondSignonuiData* ui_request, 
72                                           gpointer user_data)
73 {
74     GSignondSignonuiData** user_data_p = user_data;
75     *user_data_p = gsignond_dictionary_copy(ui_request);
76 }
77
78 static void error_callback(GSignondPlugin* plugin, GError* error,
79                      gpointer user_data)
80 {
81     GError** user_data_p = user_data;
82     *user_data_p = g_error_copy(error);
83 }
84
85
86 START_TEST (test_passwordplugin_request)
87 {
88     gpointer plugin;
89     
90     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
91     fail_if(plugin == NULL);
92
93     GSignondSessionData* result = NULL;
94     GSignondSignonuiData* ui_action = NULL;
95     GError* error = NULL;
96     gboolean bool_res;
97
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);
102
103     GSignondSessionData* data = gsignond_dictionary_new();
104
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);
112     fail_if(g_strcmp0(
113         gsignond_session_data_get_secret(result), "megapassword") != 0);
114     gsignond_dictionary_unref(result);
115     result = NULL;
116     
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);
123     fail_if(g_strcmp0(
124         gsignond_session_data_get_username(result), "megauser") != 0);
125     fail_if(g_strcmp0(
126         gsignond_session_data_get_secret(result), "megapassword") != 0);
127     gsignond_dictionary_unref(result);
128     result = NULL;
129     
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);
142     ui_action = NULL;
143     
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);
155     ui_action = NULL;
156     
157     gsignond_dictionary_unref(data);
158     g_object_unref(plugin);
159 }
160 END_TEST
161
162 START_TEST (test_passwordplugin_user_action_finished)
163 {
164     gpointer plugin;
165     
166     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
167     fail_if(plugin == NULL);
168
169     GSignondSessionData* result = NULL;
170     GSignondSignonuiData* ui_action = NULL;
171     GError* error = NULL;
172
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);
177
178     GSignondSignonuiData* data = gsignond_dictionary_new();
179     
180     //empty data
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));
187     g_error_free(error);
188     error = NULL;
189     
190     // correct values
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);
198     fail_if(g_strcmp0(
199         gsignond_session_data_get_username(result), "megauser") != 0);
200     fail_if(g_strcmp0(
201         gsignond_session_data_get_secret(result), "megapassword") != 0);
202     gsignond_dictionary_unref(result);
203     result = NULL;
204
205     // user canceled
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));
213     g_error_free(error);
214     error = NULL;
215
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));
224     g_error_free(error);
225     error = NULL;
226     
227     gsignond_dictionary_unref(data);
228     g_object_unref(plugin);
229 }
230 END_TEST
231
232 START_TEST (test_passwordplugin_refresh)
233 {
234     gpointer plugin;
235     
236     plugin = g_object_new(GSIGNOND_TYPE_PASSWORD_PLUGIN, NULL);
237     fail_if(plugin == NULL);
238
239     GSignondSessionData* result = NULL;
240     GError* error = NULL;
241
242     g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback), &result);
243     g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
244
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);
250     result = NULL;
251     
252     gsignond_dictionary_unref(data);
253     g_object_unref(plugin);
254 }
255 END_TEST
256
257 Suite* passwordplugin_suite (void)
258 {
259     Suite *s = suite_create ("Password plugin");
260     
261     /* Core test case */
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);
268     return s;
269 }
270
271 int main (void)
272 {
273     int number_failed;
274
275 #if !GLIB_CHECK_VERSION (2, 36, 0)
276     g_type_init ();
277 #endif
278
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);
283     srunner_free(sr);
284     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
285 }
286