LCOV - code coverage report
Current view: top level - pkcs11/ssh-store - test-private-key.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 71 71
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              : /* unit-test-private-key.c: Test SSH Key Private key functionality
       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-ssh-module.h"
      26              : 
      27              : #include "gkm/gkm-credential.h"
      28              : #include "gkm/gkm-session.h"
      29              : #include "gkm/gkm-module.h"
      30              : 
      31              : #include "ssh-store/gkm-ssh-private-key.h"
      32              : 
      33              : #include "egg/egg-testing.h"
      34              : 
      35              : #include "pkcs11i.h"
      36              : 
      37              : typedef struct {
      38              :         GkmModule *module;
      39              :         GkmSession *session;
      40              :         GkmSshPrivateKey *rsakey;
      41              :         GkmSshPrivateKey *dsakey;
      42              :         GkmSshPrivateKey *ecdsakey;
      43              : } Test;
      44              : 
      45              : static void
      46            5 : setup_basic (Test *test,
      47              :              gconstpointer unused)
      48              : {
      49            5 :         test->module = test_ssh_module_initialize_and_enter ();
      50            5 :         test->session = test_ssh_module_open_session (TRUE);
      51            5 : }
      52              : 
      53              : static void
      54            5 : teardown_basic (Test *test,
      55              :                 gconstpointer unused)
      56              : {
      57            5 :         test_ssh_module_leave_and_finalize ();
      58            5 : }
      59              : 
      60              : static void
      61            2 : setup (Test *test,
      62              :        gconstpointer unused)
      63              : {
      64              :         gboolean ret;
      65              : 
      66            2 :         setup_basic (test, unused);
      67              : 
      68              :         /* RSA */
      69            2 :         test->rsakey = gkm_ssh_private_key_new (test->module, "my-unique");
      70            2 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (test->rsakey));
      71              : 
      72            2 :         ret = gkm_ssh_private_key_parse (test->rsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted.pub",
      73              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted", NULL);
      74            2 :         g_assert (ret == TRUE);
      75              : 
      76              :         /* DSA */
      77            2 :         test->dsakey = gkm_ssh_private_key_new (test->module, "my-unique");
      78            2 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (test->dsakey));
      79              : 
      80            2 :         ret = gkm_ssh_private_key_parse (test->dsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted.pub",
      81              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted", NULL);
      82              : 
      83              :         /* ECDSA */
      84            2 :         test->ecdsakey = gkm_ssh_private_key_new (test->module, "my-unique");
      85            2 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (test->ecdsakey));
      86              : 
      87            2 :         ret = gkm_ssh_private_key_parse (test->ecdsakey, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted.pub",
      88              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted", NULL);
      89            2 : }
      90              : 
      91              : static void
      92            2 : teardown (Test *test,
      93              :           gconstpointer unused)
      94              : {
      95            2 :         g_object_unref (test->rsakey);
      96            2 :         g_object_unref (test->dsakey);
      97            2 :         g_object_unref (test->ecdsakey);
      98            2 :         teardown_basic (test, unused);
      99            2 : }
     100              : 
     101              : static void
     102            1 : test_parse_plain_rsa (Test *test, gconstpointer unused)
     103              : {
     104              :         GkmSshPrivateKey *key;
     105              :         gboolean ret;
     106              : 
     107            1 :         key = gkm_ssh_private_key_new (test->module, "my-unique");
     108            1 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
     109              : 
     110            1 :         ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain.pub",
     111              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain", NULL);
     112            1 :         g_assert (ret == TRUE);
     113              : 
     114            1 :         g_object_unref (key);
     115            1 : }
     116              : 
     117              : static void
     118            1 : test_parse_plain_dsa (Test *test, gconstpointer unused)
     119              : {
     120              :         GkmSshPrivateKey *key;
     121              :         gboolean ret;
     122              : 
     123            1 :         key = gkm_ssh_private_key_new (test->module, "my-unique");
     124            1 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
     125              : 
     126            1 :         ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain.pub",
     127              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain", NULL);
     128            1 :         g_assert (ret == TRUE);
     129              : 
     130            1 :         g_object_unref (key);
     131            1 : }
     132              : 
     133              : static void
     134            1 : test_parse_plain_ecdsa (Test *test, gconstpointer unused)
     135              : {
     136              :         GkmSshPrivateKey *key;
     137              :         gboolean ret;
     138              : 
     139            1 :         key = gkm_ssh_private_key_new (test->module, "my-unique");
     140            1 :         g_assert (GKM_IS_SSH_PRIVATE_KEY (key));
     141              : 
     142            1 :         ret = gkm_ssh_private_key_parse (key, SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain.pub",
     143              :                                          SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain", NULL);
     144            1 :         g_assert (ret == TRUE);
     145              : 
     146            1 :         g_object_unref (key);
     147            1 : }
     148              : 
     149              : static void
     150            1 : test_unlock (Test *test,
     151              :              gconstpointer unused)
     152              : {
     153              :         GkmCredential *cred;
     154              :         CK_RV rv;
     155              : 
     156              :         /* RSA */
     157            1 :         rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->rsakey),
     158              :                                     (guchar*)"password", 8, &cred);
     159            1 :         g_assert (rv == CKR_OK);
     160              : 
     161            1 :         g_object_unref (cred);
     162              : 
     163              :         /* DSA */
     164            1 :         rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->dsakey),
     165              :                                     (guchar*)"password", 8, &cred);
     166            1 :         g_assert (rv == CKR_OK);
     167              : 
     168            1 :         g_object_unref (cred);
     169              : 
     170              :         /* ECDSA */
     171            1 :         rv = gkm_credential_create (test->module, NULL, GKM_OBJECT (test->ecdsakey),
     172              :                                     (guchar*)"password", 8, &cred);
     173            1 :         g_assert (rv == CKR_OK);
     174              : 
     175            1 :         g_object_unref (cred);
     176            1 : }
     177              : 
     178              : static void
     179            1 : test_internal_sha1_compat (Test *test,
     180              :                            gconstpointer unused)
     181              : {
     182              :         gpointer data;
     183              :         gsize n_data;
     184              : 
     185            1 :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->dsakey), test->session,
     186              :                                               CKA_GNOME_INTERNAL_SHA1, &n_data);
     187              : 
     188            1 :         egg_assert_cmpmem (data, n_data, ==, "\x33\x37\x31\x31\x64\x33\x33\x65\x61\x34\x31\x31\x33\x61\x35\x64\x32\x35\x38\x37\x63\x36\x66\x32\x35\x66\x39\x35\x35\x36\x39\x66\x65\x65\x38\x31\x38\x35\x39\x34", 40);
     189            1 :         g_free (data);
     190            1 : }
     191              : 
     192              : int
     193            1 : main (int argc, char **argv)
     194              : {
     195              : #if !GLIB_CHECK_VERSION(2,35,0)
     196              :         g_type_init ();
     197              : #endif
     198            1 :         g_test_init (&argc, &argv, NULL);
     199              : 
     200            1 :         g_test_add ("/ssh-store/private-key/parse_plain_rsa", Test, NULL, setup_basic, test_parse_plain_rsa, teardown_basic);
     201            1 :         g_test_add ("/ssh-store/private-key/parse_plain_dsa", Test, NULL, setup_basic, test_parse_plain_dsa, teardown_basic);
     202            1 :         g_test_add ("/ssh-store/private-key/parse_plain_ecdsa", Test, NULL, setup_basic, test_parse_plain_ecdsa, teardown_basic);
     203            1 :         g_test_add ("/ssh-store/private-key/unlock", Test, NULL, setup, test_unlock, teardown);
     204            1 :         g_test_add ("/ssh-store/private-key/internal-sha1-compat", Test, NULL, setup, test_internal_sha1_compat, teardown);
     205              : 
     206            1 :         return g_test_run ();
     207              : }
        

Generated by: LCOV version 2.0-1