1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of gsignond
*
* Copyright (C) 2012 Intel Corporation.
*
* Contact: Alexander Kanavin <alex.kanavin@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <check.h>
#include <stdlib.h>
#include <errno.h>
#include "daemon/gsignond-types.h"
#include "gsignond-plugin-remote-private.h"
#include "gsignond-plugin-remote.h"
#include "plugind/gsignond-plugin-daemon.h"
#include <gsignond/gsignond-plugin-interface.h>
#include <gsignond/gsignond-error.h>
#include <gsignond/gsignond-config.h>
#include <gsignond/gsignond-log.h>
static GMainLoop *main_loop = NULL;
guint child_watch_id = 0;
struct _GSignondAuthSession
{
GObject parent;
};
struct _GSignondAuthSessionClass
{
GObjectClass parent_class;
};
G_DEFINE_TYPE (GSignondAuthSession, gsignond_auth_session, G_TYPE_OBJECT);
static void
_stop_mainloop ()
{
if (main_loop) {
g_main_loop_quit (main_loop);
}
}
static void
_run_mainloop ()
{
g_main_loop_run (main_loop);
}
static void
_setup ()
{
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init ();
#endif
if (main_loop == NULL) {
main_loop = g_main_loop_new (NULL, FALSE);
}
}
static void
_teardown ()
{
if (main_loop) {
_stop_mainloop ();
g_main_loop_unref (main_loop);
main_loop = NULL;
}
}
static gboolean
_timeout_quit_loop (gpointer data)
{
_stop_mainloop ();
return FALSE;
}
static void
_run_loop_with_timeout (guint timeout)
{
g_timeout_add_seconds (timeout, _timeout_quit_loop, NULL);
_run_mainloop ();
}
static void
gsignond_auth_session_init (
GSignondAuthSession *self)
{
}
static void
gsignond_auth_session_class_init (
GSignondAuthSessionClass *klass)
{
}
void
gsignond_auth_session_notify_process_result (
GSignondAuthSession *iface,
GSignondSessionData *result,
gpointer user_data)
{
}
void
gsignond_auth_session_notify_process_error (
GSignondAuthSession *iface,
const GError *error,
gpointer user_data)
{
}
void
gsignond_auth_session_notify_store (
GSignondAuthSession *self,
GSignondSessionData *session_data)
{
}
void
gsignond_auth_session_notify_user_action_required (
GSignondAuthSession *self,
GSignondSignonuiData *session_data)
{
}
void
gsignond_auth_session_notify_refreshed (
GSignondAuthSession *self,
GSignondSignonuiData *session_data)
{
}
void
gsignond_auth_session_notify_state_changed (
GSignondAuthSession *self,
gint state,
const gchar *message,
gpointer user_data)
{
}
static void
check_plugin(
GSignondPlugin* plugin)
{
gchar* type;
gchar** mechanisms;
fail_if(plugin == NULL);
g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
fail_unless(g_strcmp0(type, "password") == 0);
fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
fail_unless(mechanisms[1] == NULL);
g_free(type);
g_strfreev(mechanisms);
}
static void
response_callback(
GSignondPlugin* plugin,
GSignondSessionData* result,
gpointer user_data)
{
DBG ("");
GSignondSessionData** user_data_p = user_data;
*user_data_p = gsignond_dictionary_copy(result);
_stop_mainloop ();
}
static void
user_action_required_callback(
GSignondPlugin* plugin,
GSignondSignonuiData* ui_request,
gpointer user_data)
{
DBG ("");
GSignondSignonuiData** user_data_p = user_data;
*user_data_p = gsignond_dictionary_copy(ui_request);
_stop_mainloop ();
}
static void
error_callback(
GSignondPlugin* plugin,
GError* error,
gpointer user_data)
{
DBG ("");
GError** user_data_p = user_data;
*user_data_p = g_error_copy(error);
_stop_mainloop ();
}
START_TEST (test_pluginremote_create)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondConfig* config = gsignond_config_new ();
fail_if (config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
g_object_unref (config);
check_plugin (plugin);
g_object_unref (plugin);
}
END_TEST
START_TEST (test_pluginremote_plugind_create)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondPluginRemotePrivate* priv = NULL;
GSignondConfig* config = gsignond_config_new ();
fail_if (config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
g_object_unref (config);
fail_if (plugin == NULL);
priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
fail_unless (priv->cpid > 0);
fail_unless (kill (priv->cpid, 0) == 0);
check_plugin (plugin);
g_object_unref (plugin);
}
END_TEST
START_TEST (test_pluginremote_plugind_unref)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondPluginRemotePrivate* priv = NULL;
gint cpid = 0;
GSignondConfig* config = gsignond_config_new ();
fail_if (config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
fail_if (plugin == NULL);
priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
fail_unless (priv->cpid > 0);
fail_unless (kill (priv->cpid, 0) == 0);
cpid = priv->cpid;
g_object_unref (plugin);
g_object_unref (config);
fail_unless (kill (cpid, 0) != 0);
}
END_TEST
START_TEST (test_pluginremote_plugind_kill)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondPluginRemotePrivate* priv = NULL;
gint cpid = 0;
GSignondConfig* config = gsignond_config_new ();
fail_if (config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
fail_if (plugin == NULL);
priv = (GSignondPluginRemotePrivate *)GSIGNOND_PLUGIN_REMOTE (plugin)->priv;
fail_unless (priv->cpid > 0);
fail_unless (kill (priv->cpid, 0) == 0);
cpid = priv->cpid;
check_plugin (plugin);
kill (priv->cpid, SIGTERM);
_run_loop_with_timeout (1);
fail_unless (kill (cpid, 0) != 0);
g_object_unref (plugin);
g_object_unref (config);
}
END_TEST
START_TEST (test_pluginremote_request)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondConfig* config = gsignond_config_new ();
fail_if(config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
fail_if(plugin == NULL);
GSignondSessionData* result = NULL;
GSignondSignonuiData* ui_action = NULL;
GError* error = NULL;
gboolean bool_res;
g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
&result);
g_signal_connect(plugin, "user-action-required",
G_CALLBACK(user_action_required_callback), &ui_action);
g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
GSignondSessionData* data = gsignond_dictionary_new ();
// username empty, password not empty
gsignond_session_data_set_secret(data, "megapassword");
gsignond_plugin_request_initial(plugin, data, "password");
_run_mainloop ();
fail_if(result == NULL);
fail_if(ui_action != NULL);
fail_if(error != NULL);
fail_if(gsignond_session_data_get_username(result) != NULL);
fail_if(g_strcmp0(
gsignond_session_data_get_secret(result), "megapassword") != 0);
gsignond_dictionary_unref(result);
result = NULL;
// username and password not empty
gsignond_session_data_set_username(data, "megauser");
gsignond_plugin_request_initial(plugin, data, "password");
_run_mainloop ();
fail_if(result == NULL);
fail_if(ui_action != NULL);
fail_if(error != NULL);
fail_if(g_strcmp0(
gsignond_session_data_get_username(result), "megauser") != 0);
fail_if(g_strcmp0(
gsignond_session_data_get_secret(result), "megapassword") != 0);
gsignond_dictionary_unref(result);
result = NULL;
//username and password empty
gsignond_dictionary_unref(data);
data = gsignond_dictionary_new();
gsignond_plugin_request_initial(plugin, data, "password");
_run_mainloop ();
fail_if(result != NULL);
fail_if(ui_action == NULL);
fail_if(error != NULL);
fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res)
== FALSE);
fail_if(bool_res == FALSE);
fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res)
== FALSE);
fail_if(bool_res == FALSE);
gsignond_dictionary_unref(ui_action);
ui_action = NULL;
//username not empty, password empty
gsignond_session_data_set_username(data, "megauser");
gsignond_plugin_request_initial(plugin, data, "password");
_run_mainloop ();
fail_if(result != NULL);
fail_if(ui_action == NULL);
fail_if(error != NULL);
fail_if(gsignond_signonui_data_get_query_username(ui_action, &bool_res)
== FALSE);
fail_if(bool_res == TRUE);
fail_if(gsignond_signonui_data_get_query_password(ui_action, &bool_res)
== FALSE);
fail_if(bool_res == FALSE);
gsignond_dictionary_unref(ui_action);
ui_action = NULL;
gsignond_dictionary_unref(data);
g_object_unref(config);
g_object_unref(plugin);
}
END_TEST
START_TEST (test_pluginremote_user_action_finished)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondConfig* config = gsignond_config_new ();
fail_if(config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
fail_if(plugin == NULL);
GSignondSessionData* result = NULL;
GSignondSignonuiData* ui_action = NULL;
GError* error = NULL;
g_signal_connect(plugin, "response-final", G_CALLBACK(response_callback),
&result);
g_signal_connect(plugin, "user-action-required",
G_CALLBACK(user_action_required_callback), &ui_action);
g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
GSignondSignonuiData* data = gsignond_dictionary_new();
//empty data
gsignond_plugin_user_action_finished(plugin, data);
_run_mainloop ();
fail_if(result != NULL);
fail_if(ui_action != NULL);
fail_if(error == NULL);
fail_unless (error->code == GSIGNOND_ERROR_USER_INTERACTION);
g_error_free(error);
error = NULL;
// correct values
gsignond_signonui_data_set_username(data, "megauser");
gsignond_signonui_data_set_password(data, "megapassword");
gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_NONE);
gsignond_plugin_user_action_finished(plugin, data);
_run_mainloop ();
fail_if(result == NULL);
fail_if(ui_action != NULL);
fail_if(error != NULL);
fail_if(g_strcmp0(
gsignond_session_data_get_username(result), "megauser") != 0);
fail_if(g_strcmp0(
gsignond_session_data_get_secret(result), "megapassword") != 0);
gsignond_dictionary_unref(result);
result = NULL;
// user canceled
gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_CANCELED);
gsignond_plugin_user_action_finished(plugin, data);
_run_mainloop ();
fail_if(result != NULL);
fail_if(ui_action != NULL);
fail_if(error == NULL);
fail_unless (error->code == GSIGNOND_ERROR_SESSION_CANCELED);
g_error_free(error);
error = NULL;
// error in ui request
gsignond_signonui_data_set_query_error(data, SIGNONUI_ERROR_GENERAL);
gsignond_plugin_user_action_finished(plugin, data);
_run_mainloop ();
fail_if(result != NULL);
fail_if(ui_action != NULL);
fail_if(error == NULL);
fail_unless (error->code == GSIGNOND_ERROR_USER_INTERACTION);
g_error_free(error);
error = NULL;
gsignond_dictionary_unref(data);
g_object_unref(config);
g_object_unref(plugin);
}
END_TEST
START_TEST (test_pluginremote_refresh)
{
DBG ("");
GSignondPlugin *plugin = NULL;
const gchar *plugin_type = "password";
GSignondConfig* config = gsignond_config_new ();
fail_if(config == NULL);
plugin = GSIGNOND_PLUGIN (gsignond_plugin_remote_new(config, plugin_type));
fail_if(plugin == NULL);
GSignondSessionData* result = NULL;
GError* error = NULL;
g_signal_connect(plugin, "refreshed", G_CALLBACK(response_callback),
&result);
g_signal_connect(plugin, "error", G_CALLBACK(error_callback), &error);
GSignondSessionData* data = gsignond_dictionary_new();
gsignond_plugin_refresh(plugin, data);
_run_mainloop ();
fail_if(result == NULL);
fail_if(error != NULL);
gsignond_dictionary_unref(result);
result = NULL;
gsignond_dictionary_unref(data);
g_object_unref(config);
g_object_unref(plugin);
}
END_TEST
START_TEST (test_plugind_daemon)
{
DBG ("");
GSignondPluginDaemon *daemon = NULL;
const gchar *plugin_type = "password";
GSignondConfig* config = gsignond_config_new ();
fail_if(config == NULL);
gchar *plugin_path = g_module_build_path (gsignond_config_get_string (
config, GSIGNOND_CONFIG_GENERAL_PLUGINS_DIR), "nonexisting");
fail_if (plugin_path == NULL);
daemon = gsignond_plugin_daemon_new (plugin_path, "nonexisting", 0, 1);
g_free (plugin_path);
fail_if (daemon != NULL);
plugin_path = g_module_build_path (gsignond_config_get_string (
config, GSIGNOND_CONFIG_GENERAL_PLUGINS_DIR), plugin_type);
fail_if (plugin_path == NULL);
daemon = gsignond_plugin_daemon_new (plugin_path, plugin_type, 0, 1);
g_free (plugin_path);
fail_if (daemon == NULL);
g_object_unref (daemon);
daemon = NULL;
g_object_unref(config);
}
END_TEST
Suite* pluginremote_suite (void)
{
Suite *s = suite_create ("Plugin remote");
/* Core test case */
TCase *tc_core = tcase_create ("RemoteTests");
tcase_add_checked_fixture (tc_core, _setup, _teardown);
tcase_add_test (tc_core, test_pluginremote_create);
tcase_add_test (tc_core, test_pluginremote_plugind_create);
tcase_add_test (tc_core, test_pluginremote_plugind_unref);
tcase_add_test (tc_core, test_pluginremote_plugind_kill);
tcase_add_test (tc_core, test_pluginremote_request);
tcase_add_test (tc_core, test_pluginremote_user_action_finished);
tcase_add_test (tc_core, test_pluginremote_refresh);
tcase_add_test (tc_core, test_plugind_daemon);
suite_add_tcase (s, tc_core);
return s;
}
int main (void)
{
int number_failed;
Suite *s = pluginremote_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|