LCOV - code coverage report
Current view: top level - pkcs11/secret-store - test-secret-data.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 131 131
Test Date: 2024-05-07 18:02:03 Functions: 100.0 % 12 12

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-secret-compat.c: Test secret compat files
       3              : 
       4              :    Copyright (C) 2008 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 "secret-store/gkm-secret-data.h"
      26              : 
      27              : #include "gkm/gkm-secret.h"
      28              : #include "gkm/gkm-transaction.h"
      29              : 
      30              : #include "egg/egg-secure-memory.h"
      31              : 
      32              : #include <glib.h>
      33              : 
      34              : #include <stdlib.h>
      35              : #include <stdio.h>
      36              : #include <string.h>
      37              : 
      38           40 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
      39              : 
      40              : static void
      41            1 : test_new (void)
      42              : {
      43            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      44            1 :         g_assert (GKM_IS_SECRET_DATA (data));
      45            1 :         g_object_unref (data);
      46            1 : }
      47              : 
      48              : static void
      49            1 : test_get_set (void)
      50              : {
      51            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      52            1 :         GkmSecret *secret = gkm_secret_new_from_password ("barn");
      53              :         GkmSecret *check;
      54              : 
      55            1 :         gkm_secret_data_set_secret (data, "my-identifier", secret);
      56            1 :         g_object_unref (secret);
      57              : 
      58            1 :         check = gkm_secret_data_get_secret (data, "my-identifier");
      59            1 :         g_assert (GKM_IS_SECRET (check));
      60            1 :         g_assert (secret == check);
      61            1 :         g_assert (gkm_secret_equals (check, (guchar*)"barn", -1));
      62              : 
      63            1 :         g_object_unref (data);
      64            1 : }
      65              : 
      66              : static void
      67            1 : test_get_raw (void)
      68              : {
      69            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      70            1 :         GkmSecret *secret = gkm_secret_new_from_password ("barn");
      71              :         const guchar *raw;
      72              :         gsize n_raw;
      73              : 
      74            1 :         gkm_secret_data_set_secret (data, "my-identifier", secret);
      75            1 :         g_object_unref (secret);
      76              : 
      77            1 :         raw = gkm_secret_data_get_raw (data, "my-identifier", &n_raw);
      78            1 :         g_assert (raw);
      79            1 :         g_assert_cmpuint (n_raw, ==, 4);
      80            1 :         g_assert (memcmp (raw, "barn", 4) == 0);
      81              : 
      82            1 :         raw = gkm_secret_data_get_raw (data, "not-identifier", &n_raw);
      83            1 :         g_assert (raw == NULL);
      84              : 
      85            1 :         g_object_unref (data);
      86            1 : }
      87              : 
      88              : static void
      89            1 : test_remove (void)
      90              : {
      91            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      92            1 :         GkmSecret *secret = gkm_secret_new_from_password ("barn");
      93              : 
      94            1 :         gkm_secret_data_set_secret (data, "my-identifier", secret);
      95            1 :         g_object_unref (secret);
      96              : 
      97            1 :         secret = gkm_secret_data_get_secret (data, "my-identifier");
      98            1 :         g_assert (GKM_IS_SECRET (secret));
      99              : 
     100            1 :         gkm_secret_data_remove_secret (data, "my-identifier");
     101            1 :         secret = gkm_secret_data_get_secret (data, "my-identifier");
     102            1 :         g_assert (!secret);
     103              : 
     104            1 :         g_object_unref (data);
     105            1 : }
     106              : 
     107              : static void
     108            1 : test_set_transacted (void)
     109              : {
     110            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     111            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
     112            1 :         GkmSecret *secret = gkm_secret_new_from_password ("barn");
     113              : 
     114              :         /* Transaction, but not complete */
     115            1 :         gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret);
     116            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     117            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     118              : 
     119              :         /* Transaction complete */
     120            1 :         gkm_transaction_complete (transaction);
     121            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     122            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     123              : 
     124            1 :         g_object_unref (data);
     125            1 :         g_object_unref (secret);
     126            1 :         g_object_unref (transaction);
     127            1 : }
     128              : 
     129              : static void
     130            1 : test_set_transacted_replace (void)
     131              : {
     132            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     133            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
     134            1 :         GkmSecret *old = gkm_secret_new_from_password ("old");
     135            1 :         GkmSecret *secret = gkm_secret_new_from_password ("secret");
     136              : 
     137              :         /* The old secret */
     138            1 :         gkm_secret_data_set_secret (data, "my-identifier", old);
     139            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old);
     140              : 
     141              :         /* Transaction, but not complete */
     142            1 :         gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret);
     143            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     144            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     145              : 
     146              :         /* Transaction complete */
     147            1 :         gkm_transaction_complete (transaction);
     148            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     149            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     150              : 
     151            1 :         g_object_unref (old);
     152            1 :         g_object_unref (data);
     153            1 :         g_object_unref (secret);
     154            1 :         g_object_unref (transaction);
     155            1 : }
     156              : 
     157              : static void
     158            1 : test_set_transacted_fail (void)
     159              : {
     160            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     161            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
     162            1 :         GkmSecret *secret = gkm_secret_new_from_password ("barn");
     163              : 
     164              :         /* Transaction, but not complete */
     165            1 :         gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret);
     166            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     167            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     168              : 
     169              :         /* Transaction fails here */
     170            1 :         gkm_transaction_fail (transaction, CKR_CANCEL);
     171            1 :         gkm_transaction_complete (transaction);
     172            1 :         g_assert (gkm_transaction_get_failed (transaction));
     173            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == NULL);
     174              : 
     175            1 :         g_object_unref (data);
     176            1 :         g_object_unref (secret);
     177            1 :         g_object_unref (transaction);
     178            1 : }
     179              : 
     180              : static void
     181            1 : test_set_transacted_fail_revert (void)
     182              : {
     183            1 :         GkmTransaction *transaction = gkm_transaction_new ();
     184            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
     185            1 :         GkmSecret *old = gkm_secret_new_from_password ("old");
     186            1 :         GkmSecret *secret = gkm_secret_new_from_password ("secret");
     187              : 
     188              :         /* The old secret */
     189            1 :         gkm_secret_data_set_secret (data, "my-identifier", old);
     190            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old);
     191              : 
     192              :         /* Transaction, but not complete */
     193            1 :         gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret);
     194            1 :         g_assert (!gkm_transaction_get_failed (transaction));
     195            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret);
     196              : 
     197              :         /* Transaction fails here */
     198            1 :         gkm_transaction_fail (transaction, CKR_CANCEL);
     199            1 :         gkm_transaction_complete (transaction);
     200            1 :         g_assert (gkm_transaction_get_failed (transaction));
     201            1 :         g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old);
     202              : 
     203            1 :         g_object_unref (old);
     204            1 :         g_object_unref (data);
     205            1 :         g_object_unref (secret);
     206            1 :         g_object_unref (transaction);
     207            1 : }
     208              : 
     209              : static void
     210            1 : test_get_set_master (void)
     211              : {
     212            1 :         GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
     213            1 :         GkmSecret *master = gkm_secret_new_from_password ("master");
     214              :         GkmSecret *check;
     215              : 
     216            1 :         gkm_secret_data_set_master (data, master);
     217            1 :         g_object_unref (master);
     218              : 
     219            1 :         check = gkm_secret_data_get_master (data);
     220            1 :         g_assert (GKM_IS_SECRET (check));
     221            1 :         g_assert (master == check);
     222            1 :         g_assert (gkm_secret_equals (check, (guchar*)"master", -1));
     223              : 
     224            1 :         g_object_unref (data);
     225            1 : }
     226              : 
     227              : int
     228            1 : main (int argc, char **argv)
     229              : {
     230              : #if !GLIB_CHECK_VERSION(2,35,0)
     231              :         g_type_init ();
     232              : #endif
     233            1 :         g_test_init (&argc, &argv, NULL);
     234              : 
     235            1 :         g_test_add_func ("/secret-store/data/new", test_new);
     236            1 :         g_test_add_func ("/secret-store/data/get_set", test_get_set);
     237            1 :         g_test_add_func ("/secret-store/data/get_raw", test_get_raw);
     238            1 :         g_test_add_func ("/secret-store/data/remove", test_remove);
     239            1 :         g_test_add_func ("/secret-store/data/set_transacted", test_set_transacted);
     240            1 :         g_test_add_func ("/secret-store/data/set_transacted_replace", test_set_transacted_replace);
     241            1 :         g_test_add_func ("/secret-store/data/set_transacted_fail", test_set_transacted_fail);
     242            1 :         g_test_add_func ("/secret-store/data/set_transacted_fail_revert", test_set_transacted_fail_revert);
     243            1 :         g_test_add_func ("/secret-store/data/get_set_master", test_get_set_master);
     244              : 
     245            1 :         return g_test_run ();
     246              : }
        

Generated by: LCOV version 2.0-1