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
31 #include <glib-unix.h>
32 #include "gsignond/gsignond-session-data.h"
33 #include "gsignond/gsignond-error.h"
34 #include "gsignond/gsignond-log.h"
35 #include "gsignond/gsignond-utils.h"
36 #include "common/gsignond-identity-info.h"
37 #include "common/gsignond-pipe-stream.h"
38 #include "gplugind/gsignond-plugin-loader.h"
41 _sequence_new (gchar *data)
43 GSequence *value = NULL;
44 value = g_sequence_new (NULL);
45 g_sequence_append (value, (guint8 *)data);
54 GSequenceIter *iter1 = NULL, *iter2 = NULL;
55 gboolean equal = TRUE;
57 if (one == NULL && two == NULL)
60 if ((one != NULL && two == NULL) ||
61 (one == NULL && two != NULL) ||
62 (g_sequence_get_length (one) != g_sequence_get_length (two)))
68 iter1 = g_sequence_get_begin_iter (one);
69 while (!g_sequence_iter_is_end (iter1)) {
70 iter2 = g_sequence_get_iter_at_pos (two,
71 g_sequence_iter_get_position (iter1));
72 if (g_strcmp0 (g_sequence_get (iter1), g_sequence_get (iter2)) != 0) {
76 iter1 = g_sequence_iter_next (iter1);
82 START_TEST (test_pipe_stream)
84 GSignondPipeStream *stream = NULL;
86 fail_unless (pipe (pipefd) == 0);
87 stream = gsignond_pipe_stream_new (pipefd[0], pipefd[1], TRUE);
88 fail_if (stream == NULL);
89 g_object_unref (stream);
93 START_TEST (test_session_data)
95 GSignondSessionData* data;
96 GSignondSessionData* data_from_variant;
97 GSignondSessionData* data_from_copy;
100 data = gsignond_dictionary_new();
101 fail_if(data == NULL);
103 fail_unless(gsignond_session_data_get_username(data) == NULL);
104 fail_unless(gsignond_session_data_get_secret(data) == NULL);
106 gsignond_session_data_set_username(data, "megauser");
107 gsignond_session_data_set_secret(data, "megapassword");
109 fail_unless(g_strcmp0(gsignond_session_data_get_username(data),
111 fail_unless(g_strcmp0(gsignond_session_data_get_secret(data),
112 "megapassword") == 0);
114 gsignond_session_data_set_username(data, "usermega");
115 fail_unless(g_strcmp0(gsignond_session_data_get_username(data),
118 data_from_copy = gsignond_dictionary_copy(data);
119 fail_if(data_from_copy == NULL);
121 fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_copy),
123 fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_copy),
124 "megapassword") == 0);
126 variant = gsignond_dictionary_to_variant(data);
127 fail_if(variant == NULL);
128 data_from_variant = gsignond_dictionary_new_from_variant(variant);
129 fail_if(data_from_variant == NULL);
131 fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_variant),
133 fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_variant),
134 "megapassword") == 0);
136 g_variant_unref(variant);
137 gsignond_dictionary_unref(data_from_variant);
138 gsignond_dictionary_unref(data_from_copy);
139 gsignond_dictionary_unref(data);
143 static void check_plugin(GSignondPlugin* plugin)
148 fail_if(plugin == NULL);
150 g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
152 fail_unless(g_strcmp0(type, "password") == 0);
153 fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
154 fail_unless(mechanisms[1] == NULL);
157 g_strfreev(mechanisms);
160 START_TEST (test_plugin_loader)
162 GSignondConfig* config = gsignond_config_new();
163 fail_if(config == NULL);
165 GSignondPlugin* absent_plugin = gsignond_load_plugin(config, "absentplugin");
166 fail_if(absent_plugin != NULL);
168 GSignondPlugin* plugin = gsignond_load_plugin(config, "password");
169 check_plugin(plugin);
171 g_object_unref(plugin);
172 g_object_unref(config);
176 START_TEST (test_identity_info)
180 const gchar *username = "username1";
181 const gchar *secret = "secret1";
182 const gchar *caption = "caption1";
183 GSignondIdentityInfo *identity = NULL;
184 GSignondIdentityInfo *identity2 = NULL;
185 GSignondSecurityContextList *ctx_list = NULL, *list = NULL;
186 GSignondSecurityContext *ctx, *ctx1, *ctx2, *ctx3 ;
187 GHashTable *methods = NULL, *methods2;
188 GSequence *seq1 = NULL, *seq_realms, *seq21, *mechs;
191 identity = gsignond_identity_info_new ();
192 fail_if (identity == NULL);
194 fail_unless (gsignond_identity_info_get_id (identity) == 0);
195 fail_unless (gsignond_identity_info_get_is_identity_new (identity)== TRUE);
196 fail_unless (gsignond_identity_info_get_username (identity) == NULL);
197 fail_unless (gsignond_identity_info_get_is_username_secret (
199 fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
200 fail_unless (gsignond_identity_info_get_store_secret (identity) == FALSE);
201 fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
202 fail_unless (gsignond_identity_info_get_realms (identity) == NULL);
203 fail_unless (gsignond_identity_info_get_methods (identity) == NULL);
204 fail_unless (gsignond_identity_info_get_mechanisms (
205 identity, "testmech") == NULL);
206 fail_unless (gsignond_identity_info_get_access_control_list (
208 fail_unless (gsignond_identity_info_get_owner (identity) == NULL);
209 fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
210 fail_unless (gsignond_identity_info_get_identity_type (identity) == 0);
212 fail_unless (gsignond_identity_info_set_id (identity, id) == TRUE);
214 fail_unless (id == gsignond_identity_info_get_id (identity));
216 fail_unless (gsignond_identity_info_set_identity_new (identity) == TRUE);
218 fail_unless (gsignond_identity_info_get_is_identity_new (
221 fail_unless (gsignond_identity_info_set_username (
222 identity, NULL) == TRUE);
224 fail_unless (gsignond_identity_info_get_username (identity) == NULL);
226 fail_unless (gsignond_identity_info_set_username (
227 identity, username) == TRUE);
229 fail_unless (g_strcmp0 (username, gsignond_identity_info_get_username (
232 fail_unless (gsignond_identity_info_set_username_secret (
233 identity, TRUE) == TRUE);
235 fail_unless (gsignond_identity_info_get_is_username_secret (
238 fail_unless (gsignond_identity_info_set_secret (identity, NULL) == TRUE);
240 fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
242 fail_unless (gsignond_identity_info_set_secret (identity, secret) == TRUE);
244 fail_unless (g_strcmp0 (secret, gsignond_identity_info_get_secret (
247 fail_unless (gsignond_identity_info_set_store_secret (
248 identity, TRUE) == TRUE);
250 fail_unless (gsignond_identity_info_get_store_secret (
253 fail_unless (gsignond_identity_info_set_caption (identity, NULL) == TRUE);
255 fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
257 fail_unless (gsignond_identity_info_set_caption (
258 identity, caption) == TRUE);
260 fail_unless (g_strcmp0 (caption, gsignond_identity_info_get_caption (
264 seq_realms = _sequence_new("realms1");
265 fail_unless (gsignond_identity_info_set_realms (
266 identity, seq_realms) == TRUE);
268 seq1 = gsignond_identity_info_get_realms (identity);
269 fail_if (seq1 == NULL);
270 fail_unless (_compare_sequences (seq1, seq_realms) == TRUE);
271 g_sequence_free (seq1); seq1 = NULL;
272 g_sequence_free (seq_realms);
275 methods = g_hash_table_new_full ((GHashFunc)g_str_hash,
276 (GEqualFunc)g_str_equal,
277 (GDestroyNotify)NULL,
278 (GDestroyNotify)g_sequence_free);
279 seq1 = _sequence_new("mech11"); g_sequence_append (seq1, "mech12");
280 fail_unless (gsignond_identity_info_set_methods (
281 identity, methods) == TRUE);
282 g_hash_table_insert (methods, "method1", seq1);
283 g_hash_table_insert (methods, "method2", _sequence_new("mech21"));
284 g_hash_table_insert (methods, "method3", _sequence_new("mech31"));
285 g_hash_table_insert (methods, "method4", _sequence_new("mech41"));
286 fail_unless (gsignond_identity_info_set_methods (
287 identity, methods) == TRUE);
289 methods2 = gsignond_identity_info_get_methods (identity);
290 fail_if (methods2 == NULL);
291 seq21 = g_hash_table_lookup (methods, "method1");
292 fail_if (seq21 == NULL);
293 fail_unless (_compare_sequences (seq1, seq21) == TRUE);
294 g_hash_table_unref (methods2);
295 g_hash_table_unref (methods);
297 fail_unless (gsignond_identity_info_get_mechanisms (
298 identity, "method20") == NULL);
300 mechs = gsignond_identity_info_get_mechanisms (
301 identity, "method1");
302 fail_if (mechs == NULL);
303 g_sequence_free (mechs);
305 fail_unless (gsignond_identity_info_remove_method (
306 identity, "method20") == FALSE);
307 fail_unless (gsignond_identity_info_remove_method (
308 identity, "method4") == TRUE);
311 ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
312 ctx2 = gsignond_security_context_new_from_values ("sysctx2", "appctx2");
313 ctx3 = gsignond_security_context_new_from_values ("sysctx3", "appctx3");
314 ctx_list = g_list_append (ctx_list,ctx1);
315 ctx_list = g_list_append (ctx_list,ctx2);
316 ctx_list = g_list_append (ctx_list,ctx3);
317 fail_unless (gsignond_identity_info_set_access_control_list (
318 identity, ctx_list) == TRUE);
320 list = gsignond_identity_info_get_access_control_list (identity);
321 fail_if (list == NULL);
322 list2 = g_list_nth (list, 0);
323 ctx = (GSignondSecurityContext *) list2->data;
324 fail_unless (gsignond_security_context_compare (ctx, ctx1) == 0);
325 list2 = g_list_nth (list, 1);
326 ctx = (GSignondSecurityContext *) list2->data;
327 fail_unless (gsignond_security_context_compare (ctx, ctx2) == 0);
328 list2 = g_list_nth (list, 2);
329 ctx = (GSignondSecurityContext *) list2->data;
330 fail_unless (gsignond_security_context_compare (ctx, ctx3) == 0);
331 gsignond_security_context_list_free (list); list = NULL;
334 fail_unless (gsignond_identity_info_set_owner (
335 identity, ctx1) == TRUE);
336 ctx = gsignond_identity_info_get_owner (identity);
337 fail_if (ctx == NULL);
338 fail_unless (gsignond_security_context_compare (ctx, ctx1) == 0);
339 gsignond_security_context_free (ctx); ctx = NULL;
341 fail_unless (gsignond_identity_info_set_validated (
342 identity, FALSE) == TRUE);
344 fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
346 fail_unless (gsignond_identity_info_set_identity_type (
347 identity, type) == TRUE);
349 fail_unless (type == gsignond_identity_info_get_identity_type (identity));
352 identity2 = gsignond_identity_info_copy (identity);
353 fail_if (identity2 == NULL);
354 fail_unless (gsignond_identity_info_compare (identity, identity2) == TRUE);
355 gsignond_identity_info_unref (identity2);
356 fail_unless (gsignond_identity_info_compare (identity, identity) == TRUE);
358 gsignond_security_context_list_free (ctx_list); ctx_list = NULL;
360 gsignond_identity_info_unref (identity);
364 START_TEST (test_is_host_in_domain)
366 fail_unless(gsignond_is_host_in_domain("somehost", "") == TRUE);
367 fail_unless(gsignond_is_host_in_domain("", "somedomain") == FALSE);
368 fail_unless(gsignond_is_host_in_domain("", "") == TRUE);
369 fail_unless(gsignond_is_host_in_domain("somehost", "otherdomain") == FALSE);
370 fail_unless(gsignond_is_host_in_domain("somehost", "somehost") == TRUE);
371 fail_unless(gsignond_is_host_in_domain("somehost.com", "otherdomain.com") == FALSE);
372 fail_unless(gsignond_is_host_in_domain("somehost.com", "othersomehost.com") == FALSE);
373 fail_unless(gsignond_is_host_in_domain("somehost.com", "host.com") == FALSE);
374 fail_unless(gsignond_is_host_in_domain("somehost.com", "somehost.com") == TRUE);
375 fail_unless(gsignond_is_host_in_domain("somehost.com", "subhost.somehost.com") == FALSE);
376 fail_unless(gsignond_is_host_in_domain("somehost.somedomain.com", "otherdomain.com") == FALSE);
377 fail_unless(gsignond_is_host_in_domain("somehost.somedomain.com", "somehost.otherdomain.com") == FALSE);
378 fail_unless(gsignond_is_host_in_domain("somehost.somedomain.com", "somedomain.com") == TRUE);
382 Suite* common_suite (void)
384 Suite *s = suite_create ("Common library");
387 TCase *tc_core = tcase_create ("Tests");
388 tcase_add_test (tc_core, test_identity_info);
389 tcase_add_test (tc_core, test_pipe_stream);
390 tcase_add_test (tc_core, test_session_data);
391 tcase_add_test (tc_core, test_plugin_loader);
392 tcase_add_test (tc_core, test_is_host_in_domain);
393 suite_add_tcase (s, tc_core);
401 #if !GLIB_CHECK_VERSION (2, 36, 0)
405 Suite *s = common_suite();
406 SRunner *sr = srunner_create(s);
407 srunner_run_all(sr, CK_NORMAL);
408 number_failed = srunner_ntests_failed(sr);
410 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;