953eefd5019fbbd9c3fa513a30f901be9ff9d9b6
[profile/ivi/gsignond.git] / test / plugins / digestplugintest.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-2013 Intel Corporation.
7  *
8  * Contact: Imran Zaman <imran.zaman@intel.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-digest-plugin.h"
29 #include "gsignond/gsignond-session-data.h"
30 #include "gsignond/gsignond-plugin-interface.h"
31 #include "gsignond/gsignond-error.h"
32 #include "gsignond/gsignond-log.h"
33 #include "gsignond/gsignond-config.h"
34 #include "common/gsignond-plugin-loader.h"
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, "digest") == 0);
46     fail_unless(g_strcmp0(mechanisms[0], "digest") == 0);
47     fail_unless(mechanisms[1] == NULL);
48
49     g_free(type);
50     g_strfreev(mechanisms);
51 }
52
53 START_TEST (test_digestplugin_create)
54 {
55     gpointer plugin;
56
57     plugin = g_object_new(GSIGNOND_TYPE_DIGEST_PLUGIN, NULL);
58     check_plugin(plugin);
59     g_object_unref(plugin);
60 }
61 END_TEST
62
63 static void
64 response_callback(
65         GSignondPlugin* plugin,
66         GSignondSessionData* result,
67         gpointer user_data)
68 {
69     GSignondSessionData** user_data_p = user_data;
70     *user_data_p = gsignond_dictionary_copy(result);
71 }
72
73 static void
74 user_action_required_callback(
75         GSignondPlugin* plugin,
76         GSignondSignonuiData* ui_request,
77         gpointer user_data)
78 {
79     GSignondSignonuiData** user_data_p = user_data;
80     *user_data_p = gsignond_dictionary_copy(ui_request);
81     gsignond_signonui_data_set_username(*user_data_p, "user1");
82     gsignond_signonui_data_set_password(*user_data_p, "password1");
83 }
84
85 static void
86 error_callback(
87         GSignondPlugin* plugin,
88         GError* error,
89         gpointer user_data)
90 {
91     GError** user_data_p = user_data;
92     *user_data_p = g_error_copy(error);
93 }
94
95
96 START_TEST (test_digestplugin_request)
97 {
98     gpointer plugin;
99     gboolean query_res;
100
101     plugin = g_object_new(GSIGNOND_TYPE_DIGEST_PLUGIN, NULL);
102     fail_if(plugin == NULL);
103
104     GSignondSessionData* result = NULL;
105     GSignondSignonuiData* ui_action = NULL;
106     GError* error = NULL;
107
108     g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
109             &result);
110     g_signal_connect(plugin, "user-action-required",
111             G_CALLBACK(user_action_required_callback), &ui_action);
112     g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
113
114     GSignondSessionData* data = gsignond_dictionary_new();
115
116     // set only username and password
117     gsignond_session_data_set_username(data, "user1");
118     gsignond_session_data_set_secret(data, "password1");
119
120     gsignond_plugin_request_initial(plugin, data, "digest");
121     fail_if(result != NULL);
122     fail_if(ui_action != NULL);
123     fail_if(error == NULL);
124     fail_unless(g_error_matches(error, GSIGNOND_ERROR,
125                                 GSIGNOND_ERROR_MISSING_DATA));
126     g_error_free(error);
127     error = NULL;
128
129     // set all the required stuff so that no ui-action is required
130     gsignond_session_data_set_realm(data, "realm1");
131     gsignond_dictionary_set_string(data, "Algo", "md5-sess");
132     gsignond_dictionary_set_string(data, "Nonce",
133             "abg10b1234ee1f0e8b11d0f600bfb0c093");
134     gsignond_dictionary_set_string(data, "Method", "GET");
135     gsignond_dictionary_set_string(data, "DigestUri", "/test/index.html");
136
137     gsignond_plugin_request_initial(plugin, data, "digest");
138     fail_if(result == NULL);
139     fail_if(ui_action != NULL);
140     fail_if(error != NULL);
141     fail_if(g_strcmp0(gsignond_session_data_get_username(result),
142             "user1") != 0);
143     fail_if(gsignond_dictionary_get_string(result, "Response") == NULL);
144     fail_if(gsignond_dictionary_get_string(result, "CNonce") == NULL);
145     gsignond_dictionary_unref(result);
146     result = NULL;
147
148     //remove secret so that ui action is required
149     gsignond_dictionary_remove (data, "Secret");
150     gsignond_plugin_request_initial(plugin, data, "digest");
151     fail_if(result != NULL);
152     fail_if(ui_action == NULL);
153     fail_if(error != NULL);
154     fail_if(g_strcmp0(gsignond_signonui_data_get_username(ui_action),
155             "user1") != 0);
156     fail_if(g_strcmp0(gsignond_signonui_data_get_password(ui_action),
157             "password1") != 0);
158     fail_if(gsignond_dictionary_get_string(ui_action, "Realm") == NULL);
159     fail_if(gsignond_dictionary_get_string(ui_action, "DigestUri") == NULL);
160     gsignond_signonui_data_get_query_username(ui_action, &query_res);
161     fail_if(query_res == FALSE);
162     gsignond_signonui_data_get_query_password(ui_action, &query_res);
163     fail_if(query_res == FALSE);
164     gsignond_dictionary_unref(ui_action);
165     ui_action = NULL;
166
167     gsignond_dictionary_unref(data);
168     g_object_unref(plugin);
169 }
170 END_TEST
171
172 START_TEST (test_digestplugin_user_action_finished)
173 {
174     gpointer plugin;
175
176     plugin = g_object_new(GSIGNOND_TYPE_DIGEST_PLUGIN, NULL);
177     fail_if(plugin == NULL);
178
179     GSignondSessionData *result = NULL, *data = NULL;
180     GSignondSignonuiData *ui_action = NULL, *ui_data = NULL;
181     GError* error = NULL;
182
183     g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
184             &result);
185     g_signal_connect(plugin, "user-action-required",
186             G_CALLBACK(user_action_required_callback), &ui_action);
187     g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
188
189     ui_data = gsignond_signonui_data_new();
190     gsignond_signonui_data_set_query_error(ui_data, SIGNONUI_ERROR_NONE);
191
192     //empty data
193     gsignond_plugin_user_action_finished(plugin, ui_data);
194     fail_if(result != NULL);
195     fail_if(ui_action != NULL);
196     fail_if(error == NULL);
197     fail_unless(g_error_matches(error, GSIGNOND_ERROR,
198             GSIGNOND_ERROR_USER_INTERACTION));
199     g_error_free(error);
200     error = NULL;
201
202     // user cancelled
203     gsignond_signonui_data_set_query_error(ui_data, SIGNONUI_ERROR_CANCELED);
204     gsignond_plugin_user_action_finished(plugin, ui_data);
205     fail_if(result != NULL);
206     fail_if(ui_action != NULL);
207     fail_if(error == NULL);
208     fail_unless(g_error_matches(error, GSIGNOND_ERROR,
209                                 GSIGNOND_ERROR_SESSION_CANCELED));
210     g_error_free(error);
211     error = NULL;
212
213     // error in ui request
214     gsignond_signonui_data_set_query_error(ui_data, SIGNONUI_ERROR_GENERAL);
215     gsignond_plugin_user_action_finished(plugin, ui_data);
216     fail_if(result != NULL);
217     fail_if(ui_action != NULL);
218     fail_if(error == NULL);
219     fail_unless(g_error_matches(error, GSIGNOND_ERROR,
220                                 GSIGNOND_ERROR_USER_INTERACTION));
221     g_error_free(error);
222     error = NULL;
223
224     // correct values but no session data
225     gsignond_signonui_data_set_username (ui_data, "user1");
226     gsignond_signonui_data_set_password (ui_data, "password1");
227     gsignond_signonui_data_set_query_error (ui_data, SIGNONUI_ERROR_NONE);
228     gsignond_plugin_user_action_finished (plugin, ui_data);
229     fail_if(result != NULL);
230     fail_if(ui_action != NULL);
231     fail_if(error == NULL);
232     fail_unless(g_error_matches(error, GSIGNOND_ERROR,
233             GSIGNOND_ERROR_USER_INTERACTION));
234     g_error_free(error);
235     error = NULL;
236
237     //correct values
238     data = gsignond_dictionary_new ();
239     gsignond_session_data_set_username (data, "user1");
240     gsignond_session_data_set_realm (data, "realm1");
241     gsignond_dictionary_set_string (data, "Algo", "md5-sess");
242     gsignond_dictionary_set_string (data, "Nonce",
243             "abg10b1234ee1f0e8b11d0f600bfb0c093");
244     gsignond_dictionary_set_string (data, "Method", "GET");
245     gsignond_dictionary_set_string (data, "DigestUri", "/test/index.html");
246     gsignond_plugin_request_initial (plugin, data, "digest");
247     gsignond_dictionary_unref (data); data = NULL;
248
249     gsignond_plugin_user_action_finished (plugin, ui_data);
250     fail_if (result == NULL);
251     fail_if (error != NULL);
252     fail_if(ui_action == NULL);
253     fail_if(g_strcmp0(gsignond_session_data_get_username(result),
254             "user1") != 0);
255     fail_if(gsignond_dictionary_get_string(result, "Response") == NULL);
256     fail_if(gsignond_dictionary_get_string(result, "CNonce") == NULL);
257     gsignond_dictionary_unref(result);
258     result = NULL;
259     gsignond_dictionary_unref(ui_action);
260     ui_action = NULL;
261
262     gsignond_dictionary_unref (ui_data);
263     g_object_unref (plugin);
264 }
265 END_TEST
266
267 START_TEST (test_digestplugin_refresh)
268 {
269     gpointer plugin;
270
271     plugin = g_object_new(GSIGNOND_TYPE_DIGEST_PLUGIN, NULL);
272     fail_if(plugin == NULL);
273
274     GSignondSessionData* result = NULL;
275     GError* error = NULL;
276
277     g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback),
278             &result);
279     g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
280
281     GSignondSessionData* data = gsignond_dictionary_new();
282     gsignond_plugin_refresh(plugin, data);
283     fail_if(result == NULL);
284     fail_if(error != NULL);
285     gsignond_dictionary_unref(result);
286     result = NULL;
287
288     gsignond_dictionary_unref(data);
289     g_object_unref(plugin);
290 }
291 END_TEST
292
293 Suite* digestplugin_suite (void)
294 {
295     Suite *s = suite_create ("Digest plugin");
296
297     /* Core test case */
298     TCase *tc_core = tcase_create ("Tests");
299     tcase_add_test (tc_core, test_digestplugin_create);
300     tcase_add_test (tc_core, test_digestplugin_request);
301     tcase_add_test (tc_core, test_digestplugin_user_action_finished);
302     tcase_add_test (tc_core, test_digestplugin_refresh);
303     suite_add_tcase (s, tc_core);
304     return s;
305 }
306
307 int main (void)
308 {
309     int number_failed;
310
311 #if !GLIB_CHECK_VERSION (2, 36, 0)
312     g_type_init ();
313 #endif
314
315     Suite *s = digestplugin_suite();
316     SRunner *sr = srunner_create(s);
317     srunner_run_all(sr, CK_NORMAL);
318     number_failed = srunner_ntests_failed(sr);
319     srunner_free(sr);
320     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
321 }
322