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.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <glib/gstdio.h>
31 #include "gsignond/gsignond-config.h"
32 #include "gsignond/gsignond-log.h"
33 #include "gsignond/gsignond-credentials.h"
34 #include "gsignond/gsignond-secret-storage.h"
35 #include "common/db/gsignond-db-error.h"
36 #include "common/db/gsignond-db-secret-database.h"
37 #include "common/db/gsignond-db-sql-database.h"
38 #include "daemon/gsignond-daemon.h"
39 #include "daemon/db/gsignond-db-metadata-database.h"
40 #include "daemon/db/gsignond-db-credentials-database.h"
43 _sequence_new (gchar *data)
45 GSequence *value = NULL;
46 value = g_sequence_new (NULL);
47 g_sequence_append (value, (guint8 *)data);
62 GVariant *value2 = (GVariant *)g_hash_table_lookup (user_data->table, key);
64 if (value2 && g_variant_get_size (value2) == g_variant_get_size (value2)
65 && memcmp (g_variant_get_data (value2),
66 g_variant_get_data(value),
67 g_variant_get_size(value2)) == 0) {
70 user_data->status = 0;
78 GSequenceIter *iter1 = NULL, *iter2 = NULL;
79 gboolean equal = TRUE;
81 if (one == NULL && two == NULL)
84 if ((one != NULL && two == NULL) ||
85 (one == NULL && two != NULL) ||
86 (g_sequence_get_length (one) != g_sequence_get_length (two)))
92 iter1 = g_sequence_get_begin_iter (one);
93 while (!g_sequence_iter_is_end (iter1)) {
94 iter2 = g_sequence_get_iter_at_pos (two,
95 g_sequence_iter_get_position (iter1));
96 if (g_strcmp0 (g_sequence_get (iter1), g_sequence_get (iter2)) != 0) {
100 iter1 = g_sequence_iter_next (iter1);
106 static GSignondIdentityInfo *
107 _get_filled_identity_info_2 (
108 GSignondIdentityInfo **identity_inp,
110 gboolean add_methods,
116 const gchar *username = "username1";
117 const gchar *secret = "secret1";
118 const gchar *caption = "caption1";
119 GSignondIdentityInfo *identity = NULL;
120 GSignondSecurityContextList *ctx_list = NULL;
121 GSignondSecurityContext *ctx1, *ctx2, *ctx3 ;
122 GHashTable *methods = NULL;
123 GSequence *seq1 = NULL, *seq_realms;
124 identity = *identity_inp;
126 if (identity == NULL)
127 identity = gsignond_identity_info_new ();
128 gsignond_identity_info_set_identity_new (identity);
129 gsignond_identity_info_set_secret (identity, secret);
130 gsignond_identity_info_set_store_secret (identity, TRUE);
132 gsignond_identity_info_set_username (identity, username);
133 gsignond_identity_info_set_username_secret (identity, TRUE);
134 gsignond_identity_info_set_caption (identity, caption);
139 seq_realms = _sequence_new("realms1");
140 gsignond_identity_info_set_realms (identity, seq_realms);
141 g_sequence_free (seq_realms);
146 methods = g_hash_table_new_full ((GHashFunc)g_str_hash,
147 (GEqualFunc)g_str_equal,
148 (GDestroyNotify)NULL,
149 (GDestroyNotify)g_sequence_free);
150 seq1 = _sequence_new("mech11"); g_sequence_append (seq1, "mech12");
151 g_hash_table_insert (methods, "method1", seq1);
152 g_hash_table_insert (methods, "method2", _sequence_new("mech21"));
153 g_hash_table_insert (methods, "method3", _sequence_new("mech31"));
154 gsignond_identity_info_set_methods (identity, methods);
155 g_hash_table_unref (methods);
159 ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
160 ctx2 = gsignond_security_context_new_from_values ("sysctx2", "appctx2");
161 ctx3 = gsignond_security_context_new_from_values ("sysctx3", "appctx3");
162 ctx_list = g_list_append (ctx_list,ctx1);
163 ctx_list = g_list_append (ctx_list,ctx2);
164 ctx_list = g_list_append (ctx_list,ctx3);
166 gsignond_identity_info_set_access_control_list (identity, ctx_list);
171 gsignond_identity_info_set_owner (identity, ctx1);
173 gsignond_security_context_list_free (ctx_list);
175 gsignond_identity_info_set_validated (identity, FALSE);
176 gsignond_identity_info_set_identity_type (identity, type);
180 static GSignondIdentityInfo *
181 _get_filled_identity_info (void)
183 GSignondIdentityInfo *identity = NULL;
184 return _get_filled_identity_info_2 (&identity,
185 TRUE, TRUE, TRUE, TRUE, TRUE);
188 START_TEST (test_identity_info)
192 const gchar *username = "username1";
193 const gchar *secret = "secret1";
194 const gchar *caption = "caption1";
195 GSignondIdentityInfo *identity = NULL;
196 GSignondIdentityInfo *identity2 = NULL;
197 GSignondSecurityContextList *ctx_list = NULL, *list = NULL;
198 GSignondSecurityContext *ctx, *ctx1, *ctx2, *ctx3 ;
199 GHashTable *methods = NULL, *methods2;
200 GSequence *seq1 = NULL, *seq_realms, *seq21, *mechs;
203 identity = gsignond_identity_info_new ();
204 fail_if (identity == NULL);
206 fail_unless (gsignond_identity_info_get_id (identity) == 0);
207 fail_unless (gsignond_identity_info_get_is_identity_new (identity)== TRUE);
208 fail_unless (gsignond_identity_info_get_username (identity) == NULL);
209 fail_unless (gsignond_identity_info_get_is_username_secret (
211 fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
212 fail_unless (gsignond_identity_info_get_store_secret (identity) == FALSE);
213 fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
214 fail_unless (gsignond_identity_info_get_realms (identity) == NULL);
215 fail_unless (gsignond_identity_info_get_methods (identity) == NULL);
216 fail_unless (gsignond_identity_info_get_mechanisms (
217 identity, "testmech") == NULL);
218 fail_unless (gsignond_identity_info_get_access_control_list (
220 fail_unless (gsignond_identity_info_get_owner (identity) == NULL);
221 fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
222 fail_unless (gsignond_identity_info_get_identity_type (identity) == -1);
224 fail_unless (gsignond_identity_info_set_id (identity, id) == TRUE);
226 fail_unless (id == gsignond_identity_info_get_id (identity));
228 fail_unless (gsignond_identity_info_set_identity_new (identity) == TRUE);
230 fail_unless (gsignond_identity_info_get_is_identity_new (
233 fail_unless (gsignond_identity_info_set_username (
234 identity, NULL) == FALSE);
236 fail_unless (gsignond_identity_info_get_username (identity) == NULL);
238 fail_unless (gsignond_identity_info_set_username (
239 identity, username) == TRUE);
241 fail_unless (g_strcmp0 (username, gsignond_identity_info_get_username (
244 fail_unless (gsignond_identity_info_set_username_secret (
245 identity, TRUE) == TRUE);
247 fail_unless (gsignond_identity_info_get_is_username_secret (
250 fail_unless (gsignond_identity_info_set_secret (identity, NULL) == FALSE);
252 fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
254 fail_unless (gsignond_identity_info_set_secret (identity, secret) == TRUE);
256 fail_unless (g_strcmp0 (secret, gsignond_identity_info_get_secret (
259 fail_unless (gsignond_identity_info_set_store_secret (
260 identity, TRUE) == TRUE);
262 fail_unless (gsignond_identity_info_get_store_secret (
265 fail_unless (gsignond_identity_info_set_caption (identity, NULL) == FALSE);
267 fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
269 fail_unless (gsignond_identity_info_set_caption (
270 identity, caption) == TRUE);
272 fail_unless (g_strcmp0 (caption, gsignond_identity_info_get_caption (
276 seq_realms = _sequence_new("realms1");
277 fail_unless (gsignond_identity_info_set_realms (
278 identity, seq_realms) == TRUE);
280 seq1 = gsignond_identity_info_get_realms (identity);
281 fail_if (seq1 == NULL);
282 fail_unless (_compare_sequences (seq1, seq_realms) == TRUE);
283 g_sequence_free (seq1); seq1 = NULL;
284 g_sequence_free (seq_realms);
287 methods = g_hash_table_new_full ((GHashFunc)g_str_hash,
288 (GEqualFunc)g_str_equal,
289 (GDestroyNotify)NULL,
290 (GDestroyNotify)g_sequence_free);
291 seq1 = _sequence_new("mech11"); g_sequence_append (seq1, "mech12");
292 fail_unless (gsignond_identity_info_set_methods (
293 identity, methods) == TRUE);
294 g_hash_table_insert (methods, "method1", seq1);
295 g_hash_table_insert (methods, "method2", _sequence_new("mech21"));
296 g_hash_table_insert (methods, "method3", _sequence_new("mech31"));
297 g_hash_table_insert (methods, "method4", _sequence_new("mech41"));
298 fail_unless (gsignond_identity_info_set_methods (
299 identity, methods) == TRUE);
301 methods2 = gsignond_identity_info_get_methods (identity);
302 fail_if (methods2 == NULL);
303 seq21 = g_hash_table_lookup (methods, "method1");
304 fail_if (seq21 == NULL);
305 fail_unless (_compare_sequences (seq1, seq21) == TRUE);
306 g_hash_table_unref (methods2);
307 g_hash_table_unref (methods);
309 fail_unless (gsignond_identity_info_get_mechanisms (
310 identity, "method20") == NULL);
312 mechs = gsignond_identity_info_get_mechanisms (
313 identity, "method1");
314 fail_if (mechs == NULL);
315 g_sequence_free (mechs);
317 fail_unless (gsignond_identity_info_remove_method (
318 identity, "method20") == FALSE);
319 fail_unless (gsignond_identity_info_remove_method (
320 identity, "method4") == TRUE);
323 ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
324 ctx2 = gsignond_security_context_new_from_values ("sysctx2", "appctx2");
325 ctx3 = gsignond_security_context_new_from_values ("sysctx3", "appctx3");
326 ctx_list = g_list_append (ctx_list,ctx1);
327 ctx_list = g_list_append (ctx_list,ctx2);
328 ctx_list = g_list_append (ctx_list,ctx3);
329 fail_unless (gsignond_identity_info_set_access_control_list (
330 identity, ctx_list) == TRUE);
332 list = gsignond_identity_info_get_access_control_list (identity);
333 fail_if (list == NULL);
334 list2 = g_list_nth (list, 0);
335 ctx = (GSignondSecurityContext *) list2->data;
336 fail_unless (gsignond_security_context_compare (ctx, ctx1) == 0);
337 list2 = g_list_nth (list, 1);
338 ctx = (GSignondSecurityContext *) list2->data;
339 fail_unless (gsignond_security_context_compare (ctx, ctx2) == 0);
340 list2 = g_list_nth (list, 2);
341 ctx = (GSignondSecurityContext *) list2->data;
342 fail_unless (gsignond_security_context_compare (ctx, ctx3) == 0);
343 gsignond_security_context_list_free (list); list = NULL;
346 fail_unless (gsignond_identity_info_set_owner (
347 identity, ctx1) == TRUE);
348 ctx = gsignond_identity_info_get_owner (identity);
349 fail_if (ctx == NULL);
350 fail_unless (gsignond_security_context_compare (ctx, ctx1) == 0);
351 gsignond_security_context_free (ctx); ctx = NULL;
353 fail_unless (gsignond_identity_info_set_validated (
354 identity, FALSE) == TRUE);
356 fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
358 fail_unless (gsignond_identity_info_set_identity_type (
359 identity, type) == TRUE);
361 fail_unless (type == gsignond_identity_info_get_identity_type (identity));
364 identity2 = gsignond_dictionary_copy (identity);
365 fail_if (identity2 == NULL);
366 fail_unless (gsignond_identity_info_compare (identity, identity2) == TRUE);
367 gsignond_identity_info_unref (identity2);
368 fail_unless (gsignond_identity_info_compare (identity, identity) == TRUE);
370 gsignond_security_context_list_free (ctx_list); ctx_list = NULL;
372 gsignond_identity_info_unref (identity);
377 _gsignond_query_read_int (
381 *status = sqlite3_column_int (stmt, 0);
385 _gsignond_query_read_string (
389 const gchar* str = NULL;
391 str = (const gchar *)sqlite3_column_text (stmt, 0);
392 if (str && strlen(str) > 0 &&
393 g_strcmp0 (str, "username1") == 0) {
399 START_TEST (test_sql_database)
401 GSignondDbSecretDatabase *database = NULL;
402 GSignondConfig *config = NULL;
403 gchar *filename = NULL;
404 const gchar *dir = NULL;
405 GSignondCredentials *creds = NULL;
406 guint32 id = 1, method = 2;
407 GHashTable *data = NULL;
408 GSignondDictionary *data2 = NULL;
410 sqlite3_stmt *stmt = NULL;
413 GHashTable* hashtable = NULL;
414 GArray *array = NULL;
415 GSignondDbSqlDatabase *sqldb = NULL;
416 GError *error = NULL;
419 database = gsignond_db_secret_database_new ();
420 fail_if (database == NULL);
421 sqldb = GSIGNOND_DB_SQL_DATABASE (database);
423 fail_unless (gsignond_db_sql_database_clear (sqldb) == FALSE);
424 fail_unless (gsignond_db_sql_database_is_open (sqldb) == FALSE);
425 fail_unless (gsignond_db_secret_database_load_credentials (
426 database, 1) == NULL);
427 fail_unless (gsignond_db_secret_database_update_credentials (
428 database, NULL) == FALSE);
429 fail_unless (gsignond_db_secret_database_remove_credentials (
430 database, 1) == FALSE);
431 fail_unless (gsignond_db_secret_database_load_data (
432 database, 1, 2) == NULL);
433 fail_unless (gsignond_db_secret_database_update_data (
434 database, 1, 2, NULL) == FALSE);
435 fail_unless (gsignond_db_secret_database_remove_data (
436 database, 1, 2) == FALSE);
438 config = gsignond_config_new ();
439 dir = gsignond_config_get_string (config,
440 GSIGNOND_CONFIG_GENERAL_STORAGE_PATH);
442 dir = g_get_user_data_dir ();
444 g_mkdir_with_parents (dir, S_IRWXU);
445 filename = g_build_filename (dir, "sql_db_test.db", NULL);
446 fail_unless (gsignond_db_sql_database_open (sqldb, filename,
447 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) == TRUE);
448 /* don't open the db again if its already open */
449 fail_unless (gsignond_db_sql_database_open (sqldb, filename,
450 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) == TRUE);
452 g_object_unref(config);
454 creds = gsignond_credentials_new ();
455 fail_if (creds == NULL);
457 fail_unless (gsignond_credentials_set_data (
458 creds, id, "user 1", "pass 1") == TRUE);
460 fail_unless (gsignond_db_secret_database_update_credentials (
461 database, creds) == TRUE);
462 g_object_unref (creds); creds = NULL;
464 creds = gsignond_db_secret_database_load_credentials (database, id);
465 fail_if (creds == NULL);
466 g_object_unref (creds);
468 /* remove the added credentials */
469 fail_unless (gsignond_db_secret_database_remove_credentials (
470 database, id) == TRUE);
472 /* add data to store */
473 data = g_hash_table_new_full ((GHashFunc)g_str_hash,
474 (GEqualFunc)g_str_equal,
475 (GDestroyNotify)NULL,
476 (GDestroyNotify)g_variant_unref);
477 fail_if (data == NULL);
479 g_hash_table_insert (data,"key1",g_variant_new_string ("string_value"));
480 g_hash_table_insert (data,"key2",g_variant_new_double (12223.4223));
481 g_hash_table_insert (data,"key3",g_variant_new_uint16(20));
482 g_hash_table_insert (data,"key4",g_variant_new("^ay", "byte_value"));
483 fail_unless (gsignond_db_secret_database_update_data (
484 database, id, method, data) == TRUE);
485 data2 = gsignond_db_secret_database_load_data (database, id, method);
486 fail_if (data2 == NULL);
489 g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
490 fail_if (input.status != 1);
492 gsignond_dictionary_unref (data2);
493 g_hash_table_unref(data);
496 /*sql database tests*/
497 fail_unless (gsignond_db_sql_database_clear (sqldb) == TRUE);
498 stmt = gsignond_db_sql_database_prepare_statement (
499 sqldb, "INSERT INTO CREDENTIALS "
500 "(id, username, password) VALUES (1, \"username1\",\"password\");");
501 fail_if (stmt == NULL);
502 fail_unless (sqlite3_finalize (stmt) == SQLITE_OK); stmt = NULL;
504 fail_unless (gsignond_db_sql_database_exec (
505 sqldb, "INSERT INTO CREDENTIALS (id, username, password) "
506 "VALUES (1, \"username1\",\"password\");") == TRUE);
508 fail_unless (gsignond_db_sql_database_exec (
509 sqldb, "INSERT INTO CREDENTIALS (id, username, password) "
510 "VALUES (2, \"username2\",\"password2\");") == TRUE);
512 fail_unless (gsignond_db_sql_database_exec (
513 sqldb, "SELECT id from CREDENTIALS limit 1;") == TRUE);
515 fail_unless (gsignond_db_sql_database_query_exec (
516 sqldb, "SELECT id from CREDENTIALS limit 1;",
517 (GSignondDbSqlDatabaseQueryCallback)_gsignond_query_read_int,
519 fail_unless (status == 1);
521 fail_unless (gsignond_db_sql_database_query_exec (
522 sqldb, "SELECT username from CREDENTIALS where id=1;",
523 (GSignondDbSqlDatabaseQueryCallback)_gsignond_query_read_string,
525 fail_unless (status == 1);
527 list = gsignond_db_sql_database_query_exec_string_list (
528 sqldb, "SELECT username from CREDENTIALS;");
529 fail_if (list == NULL);
530 fail_unless (g_list_length (list) == 2);
531 g_list_free_full (list, g_free);
533 hashtable = gsignond_db_sql_database_query_exec_string_tuple (
534 sqldb, "SELECT username, password from CREDENTIALS;");
535 fail_if (hashtable == NULL);
536 fail_unless (g_hash_table_size (hashtable) == 2);
537 g_hash_table_unref (hashtable);
539 hashtable = gsignond_db_sql_database_query_exec_int_string_tuple (
540 sqldb, "SELECT id, username from CREDENTIALS "
541 "where password=\"password2\";");
542 fail_if (hashtable == NULL);
543 fail_unless (g_hash_table_size (hashtable) == 1);
544 g_hash_table_unref (hashtable);
546 fail_unless (gsignond_db_sql_database_query_exec_int (
547 sqldb,"SELECT id from CREDENTIALS where username=\"username2\";",
549 fail_unless (status == 2);
551 array = gsignond_db_sql_database_query_exec_int_array (
552 sqldb,"SELECT id from CREDENTIALS;");
553 fail_if (array == NULL);
554 fail_unless (array->len == 2);
555 g_array_free (array, TRUE);
557 stmt = gsignond_db_sql_database_prepare_statement (
558 sqldb, "SELECT id from CREDENTIALS where username=\"username1\";");
559 fail_if (stmt == NULL);
560 fail_unless (gsignond_db_sql_database_query_exec_stmt (
561 sqldb, stmt, NULL, NULL) == 1);
564 fail_unless (gsignond_db_sql_database_start_transaction (sqldb) == TRUE);
565 fail_unless (gsignond_db_sql_database_commit_transaction (sqldb) == TRUE);
566 fail_unless (gsignond_db_sql_database_start_transaction (sqldb) == TRUE);
567 fail_unless (gsignond_db_sql_database_rollback_transaction (sqldb) == TRUE);
568 fail_unless (gsignond_db_sql_database_start_transaction (sqldb) == TRUE);
569 fail_unless (gsignond_db_sql_database_start_transaction (sqldb) == FALSE);
570 fail_unless (gsignond_db_sql_database_rollback_transaction (sqldb) == TRUE);
572 fail_unless (gsignond_db_sql_database_transaction_exec (
573 sqldb, "SELECT id from CREDENTIALS "
574 "where username=\"username1\";") == TRUE);
576 fail_unless (gsignond_db_sql_database_get_db_version (
577 sqldb, "PRAGMA user_version;") == 1);
579 error = gsignond_db_create_error(GSIGNOND_DB_ERROR_UNKNOWN,"Unknown error");
580 gsignond_db_sql_database_clear_last_error (sqldb);
581 fail_unless (gsignond_db_sql_database_get_last_error (sqldb) == NULL);
582 gsignond_db_sql_database_set_last_error (sqldb, error);
583 fail_unless (gsignond_db_sql_database_get_last_error (sqldb) != NULL);
584 gsignond_db_sql_database_clear_last_error (sqldb);
585 fail_unless (gsignond_db_sql_database_get_last_error (sqldb) == NULL);
587 fail_unless (gsignond_db_sql_database_exec (
588 sqldb, "INSERT INTO CREDENTIALS (id, username, password) "
589 "VALUES (4, \"username4\",\"password3\");") == TRUE);
590 fail_unless (gsignond_db_sql_database_get_last_insert_rowid (
593 fail_unless (gsignond_db_secret_database_remove_data (
594 database, id, method) == TRUE);
595 fail_unless (gsignond_db_sql_database_clear (sqldb) == TRUE);
596 fail_unless (gsignond_db_sql_database_close (sqldb) == TRUE);
597 g_object_unref(database);
601 START_TEST (test_secret_storage)
603 GSignondSecretStorage *storage = NULL;
604 GSignondConfig *config = NULL;
605 GSignondCredentials *creds = NULL;
606 guint32 id = 1, method = 2;
607 GHashTable *data = NULL;
608 GHashTable *data2 = NULL;
610 const gchar *dir = NULL;
612 config = gsignond_config_new ();
614 storage = g_object_new (GSIGNOND_TYPE_SECRET_STORAGE,
615 "config", config, NULL);
616 g_object_unref(config);
617 fail_if (storage == NULL);
619 dir = gsignond_config_get_string (config,
620 GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
622 dir = g_get_user_data_dir ();
624 g_mkdir_with_parents (dir, S_IRWXU);
626 fail_unless (gsignond_secret_storage_get_last_error (storage) == NULL);
627 fail_unless (gsignond_secret_storage_clear_db (storage) == FALSE);
628 fail_unless (gsignond_secret_storage_is_open_db (storage) == FALSE);
629 fail_unless (gsignond_secret_storage_load_credentials (storage, 1) == NULL);
630 fail_unless (gsignond_secret_storage_update_credentials (
631 storage, NULL) == FALSE);
632 fail_unless (gsignond_secret_storage_remove_credentials (
633 storage, 1) == FALSE);
634 fail_unless (gsignond_secret_storage_load_data (
635 storage, 1, 2) == NULL);
636 fail_unless (gsignond_secret_storage_update_data (
637 storage, 1, 2, NULL) == FALSE);
638 fail_unless (gsignond_secret_storage_remove_data (
639 storage, 1, 2) == FALSE);
641 fail_unless (gsignond_secret_storage_open_db (storage) == TRUE);
642 /* don't open the db again if its already open */
643 fail_unless (gsignond_secret_storage_open_db (storage) == TRUE);
645 creds = gsignond_credentials_new ();
646 fail_if (creds == NULL);
648 fail_unless (gsignond_credentials_set_data (
649 creds, id, "user 1", "pass 1") == TRUE);
651 fail_unless (gsignond_secret_storage_update_credentials (
652 storage, creds) == TRUE);
653 g_object_unref (creds); creds = NULL;
655 creds = gsignond_secret_storage_load_credentials (storage, id);
656 fail_if (creds == NULL);
658 fail_unless (gsignond_secret_storage_check_credentials (
659 storage, creds) == TRUE);
661 gsignond_credentials_set_id (creds, 3);
662 fail_unless (gsignond_secret_storage_check_credentials (
663 storage, creds) == FALSE);
664 g_object_unref (creds);
666 /* remove the added credentials */
667 fail_unless (gsignond_secret_storage_remove_credentials (
668 storage, id) == TRUE);
670 /* add data to store */
671 data = g_hash_table_new_full ((GHashFunc)g_str_hash,
672 (GEqualFunc)g_str_equal,
673 (GDestroyNotify)NULL,
674 (GDestroyNotify)g_variant_unref);
675 fail_if (data == NULL);
677 g_hash_table_insert (data,"key1",g_variant_new_string ("string_value"));
678 g_hash_table_insert (data,"key2",g_variant_new_double (12223.4223));
679 g_hash_table_insert (data,"key3",g_variant_new_uint16(20));
680 g_hash_table_insert (data,"key4",g_variant_new("^ay", "byte_value"));
682 fail_unless (gsignond_secret_storage_update_data (
683 storage, id, method, data) == TRUE);
684 data2 = gsignond_secret_storage_load_data (storage, id, method);
685 fail_if (data2 == NULL);
688 g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
689 fail_if (input.status != 1);
691 gsignond_dictionary_unref(data2);
692 g_hash_table_unref(data);
694 fail_unless (gsignond_secret_storage_remove_data (
695 storage, id, method) == TRUE);
696 fail_unless (gsignond_secret_storage_clear_db (storage) == TRUE);
697 fail_unless (gsignond_secret_storage_close_db (storage) == TRUE);
698 g_object_unref(storage);
702 START_TEST (test_metadata_database)
704 GSignondConfig *config = NULL;
705 guint32 methodid = 0;
706 guint32 identity_id = 5;
707 const gchar *method1 = "method1";
708 GSignondIdentityInfo *identity = NULL, *identity2= NULL;
709 GSignondIdentityInfoList *identities = NULL;
710 GSignondSecurityContext *ctx1 = NULL;
711 GList *methods = NULL, *reflist = NULL;
712 GSignondSecurityContextList *acl;
713 GSignondSecurityContext *owner = NULL;
715 config = gsignond_config_new ();
716 GSignondDbMetadataDatabase* metadata_db = NULL;
717 metadata_db = gsignond_db_metadata_database_new (config);
718 g_object_unref(config);
719 fail_if (metadata_db == NULL);
721 ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
722 identity = _get_filled_identity_info_2 (&identity,
723 FALSE, FALSE, FALSE, FALSE, FALSE);
724 fail_unless (gsignond_db_metadata_database_insert_method (
725 metadata_db, method1, &methodid) == FALSE);
726 fail_unless (gsignond_db_metadata_database_get_method_id (
727 metadata_db, method1) == 0);
728 fail_unless (gsignond_db_metadata_database_get_methods (
729 metadata_db, identity_id, ctx1) == NULL);
730 fail_unless (gsignond_db_metadata_database_get_methods (
731 metadata_db, identity_id, ctx1) == NULL);
732 fail_unless (gsignond_db_metadata_database_update_identity (
733 metadata_db, identity) == FALSE);
734 fail_unless (gsignond_db_metadata_database_get_identity (
735 metadata_db, identity_id) == NULL);
736 fail_unless (gsignond_db_metadata_database_get_identities (
737 metadata_db, NULL) == NULL);
738 fail_unless (gsignond_db_metadata_database_remove_identity (
739 metadata_db, identity_id) == FALSE);
740 fail_unless (gsignond_db_metadata_database_remove_reference (
741 metadata_db, identity_id, ctx1, "reference1") == FALSE);
742 fail_unless (gsignond_db_metadata_database_get_references (
743 metadata_db, identity_id, ctx1) == NULL);
744 fail_unless (gsignond_db_metadata_database_get_accesscontrol_list (
745 metadata_db, identity_id) == NULL);
746 fail_unless (gsignond_db_metadata_database_get_owner (
747 metadata_db, identity_id) == NULL);
749 fail_unless (gsignond_db_metadata_database_open (metadata_db) == TRUE);
751 fail_unless (gsignond_db_metadata_database_open (metadata_db) == TRUE);
753 fail_unless (gsignond_db_sql_database_clear (
754 GSIGNOND_DB_SQL_DATABASE (metadata_db)) == TRUE);
756 fail_unless (gsignond_db_metadata_database_get_accesscontrol_list (
757 metadata_db, identity_id) == NULL);
759 fail_unless (gsignond_db_metadata_database_get_owner (
760 metadata_db, identity_id) == NULL);
762 fail_unless (gsignond_db_metadata_database_get_method_id (
763 metadata_db, method1) == 0);
764 fail_unless (gsignond_db_metadata_database_insert_method (
765 metadata_db, method1, &methodid) == TRUE);
767 fail_unless (methodid == gsignond_db_metadata_database_get_method_id (
768 metadata_db, method1));
771 identity = _get_filled_identity_info_2 (&identity,
772 TRUE, FALSE, FALSE, FALSE, FALSE);
773 fail_unless (gsignond_db_metadata_database_update_identity (
774 metadata_db, identity) == 0);
776 identity = _get_filled_identity_info_2 (&identity,
777 FALSE, TRUE, FALSE, FALSE, FALSE);
778 fail_unless (gsignond_db_metadata_database_update_identity (
779 metadata_db, identity) == 0);
781 identity = _get_filled_identity_info_2 (&identity,
782 FALSE, FALSE, TRUE, FALSE, FALSE);
783 fail_unless (gsignond_db_metadata_database_update_identity (
784 metadata_db, identity) == 0);
786 identity = _get_filled_identity_info_2 (&identity,
787 FALSE, FALSE, FALSE, TRUE, FALSE);
788 fail_unless (gsignond_db_metadata_database_update_identity (
789 metadata_db, identity) == 0);
791 identity = _get_filled_identity_info_2 (&identity,
792 FALSE, FALSE, FALSE, FALSE, TRUE);
793 identity_id = gsignond_db_metadata_database_update_identity (
794 metadata_db, identity);
795 fail_unless (identity_id != 0);
796 gsignond_identity_info_set_id (identity, identity_id);
798 identity2 = gsignond_db_metadata_database_get_identity (
799 metadata_db, identity_id);
800 fail_if (identity2 == NULL);
801 gsignond_identity_info_unref (identity2);
803 /*get_identity/identities*/
804 fail_unless (gsignond_db_metadata_database_get_identity (
805 metadata_db, 2222) == NULL);
807 identities = gsignond_db_metadata_database_get_identities (metadata_db, NULL);
808 fail_unless (identities != NULL);
809 fail_unless (g_list_length (identities) == 1);
810 gsignond_identity_info_list_free (identities);
813 methods = gsignond_db_metadata_database_get_methods (metadata_db,
815 fail_if (methods == NULL);
816 g_list_free_full (methods, g_free);
819 fail_unless (gsignond_db_metadata_database_get_references (
820 metadata_db, identity_id, ctx1) == NULL);
821 fail_unless (gsignond_db_metadata_database_remove_reference (
822 metadata_db, identity_id, ctx1, "reference1" ) == FALSE);
824 fail_unless (gsignond_db_metadata_database_insert_reference (
825 metadata_db, identity_id, ctx1, "reference1") == TRUE);
827 fail_unless (gsignond_db_metadata_database_insert_reference (
828 metadata_db, identity_id, ctx1, "reference1") == TRUE);
830 reflist = gsignond_db_metadata_database_get_references (
831 metadata_db, identity_id, ctx1);
832 fail_if (reflist == NULL);
833 fail_unless (g_list_length (reflist) == 1);
834 g_list_free_full (reflist, g_free);
836 fail_unless (gsignond_db_metadata_database_remove_reference (
837 metadata_db, identity_id, ctx1, "reference1" ) == TRUE);
838 gsignond_security_context_free (ctx1);
841 acl = gsignond_db_metadata_database_get_accesscontrol_list (metadata_db,
843 fail_if (acl == NULL);
844 gsignond_security_context_list_free (acl);
847 owner = gsignond_db_metadata_database_get_owner (metadata_db,
849 fail_if (owner == NULL);
851 fail_unless (gsignond_db_metadata_database_remove_identity (
852 metadata_db, identity_id) == TRUE);
853 fail_unless (gsignond_db_metadata_database_get_identities (
854 metadata_db, NULL) == NULL);
856 fail_unless (gsignond_db_metadata_database_get_methods (
857 metadata_db, identity_id, owner) == NULL);
859 gsignond_security_context_free (owner);
861 gsignond_identity_info_unref (identity);
863 fail_unless (gsignond_db_sql_database_close (
864 GSIGNOND_DB_SQL_DATABASE (metadata_db)) == TRUE);
865 g_object_unref(metadata_db);
869 START_TEST (test_credentials_database)
871 GSignondConfig *config = NULL;
872 guint32 identity_id = 5;
873 GSignondIdentityInfo *identity = NULL, *identity2= NULL;
874 GSignondIdentityInfoList *identities = NULL;
875 GSignondSecurityContext *ctx1 = NULL;
876 GList *methods = NULL, *reflist = NULL;
877 GSignondSecurityContextList *acl = NULL ;
878 GSignondSecurityContext *owner = NULL;
879 GSignondDbCredentialsDatabase *credentials_db = NULL;
880 GSignondSecretStorage *storage =NULL;
881 GHashTable *data = NULL;
882 GHashTable *data2 = NULL;
884 GSignondDictionary *cap_filter = NULL;
885 GSignondDictionary *type_filter = NULL;
886 GSignondDictionary *cap_type_filter = NULL;
887 GSignondDictionary *no_cap_filter = NULL;
889 config = gsignond_config_new ();
890 storage = g_object_new (GSIGNOND_TYPE_SECRET_STORAGE,
891 "config", config, NULL);
892 g_object_unref(config);
893 credentials_db = gsignond_db_credentials_database_new (
895 g_object_unref (storage);
896 fail_if (credentials_db == NULL);
898 fail_unless (gsignond_db_credentials_database_open_secret_storage (
899 credentials_db) == TRUE);
901 fail_unless (gsignond_db_credentials_database_clear (
902 credentials_db) == TRUE);
904 identity = _get_filled_identity_info ();
906 /*identity load/update*/
907 identity_id = gsignond_db_credentials_database_update_identity (
908 credentials_db, identity);
909 fail_unless (identity_id != 0);
910 gsignond_identity_info_set_id (identity, identity_id);
912 fail_unless (gsignond_db_credentials_database_load_identity (
913 credentials_db, 555, FALSE) == NULL);
914 identity2 = gsignond_db_credentials_database_load_identity (
915 credentials_db, identity_id, FALSE);
916 fail_if (identity2 == NULL);
917 gsignond_identity_info_unref (identity2);
919 identity2 = gsignond_db_credentials_database_load_identity (
920 credentials_db, identity_id, TRUE);
921 fail_if (identity2 == NULL);
923 fail_unless (g_strcmp0 (gsignond_identity_info_get_username (
924 identity2), "username1") == 0);
925 fail_unless (g_strcmp0 (gsignond_identity_info_get_secret (
926 identity2), "secret1") == 0);
927 gsignond_identity_info_unref (identity2);
929 fail_unless (gsignond_db_credentials_database_check_secret (
930 credentials_db, identity_id, "username2", "secret1") == FALSE);
932 fail_unless (gsignond_db_credentials_database_check_secret (
933 credentials_db, identity_id, "username1", "secret2") == FALSE);
935 fail_unless (gsignond_db_credentials_database_check_secret (
936 credentials_db, 0, "username1", "secret2") == FALSE);
938 fail_unless (gsignond_db_credentials_database_check_secret (
939 credentials_db, identity_id, "username1", "secret1") == TRUE);
941 ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
942 methods = gsignond_db_credentials_database_get_methods (credentials_db,
944 fail_if (methods == NULL);
945 g_list_free_full (methods, g_free);
947 /* add data to store */
948 data = g_hash_table_new_full ((GHashFunc)g_str_hash,
949 (GEqualFunc)g_str_equal,
950 (GDestroyNotify)NULL,
951 (GDestroyNotify)g_variant_unref);
952 g_hash_table_insert (data,"key1",g_variant_new_string ("string_value"));
953 g_hash_table_insert (data,"key2",g_variant_new_double (12223.4223));
954 g_hash_table_insert (data,"key3",g_variant_new_uint16(20));
955 g_hash_table_insert (data,"key4",g_variant_new("^ay", "byte_value"));
957 fail_unless (gsignond_db_credentials_database_update_data (
958 credentials_db, 0, "method1", data) == FALSE);
960 fail_unless (gsignond_db_credentials_database_update_data (
961 credentials_db, identity_id, "method1", data) == TRUE);
963 fail_unless (gsignond_db_credentials_database_update_data (
964 credentials_db, identity_id, "method1", data) == TRUE);
966 fail_unless (gsignond_db_credentials_database_load_data (
967 credentials_db, 0, "method1") == NULL);
968 fail_unless (gsignond_db_credentials_database_load_data (
969 credentials_db, identity_id, "method2") == NULL);
971 data2 = gsignond_db_credentials_database_load_data (credentials_db,
972 identity_id, "method1");
973 fail_if (data2 == NULL);
976 g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
977 fail_if (input.status != 1);
978 gsignond_dictionary_unref(data2);
979 g_hash_table_unref(data);
981 fail_unless (gsignond_db_credentials_database_remove_data (
982 credentials_db, 0, "method1") == FALSE);
984 fail_unless (gsignond_db_credentials_database_remove_data (
985 credentials_db, identity_id, "method1") == TRUE);
988 fail_unless (gsignond_db_credentials_database_insert_reference (
989 credentials_db, identity_id, ctx1, "reference1") == TRUE);
991 reflist = gsignond_db_credentials_database_get_references (credentials_db,
993 fail_if (reflist == NULL);
994 fail_unless (g_list_length (reflist) == 1);
995 g_list_free_full (reflist, g_free);
997 fail_unless (gsignond_db_credentials_database_remove_reference (
998 credentials_db, identity_id, ctx1, "reference2") == FALSE);
1000 fail_unless (gsignond_db_credentials_database_remove_reference (
1001 credentials_db, identity_id, ctx1, "reference1") == TRUE);
1002 gsignond_security_context_free (ctx1);
1004 acl = gsignond_db_credentials_database_get_accesscontrol_list (
1005 credentials_db, identity_id);
1006 fail_if (acl == NULL);
1007 gsignond_security_context_list_free (acl);
1009 owner = gsignond_db_credentials_database_get_owner (
1010 credentials_db, identity_id);
1011 fail_if (owner == NULL);
1012 gsignond_security_context_free (owner);
1014 owner = gsignond_db_credentials_database_get_identity_owner (
1015 credentials_db, identity_id);
1016 fail_if (owner == NULL);
1017 gsignond_security_context_free (owner);
1019 /* load_identities : matched with caption and security context */
1020 cap_filter = gsignond_dictionary_new ();
1021 GSignondSecurityContext *ctx =
1022 gsignond_security_context_new_from_values("sysctx1", "appctx1");
1023 gsignond_dictionary_set_string (cap_filter, "Caption", "cap");
1024 gsignond_dictionary_set(cap_filter, "Owner",
1025 gsignond_security_context_to_variant(ctx));
1026 gsignond_security_context_free (ctx);
1027 identities = gsignond_db_credentials_database_load_identities (
1028 credentials_db, cap_filter);
1029 gsignond_dictionary_unref (cap_filter);
1031 fail_if (identities == NULL);
1032 fail_unless (g_list_length (identities) == 1);
1033 gsignond_identity_info_list_free (identities);
1035 /* load_identities: matched with type */
1036 type_filter = gsignond_dictionary_new();
1037 gsignond_dictionary_set_int32 (type_filter, "Type", 456);
1038 identities = gsignond_db_credentials_database_load_identities (
1039 credentials_db, type_filter);
1040 gsignond_dictionary_unref (type_filter);
1042 fail_if (identities == NULL);
1043 fail_unless (g_list_length (identities) == 1);
1044 gsignond_identity_info_list_free (identities);
1046 /* load_identities: matched with type and caption */
1047 cap_type_filter = gsignond_dictionary_new();
1048 gsignond_dictionary_set_int32 (cap_type_filter, "Type", 456);
1049 gsignond_dictionary_set_string (cap_type_filter, "Caption", "CAP");
1050 identities = gsignond_db_credentials_database_load_identities (
1051 credentials_db, cap_type_filter);
1052 gsignond_dictionary_unref (cap_type_filter);
1054 fail_if (identities == NULL);
1055 fail_unless (g_list_length (identities) == 1);
1056 gsignond_identity_info_list_free (identities);
1058 /* Negative load_identities query */
1059 no_cap_filter = gsignond_dictionary_new();
1060 gsignond_dictionary_set_string (no_cap_filter, "Caption", "non_existing");
1062 identities = gsignond_db_credentials_database_load_identities (
1063 credentials_db, no_cap_filter);
1064 gsignond_dictionary_unref (no_cap_filter);
1065 fail_unless (identities == NULL);
1067 fail_unless (gsignond_db_credentials_database_remove_identity (
1068 credentials_db, identity_id) == TRUE);
1069 gsignond_identity_info_unref (identity);
1071 g_object_unref(credentials_db);
1075 Suite* db_suite (void)
1077 Suite *s = suite_create ("Database");
1079 TCase *tc_core = tcase_create ("Tests");
1080 tcase_add_test (tc_core, test_identity_info);
1082 tcase_add_test (tc_core, test_sql_database);
1083 tcase_add_test (tc_core, test_secret_storage);
1084 tcase_add_test (tc_core, test_metadata_database);
1085 tcase_add_test (tc_core, test_credentials_database);
1086 suite_add_tcase (s, tc_core);
1094 #if !GLIB_CHECK_VERSION (2, 36, 0)
1098 Suite *s = db_suite ();
1099 SRunner *sr = srunner_create (s);
1100 srunner_run_all (sr, CK_NORMAL);
1101 number_failed = srunner_ntests_failed (sr);
1103 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;