LCOV - code coverage report
Current view: top level - pkcs11/gnome2-store - test-gnome2-private-key.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.7 % 55 51
Test Date: 2024-12-15 20:37:51 Functions: 100.0 % 8 8

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-object.c: Test GkmObject
       3              : 
       4              :    Copyright (C) 2012 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 <stefw@gnome.org>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "mock-gnome2-module.h"
      26              : 
      27              : #include "gnome2-store/gkm-gnome2-private-key.h"
      28              : 
      29              : #if 0
      30              : #include "gkm/gkm-attributes.h"
      31              : #include "gkm/gkm-certificate.h"
      32              : #include "gkm/gkm-object.h"
      33              : #endif
      34              : #include "gkm/gkm-data-der.h"
      35              : #include "gkm/gkm-module.h"
      36              : #include "gkm/gkm-serializable.h"
      37              : #include "gkm/gkm-session.h"
      38              : #include "gkm/gkm-test.h"
      39              : 
      40              : #include "egg/egg-testing.h"
      41              : 
      42              : #include "pkcs11i.h"
      43              : 
      44              : typedef struct {
      45              :         GkmModule *module;
      46              :         GkmSession *session;
      47              :         GBytes *key_data;
      48              :         GkmGnome2PrivateKey *key;
      49              : } Test;
      50              : 
      51              : static void
      52            2 : setup_basic (Test* test,
      53              :              gconstpointer unused)
      54              : {
      55              :         gchar *data;
      56              :         gsize length;
      57              : 
      58            2 :         test->module = mock_gnome2_module_initialize_and_enter ();
      59            2 :         test->session = mock_gnome2_module_open_session (TRUE);
      60              : 
      61            2 :         if (!g_file_get_contents (SRCDIR "/pkcs11/gnome2-store/fixtures/der-key-v2-des3.p8", &data, &length, NULL))
      62            0 :                 g_assert_not_reached ();
      63              : 
      64            2 :         test->key_data = g_bytes_new_take (data, length);
      65            2 : }
      66              : 
      67              : static void
      68            2 : teardown_basic (Test* test,
      69              :                 gconstpointer unused)
      70              : {
      71            2 :         g_bytes_unref (test->key_data);
      72            2 :         mock_gnome2_module_leave_and_finalize ();
      73            2 : }
      74              : 
      75              : static void
      76            1 : setup (Test *test,
      77              :        gconstpointer unused)
      78              : {
      79              :         GkmSecret *login;
      80              : 
      81            1 :         setup_basic (test, unused);
      82              : 
      83            1 :         test->key = g_object_new (GKM_TYPE_GNOME2_PRIVATE_KEY,
      84              :                                   "unique", "test-key",
      85              :                                   "module", gkm_session_get_module (test->session),
      86              :                                   "manager", gkm_session_get_manager (test->session),
      87              :                                   NULL);
      88              : 
      89            1 :         login = gkm_secret_new_from_password ("booo");
      90            1 :         if (!gkm_serializable_load (GKM_SERIALIZABLE (test->key), login, test->key_data))
      91            0 :                 g_assert_not_reached ();
      92            1 :         g_object_unref (login);
      93            1 : }
      94              : 
      95              : static void
      96            1 : teardown (Test *test,
      97              :           gconstpointer unused)
      98              : {
      99            1 :         g_clear_object (&test->key);
     100            1 :         teardown_basic (test, unused);
     101            1 : }
     102              : 
     103              : static void
     104            1 : test_load_private_key (Test *test,
     105              :                        gconstpointer unused)
     106              : {
     107              :         GkmGnome2PrivateKey *key;
     108              :         GkmSecret *login;
     109              : 
     110            1 :         key = g_object_new (GKM_TYPE_GNOME2_PRIVATE_KEY,
     111              :                             "unique", "test-key",
     112              :                             "module", gkm_session_get_module (test->session),
     113              :                             "manager", gkm_session_get_manager (test->session),
     114              :                             NULL);
     115              : 
     116              :         /* It's encrypted, this should fail */
     117            1 :         if (gkm_serializable_load (GKM_SERIALIZABLE (key), NULL, test->key_data))
     118            0 :                 g_assert_not_reached ();
     119              : 
     120            1 :         login = gkm_secret_new_from_password ("booo");
     121            1 :         if (!gkm_serializable_load (GKM_SERIALIZABLE (key), login, test->key_data))
     122            0 :                 g_assert_not_reached ();
     123            1 :         g_object_unref (login);
     124              : 
     125            1 :         g_object_unref (key);
     126            1 : }
     127              : 
     128              : static void
     129            1 : test_save_private_key (Test *test,
     130              :                        gconstpointer unused)
     131              : {
     132              :         GkmSecret *login;
     133              :         GBytes *data;
     134              :         gcry_sexp_t sexp;
     135              : 
     136              :         /* Save unencrypted */
     137            1 :         data = gkm_serializable_save (GKM_SERIALIZABLE (test->key), NULL);
     138            1 :         g_assert (data != NULL);
     139            1 :         g_assert (gkm_data_der_read_private_pkcs8_plain (data, &sexp) == GKM_DATA_SUCCESS);
     140            1 :         g_bytes_unref (data);
     141            1 :         gcry_sexp_release (sexp);
     142              : 
     143              :         /* Save encrypted */
     144            1 :         login = gkm_secret_new_from_password ("booo");
     145            1 :         data = gkm_serializable_save (GKM_SERIALIZABLE (test->key), login);
     146            1 :         g_assert (data != NULL);
     147            1 :         g_assert (gkm_data_der_read_private_pkcs8_crypted (data, "booo", 4, &sexp) == GKM_DATA_SUCCESS);
     148            1 :         g_bytes_unref (data);
     149            1 :         gcry_sexp_release (sexp);
     150            1 :         g_object_unref (login);
     151            1 : }
     152              : 
     153              : #if 0
     154              : static void
     155              : test_attribute_check_value (Test* test,
     156              :                             gconstpointer unused)
     157              : {
     158              :         gpointer data;
     159              :         gsize n_data;
     160              : 
     161              :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
     162              :                                               test->session, CKA_CHECK_VALUE, &n_data);
     163              : 
     164              :         egg_assert_cmpmem (data, n_data, ==, "\x36\x86\x35", 3);
     165              :         g_free (data);
     166              : }
     167              : 
     168              : static void
     169              : test_attribute_issuer (Test* test,
     170              :                        gconstpointer unused)
     171              : {
     172              :         gpointer data;
     173              :         gsize n_data;
     174              : 
     175              :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
     176              :                                               test->session, CKA_ISSUER, &n_data);
     177              : 
     178              :         egg_assert_cmpmem (data, n_data, ==, "\x30\x81\xCF\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x5A\x41\x31\x15\x30\x13\x06\x03\x55\x04\x08\x13\x0C\x57\x65\x73\x74\x65\x72\x6E\x20\x43\x61\x70\x65\x31\x12\x30\x10\x06\x03\x55\x04\x07\x13\x09\x43\x61\x70\x65\x20\x54\x6F\x77\x6E\x31\x1A\x30\x18\x06\x03\x55\x04\x0A\x13\x11\x54\x68\x61\x77\x74\x65\x20\x43\x6F\x6E\x73\x75\x6C\x74\x69\x6E\x67\x31\x28\x30\x26\x06\x03\x55\x04\x0B\x13\x1F\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x69\x6F\x6E\x20\x53\x65\x72\x76\x69\x63\x65\x73\x20\x44\x69\x76\x69\x73\x69\x6F\x6E\x31\x23\x30\x21\x06\x03\x55\x04\x03\x13\x1A\x54\x68\x61\x77\x74\x65\x20\x50\x65\x72\x73\x6F\x6E\x61\x6C\x20\x50\x72\x65\x6D\x69\x75\x6D\x20\x43\x41\x31\x2A\x30\x28\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01\x16\x1B\x70\x65\x72\x73\x6F\x6E\x61\x6C\x2D\x70\x72\x65\x6D\x69\x75\x6D\x40\x74\x68\x61\x77\x74\x65\x2E\x63\x6F\x6D", 210);
     179              :         g_free (data);
     180              : }
     181              : 
     182              : static void
     183              : test_attribute_subject (Test* test,
     184              :                         gconstpointer unused)
     185              : {
     186              :         gpointer data;
     187              :         gsize n_data;
     188              : 
     189              :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
     190              :                                               test->session, CKA_SUBJECT, &n_data);
     191              : 
     192              :         egg_assert_cmpmem (data, n_data, ==, "\x30\x81\xCF\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x5A\x41\x31\x15\x30\x13\x06\x03\x55\x04\x08\x13\x0C\x57\x65\x73\x74\x65\x72\x6E\x20\x43\x61\x70\x65\x31\x12\x30\x10\x06\x03\x55\x04\x07\x13\x09\x43\x61\x70\x65\x20\x54\x6F\x77\x6E\x31\x1A\x30\x18\x06\x03\x55\x04\x0A\x13\x11\x54\x68\x61\x77\x74\x65\x20\x43\x6F\x6E\x73\x75\x6C\x74\x69\x6E\x67\x31\x28\x30\x26\x06\x03\x55\x04\x0B\x13\x1F\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x69\x6F\x6E\x20\x53\x65\x72\x76\x69\x63\x65\x73\x20\x44\x69\x76\x69\x73\x69\x6F\x6E\x31\x23\x30\x21\x06\x03\x55\x04\x03\x13\x1A\x54\x68\x61\x77\x74\x65\x20\x50\x65\x72\x73\x6F\x6E\x61\x6C\x20\x50\x72\x65\x6D\x69\x75\x6D\x20\x43\x41\x31\x2A\x30\x28\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01\x16\x1B\x70\x65\x72\x73\x6F\x6E\x61\x6C\x2D\x70\x72\x65\x6D\x69\x75\x6D\x40\x74\x68\x61\x77\x74\x65\x2E\x63\x6F\x6D", 210);
     193              :         g_free (data);
     194              : }
     195              : 
     196              : static void
     197              : test_attribute_serial_number (Test* test,
     198              :                               gconstpointer unused)
     199              : {
     200              :         gpointer data;
     201              :         gsize n_data;
     202              : 
     203              :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
     204              :                                               test->session, CKA_SERIAL_NUMBER, &n_data);
     205              : 
     206              :         egg_assert_cmpmem (data, n_data, ==, "\x02\x01\x00", 3);
     207              :         g_free (data);
     208              : }
     209              : 
     210              : static void
     211              : test_attribute_value (Test* test,
     212              :                       gconstpointer unused)
     213              : {
     214              :         gconstpointer raw;
     215              :         gpointer data;
     216              :         gsize n_data, n_raw;
     217              : 
     218              :         data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
     219              :                                               test->session, CKA_VALUE, &n_data);
     220              : 
     221              :         raw = egg_bytes_get_data (test->certificate_data);
     222              :         n_raw = egg_bytes_get_size (test->certificate_data);
     223              :         egg_assert_cmpmem (data, n_data, ==, raw, n_raw);
     224              :         g_free (data);
     225              : }
     226              : 
     227              : static void
     228              : test_hash (Test* test,
     229              :            gconstpointer unused)
     230              : {
     231              :         gpointer hash;
     232              :         gsize n_hash;
     233              : 
     234              :         hash = gkm_certificate_hash (test->certificate, GCRY_MD_SHA1, &n_hash);
     235              : 
     236              :         egg_assert_cmpmem (hash, n_hash, ==, "\x36\x86\x35\x63\xFD\x51\x28\xC7\xBE\xA6\xF0\x05\xCF\xE9\xB4\x36\x68\x08\x6C\xCE", 20);
     237              :         g_free (hash);
     238              : }
     239              : #endif
     240              : 
     241              : static void
     242            8 : null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
     243              :                   const gchar *message, gpointer user_data)
     244              : {
     245              : 
     246            8 : }
     247              : 
     248              : int
     249            1 : main (int argc, char **argv)
     250              : {
     251              : #if !GLIB_CHECK_VERSION(2,35,0)
     252              :         g_type_init ();
     253              : #endif
     254            1 :         g_test_init (&argc, &argv, NULL);
     255              : 
     256              :         /* Suppress these messages in tests */
     257            1 :         g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
     258              :                            null_log_handler, NULL);
     259              : 
     260            1 :         g_test_add ("/gnome2-store/private-key/load", Test, NULL, setup_basic, test_load_private_key, teardown_basic);
     261            1 :         g_test_add ("/gnome2-store/private-key/save", Test, NULL, setup, test_save_private_key, teardown);
     262              : 
     263            1 :         return egg_tests_run_in_thread_with_loop ();
     264              : }
        

Generated by: LCOV version 2.0-1