Update to upstream 1.0.1
[profile/ivi/gsignond.git] / test / common / commontest.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 <unistd.h>
29 #include <fcntl.h>
30 #include <glib.h>
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"
39
40 static GSequence*
41 _sequence_new (gchar *data)
42 {
43     GSequence *value = NULL;
44     value = g_sequence_new (NULL);
45     g_sequence_append (value, (guint8 *)data);
46     return value;
47 }
48
49 static gboolean
50 _compare_sequences (
51         GSequence *one,
52         GSequence *two)
53 {
54     GSequenceIter *iter1 = NULL, *iter2 = NULL;
55     gboolean equal = TRUE;
56
57     if (one == NULL && two == NULL)
58         return TRUE;
59
60     if ((one != NULL && two == NULL) ||
61         (one == NULL && two != NULL) ||
62         (g_sequence_get_length (one) != g_sequence_get_length (two)))
63         return FALSE;
64
65     if (one == two)
66         return TRUE;
67
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) {
73             equal = FALSE;
74             break;
75         }
76         iter1 = g_sequence_iter_next (iter1);
77     }
78
79     return equal;
80 }
81
82 START_TEST (test_pipe_stream)
83 {
84     GSignondPipeStream *stream = NULL;
85     gint pipefd[2];
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);
90 }
91 END_TEST
92
93 START_TEST (test_session_data)
94 {
95     GSignondSessionData* data;
96     GSignondSessionData* data_from_variant;
97     GSignondSessionData* data_from_copy;
98     GVariant* variant;
99     
100     data = gsignond_dictionary_new();
101     fail_if(data == NULL);
102     
103     fail_unless(gsignond_session_data_get_username(data) == NULL);
104     fail_unless(gsignond_session_data_get_secret(data) == NULL);
105
106     gsignond_session_data_set_username(data, "megauser");
107     gsignond_session_data_set_secret(data, "megapassword");
108     
109     fail_unless(g_strcmp0(gsignond_session_data_get_username(data), 
110                           "megauser") == 0);
111     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data), 
112                           "megapassword") == 0);
113
114     gsignond_session_data_set_username(data, "usermega");
115     fail_unless(g_strcmp0(gsignond_session_data_get_username(data), 
116                           "usermega") == 0);
117     
118     data_from_copy = gsignond_dictionary_copy(data);
119     fail_if(data_from_copy == NULL);
120
121     fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_copy), 
122                           "usermega") == 0);
123     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_copy), 
124                           "megapassword") == 0);
125
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);
130
131     fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_variant), 
132                           "usermega") == 0);
133     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_variant), 
134                           "megapassword") == 0);
135     
136     g_variant_unref(variant);
137     gsignond_dictionary_unref(data_from_variant);
138     gsignond_dictionary_unref(data_from_copy);
139     gsignond_dictionary_unref(data);
140 }
141 END_TEST
142
143 static void check_plugin(GSignondPlugin* plugin)
144 {
145     gchar* type;
146     gchar** mechanisms;
147
148     fail_if(plugin == NULL);
149     
150     g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
151     
152     fail_unless(g_strcmp0(type, "password") == 0);
153     fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
154     fail_unless(mechanisms[1] == NULL);
155     
156     g_free(type);
157     g_strfreev(mechanisms);
158 }
159
160 START_TEST (test_plugin_loader)
161 {
162     GSignondConfig* config = gsignond_config_new();
163     fail_if(config == NULL);
164
165     GSignondPlugin* absent_plugin = gsignond_load_plugin(config, "absentplugin");
166     fail_if(absent_plugin != NULL);
167     
168     GSignondPlugin* plugin = gsignond_load_plugin(config, "password");
169     check_plugin(plugin);    
170     
171     g_object_unref(plugin);
172     g_object_unref(config);
173 }
174 END_TEST
175
176 START_TEST (test_identity_info)
177 {
178     guint32 id = 125;
179     guint32 type = 456;
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;
189     GList *list2;
190
191     identity = gsignond_identity_info_new ();
192     fail_if (identity == NULL);
193
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 (
198                 identity) == FALSE);
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 (
207                 identity) == NULL);
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);
211
212     fail_unless (gsignond_identity_info_set_id (identity, id) == TRUE);
213
214     fail_unless (id == gsignond_identity_info_get_id (identity));
215
216     fail_unless (gsignond_identity_info_set_identity_new (identity) == TRUE);
217
218     fail_unless (gsignond_identity_info_get_is_identity_new (
219                 identity) == TRUE);
220
221     fail_unless (gsignond_identity_info_set_username (
222                 identity, NULL) == TRUE);
223
224     fail_unless (gsignond_identity_info_get_username (identity) == NULL);
225
226     fail_unless (gsignond_identity_info_set_username (
227                 identity, username) == TRUE);
228
229     fail_unless (g_strcmp0 (username, gsignond_identity_info_get_username (
230                     identity)) == 0);
231
232     fail_unless (gsignond_identity_info_set_username_secret (
233                 identity, TRUE) == TRUE);
234
235     fail_unless (gsignond_identity_info_get_is_username_secret (
236                 identity) == TRUE);
237
238     fail_unless (gsignond_identity_info_set_secret (identity, NULL) == TRUE);
239
240     fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
241
242     fail_unless (gsignond_identity_info_set_secret (identity, secret) == TRUE);
243
244     fail_unless (g_strcmp0 (secret, gsignond_identity_info_get_secret (
245                     identity)) == 0);
246
247     fail_unless (gsignond_identity_info_set_store_secret (
248                 identity, TRUE) == TRUE);
249
250     fail_unless (gsignond_identity_info_get_store_secret (
251                 identity) == TRUE);
252
253     fail_unless (gsignond_identity_info_set_caption (identity, NULL) == TRUE);
254
255     fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
256
257     fail_unless (gsignond_identity_info_set_caption (
258                 identity, caption) == TRUE);
259
260     fail_unless (g_strcmp0 (caption, gsignond_identity_info_get_caption (
261                     identity)) == 0);
262
263     /*realms*/
264     seq_realms = _sequence_new("realms1");
265     fail_unless (gsignond_identity_info_set_realms (
266                 identity, seq_realms) == TRUE);
267
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);
273
274     /*methods*/
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);
288
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);
296
297     fail_unless (gsignond_identity_info_get_mechanisms (
298                 identity, "method20") == NULL);
299
300     mechs = gsignond_identity_info_get_mechanisms (
301             identity, "method1");
302     fail_if (mechs == NULL);
303     g_sequence_free (mechs);
304
305     fail_unless (gsignond_identity_info_remove_method (
306                 identity, "method20") == FALSE);
307     fail_unless (gsignond_identity_info_remove_method (
308                 identity, "method4") == TRUE);
309
310     /*acl*/
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);
319
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;
332
333     /*owners*/
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;
340
341     fail_unless (gsignond_identity_info_set_validated (
342                 identity, FALSE) == TRUE);
343
344     fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
345
346     fail_unless (gsignond_identity_info_set_identity_type (
347                 identity, type) == TRUE);
348
349     fail_unless (type == gsignond_identity_info_get_identity_type (identity));
350
351     /*copy*/
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);
357
358     gsignond_security_context_list_free (ctx_list); ctx_list = NULL;
359
360     gsignond_identity_info_unref (identity);
361 }
362 END_TEST
363
364 START_TEST (test_is_host_in_domain)
365 {
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);
379 }
380 END_TEST
381
382 Suite* common_suite (void)
383 {
384     Suite *s = suite_create ("Common library");
385     
386     /* Core test case */
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);
394     return s;
395 }
396
397 int main (void)
398 {
399     int number_failed;
400
401 #if !GLIB_CHECK_VERSION (2, 36, 0)
402     g_type_init ();
403 #endif
404
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);
409     srunner_free(sr);
410     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
411 }
412