1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2019 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSequencer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <glib.h>
21 #include <glib-object.h>
22 
23 #include <ags/libags.h>
24 
25 #include <CUnit/CUnit.h>
26 #include <CUnit/Automated.h>
27 #include <CUnit/Basic.h>
28 
29 int ags_xml_authentication_test_init_suite();
30 int ags_xml_authentication_test_clean_suite();
31 
32 void ags_xml_authentication_test_get_authentication_module();
33 void ags_xml_authentication_test_login();
34 void ags_xml_authentication_test_logout();
35 void ags_xml_authentication_test_generate_token();
36 void ags_xml_authentication_test_get_digest();
37 void ags_xml_authentication_test_is_session_active();
38 void ags_xml_authentication_test_open_filename();
39 void ags_xml_authentication_test_find_user_uuid();
40 
41 #define AGS_XML_AUTHENTICATION_TEST_LOGIN_DEFAULT_LOGIN "ags-test-user"
42 #define AGS_XML_AUTHENTICATION_TEST_LOGIN_DEFAULT_PASSWORD "ags-test-password"
43 
44 #define AGS_XML_AUTHENTICATION_TEST_LOGOUT_DEFAULT_LOGIN "ags-test-user"
45 #define AGS_XML_AUTHENTICATION_TEST_LOGOUT_DEFAULT_SECURITY_TOKEN "ags-test-security-token"
46 
47 #define AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_REALM "ags-test-realm"
48 #define AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_LOGIN "ags-test-user"
49 #define AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_SECURITY_TOKEN "ags-test-security-token"
50 
51 #define AGS_XML_AUTHENTICATION_TEST_IS_SESSION_ACTIVE_DEFAULT_LOGIN "ags-test-user"
52 #define AGS_XML_AUTHENTICATION_TEST_IS_SESSION_ACTIVE_DEFAULT_SECURITY_TOKEN "ags-test-security-token"
53 
54 #define AGS_XML_AUTHENTICATION_TEST_OPEN_FILENAME SRCDIR "/" "ags_authentication_test.xml"
55 
56 AgsServerApplicationContext *server_application_context;
57 
58 /* The suite initialization time.
59  * Opens the temporary file used by the tests.
60  * Returns zero on success, non-zero otherwise.
61  */
62 int
ags_xml_authentication_test_init_suite()63 ags_xml_authentication_test_init_suite()
64 {
65   AgsConfig *config;
66 
67   ags_priority_load_defaults(ags_priority_get_instance());
68 
69   config = ags_config_get_instance();
70 
71   server_application_context = (AgsApplicationContext *) ags_server_application_context_new();
72   g_object_ref(server_application_context);
73 
74   ags_application_context_prepare(server_application_context);
75   ags_application_context_setup(server_application_context);
76 
77   return(0);
78 }
79 
80 /* The suite cleanup time.
81  * Closes the temporary file used by the tests.
82  * Returns zero on success, non-zero otherwise.
83  */
84 int
ags_xml_authentication_test_clean_suite()85 ags_xml_authentication_test_clean_suite()
86 {
87   return(0);
88 }
89 
90 void
ags_xml_authentication_test_get_authentication_module()91 ags_xml_authentication_test_get_authentication_module()
92 {
93   AgsXmlAuthentication *xml_authentication;
94 
95   gchar **authentication_module;
96   gchar **iter;
97 
98   guint i;
99 
100   xml_authentication = ags_xml_authentication_new();
101 
102   authentication_module = ags_authentication_get_authentication_module(AGS_AUTHENTICATION(xml_authentication));
103 
104   CU_ASSERT(authentication_module != NULL);
105 
106   i = 0;
107 
108   if(authentication_module != NULL){
109     iter = authentication_module;
110 
111     for(; iter[0] != NULL; i++, iter++);
112   }
113 
114   CU_ASSERT(i > 0);
115 }
116 
117 void
ags_xml_authentication_test_login()118 ags_xml_authentication_test_login()
119 {
120   AgsXmlAuthentication *xml_authentication;
121 
122   gchar *user_uuid, *security_token;
123 
124   gboolean success;
125 
126   GError *error;
127 
128   xml_authentication = ags_xml_authentication_new();
129 
130   error = NULL;
131   success = ags_authentication_login(AGS_AUTHENTICATION(xml_authentication),
132 				     AGS_XML_AUTHENTICATION_TEST_LOGIN_DEFAULT_LOGIN, AGS_XML_AUTHENTICATION_TEST_LOGIN_DEFAULT_PASSWORD,
133 				     &user_uuid, &security_token,
134 				     &error);
135 
136   CU_ASSERT(success == FALSE);
137   CU_ASSERT(user_uuid == NULL);
138   CU_ASSERT(security_token == NULL);
139 
140   //TODO:JK: implement me
141 }
142 
143 void
ags_xml_authentication_test_logout()144 ags_xml_authentication_test_logout()
145 {
146   AgsXmlAuthentication *xml_authentication;
147   AgsSecurityContext *security_context;
148 
149   gboolean success;
150 
151   GError *error;
152 
153   xml_authentication = ags_xml_authentication_new();
154 
155   security_context = ags_auth_security_context_get_instance();
156 
157   error = NULL;
158   success = ags_authentication_logout(AGS_AUTHENTICATION(xml_authentication),
159 				      security_context,
160 				      AGS_XML_AUTHENTICATION_TEST_LOGOUT_DEFAULT_LOGIN, AGS_XML_AUTHENTICATION_TEST_LOGOUT_DEFAULT_SECURITY_TOKEN,
161 				      &error);
162 
163   CU_ASSERT(success == FALSE);
164 
165   //TODO:JK: implement me
166 }
167 
168 void
ags_xml_authentication_test_generate_token()169 ags_xml_authentication_test_generate_token()
170 {
171   AgsXmlAuthentication *xml_authentication;
172 
173   gchar *security_token;
174 
175   GError *error;
176 
177   xml_authentication = ags_xml_authentication_new();
178 
179   error = NULL;
180   security_token = ags_authentication_generate_token(AGS_AUTHENTICATION(xml_authentication),
181 						     &error);
182 
183   CU_ASSERT(security_token != NULL);
184 
185   g_free(security_token);
186 }
187 
188 void
ags_xml_authentication_test_get_digest()189 ags_xml_authentication_test_get_digest()
190 {
191   AgsXmlAuthentication *xml_authentication;
192 
193   gchar *digest;
194 
195   GError *error;
196 
197   xml_authentication = ags_xml_authentication_new();
198 
199   error = NULL;
200   digest = ags_authentication_get_digest(AGS_AUTHENTICATION(xml_authentication),
201 					 AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_REALM,
202 					 AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_LOGIN,
203 					 AGS_XML_AUTHENTICATION_TEST_GET_DIGEST_DEFAULT_SECURITY_TOKEN,
204 					 &error);
205 
206   CU_ASSERT(digest == NULL);
207 
208   //TODO:JK: implement me
209 }
210 
211 void
ags_xml_authentication_test_is_session_active()212 ags_xml_authentication_test_is_session_active()
213 {
214   AgsXmlAuthentication *xml_authentication;
215   AgsSecurityContext *security_context;
216 
217   gboolean success;
218 
219   GError *error;
220 
221   xml_authentication = ags_xml_authentication_new();
222 
223   security_context = ags_auth_security_context_get_instance();
224 
225   error = NULL;
226   success = ags_authentication_logout(AGS_AUTHENTICATION(xml_authentication),
227 				      security_context,
228 				      AGS_XML_AUTHENTICATION_TEST_IS_SESSION_ACTIVE_DEFAULT_LOGIN, AGS_XML_AUTHENTICATION_TEST_IS_SESSION_ACTIVE_DEFAULT_SECURITY_TOKEN,
229 				      &error);
230 
231   CU_ASSERT(success == FALSE);
232 
233   //TODO:JK: implement me
234 }
235 
236 void
ags_xml_authentication_test_open_filename()237 ags_xml_authentication_test_open_filename()
238 {
239   AgsXmlAuthentication *xml_authentication;
240 
241   xml_authentication = ags_xml_authentication_new();
242 
243   ags_xml_authentication_open_filename(xml_authentication,
244 				       AGS_XML_AUTHENTICATION_TEST_OPEN_FILENAME);
245 
246   CU_ASSERT(xml_authentication->filename != NULL);
247   CU_ASSERT(xml_authentication->doc != NULL);
248   CU_ASSERT(xml_authentication->root_node != NULL);
249 }
250 
251 void
ags_xml_authentication_test_find_user_uuid()252 ags_xml_authentication_test_find_user_uuid()
253 {
254   //TODO:JK: implement me
255 }
256 
257 int
main(int argc,char ** argv)258 main(int argc, char **argv)
259 {
260   CU_pSuite pSuite = NULL;
261 
262   /* initialize the CUnit test registry */
263   if(CUE_SUCCESS != CU_initialize_registry()){
264     return CU_get_error();
265   }
266 
267   /* add a suite to the registry */
268   pSuite = CU_add_suite("AgsXmlAuthenticationTest", ags_xml_authentication_test_init_suite, ags_xml_authentication_test_clean_suite);
269 
270   if(pSuite == NULL){
271     CU_cleanup_registry();
272 
273     return CU_get_error();
274   }
275 
276   /* add the tests to the suite */
277   if((CU_add_test(pSuite, "test of AgsXmlAuthentication get authentication module", ags_xml_authentication_test_get_authentication_module) == NULL) ||
278      (CU_add_test(pSuite, "test of AgsXmlAuthentication login", ags_xml_authentication_test_login) == NULL) ||
279      (CU_add_test(pSuite, "test of AgsXmlAuthentication logout", ags_xml_authentication_test_logout) == NULL) ||
280      (CU_add_test(pSuite, "test of AgsXmlAuthentication generate token", ags_xml_authentication_test_generate_token) == NULL) ||
281      (CU_add_test(pSuite, "test of AgsXmlAuthentication get digest", ags_xml_authentication_test_get_digest) == NULL) ||
282      (CU_add_test(pSuite, "test of AgsXmlAuthentication is session active", ags_xml_authentication_test_is_session_active) == NULL) ||
283      (CU_add_test(pSuite, "test of AgsXmlAuthentication open filename", ags_xml_authentication_test_open_filename) == NULL) ||
284      (CU_add_test(pSuite, "test of AgsXmlAuthentication find user UUID", ags_xml_authentication_test_find_user_uuid) == NULL)){
285     CU_cleanup_registry();
286 
287     return CU_get_error();
288   }
289 
290   /* Run all tests using the CUnit Basic interface */
291   CU_basic_set_mode(CU_BRM_VERBOSE);
292   CU_basic_run_tests();
293 
294   CU_cleanup_registry();
295 
296   return(CU_get_error());
297 }
298