81028f79ac4a8016916f17b4053d77bbfe90a528
[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-unix.h>
31 #include "gsignond/gsignond-session-data.h"
32 #include "gsignond/gsignond-error.h"
33 #include "gsignond/gsignond-log.h"
34 #include "common/gsignond-pipe-stream.h"
35 #include "common/gsignond-plugin-loader.h"
36
37 START_TEST (test_pipe_stream)
38 {
39     GSignondPipeStream *stream = NULL;
40     gint pipefd[2];
41     fail_unless (pipe (pipefd) == 0);
42     stream = gsignond_pipe_stream_new (pipefd[0], pipefd[1], TRUE);
43     fail_if (stream == NULL);
44     g_object_unref (stream);
45 }
46 END_TEST
47
48 START_TEST (test_session_data)
49 {
50     GSignondSessionData* data;
51     GSignondSessionData* data_from_variant;
52     GSignondSessionData* data_from_copy;
53     GVariant* variant;
54     
55     data = gsignond_dictionary_new();
56     fail_if(data == NULL);
57     
58     fail_unless(gsignond_session_data_get_username(data) == NULL);
59     fail_unless(gsignond_session_data_get_secret(data) == NULL);
60
61     gsignond_session_data_set_username(data, "megauser");
62     gsignond_session_data_set_secret(data, "megapassword");
63     
64     fail_unless(g_strcmp0(gsignond_session_data_get_username(data), 
65                           "megauser") == 0);
66     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data), 
67                           "megapassword") == 0);
68
69     gsignond_session_data_set_username(data, "usermega");
70     fail_unless(g_strcmp0(gsignond_session_data_get_username(data), 
71                           "usermega") == 0);
72     
73     data_from_copy = gsignond_dictionary_copy(data);
74     fail_if(data_from_copy == NULL);
75
76     fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_copy), 
77                           "usermega") == 0);
78     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_copy), 
79                           "megapassword") == 0);
80
81     variant = gsignond_dictionary_to_variant(data);
82     fail_if(variant == NULL);
83     data_from_variant = gsignond_dictionary_new_from_variant(variant);
84     fail_if(data_from_variant == NULL);
85
86     fail_unless(g_strcmp0(gsignond_session_data_get_username(data_from_variant), 
87                           "usermega") == 0);
88     fail_unless(g_strcmp0(gsignond_session_data_get_secret(data_from_variant), 
89                           "megapassword") == 0);
90     
91     g_variant_unref(variant);
92     gsignond_dictionary_unref(data_from_variant);
93     gsignond_dictionary_unref(data_from_copy);
94     gsignond_dictionary_unref(data);
95 }
96 END_TEST
97
98 static void check_plugin(GSignondPlugin* plugin)
99 {
100     gchar* type;
101     gchar** mechanisms;
102
103     fail_if(plugin == NULL);
104     
105     g_object_get(plugin, "type", &type, "mechanisms", &mechanisms, NULL);
106     
107     fail_unless(g_strcmp0(type, "password") == 0);
108     fail_unless(g_strcmp0(mechanisms[0], "password") == 0);
109     fail_unless(mechanisms[1] == NULL);
110     
111     g_free(type);
112     g_strfreev(mechanisms);
113 }
114
115 START_TEST (test_plugin_loader)
116 {
117     GSignondConfig* config = gsignond_config_new();
118     fail_if(config == NULL);
119
120     GSignondPlugin* absent_plugin = gsignond_load_plugin(config, "absentplugin");
121     fail_if(absent_plugin != NULL);
122     
123     GSignondPlugin* plugin = gsignond_load_plugin(config, "password");
124     check_plugin(plugin);    
125     
126     g_object_unref(plugin);
127     g_object_unref(config);
128 }
129 END_TEST
130
131 Suite* common_suite (void)
132 {
133     Suite *s = suite_create ("Common library");
134     
135     /* Core test case */
136     TCase *tc_core = tcase_create ("Tests");
137     tcase_add_test (tc_core, test_pipe_stream);
138     tcase_add_test (tc_core, test_session_data);
139     tcase_add_test (tc_core, test_plugin_loader);
140     suite_add_tcase (s, tc_core);
141     return s;
142 }
143
144 int main (void)
145 {
146     int number_failed;
147
148 #if !GLIB_CHECK_VERSION (2, 36, 0)
149     g_type_init ();
150 #endif
151
152     Suite *s = common_suite();
153     SRunner *sr = srunner_create(s);
154     srunner_run_all(sr, CK_NORMAL);
155     number_failed = srunner_ntests_failed(sr);
156     srunner_free(sr);
157     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
158 }
159