LCOV - code coverage report
Current view: top level - gcr - test-subject-public-key.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 339 339 100.0 %
Date: 2022-09-04 10:20:22 Functions: 47 47 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 76 144 52.8 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /*
       3                 :            :    Copyright (C) 2010 Collabora Ltd
       4                 :            : 
       5                 :            :    The Gnome Keyring Library is free software; you can redistribute it and/or
       6                 :            :    modify it under the terms of the GNU Library General Public License as
       7                 :            :    published by the Free Software Foundation; either version 2 of the
       8                 :            :    License, or (at your option) any later version.
       9                 :            : 
      10                 :            :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      11                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13                 :            :    Library General Public License for more details.
      14                 :            : 
      15                 :            :    You should have received a copy of the GNU Library General Public
      16                 :            :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      17                 :            :    see <http://www.gnu.org/licenses/>.
      18                 :            : 
      19                 :            :    Author: Stef Walter <stefw@collabora.co.uk>
      20                 :            : */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : 
      24                 :            : #include "gcr/gcr.h"
      25                 :            : #include "gcr/gcr-subject-public-key.h"
      26                 :            : 
      27                 :            : #include "gck/gck-mock.h"
      28                 :            : #include "gck/gck-test.h"
      29                 :            : 
      30                 :            : #include "egg/egg-asn1x.h"
      31                 :            : #include "egg/egg-asn1-defs.h"
      32                 :            : #include "egg/egg-testing.h"
      33                 :            : 
      34                 :            : #include <glib.h>
      35                 :            : 
      36                 :            : #include <errno.h>
      37                 :            : 
      38                 :            : typedef struct {
      39                 :            :         const gchar *name;
      40                 :            :         const gchar *basename;
      41                 :            :         guint key_size;
      42                 :            : } TestFixture;
      43                 :            : 
      44                 :            : typedef struct {
      45                 :            :         GBytes *crt_data;
      46                 :            :         GckAttributes *crt_attrs;
      47                 :            :         GBytes *key_data;
      48                 :            :         GckAttributes *prv_attrs;
      49                 :            :         GBytes *spk_data;
      50                 :            :         GckAttributes *pub_attrs;
      51                 :            : } TestAttributes;
      52                 :            : 
      53                 :            : static void
      54                 :        162 : on_parser_parsed (GcrParser *parser,
      55                 :            :                   gpointer user_data)
      56                 :            : {
      57                 :        162 :         GckAttributes **attrs = user_data;
      58         [ -  + ]:        162 :         g_assert (*attrs == NULL);
      59                 :        162 :         *attrs = gcr_parser_get_parsed_attributes (parser);
      60         [ -  + ]:        162 :         g_assert (*attrs != NULL);
      61                 :        162 :         gck_attributes_ref (*attrs);
      62                 :        162 : }
      63                 :            : 
      64                 :            : static GckAttributes *
      65                 :        162 : parse_attributes (GBytes *data,
      66                 :            :                   GcrDataFormat format)
      67                 :            : {
      68                 :            :         GcrParser *parser;
      69                 :        162 :         GckAttributes *attrs = NULL;
      70                 :        162 :         GError *error = NULL;
      71                 :            : 
      72                 :        162 :         parser = gcr_parser_new ();
      73                 :        162 :         gcr_parser_format_disable (parser, GCR_FORMAT_ALL);
      74                 :        162 :         gcr_parser_format_enable (parser, format);
      75                 :        162 :         g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &attrs);
      76                 :        162 :         gcr_parser_parse_bytes (parser, data, &error);
      77         [ -  + ]:        162 :         g_assert_no_error (error);
      78                 :        162 :         g_object_unref (parser);
      79                 :            : 
      80         [ -  + ]:        162 :         g_assert (attrs);
      81                 :        162 :         return attrs;
      82                 :            : }
      83                 :            : 
      84                 :            : static void
      85                 :         54 : setup_attributes (TestAttributes *test,
      86                 :            :                   gconstpointer data)
      87                 :            : {
      88                 :         54 :         const TestFixture *fixture = data;
      89                 :         54 :         GError *error = NULL;
      90                 :            :         gchar *contents;
      91                 :            :         gchar *filename;
      92                 :            :         gsize length;
      93                 :            :         gulong klass;
      94                 :            : 
      95                 :         54 :         filename = g_strdup_printf (SRCDIR "/gcr/fixtures/%s.crt", fixture->basename);
      96                 :         54 :         g_file_get_contents (filename, &contents, &length, &error);
      97         [ -  + ]:         54 :         g_assert_no_error (error);
      98                 :         54 :         test->crt_data = g_bytes_new_take (contents, length);
      99                 :         54 :         test->crt_attrs = parse_attributes (test->crt_data, GCR_FORMAT_DER_CERTIFICATE_X509);
     100         [ -  + ]:         54 :         g_assert (gck_attributes_find_ulong (test->crt_attrs, CKA_CLASS, &klass));
     101         [ -  + ]:         54 :         gck_assert_cmpulong (klass, ==, CKO_CERTIFICATE);
     102                 :         54 :         g_free (filename);
     103                 :            : 
     104                 :         54 :         filename = g_strdup_printf (SRCDIR "/gcr/fixtures/%s.key", fixture->basename);
     105                 :         54 :         g_file_get_contents (filename, &contents, &length, &error);
     106         [ -  + ]:         54 :         g_assert_no_error (error);
     107                 :         54 :         test->key_data = g_bytes_new_take (contents, length);
     108                 :         54 :         test->prv_attrs = parse_attributes (test->key_data, GCR_FORMAT_ALL);
     109         [ -  + ]:         54 :         g_assert (gck_attributes_find_ulong (test->prv_attrs, CKA_CLASS, &klass));
     110         [ -  + ]:         54 :         gck_assert_cmpulong (klass, ==, CKO_PRIVATE_KEY);
     111                 :         54 :         g_free (filename);
     112                 :            : 
     113                 :         54 :         filename = g_strdup_printf (SRCDIR "/gcr/fixtures/%s.spk", fixture->basename);
     114                 :         54 :         g_file_get_contents (filename, &contents, &length, &error);
     115         [ -  + ]:         54 :         g_assert_no_error (error);
     116                 :         54 :         test->spk_data = g_bytes_new_take (contents, length);
     117                 :         54 :         test->pub_attrs = parse_attributes (test->spk_data, GCR_FORMAT_DER_SUBJECT_PUBLIC_KEY);
     118         [ -  + ]:         54 :         g_assert (gck_attributes_find_ulong (test->pub_attrs, CKA_CLASS, &klass));
     119         [ -  + ]:         54 :         gck_assert_cmpulong (klass, ==, CKO_PUBLIC_KEY);
     120                 :         54 :         g_free (filename);
     121                 :         54 : }
     122                 :            : 
     123                 :            : static void
     124                 :         54 : teardown_attributes (TestAttributes *test,
     125                 :            :                      gconstpointer unused)
     126                 :            : {
     127                 :         54 :         g_bytes_unref (test->crt_data);
     128                 :         54 :         g_bytes_unref (test->key_data);
     129                 :         54 :         g_bytes_unref (test->spk_data);
     130                 :         54 :         gck_attributes_unref (test->crt_attrs);
     131                 :         54 :         gck_attributes_unref (test->prv_attrs);
     132                 :         54 :         gck_attributes_unref (test->pub_attrs);
     133                 :         54 : }
     134                 :            : 
     135                 :            : static void
     136                 :          9 : perform_for_attributes (TestAttributes *test,
     137                 :            :                         GckAttributes *attrs)
     138                 :            : {
     139                 :            :         GNode *info;
     140                 :            :         GBytes *data;
     141                 :            : 
     142                 :          9 :         info = _gcr_subject_public_key_for_attributes (attrs);
     143         [ -  + ]:          9 :         g_assert (info != NULL);
     144                 :            : 
     145                 :          9 :         data = egg_asn1x_encode (info, NULL);
     146   [ +  -  +  - ]:          9 :         egg_assert_cmpbytes (data, ==, g_bytes_get_data (test->spk_data, NULL),
     147                 :            :                                        g_bytes_get_size (test->spk_data));
     148                 :            : 
     149                 :          9 :         g_bytes_unref (data);
     150                 :          9 :         egg_asn1x_destroy (info);
     151                 :            : 
     152                 :          9 : }
     153                 :            : 
     154                 :            : static void
     155                 :          3 : test_for_cert_attributes (TestAttributes *test,
     156                 :            :                           gconstpointer unused)
     157                 :            : {
     158                 :          3 :         perform_for_attributes (test, test->crt_attrs);
     159                 :          3 : }
     160                 :            : 
     161                 :            : static void
     162                 :          3 : test_for_private_key_attributes (TestAttributes *test,
     163                 :            :                                  gconstpointer unused)
     164                 :            : {
     165                 :          3 :         perform_for_attributes (test, test->prv_attrs);
     166                 :          3 : }
     167                 :            : 
     168                 :            : static void
     169                 :          3 : test_for_public_key_attributes (TestAttributes *test,
     170                 :            :                                 gconstpointer unused)
     171                 :            : {
     172                 :          3 :         perform_for_attributes (test, test->pub_attrs);
     173                 :          3 : }
     174                 :            : 
     175                 :            : static void
     176                 :          9 : perform_calculate_size (TestAttributes *test,
     177                 :            :                         GckAttributes *attrs,
     178                 :            :                         const TestFixture *fixture)
     179                 :            : {
     180                 :            :         GNode *info;
     181                 :            :         guint size;
     182                 :            : 
     183                 :          9 :         info = _gcr_subject_public_key_for_attributes (attrs);
     184         [ -  + ]:          9 :         g_assert (info != NULL);
     185                 :            : 
     186                 :            :         /* TODO: until encoding, we don't have readable attributes */
     187                 :          9 :         g_bytes_unref (egg_asn1x_encode (info, NULL));
     188                 :            : 
     189                 :          9 :         size = _gcr_subject_public_key_calculate_size (info);
     190         [ -  + ]:          9 :         g_assert_cmpuint (size, ==, fixture->key_size);
     191                 :            : 
     192                 :          9 :         egg_asn1x_destroy (info);
     193                 :            : 
     194                 :          9 : }
     195                 :            : 
     196                 :            : static void
     197                 :          3 : test_certificate_calculate_size (TestAttributes *test,
     198                 :            :                                  gconstpointer fixture)
     199                 :            : {
     200                 :          3 :         perform_calculate_size (test, test->crt_attrs, fixture);
     201                 :          3 : }
     202                 :            : 
     203                 :            : static void
     204                 :          3 : test_public_key_calculate_size (TestAttributes *test,
     205                 :            :                                 gconstpointer fixture)
     206                 :            : {
     207                 :          3 :         perform_calculate_size (test, test->pub_attrs, fixture);
     208                 :          3 : }
     209                 :            : 
     210                 :            : static void
     211                 :          3 : test_private_key_calculate_size (TestAttributes *test,
     212                 :            :                                  gconstpointer fixture)
     213                 :            : {
     214                 :          3 :         perform_calculate_size (test, test->prv_attrs, fixture);
     215                 :          3 : }
     216                 :            : 
     217                 :            : typedef struct {
     218                 :            :         CK_FUNCTION_LIST funcs;
     219                 :            :         GckModule *module;
     220                 :            :         GckSession *session;
     221                 :            : } TestModule;
     222                 :            : 
     223                 :            : static void
     224                 :         38 : setup_module (TestModule *test,
     225                 :            :                gconstpointer unused)
     226                 :            : {
     227                 :            :         CK_FUNCTION_LIST_PTR f;
     228                 :         38 :         GError *error = NULL;
     229                 :            :         GckSlot *slot;
     230                 :            :         CK_RV rv;
     231                 :            : 
     232                 :         38 :         rv = gck_mock_C_GetFunctionList (&f);
     233         [ -  + ]:         38 :         gck_assert_cmprv (rv, ==, CKR_OK);
     234                 :         38 :         memcpy (&test->funcs, f, sizeof (test->funcs));
     235                 :            : 
     236                 :            :         /* Open a session */
     237                 :         38 :         rv = (test->funcs.C_Initialize) (NULL);
     238         [ -  + ]:         38 :         gck_assert_cmprv (rv, ==, CKR_OK);
     239                 :            : 
     240                 :         38 :         test->module = gck_module_new (&test->funcs);
     241                 :         38 :         g_object_add_weak_pointer (G_OBJECT (test->module), (gpointer *)&test->module);
     242                 :            : 
     243                 :         38 :         slot = gck_slot_from_handle (test->module, GCK_MOCK_SLOT_ONE_ID);
     244                 :         38 :         test->session = gck_session_open (slot, GCK_SESSION_READ_ONLY, NULL, NULL, &error);
     245         [ -  + ]:         38 :         g_assert_no_error (error);
     246                 :         38 :         g_object_add_weak_pointer (G_OBJECT (test->session), (gpointer *)&test->session);
     247                 :            : 
     248                 :         38 :         g_object_unref (slot);
     249                 :         38 : }
     250                 :            : 
     251                 :            : static void
     252                 :         38 : teardown_module (TestModule *test,
     253                 :            :                  gconstpointer fixture)
     254                 :            : {
     255                 :            :         CK_RV rv;
     256                 :            : 
     257                 :         38 :         g_object_unref (test->session);
     258                 :         38 :         g_object_unref (test->module);
     259                 :            : 
     260   [ -  +  -  + ]:         38 :         egg_test_wait_for_gtask_thread (test->session || test->module);
     261                 :            : 
     262         [ -  + ]:         38 :         g_assert (test->session == NULL);
     263         [ -  + ]:         38 :         g_assert (test->module == NULL);
     264                 :            : 
     265                 :         38 :         rv = (test->funcs.C_Finalize) (NULL);
     266         [ -  + ]:         38 :         gck_assert_cmprv (rv, ==, CKR_OK);
     267                 :         38 : }
     268                 :            : 
     269                 :            : typedef struct {
     270                 :            :         TestAttributes at;
     271                 :            :         TestModule mo;
     272                 :            :         GckObject *crt_object;
     273                 :            :         GckObject *pub_object;
     274                 :            :         GckObject *prv_object;
     275                 :            : } TestLoading;
     276                 :            : 
     277                 :            : static void
     278                 :         36 : setup_loading (TestLoading *test,
     279                 :            :                gconstpointer fixture)
     280                 :            : {
     281                 :         36 :         GckBuilder builder = GCK_BUILDER_INIT;
     282                 :         36 :         const gchar *id = "test-id";
     283                 :            :         gulong handle;
     284                 :            : 
     285                 :         36 :         setup_attributes (&test->at, fixture);
     286                 :         36 :         setup_module (&test->mo, NULL);
     287                 :            : 
     288                 :         36 :         gck_builder_add_all (&builder, test->at.crt_attrs);
     289                 :         36 :         gck_builder_add_string (&builder, CKA_ID, id);
     290                 :         36 :         handle = gck_mock_module_add_object (gck_builder_end (&builder));
     291                 :         36 :         test->crt_object = gck_object_from_handle (test->mo.session, handle);
     292                 :         36 :         g_object_add_weak_pointer (G_OBJECT (test->crt_object), (gpointer *)&test->crt_object);
     293                 :            : 
     294                 :         36 :         gck_builder_add_all (&builder, test->at.pub_attrs);
     295                 :         36 :         gck_builder_add_string (&builder, CKA_ID, id);
     296                 :         36 :         handle = gck_mock_module_add_object (gck_builder_end (&builder));
     297                 :         36 :         test->pub_object = gck_object_from_handle (test->mo.session, handle);
     298                 :         36 :         g_object_add_weak_pointer (G_OBJECT (test->pub_object), (gpointer *)&test->pub_object);
     299                 :            : 
     300                 :         36 :         gck_builder_add_all (&builder, test->at.prv_attrs);
     301                 :         36 :         gck_builder_add_string (&builder, CKA_ID, id);
     302                 :         36 :         handle = gck_mock_module_add_object (gck_builder_end (&builder));
     303                 :         36 :         test->prv_object = gck_object_from_handle (test->mo.session, handle);
     304                 :         36 :         g_object_add_weak_pointer (G_OBJECT (test->prv_object), (gpointer *)&test->prv_object);
     305                 :         36 : }
     306                 :            : 
     307                 :            : static void
     308                 :         36 : teardown_loading (TestLoading *test,
     309                 :            :                   gconstpointer fixture)
     310                 :            : {
     311                 :         36 :         g_object_unref (test->crt_object);
     312                 :         36 :         g_object_unref (test->prv_object);
     313                 :         36 :         g_object_unref (test->pub_object);
     314                 :            : 
     315   [ -  +  -  +  :         36 :         egg_test_wait_for_gtask_thread (test->crt_object || test->prv_object || test->pub_object);
                   -  + ]
     316                 :            : 
     317         [ -  + ]:         36 :         g_assert (test->crt_object == NULL);
     318         [ -  + ]:         36 :         g_assert (test->prv_object == NULL);
     319         [ -  + ]:         36 :         g_assert (test->pub_object == NULL);
     320                 :            : 
     321                 :         36 :         teardown_module (&test->mo, NULL);
     322                 :         36 :         teardown_attributes (&test->at, fixture);
     323                 :         36 : }
     324                 :            : 
     325                 :            : static void
     326                 :          9 : perform_load (TestLoading *test,
     327                 :            :               GckObject *object)
     328                 :            : {
     329                 :          9 :         GError *error = NULL;
     330                 :            :         GBytes *data;
     331                 :            :         GNode *info;
     332                 :            : 
     333                 :          9 :         info = _gcr_subject_public_key_load (object, NULL, &error);
     334         [ -  + ]:          9 :         g_assert_no_error (error);
     335         [ -  + ]:          9 :         g_assert (info != NULL);
     336                 :            : 
     337                 :          9 :         data = egg_asn1x_encode (info, NULL);
     338   [ +  -  +  - ]:          9 :         egg_assert_cmpbytes (data, ==, g_bytes_get_data (test->at.spk_data, NULL),
     339                 :            :                                        g_bytes_get_size (test->at.spk_data));
     340                 :            : 
     341                 :          9 :         g_bytes_unref (data);
     342                 :          9 :         egg_asn1x_destroy (info);
     343                 :          9 : }
     344                 :            : 
     345                 :            : static void
     346                 :          3 : test_certificate_load (TestLoading *test,
     347                 :            :                        gconstpointer unused)
     348                 :            : {
     349                 :          3 :         perform_load (test, test->crt_object);
     350                 :          3 : }
     351                 :            : 
     352                 :            : static void
     353                 :          3 : test_public_key_load (TestLoading *test,
     354                 :            :                       gconstpointer unused)
     355                 :            : {
     356                 :          3 :         perform_load (test, test->pub_object);
     357                 :          3 : }
     358                 :            : 
     359                 :            : static void
     360                 :          3 : test_private_key_load (TestLoading *test,
     361                 :            :                        gconstpointer unused)
     362                 :            : {
     363                 :          3 :         perform_load (test, test->prv_object);
     364                 :          3 : }
     365                 :            : 
     366                 :            : static void
     367                 :          9 : on_async_result (GObject *source,
     368                 :            :                  GAsyncResult *result,
     369                 :            :                  gpointer user_data)
     370                 :            : {
     371                 :          9 :         GAsyncResult **ret = user_data;
     372         [ -  + ]:          9 :         g_assert (ret != NULL);
     373         [ -  + ]:          9 :         g_assert (*ret == NULL);
     374   [ -  +  +  -  :          9 :         g_assert (G_IS_ASYNC_RESULT (result));
             -  +  -  + ]
     375                 :          9 :         *ret = g_object_ref (result);
     376                 :          9 :         egg_test_wait_stop ();
     377                 :          9 : }
     378                 :            : 
     379                 :            : static void
     380                 :          9 : perform_load_async (TestLoading *test,
     381                 :            :                     GckObject *object)
     382                 :            : {
     383                 :          9 :         GAsyncResult *result = NULL;
     384                 :          9 :         GError *error = NULL;
     385                 :            :         GBytes *data;
     386                 :            :         GNode *info;
     387                 :            : 
     388                 :          9 :         _gcr_subject_public_key_load_async (object, NULL, on_async_result, &result);
     389         [ -  + ]:          9 :         g_assert (result == NULL);
     390         [ -  + ]:          9 :         egg_test_wait ();
     391                 :            : 
     392         [ -  + ]:          9 :         g_assert (result != NULL);
     393                 :          9 :         info = _gcr_subject_public_key_load_finish (result, &error);
     394         [ -  + ]:          9 :         g_assert_no_error (error);
     395         [ -  + ]:          9 :         g_assert (info != NULL);
     396                 :          9 :         g_object_unref (result);
     397                 :            : 
     398                 :          9 :         data = egg_asn1x_encode (info, NULL);
     399   [ +  -  +  - ]:          9 :         egg_assert_cmpbytes (data, ==, g_bytes_get_data (test->at.spk_data, NULL),
     400                 :            :                                        g_bytes_get_size (test->at.spk_data));
     401                 :            : 
     402                 :          9 :         g_bytes_unref (data);
     403                 :          9 :         egg_asn1x_destroy (info);
     404                 :          9 : }
     405                 :            : 
     406                 :            : static void
     407                 :          3 : test_certificate_load_async (TestLoading *test,
     408                 :            :                              gconstpointer unused)
     409                 :            : {
     410                 :          3 :         perform_load_async (test, test->crt_object);
     411                 :          3 : }
     412                 :            : 
     413                 :            : static void
     414                 :          3 : test_public_key_load_async (TestLoading *test,
     415                 :            :                             gconstpointer unused)
     416                 :            : {
     417                 :          3 :         perform_load_async (test, test->pub_object);
     418                 :          3 : }
     419                 :            : 
     420                 :            : static void
     421                 :          3 : test_private_key_load_async (TestLoading *test,
     422                 :            :                              gconstpointer unused)
     423                 :            : {
     424                 :          3 :         perform_load_async (test, test->prv_object);
     425                 :          3 : }
     426                 :            : 
     427                 :            : enum { PROP_ATTRIBUTES = 1 };
     428                 :            : typedef struct { GckObject parent; GckAttributes *attrs; } MockObject;
     429                 :            : typedef struct { GckObjectClass parent; } MockObjectClass;
     430                 :            : 
     431                 :            : GType mock_object_get_type (void) G_GNUC_CONST;
     432                 :            : static void mock_object_cache_init (GckObjectCacheInterface *iface);
     433   [ +  +  +  -  :         22 : G_DEFINE_TYPE_WITH_CODE (MockObject, mock_object, GCK_TYPE_OBJECT,
                   +  + ]
     434                 :            :                          G_IMPLEMENT_INTERFACE (GCK_TYPE_OBJECT_CACHE, mock_object_cache_init)
     435                 :            : );
     436                 :            : 
     437                 :            : static void
     438                 :         20 : mock_object_init (MockObject *self)
     439                 :            : {
     440                 :            : 
     441                 :         20 : }
     442                 :            : 
     443                 :            : static void
     444                 :         51 : mock_object_get_property (GObject *obj,
     445                 :            :                           guint prop_id,
     446                 :            :                           GValue *value,
     447                 :            :                           GParamSpec *pspec)
     448                 :            : {
     449         [ -  + ]:         51 :         g_assert (prop_id == PROP_ATTRIBUTES);
     450                 :         51 :         g_value_set_boxed (value, ((MockObject *)obj)->attrs);
     451                 :         51 : }
     452                 :            : 
     453                 :            : static void
     454                 :         19 : mock_object_set_property (GObject *obj,
     455                 :            :                           guint prop_id,
     456                 :            :                           const GValue *value,
     457                 :            :                           GParamSpec *pspec)
     458                 :            : {
     459         [ -  + ]:         19 :         g_assert (prop_id == PROP_ATTRIBUTES);
     460                 :         19 :         ((MockObject *)obj)->attrs = g_value_dup_boxed (value);
     461                 :         19 : }
     462                 :            : 
     463                 :            : static void
     464                 :         20 : mock_object_finalize (GObject *obj)
     465                 :            : {
     466                 :         20 :         MockObject *self = (MockObject *)obj;
     467                 :         20 :         gck_attributes_unref (self->attrs);
     468                 :         20 :         G_OBJECT_CLASS (mock_object_parent_class)->finalize (obj);
     469                 :         20 : }
     470                 :            : 
     471                 :            : static void
     472                 :          1 : mock_object_class_init (MockObjectClass *klass)
     473                 :            : {
     474                 :          1 :         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     475                 :            : 
     476                 :          1 :         gobject_class->get_property = mock_object_get_property;
     477                 :          1 :         gobject_class->set_property = mock_object_set_property;
     478                 :          1 :         gobject_class->finalize = mock_object_finalize;
     479                 :            : 
     480                 :          1 :         g_object_class_override_property (gobject_class, PROP_ATTRIBUTES, "attributes");
     481                 :          1 : }
     482                 :            : 
     483                 :            : static void
     484                 :         15 : mock_object_fill (GckObjectCache *object,
     485                 :            :                   GckAttributes *attrs)
     486                 :            : {
     487                 :         15 :         GckBuilder builder = GCK_BUILDER_INIT;
     488                 :         15 :         MockObject *self = (MockObject *)object;
     489                 :            : 
     490                 :         15 :         gck_builder_add_all (&builder, self->attrs);
     491                 :         15 :         gck_builder_set_all (&builder, attrs);
     492                 :            : 
     493                 :         15 :         gck_attributes_unref (self->attrs);
     494                 :         15 :         self->attrs = gck_builder_end (&builder);
     495                 :         15 : }
     496                 :            : 
     497                 :            : static void
     498                 :          1 : mock_object_cache_init (GckObjectCacheInterface *iface)
     499                 :            : {
     500                 :          1 :         iface->default_types = NULL;
     501                 :          1 :         iface->n_default_types = 0;
     502                 :          1 :         iface->fill = mock_object_fill;
     503                 :          1 : }
     504                 :            : 
     505                 :            : static void
     506                 :          9 : perform_load_already (TestLoading *test,
     507                 :            :                       GckAttributes *attributes)
     508                 :            : {
     509                 :          9 :         const gulong INVALID = 0xFFF00FF; /* invalid handle, should not be used */
     510                 :            :         GckObject *object;
     511                 :          9 :         GError *error = NULL;
     512                 :            :         GBytes *data;
     513                 :            :         GNode *info;
     514                 :            : 
     515                 :          9 :         object = g_object_new (mock_object_get_type (),
     516                 :            :                                "module", test->mo.module,
     517                 :            :                                "session", test->mo.session,
     518                 :            :                                "handle", INVALID,
     519                 :            :                                "attributes", attributes,
     520                 :            :                                NULL);
     521                 :            : 
     522                 :          9 :         info = _gcr_subject_public_key_load (object, NULL, &error);
     523         [ -  + ]:          9 :         g_assert_no_error (error);
     524         [ -  + ]:          9 :         g_assert (info != NULL);
     525                 :            : 
     526                 :          9 :         data = egg_asn1x_encode (info, NULL);
     527   [ +  -  +  - ]:          9 :         egg_assert_cmpbytes (data, ==, g_bytes_get_data (test->at.spk_data, NULL),
     528                 :            :                                        g_bytes_get_size (test->at.spk_data));
     529                 :            : 
     530                 :          9 :         g_bytes_unref (data);
     531                 :          9 :         egg_asn1x_destroy (info);
     532                 :          9 :         g_object_unref (object);
     533                 :          9 : }
     534                 :            : 
     535                 :            : static void
     536                 :          3 : test_certificate_load_already (TestLoading *test,
     537                 :            :                                gconstpointer unused)
     538                 :            : {
     539                 :          3 :         perform_load_already (test, test->at.crt_attrs);
     540                 :          3 : }
     541                 :            : 
     542                 :            : static void
     543                 :          3 : test_public_key_load_already (TestLoading *test,
     544                 :            :                               gconstpointer unused)
     545                 :            : {
     546                 :          3 :         perform_load_already (test, test->at.pub_attrs);
     547                 :          3 : }
     548                 :            : 
     549                 :            : static void
     550                 :          3 : test_private_key_load_already (TestLoading *test,
     551                 :            :                                gconstpointer unused)
     552                 :            : {
     553                 :          3 :         perform_load_already (test, test->at.prv_attrs);
     554                 :          3 : }
     555                 :            : 
     556                 :            : static void
     557                 :          9 : perform_load_partial (TestLoading *test,
     558                 :            :                       GckObject *original,
     559                 :            :                       GckAttributes *attributes)
     560                 :            : {
     561                 :          9 :         GckBuilder builder = GCK_BUILDER_INIT;
     562                 :            :         GckAttributes *partial;
     563                 :            :         GckObject *object;
     564                 :          9 :         GError *error = NULL;
     565                 :            :         GBytes *data;
     566                 :            :         GNode *info;
     567                 :            :         guint i;
     568                 :            : 
     569         [ +  + ]:         37 :         for (i = 0; i < gck_attributes_count (attributes); i += 2)
     570                 :         28 :                 gck_builder_add_attribute (&builder, gck_attributes_at (attributes, i));
     571                 :          9 :         partial = gck_builder_end (&builder);
     572                 :            : 
     573                 :          9 :         object = g_object_new (mock_object_get_type (),
     574                 :            :                                "module", test->mo.module,
     575                 :            :                                "session", test->mo.session,
     576                 :            :                                "handle", gck_object_get_handle (original),
     577                 :            :                                "attributes", partial,
     578                 :            :                                NULL);
     579                 :          9 :         gck_attributes_unref (partial);
     580                 :            : 
     581                 :          9 :         info = _gcr_subject_public_key_load (object, NULL, &error);
     582         [ -  + ]:          9 :         g_assert_no_error (error);
     583         [ -  + ]:          9 :         g_assert (info != NULL);
     584                 :            : 
     585                 :          9 :         data = egg_asn1x_encode (info, NULL);
     586   [ +  -  +  - ]:          9 :         egg_assert_cmpbytes (data, ==, g_bytes_get_data (test->at.spk_data, NULL),
     587                 :            :                                        g_bytes_get_size (test->at.spk_data));
     588                 :            : 
     589                 :          9 :         g_bytes_unref (data);
     590                 :          9 :         egg_asn1x_destroy (info);
     591                 :          9 :         g_object_unref (object);
     592                 :          9 : }
     593                 :            : 
     594                 :            : static void
     595                 :          3 : test_certificate_load_partial (TestLoading *test,
     596                 :            :                                gconstpointer unused)
     597                 :            : {
     598                 :          3 :         perform_load_partial (test, test->crt_object, test->at.crt_attrs);
     599                 :          3 : }
     600                 :            : 
     601                 :            : static void
     602                 :          3 : test_public_key_load_partial (TestLoading *test,
     603                 :            :                               gconstpointer unused)
     604                 :            : {
     605                 :          3 :         perform_load_partial (test, test->pub_object, test->at.pub_attrs);
     606                 :          3 : }
     607                 :            : 
     608                 :            : static void
     609                 :          3 : test_private_key_load_partial (TestLoading *test,
     610                 :            :                                gconstpointer unused)
     611                 :            : {
     612                 :          3 :         perform_load_partial (test, test->prv_object, test->at.prv_attrs);
     613                 :          3 : }
     614                 :            : 
     615                 :            : static void
     616                 :          1 : test_load_failure_lookup (TestModule *test,
     617                 :            :                           gconstpointer fixture)
     618                 :            : {
     619                 :          1 :         const gulong INVALID = 0xFFF00FF; /* invalid handle, should fail */
     620                 :            :         GckObject *object;
     621                 :          1 :         GError *error = NULL;
     622                 :            :         GNode *info;
     623                 :            : 
     624                 :          1 :         object = g_object_new (mock_object_get_type (),
     625                 :            :                                "module", test->module,
     626                 :            :                                "session", test->session,
     627                 :            :                                "handle", INVALID,
     628                 :            :                                NULL);
     629                 :            : 
     630                 :          1 :         info = _gcr_subject_public_key_load (object, NULL, &error);
     631   [ +  -  +  -  :          1 :         g_assert_error (error, GCK_ERROR, CKR_OBJECT_HANDLE_INVALID);
                   -  + ]
     632         [ -  + ]:          1 :         g_assert (info == NULL);
     633                 :          1 :         g_error_free (error);
     634                 :            : 
     635                 :          1 :         g_object_unref (object);
     636                 :          1 : }
     637                 :            : 
     638                 :            : static void
     639                 :          1 : test_load_failure_build (TestModule *test,
     640                 :            :                          gconstpointer fixture)
     641                 :            : {
     642                 :          1 :         GckBuilder builder = GCK_BUILDER_INIT;
     643                 :            :         GckAttributes *attributes;
     644                 :          1 :         const gulong INVALID = 0xFFF00FF; /* invalid handle, shouldn't be used */
     645                 :            :         GckObject *object;
     646                 :          1 :         GError *error = NULL;
     647                 :            :         GNode *info;
     648                 :            : 
     649                 :          1 :         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_CERTIFICATE);
     650                 :          1 :         gck_builder_add_ulong (&builder, CKA_CERTIFICATE_TYPE, CKC_X_509);
     651                 :          1 :         gck_builder_add_string (&builder, CKA_VALUE, "invalid value");
     652                 :          1 :         attributes = gck_builder_end (&builder);
     653                 :            : 
     654                 :          1 :         object = g_object_new (mock_object_get_type (),
     655                 :            :                                "module", test->module,
     656                 :            :                                "session", test->session,
     657                 :            :                                "handle", INVALID,
     658                 :            :                                "attributes", attributes,
     659                 :            :                                NULL);
     660                 :            : 
     661                 :          1 :         gck_attributes_unref (attributes);
     662                 :            : 
     663                 :          1 :         info = _gcr_subject_public_key_load (object, NULL, &error);
     664   [ +  -  +  -  :          1 :         g_assert_error (error, GCK_ERROR, CKR_TEMPLATE_INCONSISTENT);
                   -  + ]
     665         [ -  + ]:          1 :         g_assert (info == NULL);
     666                 :          1 :         g_error_free (error);
     667                 :            : 
     668                 :          1 :         g_object_unref (object);
     669                 :          1 : }
     670                 :            : 
     671                 :            : static const TestFixture FIXTURES[] = {
     672                 :            :         { "rsa", "client", 2048 },
     673                 :            :         { "dsa", "generic-dsa", 1024 },
     674                 :            :         { "ec", "ecc-strong", 521 },
     675                 :            : };
     676                 :            : 
     677                 :            : static GPtrArray *test_names = NULL;
     678                 :            : 
     679                 :            : static const gchar *
     680                 :         54 : test_name (const gchar *format,
     681                 :            :            const gchar *basename)
     682                 :            : {
     683                 :         54 :         gchar *name = g_strdup_printf (format, basename);
     684                 :         54 :         g_ptr_array_add (test_names, name);
     685                 :         54 :         return name;
     686                 :            : }
     687                 :            : 
     688                 :            : int
     689                 :          1 : main (int argc, char **argv)
     690                 :            : {
     691                 :            :         const TestFixture *fixture;
     692                 :            :         gint ret;
     693                 :            :         guint i;
     694                 :            : 
     695                 :          1 :         g_test_init (&argc, &argv, NULL);
     696                 :            : 
     697                 :          1 :         test_names = g_ptr_array_new_with_free_func (g_free);
     698                 :            : 
     699         [ +  + ]:          4 :         for (i = 0; i < G_N_ELEMENTS (FIXTURES); i++) {
     700                 :          3 :                 fixture = &FIXTURES[i];
     701                 :            : 
     702                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/cert-attributes", fixture->name), TestAttributes, fixture,
     703                 :            :                             setup_attributes, test_for_cert_attributes, teardown_attributes);
     704                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-attributes", fixture->name), TestAttributes, fixture,
     705                 :            :                             setup_attributes, test_for_public_key_attributes, teardown_attributes);
     706                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-attributes", fixture->name), TestAttributes, fixture,
     707                 :            :                             setup_attributes, test_for_private_key_attributes, teardown_attributes);
     708                 :            : 
     709                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/certificate-size", fixture->name), TestAttributes, fixture,
     710                 :            :                             setup_attributes, test_certificate_calculate_size, teardown_attributes);
     711                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-size", fixture->name), TestAttributes, fixture,
     712                 :            :                             setup_attributes, test_public_key_calculate_size, teardown_attributes);
     713                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-size", fixture->name), TestAttributes, fixture,
     714                 :            :                             setup_attributes, test_private_key_calculate_size, teardown_attributes);
     715                 :            : 
     716                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/certificate-load", fixture->name), TestLoading, fixture,
     717                 :            :                             setup_loading, test_certificate_load, teardown_loading);
     718                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-load", fixture->name), TestLoading, fixture,
     719                 :            :                             setup_loading, test_public_key_load, teardown_loading);
     720                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-load", fixture->name), TestLoading, fixture,
     721                 :            :                             setup_loading, test_private_key_load, teardown_loading);
     722                 :            : 
     723                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/certificate-load-async", fixture->name), TestLoading, fixture,
     724                 :            :                             setup_loading, test_certificate_load_async, teardown_loading);
     725                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-load-async", fixture->name), TestLoading, fixture,
     726                 :            :                             setup_loading, test_public_key_load_async, teardown_loading);
     727                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-load-async", fixture->name), TestLoading, fixture,
     728                 :            :                             setup_loading, test_private_key_load_async, teardown_loading);
     729                 :            : 
     730                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/certificate-load-already", fixture->name), TestLoading, fixture,
     731                 :            :                             setup_loading, test_certificate_load_already, teardown_loading);
     732                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-load-already", fixture->name), TestLoading, fixture,
     733                 :            :                             setup_loading, test_public_key_load_already, teardown_loading);
     734                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-load-already", fixture->name), TestLoading, fixture,
     735                 :            :                             setup_loading, test_private_key_load_already, teardown_loading);
     736                 :            : 
     737                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/certificate-load-partial", fixture->name), TestLoading, fixture,
     738                 :            :                             setup_loading, test_certificate_load_partial, teardown_loading);
     739                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/public-key-load-partial", fixture->name), TestLoading, fixture,
     740                 :            :                             setup_loading, test_public_key_load_partial, teardown_loading);
     741                 :          3 :                 g_test_add (test_name ("/gcr/subject-public-key/%s/private-key-load-partial", fixture->name), TestLoading, fixture,
     742                 :            :                             setup_loading, test_private_key_load_partial, teardown_loading);
     743                 :            :         }
     744                 :            : 
     745                 :          1 :         g_test_add ("/gcr/subject-public-key/load-failure-lookup", TestModule, NULL,
     746                 :            :                     setup_module, test_load_failure_lookup, teardown_module);
     747                 :          1 :         g_test_add ("/gcr/subject-public-key/load-failure-build", TestModule, NULL,
     748                 :            :                     setup_module, test_load_failure_build, teardown_module);
     749                 :            : 
     750                 :          1 :         ret = egg_tests_run_with_loop ();
     751                 :            : 
     752                 :          1 :         g_ptr_array_free (test_names, TRUE);
     753                 :          1 :         return ret;
     754                 :            : }

Generated by: LCOV version 1.14