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

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-xdg-module.c: A test PKCS#11 module implementation
       3              : 
       4              :    Copyright (C) 2010 Stefan Walter
       5              :    Copyright (C) 2010 Collabora Ltd
       6              : 
       7              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       8              :    modify it under the terms of the GNU Library General Public License as
       9              :    published by the Free Software Foundation; either version 2 of the
      10              :    License, or (at your option) any later version.
      11              : 
      12              :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      13              :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    Library General Public License for more details.
      16              : 
      17              :    You should have received a copy of the GNU Library General Public
      18              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      19              :    <http://www.gnu.org/licenses/>.
      20              : 
      21              :    Author: Stef Walter <stef@memberwebs.com>
      22              : */
      23              : 
      24              : #include "config.h"
      25              : 
      26              : #include "mock-xdg-module.h"
      27              : 
      28              : #include "xdg-store/gkm-xdg-store.h"
      29              : 
      30              : #include "egg/egg-secure-memory.h"
      31              : #include "egg/egg-testing.h"
      32              : 
      33              : #include "gkm/gkm-session.h"
      34              : #include "gkm/gkm-module.h"
      35              : 
      36              : #include <glib/gstdio.h>
      37              : 
      38              : #include <errno.h>
      39              : #include <sys/time.h>
      40              : 
      41              : #include <string.h>
      42              : 
      43          988 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
      44              : 
      45              : static GMutex *mutex = NULL;
      46              : static gchar *directory = NULL;
      47              : 
      48              : GkmModule*  _gkm_xdg_store_get_module_for_testing (void);
      49              : GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
      50              : 
      51              : void
      52            1 : mock_xdg_module_remove_file (const gchar *name)
      53              : {
      54              :         gchar *filename;
      55              :         gchar *basename;
      56              : 
      57            1 :         basename = g_path_get_basename (name);
      58            1 :         filename = g_build_filename (directory, basename, NULL);
      59            1 :         if (g_unlink (filename) < 0)
      60            0 :                 g_error ("couldn't remove: %s", filename);
      61            1 :         g_free (filename);
      62            1 :         g_free (basename);
      63            1 : }
      64              : 
      65              : 
      66              : void
      67           97 : mock_xdg_module_empty_file (const gchar *name)
      68              : {
      69              :         gchar *filename;
      70              :         gchar *basename;
      71              : 
      72           97 :         basename = g_path_get_basename (name);
      73           97 :         filename = g_build_filename (directory, basename, NULL);
      74           97 :         if (!g_file_set_contents (filename, "", 0, NULL))
      75            0 :                 g_error ("couldn't write: %s", filename);
      76           97 :         g_free (filename);
      77           97 :         g_free (basename);
      78           97 : }
      79              : 
      80              : void
      81            2 : mock_xdg_module_touch_file (const gchar *name, gint future)
      82              : {
      83              :         gchar *basename;
      84              :         gchar *filename;
      85              :         struct timeval tv[2];
      86              : 
      87            2 :         basename = g_path_get_basename (name);
      88            2 :         filename = g_build_filename (directory, basename, NULL);
      89              : 
      90              :         /* Initialize the access and modification times */
      91            2 :         gettimeofday (tv, NULL);
      92            2 :         tv[0].tv_sec += future;
      93            2 :         memcpy (tv + 1, tv, sizeof (struct timeval));
      94              : 
      95            2 :         if (utimes (filename, tv) < 0)
      96            0 :                 g_error ("couldn't update file time: %s: %s", filename, g_strerror (errno));
      97              : 
      98            2 :         g_free (basename);
      99            2 :         g_free (filename);
     100            2 : }
     101              : 
     102              : GkmModule*
     103           32 : mock_xdg_module_initialize_and_enter (void)
     104              : {
     105              :         CK_FUNCTION_LIST_PTR funcs;
     106              :         CK_C_INITIALIZE_ARGS args;
     107              :         GkmModule *module;
     108              :         gchar *string;
     109              :         CK_RV rv;
     110              : 
     111           32 :         directory = egg_tests_create_scratch_directory (
     112              :                 SRCDIR "/pkcs11/xdg-store/fixtures/test-refer-1.trust",
     113              :                 SRCDIR "/pkcs11/xdg-store/fixtures/test-certificate-1.cer",
     114              :                 NULL);
     115              : 
     116              :         /* Setup test directory to work in */
     117           32 :         memset (&args, 0, sizeof (args));
     118           32 :         string = g_strdup_printf ("directory='%s'", directory);
     119           32 :         args.pReserved = string;
     120           32 :         args.flags = CKF_OS_LOCKING_OK;
     121              : 
     122              :         /* Copy fixtures from test-data to scratch */
     123           32 :         mock_xdg_module_empty_file ("invalid-without-ext");
     124           32 :         mock_xdg_module_empty_file ("test-file.unknown");
     125           32 :         mock_xdg_module_empty_file ("test-invalid.trust");
     126              : 
     127           32 :         funcs = gkm_xdg_store_get_functions ();
     128           32 :         rv = (funcs->C_Initialize) (&args);
     129           32 :         g_return_val_if_fail (rv == CKR_OK, NULL);
     130              : 
     131           32 :         module = _gkm_xdg_store_get_module_for_testing ();
     132           32 :         g_return_val_if_fail (module, NULL);
     133              : 
     134           32 :         mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
     135           32 :         mock_xdg_module_enter ();
     136              : 
     137           32 :         g_free (string);
     138              : 
     139           32 :         return module;
     140              : }
     141              : 
     142              : void
     143           32 : mock_xdg_module_leave_and_finalize (void)
     144              : {
     145              :         CK_FUNCTION_LIST_PTR funcs;
     146              :         CK_RV rv;
     147              : 
     148           32 :         mock_xdg_module_leave ();
     149              : 
     150           32 :         funcs = gkm_xdg_store_get_functions ();
     151           32 :         rv = (funcs->C_Finalize) (NULL);
     152           32 :         g_return_if_fail (rv == CKR_OK);
     153              : 
     154           32 :         egg_tests_remove_scratch_directory (directory);
     155           32 :         g_free (directory);
     156           32 :         directory = NULL;
     157              : }
     158              : 
     159              : void
     160           32 : mock_xdg_module_leave (void)
     161              : {
     162           32 :         g_assert (mutex);
     163           32 :         g_mutex_unlock (mutex);
     164           32 : }
     165              : 
     166              : void
     167           32 : mock_xdg_module_enter (void)
     168              : {
     169           32 :         g_assert (mutex);
     170           32 :         g_mutex_lock (mutex);
     171           32 : }
     172              : 
     173              : GkmSession*
     174           32 : mock_xdg_module_open_session (gboolean writable)
     175              : {
     176           32 :         CK_ULONG flags = CKF_SERIAL_SESSION;
     177              :         CK_SESSION_HANDLE handle;
     178              :         GkmModule *module;
     179              :         GkmSession *session;
     180              :         CK_RV rv;
     181              : 
     182           32 :         module = _gkm_xdg_store_get_module_for_testing ();
     183           32 :         g_return_val_if_fail (module, NULL);
     184              : 
     185           32 :         if (writable)
     186           32 :                 flags |= CKF_RW_SESSION;
     187              : 
     188           32 :         rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
     189           32 :         g_assert (rv == CKR_OK);
     190              : 
     191           32 :         session = gkm_module_lookup_session (module, handle);
     192           32 :         g_assert (session);
     193              : 
     194           32 :         return session;
     195              : }
        

Generated by: LCOV version 2.0-1