LCOV - code coverage report
Current view: top level - pkcs11/secret-store - test-secret-object.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 143 143
Test Date: 2024-04-08 13:24:42 Functions: 100.0 % 17 17

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /*
       3              :    Copyright (C) 2009 Stefan Walter
       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              :    <http://www.gnu.org/licenses/>.
      18              : 
      19              :    Author: Stef Walter <stef@memberwebs.com>
      20              : */
      21              : 
      22              : #include "config.h"
      23              : 
      24              : #include "mock-secret-module.h"
      25              : 
      26              : #include "secret-store/gkm-secret-object.h"
      27              : 
      28              : #include "gkm/gkm-transaction.h"
      29              : 
      30              : #include "pkcs11/pkcs11i.h"
      31              : 
      32              : #include <glib.h>
      33              : 
      34              : #include <stdlib.h>
      35              : #include <stdio.h>
      36              : #include <string.h>
      37              : 
      38              : typedef struct {
      39              :         GkmModule *module;
      40              :         GkmSession *session;
      41              :         GkmSecretObject *object;
      42              : } Test;
      43              : 
      44              : static void
      45           13 : setup (Test *test, gconstpointer unused)
      46              : {
      47           13 :         test->module = test_secret_module_initialize_and_enter ();
      48           13 :         test->session = test_secret_module_open_session (TRUE);
      49              : 
      50           13 :         test->object = g_object_new (GKM_TYPE_SECRET_OBJECT,
      51              :                                "module", test->module,
      52              :                                "identifier", "my-identifier",
      53              :                                NULL);
      54              : 
      55           13 :         g_assert (GKM_IS_SECRET_OBJECT (test->object));
      56           13 : }
      57              : 
      58              : static void
      59           13 : teardown (Test *test, gconstpointer unused)
      60              : {
      61           13 :         g_object_unref (test->object);
      62           13 :         test_secret_module_leave_and_finalize ();
      63           13 : }
      64              : 
      65              : static void
      66            1 : test_is_locked (Test *test, gconstpointer unused)
      67              : {
      68              :         /* Plain GkmSecretObject is never locked */
      69            1 :         g_assert (!gkm_secret_object_is_locked (test->object, test->session));
      70            1 : }
      71              : 
      72              : static void
      73            1 : test_identifier_prop (Test *test, gconstpointer unused)
      74              : {
      75              :         const gchar *identifier;
      76            1 :         identifier = gkm_secret_object_get_identifier (test->object);
      77            1 :         g_assert_cmpstr (identifier, ==, "my-identifier");
      78            1 : }
      79              : 
      80              : static void
      81            4 : was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data)
      82              : {
      83            4 :         gboolean* notified = user_data;
      84            4 :         g_assert (user_data);
      85            4 :         *notified = TRUE;
      86            4 : }
      87              : 
      88              : static void
      89            1 : test_created_prop (Test *test, gconstpointer unused)
      90              : {
      91              :         glong created;
      92              : 
      93              :         /* Monitor for changes */
      94            1 :         gboolean notified = FALSE;
      95            1 :         g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), &notified);
      96              : 
      97              :         /* Default value */
      98            1 :         created = gkm_secret_object_get_created (test->object);
      99            1 :         g_assert (created == 0);
     100              : 
     101              :         /* Set a new value */
     102            1 :         gkm_secret_object_set_created (test->object, 1247930171);
     103            1 :         g_assert (notified);
     104            1 :         created = gkm_secret_object_get_created (test->object);
     105            1 :         g_assert (created == 1247930171);
     106            1 : }
     107              : 
     108              : static void
     109            1 : test_modified_prop (Test *test, gconstpointer unused)
     110              : {
     111              :         glong modified;
     112              : 
     113              :         /* Monitor for changes */
     114            1 :         gboolean notified = FALSE;
     115            1 :         g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), &notified);
     116              : 
     117              :         /* Default value */
     118            1 :         modified = gkm_secret_object_get_modified (test->object);
     119            1 :         g_assert (modified == 0);
     120              : 
     121              :         /* Set a new value */
     122            1 :         gkm_secret_object_set_modified (test->object, 1247930171);
     123            1 :         g_assert (notified);
     124            1 :         modified = gkm_secret_object_get_modified (test->object);
     125            1 :         g_assert (modified == 1247930171);
     126            1 : }
     127              : 
     128              : static void
     129            1 : test_was_modified (Test *test, gconstpointer unused)
     130              : {
     131              :         GkmTransaction *transaction;
     132              :         gint64 now;
     133              : 
     134            1 :         now = g_get_real_time () / G_USEC_PER_SEC;
     135              : 
     136            1 :         transaction = gkm_transaction_new ();
     137            1 :         gkm_secret_object_begin_modified (test->object, transaction);
     138            1 :         g_assert_cmpuint (gkm_transaction_complete_and_unref (transaction), ==, CKR_OK);
     139              : 
     140            1 :         g_assert (now == gkm_secret_object_get_modified (test->object));
     141            1 : }
     142              : 
     143              : static void
     144            1 : test_label_prop (Test *test, gconstpointer unused)
     145              : {
     146              :         const gchar *label;
     147              : 
     148              :         /* Monitor for changes */
     149            1 :         gboolean notified = FALSE;
     150            1 :         g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
     151              : 
     152              :         /* Default value */
     153            1 :         label = gkm_secret_object_get_label (test->object);
     154            1 :         g_assert_cmpstr (label, ==, "");
     155              : 
     156              :         /* Set a new value */
     157            1 :         gkm_secret_object_set_label (test->object, "hello");
     158            1 :         g_assert (notified);
     159            1 :         label = gkm_secret_object_get_label (test->object);
     160            1 :         g_assert_cmpstr (label, ==, "hello");
     161            1 : }
     162              : 
     163              : static void
     164            1 : test_identifier_get_attr (Test *test, gconstpointer unused)
     165              : {
     166              :         gchar buffer[32];
     167            1 :         CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 };
     168              :         CK_RV rv;
     169              : 
     170            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
     171            1 :         g_assert (rv == CKR_OK);
     172            1 :         g_assert (attr.ulValueLen == 13);
     173            1 :         g_assert (memcmp (buffer, "my-identifier", 13) == 0);
     174            1 : }
     175              : 
     176              : static void
     177            1 : test_label_get_attr (Test *test, gconstpointer unused)
     178              : {
     179              :         gchar buffer[32];
     180            1 :         CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 };
     181              :         CK_RV rv;
     182              : 
     183            1 :         gkm_secret_object_set_label (test->object, "hello");
     184            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
     185            1 :         g_assert (rv == CKR_OK);
     186            1 :         g_assert (attr.ulValueLen == 5);
     187            1 :         g_assert (memcmp (buffer, "hello", 5) == 0);
     188            1 : }
     189              : 
     190              : static void
     191            1 : test_label_set_attr (Test *test, gconstpointer unused)
     192              : {
     193            1 :         CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
     194            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     195              : 
     196              :         /* Monitor for changes */
     197            1 :         gboolean notified = FALSE;
     198            1 :         g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
     199              : 
     200            1 :         gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
     201            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     202            1 :         g_assert (!notified); /* Not notified yet */
     203              : 
     204            1 :         g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
     205              : 
     206            1 :         gkm_transaction_complete (transaction);
     207            1 :         g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
     208            1 :         g_assert (notified); /* Notified after transaction complete */
     209              : 
     210            1 :         g_object_unref (transaction);
     211            1 : }
     212              : 
     213              : static void
     214            1 : test_label_set_attr_fail (Test *test, gconstpointer unused)
     215              : {
     216            1 :         CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
     217            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     218            1 :         gboolean notified = FALSE;
     219              : 
     220              :         /* Set an old value */
     221            1 :         gkm_secret_object_set_label (test->object, "old");
     222              : 
     223              :         /* Monitor for changes */
     224            1 :         g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
     225              : 
     226              :         /* Set a new value */
     227            1 :         gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
     228            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     229            1 :         g_assert (!notified); /* Not notified yet */
     230              : 
     231              :         /* Temporarily has new value */
     232            1 :         g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
     233              : 
     234              :         /* Fail and complete transaction */
     235            1 :         gkm_transaction_fail (transaction, CKR_CANCEL);
     236            1 :         gkm_transaction_complete (transaction);
     237              : 
     238              :         /* Back to old value */
     239            1 :         g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old");
     240            1 :         g_assert (!notified); /* Should never have notified */
     241              : 
     242            1 :         g_object_unref (transaction);
     243            1 : }
     244              : 
     245              : static void
     246            1 : test_modified_get_attr (Test *test, gconstpointer unused)
     247              : {
     248              :         gchar buffer[32];
     249            1 :         CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 };
     250              :         CK_RV rv;
     251              : 
     252            1 :         gkm_secret_object_set_modified (test->object, 1247930171);
     253            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
     254            1 :         g_assert (rv == CKR_OK);
     255            1 :         g_assert (attr.ulValueLen == 16);
     256            1 :         g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
     257            1 : }
     258              : 
     259              : static void
     260            1 : test_created_get_attr (Test *test, gconstpointer unused)
     261              : {
     262              :         gchar buffer[32];
     263            1 :         CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 };
     264              :         CK_RV rv;
     265              : 
     266            1 :         gkm_secret_object_set_created (test->object, 1247930171);
     267            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
     268            1 :         g_assert (rv == CKR_OK);
     269            1 :         g_assert (attr.ulValueLen == 16);
     270            1 :         g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
     271            1 : }
     272              : 
     273              : static void
     274            1 : test_locked_get_attr (Test *test, gconstpointer unused)
     275              : {
     276              :         gchar buffer[32];
     277            1 :         CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 };
     278              :         CK_RV rv;
     279              : 
     280            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
     281            1 :         g_assert (rv == CKR_OK);
     282            1 :         g_assert (attr.ulValueLen == 1);
     283            1 :         g_assert (memcmp (buffer, "\0", 1) == 0);
     284            1 : }
     285              : 
     286              : int
     287            1 : main (int argc, char **argv)
     288              : {
     289              : #if !GLIB_CHECK_VERSION(2,35,0)
     290              :         g_type_init ();
     291              : #endif
     292            1 :         g_test_init (&argc, &argv, NULL);
     293              : 
     294            1 :         g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown);
     295            1 :         g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown);
     296            1 :         g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown);
     297            1 :         g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown);
     298            1 :         g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown);
     299            1 :         g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown);
     300            1 :         g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown);
     301            1 :         g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown);
     302            1 :         g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown);
     303            1 :         g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown);
     304            1 :         g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown);
     305            1 :         g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown);
     306            1 :         g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown);
     307              : 
     308            1 :         return g_test_run ();
     309              : }
        

Generated by: LCOV version 2.0-1