gsignond first release 0.0.1
[profile/ivi/gsignond.git] / test / db / dbtest.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  *
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.
13  *
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.
18  *
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
22  * 02110-1301 USA
23  */
24
25 #include <check.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sqlite3.h>
29 #include <glib/gstdio.h>
30
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"
41
42 static GSequence*
43 _sequence_new (gchar *data)
44 {
45     GSequence *value = NULL;
46     value = g_sequence_new (NULL);
47     g_sequence_append (value, (guint8 *)data);
48     return value;
49 }
50
51 typedef struct {
52     GHashTable *table;
53     int status;
54 } Data;
55
56 static void
57 _compare_key_value(
58         gchar *key,
59         GVariant *value,
60         Data *user_data)
61 {
62     GVariant *value2 = (GVariant *)g_hash_table_lookup (user_data->table, key);
63
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) {
68         return;
69     }
70     user_data->status = 0;
71 }
72
73 static gboolean
74 _compare_sequences (
75         GSequence *one,
76         GSequence *two)
77 {
78     GSequenceIter *iter1 = NULL, *iter2 = NULL;
79     gboolean equal = TRUE;
80
81     if (one == NULL && two == NULL)
82         return TRUE;
83
84     if ((one != NULL && two == NULL) ||
85         (one == NULL && two != NULL) ||
86         (g_sequence_get_length (one) != g_sequence_get_length (two)))
87         return FALSE;
88
89     if (one == two)
90         return TRUE;
91
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) {
97             equal = FALSE;
98             break;
99         }
100         iter1 = g_sequence_iter_next (iter1);
101     }
102
103     return equal;
104 }
105
106 static GSignondIdentityInfo *
107 _get_filled_identity_info_2 (
108         GSignondIdentityInfo **identity_inp,
109         gboolean add_creds,
110         gboolean add_methods,
111         gboolean add_realms,
112         gboolean add_acl,
113         gboolean add_owner)
114 {
115     guint32 type = 456;
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;
125
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);
131     if (add_creds) {
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);
135     }
136
137     /*realms*/
138     if (add_realms) {
139         seq_realms = _sequence_new("realms1");
140         gsignond_identity_info_set_realms (identity, seq_realms);
141         g_sequence_free (seq_realms);
142     }
143
144     /*methods*/
145     if (add_methods) {
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);
156     }
157
158     /*acl*/
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);
165     if (add_acl) {
166         gsignond_identity_info_set_access_control_list (identity, ctx_list);
167     }
168
169     /*owners*/
170     if (add_owner) {
171         gsignond_identity_info_set_owner (identity, ctx1);
172     }
173     gsignond_security_context_list_free (ctx_list);
174
175     gsignond_identity_info_set_validated (identity, FALSE);
176     gsignond_identity_info_set_identity_type (identity, type);
177     return identity;
178 }
179
180 static GSignondIdentityInfo *
181 _get_filled_identity_info (void)
182 {
183     GSignondIdentityInfo *identity = NULL;
184     return _get_filled_identity_info_2 (&identity,
185             TRUE, TRUE, TRUE, TRUE, TRUE);
186 }
187
188 START_TEST (test_identity_info)
189 {
190     guint32 id = 125;
191     guint32 type = 456;
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;
201     GList *list2;
202
203     identity = gsignond_identity_info_new ();
204     fail_if (identity == NULL);
205
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 (
210             identity) == FALSE);
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 (
219             identity) == NULL);
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);
223
224     fail_unless (gsignond_identity_info_set_id (identity, id) == TRUE);
225
226     fail_unless (id == gsignond_identity_info_get_id (identity));
227
228     fail_unless (gsignond_identity_info_set_identity_new (identity) == TRUE);
229
230     fail_unless (gsignond_identity_info_get_is_identity_new (
231             identity) == TRUE);
232
233     fail_unless (gsignond_identity_info_set_username (
234             identity, NULL) == FALSE);
235
236     fail_unless (gsignond_identity_info_get_username (identity) == NULL);
237
238     fail_unless (gsignond_identity_info_set_username (
239             identity, username) == TRUE);
240
241     fail_unless (g_strcmp0 (username, gsignond_identity_info_get_username (
242             identity)) == 0);
243
244     fail_unless (gsignond_identity_info_set_username_secret (
245             identity, TRUE) == TRUE);
246
247     fail_unless (gsignond_identity_info_get_is_username_secret (
248             identity) == TRUE);
249
250     fail_unless (gsignond_identity_info_set_secret (identity, NULL) == FALSE);
251
252     fail_unless (gsignond_identity_info_get_secret (identity) == NULL);
253
254     fail_unless (gsignond_identity_info_set_secret (identity, secret) == TRUE);
255
256     fail_unless (g_strcmp0 (secret, gsignond_identity_info_get_secret (
257             identity)) == 0);
258
259     fail_unless (gsignond_identity_info_set_store_secret (
260             identity, TRUE) == TRUE);
261
262     fail_unless (gsignond_identity_info_get_store_secret (
263             identity) == TRUE);
264
265     fail_unless (gsignond_identity_info_set_caption (identity, NULL) == FALSE);
266
267     fail_unless (gsignond_identity_info_get_caption (identity) == NULL);
268
269     fail_unless (gsignond_identity_info_set_caption (
270             identity, caption) == TRUE);
271
272     fail_unless (g_strcmp0 (caption, gsignond_identity_info_get_caption (
273             identity)) == 0);
274
275     /*realms*/
276     seq_realms = _sequence_new("realms1");
277     fail_unless (gsignond_identity_info_set_realms (
278             identity, seq_realms) == TRUE);
279
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);
285
286     /*methods*/
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);
300
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);
308
309     fail_unless (gsignond_identity_info_get_mechanisms (
310             identity, "method20") == NULL);
311
312     mechs = gsignond_identity_info_get_mechanisms (
313                 identity, "method1");
314     fail_if (mechs == NULL);
315     g_sequence_free (mechs);
316
317     fail_unless (gsignond_identity_info_remove_method (
318             identity, "method20") == FALSE);
319     fail_unless (gsignond_identity_info_remove_method (
320             identity, "method4") == TRUE);
321
322     /*acl*/
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);
331
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;
344
345     /*owners*/
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;
352
353     fail_unless (gsignond_identity_info_set_validated (
354             identity, FALSE) == TRUE);
355
356     fail_unless (gsignond_identity_info_get_validated (identity) == FALSE);
357
358     fail_unless (gsignond_identity_info_set_identity_type (
359             identity, type) == TRUE);
360
361     fail_unless (type == gsignond_identity_info_get_identity_type (identity));
362
363     /*copy*/
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);
369
370     gsignond_security_context_list_free (ctx_list); ctx_list = NULL;
371
372     gsignond_identity_info_unref (identity);
373 }
374 END_TEST
375
376 static gboolean
377 _gsignond_query_read_int (
378         sqlite3_stmt *stmt,
379         gint *status)
380 {
381     *status = sqlite3_column_int (stmt, 0);
382     return TRUE;
383 }
384 static gboolean
385 _gsignond_query_read_string (
386         sqlite3_stmt *stmt,
387         gint *status)
388 {
389     const gchar* str = NULL;
390     *status = 0;
391     str = (const gchar *)sqlite3_column_text (stmt, 0);
392     if (str && strlen(str) > 0 &&
393         g_strcmp0 (str, "username1") == 0) {
394         *status = 1;
395     }
396     return TRUE;
397 }
398
399 START_TEST (test_sql_database)
400 {
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;
409     Data input;
410     sqlite3_stmt *stmt = NULL;
411     gint status=0;
412     GList *list = NULL;
413     GHashTable* hashtable = NULL;
414     GArray *array = NULL;
415     GSignondDbSqlDatabase *sqldb = NULL;
416     GError *error = NULL;
417
418     /* Secret Storage */
419     database = gsignond_db_secret_database_new ();
420     fail_if (database == NULL);
421     sqldb = GSIGNOND_DB_SQL_DATABASE (database);
422
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);
437
438     config = gsignond_config_new ();
439     dir = gsignond_config_get_string (config,
440             GSIGNOND_CONFIG_GENERAL_STORAGE_PATH);
441     if (!dir) {
442         dir = g_get_user_data_dir ();
443     }
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);
451     g_free (filename);
452     g_object_unref(config);
453
454     creds = gsignond_credentials_new ();
455     fail_if (creds == NULL);
456
457     fail_unless (gsignond_credentials_set_data (
458             creds, id, "user 1", "pass 1") == TRUE);
459
460     fail_unless (gsignond_db_secret_database_update_credentials (
461             database, creds) == TRUE);
462     g_object_unref (creds); creds = NULL;
463
464     creds = gsignond_db_secret_database_load_credentials (database, id);
465     fail_if (creds == NULL);
466     g_object_unref (creds);
467
468     /* remove the added credentials */
469     fail_unless (gsignond_db_secret_database_remove_credentials (
470             database, id) == TRUE);
471
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);
478
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);
487     input.table = data;
488     input.status = 1;
489     g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
490     fail_if (input.status != 1);
491
492     gsignond_dictionary_unref (data2);
493     g_hash_table_unref(data);
494
495
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;
503
504     fail_unless (gsignond_db_sql_database_exec (
505             sqldb, "INSERT INTO CREDENTIALS (id, username, password) "
506                     "VALUES (1, \"username1\",\"password\");") == TRUE);
507
508     fail_unless (gsignond_db_sql_database_exec (
509             sqldb, "INSERT INTO CREDENTIALS (id, username, password) "
510                     "VALUES (2, \"username2\",\"password2\");") == TRUE);
511
512     fail_unless (gsignond_db_sql_database_exec (
513             sqldb, "SELECT id from CREDENTIALS limit 1;") == TRUE);
514
515     fail_unless (gsignond_db_sql_database_query_exec (
516             sqldb, "SELECT id from CREDENTIALS limit 1;",
517             (GSignondDbSqlDatabaseQueryCallback)_gsignond_query_read_int,
518             &status) == 1);
519     fail_unless (status == 1);
520
521     fail_unless (gsignond_db_sql_database_query_exec (
522             sqldb, "SELECT username from CREDENTIALS where id=1;",
523             (GSignondDbSqlDatabaseQueryCallback)_gsignond_query_read_string,
524             &status) == 1);
525     fail_unless (status == 1);
526
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);
532
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);
538
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);
545
546     fail_unless (gsignond_db_sql_database_query_exec_int (
547             sqldb,"SELECT id from CREDENTIALS where username=\"username2\";",
548             &status) == TRUE);
549     fail_unless (status == 2);
550
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);
556
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);
562     stmt = NULL;
563
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);
571
572     fail_unless (gsignond_db_sql_database_transaction_exec (
573             sqldb, "SELECT id from CREDENTIALS "
574                    "where username=\"username1\";") == TRUE);
575
576     fail_unless (gsignond_db_sql_database_get_db_version (
577             sqldb, "PRAGMA user_version;") == 1);
578
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);
586
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 (
591             sqldb) == 4);
592
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);
598 }
599 END_TEST
600
601 START_TEST (test_secret_storage)
602 {
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;
609     Data input;
610     const gchar *dir = NULL;
611
612     config = gsignond_config_new ();
613     /* Secret Storage */
614     storage = g_object_new (GSIGNOND_TYPE_SECRET_STORAGE,
615             "config", config, NULL);
616     g_object_unref(config);
617     fail_if (storage == NULL);
618
619     dir = gsignond_config_get_string (config,
620             GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
621     if (!dir) {
622         dir = g_get_user_data_dir ();
623     }
624     g_mkdir_with_parents (dir, S_IRWXU);
625
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);
640
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);
644
645     creds = gsignond_credentials_new ();
646     fail_if (creds == NULL);
647
648     fail_unless (gsignond_credentials_set_data (
649             creds, id, "user 1", "pass 1") == TRUE);
650
651     fail_unless (gsignond_secret_storage_update_credentials (
652             storage, creds) == TRUE);
653     g_object_unref (creds); creds = NULL;
654
655     creds = gsignond_secret_storage_load_credentials (storage, id);
656     fail_if (creds == NULL);
657
658     fail_unless (gsignond_secret_storage_check_credentials (
659             storage, creds) == TRUE);
660
661     gsignond_credentials_set_id (creds, 3);
662     fail_unless (gsignond_secret_storage_check_credentials (
663             storage, creds) == FALSE);
664     g_object_unref (creds);
665
666     /* remove the added credentials */
667     fail_unless (gsignond_secret_storage_remove_credentials (
668             storage, id) == TRUE);
669
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);
676
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"));
681
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);
686     input.table = data;
687     input.status = 1;
688     g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
689     fail_if (input.status != 1);
690
691     gsignond_dictionary_unref(data2);
692     g_hash_table_unref(data);
693
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);
699 }
700 END_TEST
701
702 START_TEST (test_metadata_database)
703 {
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;
714
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);
720
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);
748
749     fail_unless (gsignond_db_metadata_database_open (metadata_db) == TRUE);
750
751     fail_unless (gsignond_db_metadata_database_open (metadata_db) == TRUE);
752
753     fail_unless (gsignond_db_sql_database_clear (
754             GSIGNOND_DB_SQL_DATABASE (metadata_db)) == TRUE);
755
756     fail_unless (gsignond_db_metadata_database_get_accesscontrol_list (
757             metadata_db, identity_id) == NULL);
758
759     fail_unless (gsignond_db_metadata_database_get_owner (
760             metadata_db, identity_id) == NULL);
761
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);
766
767     fail_unless (methodid == gsignond_db_metadata_database_get_method_id (
768             metadata_db, method1));
769
770     /*update_identity*/
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);
775
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);
780
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);
785
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);
790
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);
797
798     identity2 = gsignond_db_metadata_database_get_identity (
799             metadata_db, identity_id);
800     fail_if (identity2 == NULL);
801     gsignond_identity_info_unref (identity2);
802
803     /*get_identity/identities*/
804     fail_unless (gsignond_db_metadata_database_get_identity (
805             metadata_db, 2222) == NULL);
806
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);
811
812     /*methods*/
813     methods = gsignond_db_metadata_database_get_methods (metadata_db,
814                 identity_id, ctx1);
815     fail_if (methods == NULL);
816     g_list_free_full (methods, g_free);
817
818     /*references*/
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);
823
824     fail_unless (gsignond_db_metadata_database_insert_reference (
825             metadata_db, identity_id, ctx1, "reference1") == TRUE);
826
827     fail_unless (gsignond_db_metadata_database_insert_reference (
828             metadata_db, identity_id, ctx1, "reference1") == TRUE);
829
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);
835
836     fail_unless (gsignond_db_metadata_database_remove_reference (
837             metadata_db, identity_id, ctx1, "reference1" ) == TRUE);
838     gsignond_security_context_free (ctx1);
839
840     /*acl*/
841     acl = gsignond_db_metadata_database_get_accesscontrol_list (metadata_db,
842             identity_id);
843     fail_if (acl == NULL);
844     gsignond_security_context_list_free (acl);
845
846     /*owner*/
847     owner = gsignond_db_metadata_database_get_owner (metadata_db,
848             identity_id);
849     fail_if (owner == NULL);
850
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);
855
856     fail_unless (gsignond_db_metadata_database_get_methods (
857             metadata_db, identity_id, owner) == NULL);
858
859     gsignond_security_context_free (owner);
860
861     gsignond_identity_info_unref (identity);
862
863     fail_unless (gsignond_db_sql_database_close (
864             GSIGNOND_DB_SQL_DATABASE (metadata_db)) == TRUE);
865     g_object_unref(metadata_db);
866 }
867 END_TEST
868
869 START_TEST (test_credentials_database)
870 {
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;
883     Data input;
884     GSignondDictionary *cap_filter = NULL;
885     GSignondDictionary *type_filter = NULL;
886     GSignondDictionary *cap_type_filter = NULL;
887     GSignondDictionary *no_cap_filter = NULL;
888
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 (
894             config, storage);
895     g_object_unref (storage);
896     fail_if (credentials_db == NULL);
897
898     fail_unless (gsignond_db_credentials_database_open_secret_storage (
899             credentials_db) == TRUE);
900
901     fail_unless (gsignond_db_credentials_database_clear (
902             credentials_db) == TRUE);
903
904     identity = _get_filled_identity_info ();
905
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);
911
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);
918
919     identity2 = gsignond_db_credentials_database_load_identity (
920             credentials_db, identity_id, TRUE);
921     fail_if (identity2 == NULL);
922
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);
928
929     fail_unless (gsignond_db_credentials_database_check_secret (
930             credentials_db, identity_id, "username2", "secret1") == FALSE);
931
932     fail_unless (gsignond_db_credentials_database_check_secret (
933             credentials_db, identity_id, "username1", "secret2") == FALSE);
934
935     fail_unless (gsignond_db_credentials_database_check_secret (
936             credentials_db, 0, "username1", "secret2") == FALSE);
937
938     fail_unless (gsignond_db_credentials_database_check_secret (
939             credentials_db, identity_id, "username1", "secret1") == TRUE);
940
941     ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
942     methods = gsignond_db_credentials_database_get_methods (credentials_db,
943                 identity_id, ctx1);
944     fail_if (methods == NULL);
945     g_list_free_full (methods, g_free);
946
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"));
956
957     fail_unless (gsignond_db_credentials_database_update_data (
958             credentials_db, 0, "method1", data) == FALSE);
959
960     fail_unless (gsignond_db_credentials_database_update_data (
961             credentials_db, identity_id, "method1", data) == TRUE);
962
963     fail_unless (gsignond_db_credentials_database_update_data (
964             credentials_db, identity_id, "method1", data) == TRUE);
965
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);
970
971     data2 = gsignond_db_credentials_database_load_data (credentials_db,
972             identity_id, "method1");
973     fail_if (data2 == NULL);
974     input.table = data;
975     input.status = 1;
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);
980
981     fail_unless (gsignond_db_credentials_database_remove_data (
982             credentials_db, 0, "method1") == FALSE);
983
984     fail_unless (gsignond_db_credentials_database_remove_data (
985             credentials_db, identity_id, "method1") == TRUE);
986
987     /*references*/
988     fail_unless (gsignond_db_credentials_database_insert_reference (
989             credentials_db, identity_id, ctx1, "reference1") == TRUE);
990
991     reflist = gsignond_db_credentials_database_get_references (credentials_db,
992                 identity_id, ctx1);
993     fail_if (reflist == NULL);
994     fail_unless (g_list_length (reflist) == 1);
995     g_list_free_full (reflist, g_free);
996
997     fail_unless (gsignond_db_credentials_database_remove_reference (
998             credentials_db, identity_id, ctx1, "reference2") == FALSE);
999
1000     fail_unless (gsignond_db_credentials_database_remove_reference (
1001             credentials_db, identity_id, ctx1, "reference1") == TRUE);
1002     gsignond_security_context_free (ctx1);
1003
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);
1008
1009     owner = gsignond_db_credentials_database_get_owner (
1010             credentials_db, identity_id);
1011     fail_if (owner == NULL);
1012     gsignond_security_context_free (owner);
1013
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);
1018
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);
1030
1031     fail_if (identities == NULL);
1032     fail_unless (g_list_length (identities) == 1);
1033     gsignond_identity_info_list_free (identities);
1034
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);
1041
1042     fail_if (identities == NULL);
1043     fail_unless (g_list_length (identities) == 1);
1044     gsignond_identity_info_list_free (identities);
1045
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);
1053
1054     fail_if (identities == NULL);
1055     fail_unless (g_list_length (identities) == 1);
1056     gsignond_identity_info_list_free (identities);
1057
1058     /* Negative load_identities query */
1059     no_cap_filter = gsignond_dictionary_new();
1060     gsignond_dictionary_set_string (no_cap_filter, "Caption", "non_existing");
1061
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);
1066
1067     fail_unless (gsignond_db_credentials_database_remove_identity (
1068             credentials_db, identity_id) == TRUE);
1069     gsignond_identity_info_unref (identity);
1070
1071     g_object_unref(credentials_db);
1072 }
1073 END_TEST
1074
1075 Suite* db_suite (void)
1076 {
1077     Suite *s = suite_create ("Database");
1078
1079     TCase *tc_core = tcase_create ("Tests");
1080     tcase_add_test (tc_core, test_identity_info);
1081
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);
1087     return s;
1088 }
1089
1090 int main (void)
1091 {
1092     int number_failed;
1093
1094 #if !GLIB_CHECK_VERSION (2, 36, 0)
1095     g_type_init ();
1096 #endif
1097
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);
1102     srunner_free (sr);
1103     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1104 }
1105