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

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-credential.c: Test credentials
       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-module.h"
      26              : #include "mock-locked-object.h"
      27              : 
      28              : #include "egg/egg-testing.h"
      29              : 
      30              : #include "gkm/gkm-attributes.h"
      31              : #include "gkm/gkm-credential.h"
      32              : #include "gkm/gkm-object.h"
      33              : #include "gkm/gkm-secret.h"
      34              : #include "gkm/gkm-session.h"
      35              : #include "gkm/gkm-module.h"
      36              : #include "gkm/gkm-test.h"
      37              : 
      38              : #include "pkcs11i.h"
      39              : 
      40              : typedef struct {
      41              :         GkmModule *module;
      42              :         GkmSession *session;
      43              :         GkmObject *object;
      44              : } Test;
      45              : 
      46              : static void
      47           11 : setup (Test *test, gconstpointer unused)
      48              : {
      49              :         CK_RV rv;
      50           11 :         test->module = mock_module_initialize_and_enter ();
      51           11 :         test->session = mock_module_open_session (TRUE);
      52              : 
      53           11 :         rv = gkm_module_C_Login (test->module, gkm_session_get_handle (test->session), CKU_USER, NULL, 0);
      54           11 :         g_assert (rv == CKR_OK);
      55              : 
      56           11 :         test->object = mock_locked_object_new (test->module, gkm_module_get_manager (test->module));
      57           11 :         gkm_object_expose (test->object, TRUE);
      58           11 : }
      59              : 
      60              : static void
      61           11 : teardown (Test *test, gconstpointer unused)
      62              : {
      63           11 :         g_object_unref (test->object);
      64           11 :         mock_module_leave_and_finalize ();
      65           11 : }
      66              : 
      67              : static void
      68            1 : test_create (Test *test, gconstpointer unused)
      69              : {
      70            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
      71            1 :         CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object);
      72              : 
      73            1 :         CK_ATTRIBUTE attrs[] = {
      74              :                 { CKA_CLASS, &klass, sizeof (klass) },
      75              :                 { CKA_G_OBJECT, &locked, sizeof (locked) },
      76              :                 { CKA_VALUE, "mock", 4 },
      77              :         };
      78              : 
      79              :         CK_OBJECT_HANDLE handle;
      80              :         CK_RV rv;
      81              : 
      82            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle);
      83            1 :         g_assert (rv == CKR_OK);
      84            1 :         g_assert (handle != 0);
      85              : 
      86            1 :         rv = gkm_session_C_DestroyObject (test->session, handle);
      87            1 :         g_assert (rv == CKR_OK);
      88            1 : }
      89              : 
      90              : static void
      91            1 : test_create_missing_pin (Test *test, gconstpointer unused)
      92              : {
      93            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
      94            1 :         CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object);
      95              : 
      96            1 :         CK_ATTRIBUTE attrs[] = {
      97              :                 { CKA_CLASS, &klass, sizeof (klass) },
      98              :                 { CKA_G_OBJECT, &locked, sizeof (locked) },
      99              :         };
     100              : 
     101              :         CK_OBJECT_HANDLE handle;
     102              :         CK_RV rv;
     103              : 
     104            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle);
     105            1 :         g_assert (rv == CKR_USER_NOT_LOGGED_IN);
     106            1 : }
     107              : 
     108              : static void
     109            1 : test_create_no_object (Test *test, gconstpointer unused)
     110              : {
     111            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     112            1 :         CK_BBOOL token = CK_FALSE;
     113            1 :         CK_OBJECT_HANDLE objhand = (CK_ULONG)-1;
     114              :         CK_ATTRIBUTE attr;
     115              : 
     116            1 :         CK_ATTRIBUTE attrs[] = {
     117              :                 { CKA_TOKEN, &token, sizeof (token) },
     118              :                 { CKA_CLASS, &klass, sizeof (klass) },
     119              :         };
     120              : 
     121              :         CK_OBJECT_HANDLE handle;
     122              :         CK_RV rv;
     123              : 
     124            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle);
     125            1 :         g_assert (rv == CKR_OK);
     126            1 :         g_assert (handle != 0);
     127              : 
     128            1 :         attr.type = CKA_G_OBJECT;
     129            1 :         attr.pValue = &objhand;
     130            1 :         attr.ulValueLen = sizeof (objhand);
     131            1 :         rv = gkm_session_C_GetAttributeValue (test->session, handle, &attr, 1);
     132            1 :         g_assert (rv == CKR_OK);
     133            1 :         g_assert (objhand == 0);
     134            1 : }
     135              : 
     136              : static void
     137            1 : test_create_invalid_object (Test *test, gconstpointer unused)
     138              : {
     139            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     140            1 :         CK_OBJECT_HANDLE locked = 0;
     141            1 :         CK_BBOOL token = CK_FALSE;
     142              : 
     143            1 :         CK_ATTRIBUTE attrs[] = {
     144              :                 { CKA_TOKEN, &token, sizeof (token) },
     145              :                 { CKA_CLASS, &klass, sizeof (klass) },
     146              :                 { CKA_G_OBJECT, &locked, sizeof (locked) },
     147              :         };
     148              : 
     149              :         CK_OBJECT_HANDLE handle;
     150              :         CK_RV rv;
     151              : 
     152            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle);
     153            1 :         g_assert (rv == CKR_OBJECT_HANDLE_INVALID);
     154            1 : }
     155              : 
     156              : static void
     157            1 : test_get_attributes (Test *test, gconstpointer unused)
     158              : {
     159            1 :         CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
     160            1 :         CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object);
     161              : 
     162            1 :         CK_ATTRIBUTE attrs[] = {
     163              :                 { CKA_CLASS, &klass, sizeof (klass) },
     164              :                 { CKA_G_OBJECT, &locked, sizeof (locked) },
     165              :                 { CKA_VALUE, "mock", 4 },
     166              :         };
     167              : 
     168              :         CK_OBJECT_HANDLE handle;
     169              :         CK_ATTRIBUTE check;
     170              :         CK_ULONG value;
     171              :         CK_RV rv;
     172              : 
     173            1 :         rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle);
     174            1 :         g_assert (rv == CKR_OK);
     175            1 :         g_assert (handle != 0);
     176              : 
     177            1 :         check.type = CKA_G_OBJECT;
     178            1 :         check.pValue = &value;
     179            1 :         check.ulValueLen = sizeof (value);
     180              : 
     181            1 :         rv = gkm_session_C_GetAttributeValue (test->session, handle, &check, 1);
     182            1 :         g_assert (rv == CKR_OK);
     183            1 :         g_assert (check.ulValueLen == sizeof (value));
     184            1 :         g_assert (value == locked);
     185            1 : }
     186              : 
     187              : static void
     188            1 : test_object_property (Test *test, gconstpointer unused)
     189              : {
     190              :         GkmCredential *auth;
     191              :         GkmObject *check;
     192              :         CK_RV rv;
     193              : 
     194            1 :         rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &auth);
     195            1 :         g_assert (rv == CKR_OK);
     196            1 :         g_assert (auth);
     197              : 
     198            1 :         g_object_get (auth, "object", &check, NULL);
     199            1 :         g_assert (check == test->object);
     200            1 :         g_object_unref (check);
     201              : 
     202            1 :         check = gkm_credential_get_object (auth);
     203            1 :         g_assert (check == test->object);
     204              : 
     205            1 :         g_object_unref (auth);
     206            1 : }
     207              : 
     208              : static void
     209            1 : test_login_property (Test *test, gconstpointer unused)
     210              : {
     211              :         GkmCredential *cred;
     212              :         GkmSecret *check, *secret;
     213              :         const gchar *password;
     214              :         gsize n_password;
     215              :         CK_RV rv;
     216              : 
     217            1 :         rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred);
     218            1 :         g_assert (rv == CKR_OK);
     219            1 :         g_assert (cred);
     220              : 
     221            1 :         g_object_get (cred, "secret", &check, NULL);
     222            1 :         g_assert (check);
     223            1 :         password = gkm_secret_get_password (check, &n_password);
     224            1 :         g_assert (n_password == 4);
     225            1 :         g_assert (memcmp (password, "mock", 4) == 0);
     226            1 :         g_object_unref (check);
     227              : 
     228            1 :         check = gkm_credential_get_secret (cred);
     229            1 :         g_assert (n_password == 4);
     230            1 :         g_assert (memcmp (password, "mock", 4) == 0);
     231              : 
     232            1 :         secret = gkm_secret_new ((guchar*)"xxx", -1);
     233            1 :         gkm_credential_set_secret (cred, secret);
     234            1 :         check = gkm_credential_get_secret (cred);
     235            1 :         g_assert (check == secret);
     236            1 :         g_object_unref (secret);
     237              : 
     238            1 :         g_object_unref (cred);
     239            1 : }
     240              : 
     241              : static GType
     242            1 : boxed_string (void)
     243              : {
     244              :         static GType type = 0;
     245            1 :         if (!type)
     246            1 :                 type = g_boxed_type_register_static ("TestBoxedString",
     247              :                                                      (GBoxedCopyFunc)g_strdup,
     248              :                                                      (GBoxedFreeFunc)g_free);
     249            1 :         return type;
     250              : }
     251              : 
     252              : static void
     253            1 : test_data (Test *test, gconstpointer unused)
     254              : {
     255              :         GkmCredential *cred;
     256            1 :         GType type = boxed_string ();
     257              :         gchar *check;
     258              :         CK_RV rv;
     259              : 
     260            1 :         rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred);
     261            1 :         g_assert (rv == CKR_OK);
     262            1 :         g_assert (cred);
     263              : 
     264            1 :         g_assert (gkm_credential_peek_data (cred, type) == NULL);
     265              : 
     266            1 :         gkm_credential_set_data (cred, type, "one");
     267              : 
     268            1 :         check = gkm_credential_pop_data (cred, type);
     269            1 :         g_assert_cmpstr ("one", ==, check);
     270            1 :         g_free (check);
     271              : 
     272            1 :         g_assert_cmpstr ("one", ==, gkm_credential_peek_data (cred, type));
     273              : 
     274            1 :         gkm_credential_set_data (cred, type, "ONE");
     275            1 :         g_assert_cmpstr ("ONE", ==, gkm_credential_peek_data (cred, type));
     276              : 
     277            1 :         gkm_credential_set_data (cred, 0, NULL);
     278            1 :         g_assert (gkm_credential_peek_data (cred, 0) == NULL);
     279              : 
     280            1 :         g_object_unref (cred);
     281            1 : }
     282              : 
     283              : static void
     284            1 : test_connect_object (Test *test, gconstpointer unused)
     285              : {
     286              :         GkmCredential *cred;
     287              :         CK_RV rv;
     288              : 
     289            1 :         rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred);
     290            1 :         g_assert (rv == CKR_OK);
     291            1 :         g_assert (cred);
     292              : 
     293            1 :         gkm_credential_connect (cred, test->object);
     294            1 :         g_assert (gkm_credential_get_object (cred) == test->object);
     295              : 
     296            1 :         g_object_unref (cred);
     297            1 : }
     298              : 
     299              : static void
     300            1 : test_value_is_accessible_to_daemon (Test *test, gconstpointer unused)
     301              : {
     302              :         GkmCredential *cred;
     303              :         gchar buffer[20];
     304              :         CK_ATTRIBUTE attr;
     305              :         CK_RV rv;
     306              : 
     307            1 :         rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred);
     308            1 :         g_assert (rv == CKR_OK);
     309            1 :         g_assert (cred);
     310              : 
     311            1 :         attr.type = CKA_VALUE;
     312            1 :         attr.pValue = buffer;
     313            1 :         attr.ulValueLen = sizeof (buffer);
     314              : 
     315            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (cred), test->session, &attr);
     316            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     317            1 :         egg_assert_cmpmem ("mock", 4, ==, attr.pValue, attr.ulValueLen);
     318              : 
     319            1 :         g_object_unref (cred);
     320            1 : }
     321              : 
     322              : static void
     323            1 : test_value_is_inaccessible_to_applications (Test *test, gconstpointer unused)
     324              : {
     325              :         GkmCredential *cred;
     326              :         CK_G_APPLICATION app;
     327              :         gchar buffer[20];
     328              :         CK_ATTRIBUTE attr;
     329              :         CK_SESSION_HANDLE handle;
     330              :         GkmSession *session;
     331              :         CK_RV rv;
     332              : 
     333            1 :         memset (&app, 0, sizeof (app));
     334            1 :         rv = gkm_module_C_OpenSession (test->module, 1, CKF_SERIAL_SESSION | CKF_G_APPLICATION_SESSION, &app, NULL, &handle);
     335            1 :         gkm_assert_cmprv (rv, ==, CKR_OK);
     336              : 
     337            1 :         session = gkm_module_lookup_session (test->module, handle);
     338            1 :         g_assert (session);
     339              : 
     340            1 :         rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred);
     341            1 :         g_assert (rv == CKR_OK);
     342            1 :         g_assert (cred);
     343              : 
     344            1 :         attr.type = CKA_VALUE;
     345            1 :         attr.pValue = buffer;
     346            1 :         attr.ulValueLen = sizeof (buffer);
     347              : 
     348            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (cred), session, &attr);
     349            1 :         gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_SENSITIVE);
     350              : 
     351            1 :         g_object_unref (cred);
     352            1 : }
     353              : 
     354              : 
     355              : int
     356            1 : main (int argc, char **argv)
     357              : {
     358              : #if !GLIB_CHECK_VERSION(2,35,0)
     359              :         g_type_init ();
     360              : #endif
     361            1 :         g_test_init (&argc, &argv, NULL);
     362              : 
     363            1 :         g_test_add ("/gkm/credential/create", Test, NULL, setup, test_create, teardown);
     364            1 :         g_test_add ("/gkm/credential/create_missing_pin", Test, NULL, setup, test_create_missing_pin, teardown);
     365            1 :         g_test_add ("/gkm/credential/create_no_object", Test, NULL, setup, test_create_no_object, teardown);
     366            1 :         g_test_add ("/gkm/credential/create_invalid_object", Test, NULL, setup, test_create_invalid_object, teardown);
     367            1 :         g_test_add ("/gkm/credential/get_attributes", Test, NULL, setup, test_get_attributes, teardown);
     368            1 :         g_test_add ("/gkm/credential/object_property", Test, NULL, setup, test_object_property, teardown);
     369            1 :         g_test_add ("/gkm/credential/login_property", Test, NULL, setup, test_login_property, teardown);
     370            1 :         g_test_add ("/gkm/credential/data", Test, NULL, setup, test_data, teardown);
     371            1 :         g_test_add ("/gkm/credential/connect_object", Test, NULL, setup, test_connect_object, teardown);
     372            1 :         g_test_add ("/gkm/credential/value_is_accessible_to_daemon", Test, NULL, setup, test_value_is_accessible_to_daemon, teardown);
     373            1 :         g_test_add ("/gkm/credential/value_is_inaccessible_to_applications", Test, NULL, setup, test_value_is_inaccessible_to_applications, teardown);
     374              : 
     375            1 :         return g_test_run ();
     376              : }
        

Generated by: LCOV version 2.0-1