LCOV - code coverage report
Current view: top level - pkcs11/wrap-layer - test-create-credential.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 70 70
Test Date: 2024-04-08 13:24:42 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /*
       2              :  * gnome-keyring
       3              :  *
       4              :  * Copyright (C) 2010 Stefan Walter
       5              :  *
       6              :  * This program is free software; you can redistribute it and/or modify
       7              :  * it under the terms of the GNU Lesser General Public License as
       8              :  * published by the Free Software Foundation; either version 2.1 of
       9              :  * the License, or (at your option) any later version.
      10              :  *
      11              :  * This program is distributed in the hope that it will be useful, but
      12              :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :  * Lesser General Public License for more details.
      15              :  *
      16              :  * You should have received a copy of the GNU Lesser General Public
      17              :  * License along with this program; if not, see
      18              :  * <http://www.gnu.org/licenses/>.
      19              :  */
      20              : 
      21              : #include "config.h"
      22              : 
      23              : #include "wrap-layer/gkm-wrap-layer.h"
      24              : #include "wrap-layer/gkm-wrap-prompt.h"
      25              : 
      26              : #include "gkm/gkm-mock.h"
      27              : #include "gkm/gkm-test.h"
      28              : 
      29              : #include "egg/egg-testing.h"
      30              : 
      31              : #include <gcr/gcr-base.h>
      32              : 
      33              : #include <glib-object.h>
      34              : 
      35              : #include <string.h>
      36              : 
      37              : typedef struct {
      38              :         CK_FUNCTION_LIST functions;
      39              :         CK_FUNCTION_LIST_PTR module;
      40              :         CK_SESSION_HANDLE session;
      41              :         CK_OBJECT_HANDLE object;
      42              : } Test;
      43              : 
      44              : static void
      45            3 : setup (Test *test, gconstpointer unused)
      46              : {
      47              :         CK_FUNCTION_LIST_PTR funcs;
      48              :         CK_SLOT_ID slot_id;
      49            3 :         CK_ULONG n_slots = 1;
      50              :         const gchar *prompter;
      51              :         CK_ULONG count;
      52              :         CK_RV rv;
      53              : 
      54            3 :         CK_BBOOL always = TRUE;
      55            3 :         CK_ATTRIBUTE attrs[] = {
      56              :                 { CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
      57              :         };
      58              : 
      59              :         /* Always start off with test functions */
      60            3 :         rv = gkm_mock_C_GetFunctionList (&funcs);
      61            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      62            3 :         memcpy (&test->functions, funcs, sizeof (test->functions));
      63              : 
      64            3 :         gkm_wrap_layer_reset_modules ();
      65            3 :         gkm_wrap_layer_add_module (&test->functions);
      66            3 :         test->module = gkm_wrap_layer_get_functions ();
      67              : 
      68            3 :         prompter = gcr_mock_prompter_start ();
      69            3 :         gkm_wrap_prompt_set_prompter_name (prompter);
      70              : 
      71              :         /* Open a test->session */
      72            3 :         rv = (test->module->C_Initialize) (NULL);
      73            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      74              : 
      75            3 :         rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
      76            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      77              : 
      78            3 :         rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
      79            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      80              : 
      81              :         /* Find the always authenticate test->object */
      82            3 :         rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1);
      83            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      84              : 
      85            3 :         rv = (test->module->C_FindObjects) (test->session, &test->object, 1, &count);
      86            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      87            3 :         gkm_assert_cmpulong (count, ==, 1);
      88            3 :         gkm_assert_cmpulong (test->object, !=, 0);
      89              : 
      90            3 :         rv = (test->module->C_FindObjectsFinal) (test->session);
      91            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
      92            3 : }
      93              : 
      94              : static void
      95            3 : teardown (Test *test, gconstpointer unused)
      96              : {
      97              :         CK_RV rv;
      98              : 
      99            3 :         g_assert (!gcr_mock_prompter_is_expecting ());
     100            3 :         gcr_mock_prompter_stop ();
     101              : 
     102            3 :         rv = (test->module->C_CloseSession) (test->session);
     103            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     104              : 
     105            3 :         rv = (test->module->C_Finalize) (NULL);
     106            3 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     107            3 : }
     108              : 
     109              : static void
     110            1 : test_ok_password (Test *test, gconstpointer unused)
     111              : {
     112            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     113            1 :         CK_ATTRIBUTE attrs[] = {
     114              :                 { CKA_CLASS, &klass, sizeof (klass) },
     115            1 :                 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
     116              :                 { CKA_VALUE, NULL, 0 }
     117              :         };
     118              : 
     119            1 :         CK_OBJECT_HANDLE cred = 0;
     120              :         CK_RV rv;
     121              : 
     122            1 :         gcr_mock_prompter_expect_password_ok ("booo", NULL);
     123              : 
     124            1 :         rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
     125            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     126            1 :         gkm_assert_cmpulong (cred, !=, 0);
     127            1 : }
     128              : 
     129              : static void
     130            1 : test_bad_password_then_cancel (Test *test, gconstpointer unused)
     131              : {
     132            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     133            1 :         CK_ATTRIBUTE attrs[] = {
     134              :                 { CKA_CLASS, &klass, sizeof (klass) },
     135            1 :                 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
     136              :                 { CKA_VALUE, NULL, 0 }
     137              :         };
     138              : 
     139            1 :         CK_OBJECT_HANDLE cred = 0;
     140              :         CK_RV rv;
     141              : 
     142            1 :         gcr_mock_prompter_expect_password_ok ("bad password", NULL);
     143            1 :         gcr_mock_prompter_expect_password_cancel ();
     144              : 
     145            1 :         rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
     146            1 :         gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
     147            1 : }
     148              : 
     149              : static void
     150            1 : test_cancel_immediately (Test *test, gconstpointer unused)
     151              : {
     152            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     153            1 :         CK_ATTRIBUTE attrs[] = {
     154              :                 { CKA_CLASS, &klass, sizeof (klass) },
     155            1 :                 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
     156              :                 { CKA_VALUE, NULL, 0 }
     157              :         };
     158              : 
     159            1 :         CK_OBJECT_HANDLE cred = 0;
     160              :         CK_RV rv;
     161              : 
     162            1 :         gcr_mock_prompter_expect_password_cancel ();
     163              : 
     164            1 :         rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
     165            1 :         gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
     166            1 : }
     167              : 
     168              : int
     169            1 : main (int argc, char **argv)
     170              : {
     171              : #if !GLIB_CHECK_VERSION(2,35,0)
     172              :         g_type_init ();
     173              : #endif
     174            1 :         g_test_init (&argc, &argv, NULL);
     175              : 
     176            1 :         g_test_add ("/wrap-layer/create-credential/ok_password", Test, NULL, setup, test_ok_password, teardown);
     177            1 :         g_test_add ("/wrap-layer/create-credential/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
     178            1 :         g_test_add ("/wrap-layer/create-credential/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
     179              : 
     180            1 :         return egg_tests_run_in_thread_with_loop ();
     181              : }
        

Generated by: LCOV version 2.0-1