LCOV - code coverage report
Current view: top level - pkcs11/secret-store - mock-secret-module.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 111 111
Test Date: 2024-04-08 13:24:42 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-secret-module.c: A test PKCS#11 module implementation
       3              : 
       4              :    Copyright (C) 2009 Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring Library 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 GNU
      14              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "mock-secret-module.h"
      26              : 
      27              : #include "gkm/gkm-secret.h"
      28              : #include "gkm/gkm-module.h"
      29              : 
      30              : #include "secret-store/gkm-secret-collection.h"
      31              : #include "secret-store/gkm-secret-data.h"
      32              : #include "secret-store/gkm-secret-fields.h"
      33              : #include "secret-store/gkm-secret-item.h"
      34              : #include "secret-store/gkm-secret-object.h"
      35              : #include "secret-store/gkm-secret-store.h"
      36              : 
      37              : #include "egg/egg-secure-memory.h"
      38              : #include "egg/egg-testing.h"
      39              : 
      40              : #include <glib.h>
      41              : 
      42              : #include <string.h>
      43              : 
      44          856 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
      45              : 
      46              : static GMutex *mutex = NULL;
      47              : static gchar *directory = NULL;
      48              : 
      49              : GkmModule*  _gkm_secret_store_get_module_for_testing (void);
      50              : GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
      51              : 
      52              : GkmModule*
      53           82 : test_secret_module_initialize_and_enter (void)
      54              : {
      55              :         CK_FUNCTION_LIST_PTR funcs;
      56              :         CK_C_INITIALIZE_ARGS args;
      57              :         GkmModule *module;
      58              :         gchar *string;
      59              :         CK_RV rv;
      60              : 
      61           82 :         directory = egg_tests_create_scratch_directory (
      62              :                 SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring",
      63              :                 SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring",
      64              :                 NULL);
      65              : 
      66              :         /* Setup test directory to work in */
      67           82 :         memset (&args, 0, sizeof (args));
      68           82 :         string = g_strdup_printf ("directory='%s'", directory);
      69           82 :         args.pReserved = string;
      70           82 :         args.flags = CKF_OS_LOCKING_OK;
      71              : 
      72           82 :         funcs = gkm_secret_store_get_functions ();
      73           82 :         rv = (funcs->C_Initialize) (&args);
      74           82 :         g_return_val_if_fail (rv == CKR_OK, NULL);
      75              : 
      76           82 :         module = _gkm_secret_store_get_module_for_testing ();
      77           82 :         g_return_val_if_fail (module, NULL);
      78              : 
      79           82 :         mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
      80           82 :         test_secret_module_enter ();
      81              : 
      82           82 :         g_free (string);
      83              : 
      84           82 :         return module;
      85              : }
      86              : 
      87              : void
      88           82 : test_secret_module_leave_and_finalize (void)
      89              : {
      90              :         CK_FUNCTION_LIST_PTR funcs;
      91              :         CK_RV rv;
      92              : 
      93           82 :         test_secret_module_leave ();
      94              : 
      95           82 :         funcs = gkm_secret_store_get_functions ();
      96           82 :         rv = (funcs->C_Finalize) (NULL);
      97           82 :         g_return_if_fail (rv == CKR_OK);
      98              : 
      99           82 :         egg_tests_remove_scratch_directory (directory);
     100           82 :         g_free (directory);
     101           82 :         directory = NULL;
     102              : }
     103              : 
     104              : void
     105           82 : test_secret_module_leave (void)
     106              : {
     107           82 :         g_assert (mutex);
     108           82 :         g_mutex_unlock (mutex);
     109           82 : }
     110              : 
     111              : void
     112           82 : test_secret_module_enter (void)
     113              : {
     114           82 :         g_assert (mutex);
     115           82 :         g_mutex_lock (mutex);
     116           82 : }
     117              : 
     118              : GkmSession*
     119           82 : test_secret_module_open_session (gboolean writable)
     120              : {
     121           82 :         CK_ULONG flags = CKF_SERIAL_SESSION;
     122              :         CK_SESSION_HANDLE handle;
     123              :         GkmModule *module;
     124              :         GkmSession *session;
     125              :         CK_RV rv;
     126              : 
     127           82 :         module = _gkm_secret_store_get_module_for_testing ();
     128           82 :         g_return_val_if_fail (module, NULL);
     129              : 
     130           82 :         if (writable)
     131           82 :                 flags |= CKF_RW_SESSION;
     132              : 
     133           82 :         rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
     134           82 :         g_assert (rv == CKR_OK);
     135              : 
     136           82 :         rv = gkm_module_C_Login (module, handle, CKU_USER, NULL, 0);
     137           82 :         g_assert (rv == CKR_OK);
     138              : 
     139           82 :         session = gkm_module_lookup_session (module, handle);
     140           82 :         g_assert (session);
     141              : 
     142           82 :         return session;
     143              : }
     144              : 
     145              : /* Validates plain.keyring and encrypted.keyring in test-data */
     146              : void
     147            7 : test_secret_collection_validate (GkmSecretCollection *collection, GkmSecretData *sdata)
     148              : {
     149              :         GkmSecretItem* item;
     150              :         GkmSecretObject *obj;
     151              :         GHashTable *fields;
     152              :         const gchar *value;
     153              :         GkmSecret *secret;
     154              :         GList *items;
     155              :         glong when;
     156              :         guint32 num;
     157              : 
     158            7 :         obj = GKM_SECRET_OBJECT (collection);
     159              : 
     160              :         /* The keyring itself */
     161              :         /* "Missing keyring name" */
     162            7 :         value = gkm_secret_object_get_label (obj);
     163            7 :         g_assert (value != NULL);
     164              :         /* "Invalid keyring name" */
     165            7 :         g_assert_cmpstr (value, ==, "unit-test-keyring");
     166              : #if 0
     167              :         /* "Bad lock settings" */
     168              :         g_assert (!keyring->lock_on_idle && keyring->lock_timeout == 0);
     169              : #endif
     170              :         /* "Bad Creation Time" */
     171            7 :         when = gkm_secret_object_get_created (obj);
     172            7 :         g_assert (when == 1198027852);
     173              :         /* "Bad Modification Time" */
     174            7 :         when = gkm_secret_object_get_modified (obj);
     175            7 :         g_assert (when == 1198027852);
     176              :         /* "Wrong number of items" */
     177            7 :         items = gkm_secret_collection_get_items (collection);
     178            7 :         g_assert_cmpint (g_list_length (items), ==, 2);
     179            7 :         g_list_free (items);
     180              : 
     181              :         /* Item #2 */
     182            7 :         item = gkm_secret_collection_get_item (collection, "2");
     183            7 :         obj = GKM_SECRET_OBJECT (item);
     184              :         /* "Couldn't find item" */
     185            7 :         g_assert (item != NULL);
     186              : #if 0
     187              :         /* "Invalid item type" */
     188              :         g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
     189              : #endif
     190              :         /* "Missing secret" */
     191            7 :         secret = gkm_secret_data_get_secret (sdata, "2");
     192            7 :         g_assert (secret != NULL);
     193              :         /* "Wrong secret" */
     194            7 :         g_assert (gkm_secret_equals (secret, (guchar*)"item-secret", -1));
     195              :         /* "Bad Creation Time" */
     196            7 :         when = gkm_secret_object_get_created (obj);
     197            7 :         g_assert_cmpint (when, ==, 1198027852);
     198              : 
     199              : #if 0
     200              :         /* Item #2 ACL */
     201              :         /* "Bad ACLs" */
     202              :         g_assert_cmpint (g_list_length (item->acl), ==, 1);
     203              :         ac = (GnomeKeyringAccessControl*)item->acl->data;
     204              :         /* "Invalid ACL" */
     205              :         g_assert (ac && ac->application);
     206              :         /* "Invalid ACL Path" */
     207              :         g_assert (ac->application->pathname && strstr (ac->application->pathname, "test-suite"));
     208              :         /* "Invalid ACL Display Name" */
     209              :         g_assert (ac->application->display_name);
     210              :         g_assert_cmpstr (ac->application->display_name, ==, "test-suite");
     211              :         /* "Invalid ACL Access Type" */
     212              :         g_assert_cmpint (ac->types_allowed, ==, (GNOME_KEYRING_ACCESS_READ | GNOME_KEYRING_ACCESS_WRITE | GNOME_KEYRING_ACCESS_REMOVE));
     213              : #endif
     214              : 
     215              :         /* Item #3 */
     216            7 :         item = gkm_secret_collection_get_item (collection, "3");
     217            7 :         obj = GKM_SECRET_OBJECT (item);
     218              :         /* "Couldn't find item #3" */
     219            7 :         g_assert (item != NULL);
     220            7 :         fields = gkm_secret_item_get_fields (item);
     221            7 :         g_assert (fields != NULL);
     222              :         /* Make fields are the same */
     223            7 :         value = gkm_secret_fields_get (fields, "dog");
     224            7 :         g_assert_cmpstr (value, ==, "woof");
     225            7 :         value = gkm_secret_fields_get (fields, "bird");
     226            7 :         g_assert_cmpstr (value, ==, "cheep");
     227            7 :         value = gkm_secret_fields_get (fields, "iguana");
     228            7 :         g_assert_cmpstr (value, ==, "");
     229            7 :         g_assert (gkm_secret_fields_get_compat_uint32 (fields, "num", &num));
     230            7 :         g_assert_cmpuint (num, ==, 3);
     231              : #if 0
     232              :         /* "Invalid item type" */
     233              :         g_assert_cmpint (item->type, ==, GNOME_KEYRING_ITEM_GENERIC_SECRET);
     234              : #endif
     235              :         /* "Missing secret" */
     236            7 :         secret = gkm_secret_data_get_secret (sdata, "3");
     237            7 :         g_assert (secret != NULL);
     238              :         /* "Wrong secret" */
     239            7 :         g_assert (gkm_secret_equals (secret, (guchar*)"item-secret", -1));
     240            7 : }
     241              : 
     242              : /* Fills a collection with some junk data */
     243              : void
     244            4 : test_secret_collection_populate (GkmSecretCollection *collection, GkmSecretData *sdata)
     245              : {
     246              :         GkmSecretItem *item;
     247              :         GHashTable *fields;
     248              :         GkmSecret *secret;
     249              : 
     250            4 :         item = gkm_secret_collection_new_item (collection, "4");
     251            4 :         gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Noises");
     252            4 :         secret = gkm_secret_new_from_password ("4's secret");
     253            4 :         gkm_secret_data_set_secret (sdata, "4", secret);
     254            4 :         g_object_unref (secret);
     255            4 :         fields = gkm_secret_item_get_fields (item);
     256            4 :         gkm_secret_fields_add (fields, "doggy", "fart");
     257            4 :         gkm_secret_fields_add (fields, "pig", "grunt");
     258            4 :         gkm_secret_fields_add_compat_uint32 (fields, "how-many", 292929);
     259              : 
     260            4 :         item = gkm_secret_collection_new_item (collection, "5");
     261            4 :         gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Colors");
     262            4 :         secret = gkm_secret_new_from_password ("5's secret");
     263            4 :         gkm_secret_data_set_secret (sdata, "5", secret);
     264            4 :         g_object_unref (secret);
     265            4 :         fields = gkm_secret_item_get_fields (item);
     266            4 :         gkm_secret_fields_add (fields, "barney", "purple");
     267            4 :         gkm_secret_fields_add (fields, "piglet", "pink");
     268            4 :         gkm_secret_fields_add_compat_uint32 (fields, "number", 8);
     269              : 
     270            4 :         item = gkm_secret_collection_new_item (collection, "6");
     271            4 :         gkm_secret_object_set_label (GKM_SECRET_OBJECT (item), "Binary Secret");
     272            4 :         secret = gkm_secret_new ((guchar*)"binary\0secret", 13);
     273            4 :         gkm_secret_data_set_secret (sdata, "6", secret);
     274            4 :         g_object_unref (secret);
     275            4 :         fields = gkm_secret_item_get_fields (item);
     276            4 :         gkm_secret_fields_add (fields, "train", "zoom");
     277            4 :         gkm_secret_fields_add (fields, "hummer", NULL);
     278            4 :         gkm_secret_fields_add_compat_uint32 (fields, "number", 2);
     279            4 : }
        

Generated by: LCOV version 2.0-1