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

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* unit-test-secret-item.c: Test secret item
       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-secret-module.h"
      26              : 
      27              : #include "secret-store/gkm-secret-collection.h"
      28              : #include "secret-store/gkm-secret-fields.h"
      29              : #include "secret-store/gkm-secret-item.h"
      30              : 
      31              : #include "gkm/gkm-credential.h"
      32              : #include "gkm/gkm-session.h"
      33              : #include "gkm/gkm-transaction.h"
      34              : 
      35              : #include "pkcs11/pkcs11i.h"
      36              : 
      37              : #include <glib.h>
      38              : 
      39              : #include <stdlib.h>
      40              : #include <stdio.h>
      41              : #include <string.h>
      42              : 
      43              : typedef struct {
      44              :         GkmModule *module;
      45              :         GkmSession *session;
      46              :         GkmSecretCollection *collection;
      47              : } Test;
      48              : 
      49              : static void
      50           18 : setup (Test *test, gconstpointer unused)
      51              : {
      52           18 :         test->module = test_secret_module_initialize_and_enter ();
      53           18 :         test->session = test_secret_module_open_session (TRUE);
      54              : 
      55           18 :         test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
      56              :                                    "module", test->module,
      57              :                                    "identifier", "test",
      58              :                                    NULL);
      59           18 : }
      60              : 
      61              : static void
      62           18 : teardown (Test *test, gconstpointer unused)
      63              : {
      64           18 :         if (test->collection)
      65           17 :                 g_object_unref (test->collection);
      66           18 :         test_secret_module_leave_and_finalize ();
      67           18 : }
      68              : 
      69              : static void
      70            5 : unlock_collection (Test *test)
      71              : {
      72              :         GkmCredential *cred;
      73              :         GkmObject *object;
      74              :         CK_RV rv;
      75              : 
      76              :         /* Create credential, which unlocks test->collection */
      77            5 :         object = GKM_OBJECT (test->collection);
      78            5 :         rv = gkm_credential_create (gkm_object_get_module (object),
      79              :                                     gkm_session_get_manager (test->session),
      80              :                                     object, NULL, 0, &cred);
      81            5 :         g_assert (rv == CKR_OK);
      82              : 
      83            5 :         gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred));
      84            5 :         g_object_unref (cred);
      85            5 : }
      86              : 
      87              : static void
      88            1 : test_new (Test *test, gconstpointer unused)
      89              : {
      90              :         GkmSecretItem *item;
      91              : 
      92            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
      93            1 :         g_assert (GKM_IS_SECRET_ITEM (item));
      94            1 :         g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
      95            1 : }
      96              : 
      97              : static void
      98            1 : test_create (Test *test, gconstpointer unused)
      99              : {
     100              :         GkmTransaction *transaction;
     101              :         GkmSecretItem *item;
     102              : 
     103            1 :         transaction = gkm_transaction_new ();
     104            1 :         item = gkm_secret_collection_create_item (test->collection, transaction);
     105            1 :         g_assert (GKM_IS_SECRET_ITEM (item));
     106            1 :         g_object_ref (item);
     107            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     108              : 
     109            1 :         gkm_transaction_complete (transaction);
     110            1 :         g_object_unref (transaction);
     111              : 
     112              :         /* Should still be there */
     113            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     114            1 :         g_object_unref (item);
     115            1 : }
     116              : 
     117              : static void
     118            1 : test_create_failed (Test *test, gconstpointer unused)
     119              : {
     120              :         GkmTransaction *transaction;
     121              :         GkmSecretItem *item;
     122              : 
     123            1 :         transaction = gkm_transaction_new ();
     124            1 :         item = gkm_secret_collection_create_item (test->collection, transaction);
     125            1 :         g_assert (GKM_IS_SECRET_ITEM (item));
     126            1 :         g_object_ref (item);
     127            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     128              : 
     129            1 :         gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
     130            1 :         gkm_transaction_complete (transaction);
     131            1 :         g_object_unref (transaction);
     132              : 
     133              :         /* Should no longer be there */
     134            1 :         g_assert (!gkm_secret_collection_has_item (test->collection, item));
     135            1 :         g_object_unref (item);
     136            1 : }
     137              : 
     138              : static void
     139            1 : test_destroy (Test *test, gconstpointer unused)
     140              : {
     141              :         GkmTransaction *transaction;
     142              :         GkmSecretItem *item;
     143              : 
     144            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     145            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     146            1 :         g_object_ref (item);
     147              : 
     148            1 :         transaction = gkm_transaction_new ();
     149            1 :         gkm_secret_collection_destroy_item (test->collection, transaction, item);
     150            1 :         g_assert (!gkm_secret_collection_has_item (test->collection, item));
     151              : 
     152            1 :         gkm_transaction_complete (transaction);
     153            1 :         g_object_unref (transaction);
     154              : 
     155              :         /* Should not be there */
     156            1 :         g_assert (!gkm_secret_collection_has_item (test->collection, item));
     157            1 :         g_object_unref (item);
     158            1 : }
     159              : 
     160              : static void
     161            1 : test_destroy_failed (Test *test, gconstpointer unused)
     162              : {
     163              :         GkmTransaction *transaction;
     164              :         GkmSecretItem *item;
     165              : 
     166            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     167            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     168            1 :         g_object_ref (item);
     169              : 
     170            1 :         transaction = gkm_transaction_new ();
     171            1 :         gkm_secret_collection_destroy_item (test->collection, transaction, item);
     172            1 :         g_assert (!gkm_secret_collection_has_item (test->collection, item));
     173              : 
     174            1 :         gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
     175            1 :         gkm_transaction_complete (transaction);
     176            1 :         g_object_unref (transaction);
     177              : 
     178              :         /* Should be there */
     179            1 :         g_assert (gkm_secret_collection_has_item (test->collection, item));
     180            1 :         g_object_unref (item);
     181            1 : }
     182              : 
     183              : static void
     184            1 : test_collection_get (Test *test, gconstpointer unused)
     185              : {
     186              :         GkmSecretItem *item, *check;
     187              : 
     188            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     189            1 :         g_assert (GKM_IS_SECRET_ITEM (item));
     190              : 
     191            1 :         check = gkm_secret_collection_get_item (test->collection, "the-identifier");
     192            1 :         g_assert (item == check);
     193            1 : }
     194              : 
     195              : static void
     196            1 : test_collection_items (Test *test, gconstpointer unused)
     197              : {
     198              :         GList *l, *items;
     199              :         const gchar *identifier;
     200              : 
     201            1 :         gkm_secret_collection_new_item (test->collection, "one-identifier");
     202            1 :         gkm_secret_collection_new_item (test->collection, "two-identifier");
     203            1 :         gkm_secret_collection_new_item (test->collection, "three-identifier");
     204              : 
     205            1 :         items = gkm_secret_collection_get_items (test->collection);
     206            1 :         g_assert_cmpuint (g_list_length (items), ==, 3);
     207            4 :         for (l = items; l; l = g_list_next (l)) {
     208            3 :                 identifier = gkm_secret_object_get_identifier (l->data);
     209            3 :                 if (!g_str_equal (identifier, "one-identifier") &&
     210            2 :                     !g_str_equal (identifier, "two-identifier") &&
     211            1 :                     !g_str_equal (identifier, "three-identifier"))
     212            0 :                         g_assert_not_reached ();
     213              :         }
     214              : 
     215            1 :         g_list_free (items);
     216            1 : }
     217              : 
     218              : static void
     219            1 : test_collection_remove (Test *test, gconstpointer unused)
     220              : {
     221              :         GkmSecretItem *item;
     222              : 
     223            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     224            1 :         g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == item);
     225              : 
     226            1 :         gkm_secret_collection_remove_item (test->collection, item);
     227            1 :         g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == NULL);
     228            1 : }
     229              : 
     230              : static void
     231            1 : test_is_locked (Test *test, gconstpointer unused)
     232              : {
     233              :         GkmSecretItem *item;
     234              : 
     235            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     236            1 :         g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) ==
     237              :                   gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session));
     238              : 
     239            1 :         unlock_collection (test);
     240              : 
     241            1 :         g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
     242            1 : }
     243              : 
     244              : static void
     245            1 : test_get_collection (Test *test, gconstpointer unused)
     246              : {
     247              :         GkmSecretItem *item;
     248            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     249            1 :         g_assert (gkm_secret_item_get_collection (item) == test->collection);
     250            1 : }
     251              : 
     252              : static void
     253            1 : test_tracks_collection (Test *test, gconstpointer unused)
     254              : {
     255              :         GkmSecretItem *item;
     256            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     257            1 :         g_object_ref (item);
     258              : 
     259            1 :         unlock_collection (test);
     260              : 
     261              :         /* At this point the item should be 'unlocked' */
     262            1 :         g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
     263              : 
     264            1 :         g_object_unref (test->collection);
     265            1 :         test->collection = NULL;
     266              : 
     267              :         /* Collection went away */
     268            1 :         g_assert (gkm_secret_item_get_collection (item) == NULL);
     269            1 :         g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == TRUE);
     270              : 
     271            1 :         g_object_unref (item);
     272            1 : }
     273              : 
     274              : static void
     275            1 : test_get_set_fields (Test *test, gconstpointer unused)
     276              : {
     277            1 :         GHashTable *fields = gkm_secret_fields_new ();
     278              :         GHashTable *check;
     279              :         GkmSecretItem *item;
     280              : 
     281            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     282            1 :         gkm_secret_item_set_fields (item, fields);
     283            1 :         gkm_secret_item_set_fields (item, fields);
     284              : 
     285            1 :         check = gkm_secret_item_get_fields (item);
     286            1 :         g_assert (check == fields);
     287              : 
     288            1 :         g_hash_table_unref (fields);
     289            1 : }
     290              : 
     291              : static void
     292            1 : test_collection_attr (Test *test, gconstpointer unused)
     293              : {
     294              :         gchar buffer[32];
     295            1 :         CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
     296              :         GkmSecretItem *item;
     297              :         CK_RV rv;
     298              : 
     299            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     300            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     301            1 :         g_assert (rv == CKR_OK);
     302            1 :         g_assert (check.ulValueLen == 4);
     303            1 :         g_assert (memcmp (buffer, "test", 4) == 0);
     304            1 : }
     305              : 
     306              : static void
     307            1 : test_secret_attr (Test *test, gconstpointer unused)
     308              : {
     309            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     310            1 :         CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
     311              :         gchar buffer[32];
     312            1 :         CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
     313              :         GkmSecretItem *item;
     314              :         CK_RV rv;
     315              : 
     316            1 :         unlock_collection (test);
     317              : 
     318            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     319            1 :         gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
     320            1 :         g_assert (gkm_transaction_get_failed (transaction) == FALSE);
     321            1 :         gkm_transaction_complete (transaction);
     322            1 :         g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
     323              : 
     324            1 :         g_object_unref (transaction);
     325              : 
     326            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     327            1 :         g_assert (rv == CKR_OK);
     328            1 :         g_assert (check.ulValueLen == 5);
     329            1 :         g_assert (memcmp (buffer, "hello", 5) == 0);
     330            1 : }
     331              : 
     332              : static void
     333            1 : test_secret_attr_locked (Test *test, gconstpointer unused)
     334              : {
     335            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     336            1 :         CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
     337              :         gchar buffer[32];
     338            1 :         CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
     339              :         GkmSecretItem *item;
     340              :         CK_RV rv;
     341              : 
     342            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     343            1 :         gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
     344            1 :         g_assert (gkm_transaction_get_failed (transaction) == TRUE);
     345            1 :         gkm_transaction_complete (transaction);
     346            1 :         g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
     347              : 
     348            1 :         g_object_unref (transaction);
     349              : 
     350            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     351            1 :         g_assert (rv == CKR_USER_NOT_LOGGED_IN);
     352            1 : }
     353              : 
     354              : static void
     355            1 : test_fields_attr (Test *test, gconstpointer unused)
     356              : {
     357            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     358            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
     359              :         gchar buffer[32];
     360            1 :         CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
     361              :         GkmSecretItem *item;
     362              :         GHashTable *fields;
     363              :         const gchar *value;
     364              :         CK_RV rv;
     365              : 
     366            1 :         unlock_collection (test);
     367              : 
     368            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     369            1 :         gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
     370            1 :         g_assert (gkm_transaction_get_failed (transaction) == FALSE);
     371            1 :         gkm_transaction_complete (transaction);
     372            1 :         g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
     373              : 
     374            1 :         g_object_unref (transaction);
     375              : 
     376            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     377            1 :         g_assert (rv == CKR_OK);
     378            1 :         g_assert (check.ulValueLen == 26);
     379            1 :         g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
     380              : 
     381            1 :         fields = gkm_secret_item_get_fields (item);
     382            1 :         g_assert (fields);
     383            1 :         value = gkm_secret_fields_get (fields, "name1");
     384            1 :         g_assert_cmpstr (value, ==, "value1");
     385            1 :         value = gkm_secret_fields_get (fields, "name2");
     386            1 :         g_assert_cmpstr (value, ==, "value2");
     387            1 : }
     388              : 
     389              : static void
     390            1 : test_fields_attr_locked (Test *test, gconstpointer unused)
     391              : {
     392            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     393            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
     394              :         GkmSecretItem *item;
     395              : 
     396            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     397            1 :         gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
     398            1 :         g_assert (gkm_transaction_get_failed (transaction) == TRUE);
     399            1 :         gkm_transaction_complete (transaction);
     400            1 :         g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
     401              : 
     402            1 :         g_object_unref (transaction);
     403            1 : }
     404              : 
     405              : static void
     406            1 : test_fields_attr_reverts (Test *test, gconstpointer unused)
     407              : {
     408            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     409            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
     410              :         gchar buffer[32];
     411            1 :         CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
     412              :         GkmSecretItem *item;
     413              :         GHashTable *fields;
     414              :         CK_RV rv;
     415              : 
     416            1 :         unlock_collection (test);
     417              : 
     418            1 :         item = gkm_secret_collection_new_item (test->collection, "the-identifier");
     419              : 
     420              :         /* Set the old value like so */
     421            1 :         fields = gkm_secret_fields_new ();
     422            1 :         gkm_secret_fields_add (fields, "old", "value");
     423            1 :         gkm_secret_item_set_fields (item, fields);
     424            1 :         g_hash_table_unref (fields);
     425              : 
     426              :         /* Should show old value */
     427            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     428            1 :         g_assert (rv == CKR_OK);
     429            1 :         g_assert (check.ulValueLen == 10);
     430            1 :         g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
     431              : 
     432              :         /* Set the new values */
     433            1 :         gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
     434            1 :         g_assert (gkm_transaction_get_failed (transaction) == FALSE);
     435              : 
     436              :         /* Should have the new value */
     437            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     438            1 :         g_assert (rv == CKR_OK);
     439            1 :         g_assert (check.ulValueLen == 10);
     440            1 :         g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
     441              : 
     442              :         /* Fail the transaction */
     443            1 :         gkm_transaction_fail (transaction, CKR_CANCEL);
     444            1 :         gkm_transaction_complete (transaction);
     445              : 
     446              :         /* Should show the old value */
     447            1 :         rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
     448            1 :         g_assert (rv == CKR_OK);
     449            1 :         g_assert (check.ulValueLen == 10);
     450            1 :         g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
     451              : 
     452            1 :         g_object_unref (transaction);
     453            1 : }
     454              : 
     455              : int
     456            1 : main (int argc, char **argv)
     457              : {
     458              : #if !GLIB_CHECK_VERSION(2,35,0)
     459              :         g_type_init ();
     460              : #endif
     461            1 :         g_test_init (&argc, &argv, NULL);
     462              : 
     463            1 :         g_test_add ("/secret-store/item/new", Test, NULL, setup, test_new, teardown);
     464            1 :         g_test_add ("/secret-store/item/create", Test, NULL, setup, test_create, teardown);
     465            1 :         g_test_add ("/secret-store/item/create_failed", Test, NULL, setup, test_create_failed, teardown);
     466            1 :         g_test_add ("/secret-store/item/destroy", Test, NULL, setup, test_destroy, teardown);
     467            1 :         g_test_add ("/secret-store/item/destroy_failed", Test, NULL, setup, test_destroy_failed, teardown);
     468            1 :         g_test_add ("/secret-store/item/collection_get", Test, NULL, setup, test_collection_get, teardown);
     469            1 :         g_test_add ("/secret-store/item/collection_items", Test, NULL, setup, test_collection_items, teardown);
     470            1 :         g_test_add ("/secret-store/item/collection_remove", Test, NULL, setup, test_collection_remove, teardown);
     471            1 :         g_test_add ("/secret-store/item/is_locked", Test, NULL, setup, test_is_locked, teardown);
     472            1 :         g_test_add ("/secret-store/item/get_collection", Test, NULL, setup, test_get_collection, teardown);
     473            1 :         g_test_add ("/secret-store/item/tracks_collection", Test, NULL, setup, test_tracks_collection, teardown);
     474            1 :         g_test_add ("/secret-store/item/get_set_fields", Test, NULL, setup, test_get_set_fields, teardown);
     475            1 :         g_test_add ("/secret-store/item/collection_attr", Test, NULL, setup, test_collection_attr, teardown);
     476            1 :         g_test_add ("/secret-store/item/secret_attr", Test, NULL, setup, test_secret_attr, teardown);
     477            1 :         g_test_add ("/secret-store/item/secret_attr_locked", Test, NULL, setup, test_secret_attr_locked, teardown);
     478            1 :         g_test_add ("/secret-store/item/fields_attr", Test, NULL, setup, test_fields_attr, teardown);
     479            1 :         g_test_add ("/secret-store/item/fields_attr_locked", Test, NULL, setup, test_fields_attr_locked, teardown);
     480            1 :         g_test_add ("/secret-store/item/fields_attr_reverts", Test, NULL, setup, test_fields_attr_reverts, teardown);
     481              : 
     482            1 :         return g_test_run ();
     483              : }
        

Generated by: LCOV version 2.0-1