LCOV - code coverage report
Current view: top level - pkcs11/xdg-store - test-xdg-module.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 99.2 % 124 123
Test Date: 2024-04-08 13:24:42 Functions: 100.0 % 11 11

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-xdg-test->module.c: A test PKCS#11 test->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 "gkm/gkm-session.h"
      31              : #include "gkm/gkm-module.h"
      32              : 
      33              : #include "egg/egg-testing.h"
      34              : 
      35              : #include <errno.h>
      36              : #include <sys/times.h>
      37              : 
      38              : #include <string.h>
      39              : 
      40              : typedef struct {
      41              :         GkmModule *module;
      42              :         GkmSession *session;
      43              :         CK_SLOT_ID slot_id;
      44              : } Test;
      45              : 
      46              : static void
      47            7 : setup (Test *test, gconstpointer unused)
      48              : {
      49              :         CK_SESSION_INFO info;
      50              :         CK_RV rv;
      51              : 
      52            7 :         test->module = mock_xdg_module_initialize_and_enter ();
      53            7 :         test->session = mock_xdg_module_open_session (TRUE);
      54              : 
      55            7 :         rv = gkm_module_C_Login (test->module, gkm_session_get_handle (test->session), CKU_USER, NULL, 0);
      56            7 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      57              : 
      58            7 :         rv = gkm_session_C_GetSessionInfo (test->session, &info);
      59            7 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      60              : 
      61            7 :         test->slot_id = info.slotID;
      62            7 : }
      63              : 
      64              : static void
      65            7 : teardown (Test *test, gconstpointer unused)
      66              : {
      67            7 :         mock_xdg_module_leave_and_finalize ();
      68            7 :         test->module = NULL;
      69            7 :         test->session = NULL;
      70            7 : }
      71              : 
      72              : static void
      73            1 : test_module_find_twice_is_same (Test *test, gconstpointer unused)
      74              : {
      75              :         CK_OBJECT_HANDLE objects[256];
      76              :         CK_ULONG n_objects;
      77              :         CK_ULONG n_check;
      78              :         CK_RV rv;
      79              : 
      80            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
      81            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      82            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects);
      83            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      84            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
      85            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      86              : 
      87            1 :         gkm_assert_cmpulong (n_objects, >, 0);
      88              : 
      89              :         /* Update the time on the file */
      90            1 :         mock_xdg_module_touch_file ("test-refer-1.trust", 1);
      91              : 
      92            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
      93            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      94            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check);
      95            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      96            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
      97            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      98              : 
      99              :         /* Should have same objects after reload */
     100            1 :         gkm_assert_cmpulong (n_check, ==, n_objects);
     101            1 : }
     102              : 
     103              : static void
     104            1 : test_module_file_becomes_invalid (Test *test, gconstpointer unused)
     105              : {
     106              :         CK_OBJECT_HANDLE objects[256];
     107              :         CK_ULONG n_objects;
     108              :         CK_ULONG n_check;
     109              :         CK_RV rv;
     110              : 
     111            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
     112            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     113            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects);
     114            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     115            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
     116            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     117              : 
     118            1 :         gkm_assert_cmpulong (n_objects, >, 0);
     119              : 
     120              :         /* Overwrite the file with empty */
     121            1 :         mock_xdg_module_empty_file ("test-refer-1.trust");
     122            1 :         mock_xdg_module_touch_file ("test-refer-1.trust", 2);
     123              : 
     124            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
     125            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     126            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check);
     127            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     128            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
     129            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     130              : 
     131              :         /* Should have less objects */
     132            1 :         gkm_assert_cmpulong (n_check, <, n_objects);
     133            1 : }
     134              : 
     135              : static void
     136            1 : test_module_file_remove (Test *test, gconstpointer unused)
     137              : {
     138              :         CK_OBJECT_HANDLE objects[256];
     139              :         CK_ULONG n_objects;
     140              :         CK_ULONG n_check;
     141              :         CK_RV rv;
     142              : 
     143            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
     144            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     145            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_objects);
     146            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     147            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
     148            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     149              : 
     150            1 :         gkm_assert_cmpulong (n_objects, >, 0);
     151              : 
     152              :         /* This file goes away */
     153            1 :         mock_xdg_module_remove_file ("test-refer-1.trust");
     154              : 
     155            1 :         rv = gkm_session_C_FindObjectsInit (test->session, NULL, 0);
     156            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     157            1 :         rv = gkm_session_C_FindObjects (test->session, objects, G_N_ELEMENTS (objects), &n_check);
     158            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     159            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
     160            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     161              : 
     162              :         /* Should have less objects */
     163            1 :         gkm_assert_cmpulong (n_check, <, n_objects);
     164            1 : }
     165              : 
     166              : static void
     167            1 : test_create_and_add_object (Test *test, gconstpointer unused)
     168              : {
     169            1 :         CK_OBJECT_HANDLE object = 0;
     170            1 :         CK_OBJECT_CLASS klass = CKO_CERTIFICATE;
     171            1 :         CK_CERTIFICATE_TYPE ctype = CKC_X_509;
     172            1 :         CK_BBOOL tval = CK_TRUE;
     173              :         gchar *data;
     174              :         gsize n_data;
     175              :         CK_RV rv;
     176              : 
     177            1 :         CK_ATTRIBUTE attrs[] = {
     178              :                 { CKA_VALUE, NULL, 0 },
     179              :                 { CKA_CLASS, &klass, sizeof (klass) },
     180              :                 { CKA_TOKEN, &tval, sizeof (tval) },
     181              :                 { CKA_CERTIFICATE_TYPE, &ctype, sizeof (ctype) }
     182              :         };
     183              : 
     184            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/xdg-store/fixtures/test-certificate-2.cer", &data, &n_data, NULL))
     185            0 :                 g_assert_not_reached ();
     186              : 
     187            1 :         attrs[0].pValue = data;
     188            1 :         attrs[0].ulValueLen = n_data;
     189              : 
     190            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &object);
     191            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     192            1 :         gkm_assert_cmpulong (object, !=, 0);
     193            1 :         g_free (data);
     194            1 : }
     195              : 
     196              : static void
     197            1 : test_destroy_object (Test *test, gconstpointer unused)
     198              : {
     199            1 :         CK_OBJECT_HANDLE object = 0;
     200            1 :         CK_CERTIFICATE_TYPE ctype = CKC_X_509;
     201            1 :         CK_ULONG n_objects = 0;
     202            1 :         CK_BBOOL tval = CK_TRUE;
     203              :         CK_RV rv;
     204              : 
     205            1 :         CK_ATTRIBUTE attrs[] = {
     206              :                 { CKA_CERTIFICATE_TYPE, &ctype, sizeof (ctype) },
     207              :                 { CKA_TOKEN, &tval, sizeof (tval) }
     208              :         };
     209              : 
     210            1 :         rv = gkm_session_C_FindObjectsInit (test->session, attrs, G_N_ELEMENTS (attrs));
     211            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     212            1 :         rv = gkm_session_C_FindObjects (test->session, &object, 1, &n_objects);
     213            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     214            1 :         gkm_assert_cmpulong (n_objects, ==, 1);
     215            1 :         rv = gkm_session_C_FindObjectsFinal (test->session);
     216            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     217              : 
     218              :         /* Destroy this object, which should be stored on the disk */
     219            1 :         rv = gkm_session_C_DestroyObject (test->session, object);
     220            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     221              : 
     222              :         /* Make sure it's really gone */
     223            1 :         rv = gkm_session_C_DestroyObject (test->session, object);
     224            1 :         gkm_assert_cmprv (rv, ==, CKR_OBJECT_HANDLE_INVALID);
     225            1 : }
     226              : 
     227              : static void
     228            1 : test_get_slot_info (Test *test, gconstpointer unused)
     229              : {
     230              :         CK_SLOT_INFO info;
     231              :         const gchar *str;
     232              :         CK_RV rv;
     233              : 
     234            1 :         rv = gkm_module_C_GetSlotInfo (test->module, test->slot_id, &info);
     235            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     236              : 
     237            1 :         str = g_strstr_len ((gchar*)info.slotDescription, sizeof (info.slotDescription),
     238              :                             "User Key Storage");
     239            1 :         g_assert (str != NULL);
     240            1 : }
     241              : 
     242              : static void
     243            1 : test_get_token_info (Test *test, gconstpointer unused)
     244              : {
     245              :         CK_TOKEN_INFO info;
     246              :         const gchar *str;
     247              :         CK_RV rv;
     248              : 
     249            1 :         rv = gkm_module_C_GetTokenInfo (test->module, test->slot_id, &info);
     250            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     251              : 
     252            1 :         str = g_strstr_len ((gchar*)info.label, sizeof (info.label),
     253              :                             "User Key Storage");
     254            1 :         g_assert (str != NULL);
     255            1 : }
     256              : 
     257              : 
     258              : static void
     259           29 : null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
     260              :                   const gchar *message, gpointer user_data)
     261              : {
     262              : 
     263           29 : }
     264              : 
     265              : int
     266            1 : main (int argc, char **argv)
     267              : {
     268              : #if !GLIB_CHECK_VERSION(2,35,0)
     269              :         g_type_init ();
     270              : #endif
     271            1 :         g_test_init (&argc, &argv, NULL);
     272              : 
     273              :         /* Suppress these messages in tests */
     274            1 :         g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
     275              :                            null_log_handler, NULL);
     276              : 
     277            1 :         g_test_add ("/xdg-store/module/module_find_twice_is_same", Test, NULL, setup, test_module_find_twice_is_same, teardown);
     278            1 :         g_test_add ("/xdg-store/module/module_file_becomes_invalid", Test, NULL, setup, test_module_file_becomes_invalid, teardown);
     279            1 :         g_test_add ("/xdg-store/module/module_file_remove", Test, NULL, setup, test_module_file_remove, teardown);
     280            1 :         g_test_add ("/xdg-store/module/create_and_add_object", Test, NULL, setup, test_create_and_add_object, teardown);
     281            1 :         g_test_add ("/xdg-store/module/destroy_object", Test, NULL, setup, test_destroy_object, teardown);
     282            1 :         g_test_add ("/xdg-store/module/get_slot_info", Test, NULL, setup, test_get_slot_info, teardown);
     283            1 :         g_test_add ("/xdg-store/module/get_token_info", Test, NULL, setup, test_get_token_info, teardown);
     284              : 
     285            1 :         return egg_tests_run_in_thread_with_loop ();
     286              : }
        

Generated by: LCOV version 2.0-1