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

            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-collection.h"
      27              : #include "secret-store/gkm-secret-data.h"
      28              : #include "secret-store/gkm-secret-fields.h"
      29              : #include "secret-store/gkm-secret-item.h"
      30              : #include "secret-store/gkm-secret-textual.h"
      31              : 
      32              : #include "gkm/gkm-secret.h"
      33              : 
      34              : #include "pkcs11/pkcs11i.h"
      35              : 
      36              : #include <glib.h>
      37              : 
      38              : #include <stdlib.h>
      39              : #include <stdio.h>
      40              : #include <string.h>
      41              : 
      42              : typedef struct {
      43              :         GkmModule *module;
      44              :         GkmSession *session;
      45              :         GkmSecretCollection *collection;
      46              :         GkmSecretData *sdata;
      47              : } Test;
      48              : 
      49              : static void
      50            6 : setup (Test *test, gconstpointer unused)
      51              : {
      52            6 :         test->module = test_secret_module_initialize_and_enter ();
      53            6 :         test->session = test_secret_module_open_session (TRUE);
      54              : 
      55            6 :         test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
      56              :                                    "module", test->module,
      57              :                                    "identifier", "test",
      58              :                                    "label", "brigadooooooooooooon",
      59              :                                    NULL);
      60              : 
      61            6 :         test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      62              : 
      63            6 :         g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
      64              : 
      65            6 : }
      66              : 
      67              : static void
      68            6 : teardown (Test *test, gconstpointer unused)
      69              : {
      70            6 :         if (test->collection)
      71            6 :                 g_object_unref (test->collection);
      72            6 :         if (test->sdata)
      73            6 :                 g_object_unref (test->sdata);
      74            6 :         test_secret_module_leave_and_finalize ();
      75            6 : }
      76              : 
      77              : static void
      78            1 : test_read (Test *test, gconstpointer unused)
      79              : {
      80              :         GkmDataResult res;
      81              :         gchar *data;
      82              :         gsize n_data;
      83              : 
      84            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring", &data, &n_data, NULL))
      85            0 :                 g_assert_not_reached ();
      86            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
      87            1 :         g_free (data);
      88              : 
      89            1 :         test_secret_collection_validate (test->collection, test->sdata);
      90              : 
      91            1 :         g_assert (res == GKM_DATA_SUCCESS);
      92            1 : }
      93              : 
      94              : static void
      95            1 : test_read_wrong_format (Test *test, gconstpointer unused)
      96              : {
      97              :         GkmDataResult res;
      98              :         gchar *data;
      99              :         gsize n_data;
     100              : 
     101            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring", &data, &n_data, NULL))
     102            0 :                 g_assert_not_reached ();
     103              : 
     104            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     105            1 :         g_free (data);
     106              : 
     107            1 :         g_assert (res == GKM_DATA_UNRECOGNIZED);
     108            1 : }
     109              : 
     110              : static void
     111            1 : test_read_bad_number (Test *test, gconstpointer unused)
     112              : {
     113              :         GkmSecretItem *item;
     114              :         GkmDataResult res;
     115              :         const gchar *value;
     116              :         gchar *data;
     117              :         gsize n_data;
     118              : 
     119            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain-bad-number.keyring", &data, &n_data, NULL))
     120            0 :                 g_assert_not_reached ();
     121            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     122            1 :         g_free (data);
     123              : 
     124            1 :         g_assert (res == GKM_DATA_SUCCESS);
     125              : 
     126            1 :         item = gkm_secret_collection_get_item (test->collection, "1");
     127            1 :         g_assert (GKM_IS_SECRET_ITEM (item));
     128            1 :         value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number");
     129            1 :         g_assert (value == NULL);
     130            1 :         value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number");
     131            1 :         g_assert (value == NULL);
     132            1 : }
     133              : 
     134              : static void
     135            1 : test_write (Test *test, gconstpointer unused)
     136              : {
     137              :         GkmDataResult res;
     138              :         gpointer data;
     139              :         gsize n_data;
     140              : 
     141            1 :         test_secret_collection_populate (test->collection, test->sdata);
     142              : 
     143            1 :         res = gkm_secret_textual_write (test->collection, test->sdata, &data, &n_data);
     144            1 :         g_assert (res == GKM_DATA_SUCCESS);
     145            1 :         g_assert (data);
     146            1 :         g_assert (n_data);
     147              : 
     148              :         /* Try parsing it again */
     149            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     150            1 :         g_assert (res == GKM_DATA_SUCCESS);
     151            1 :         g_free (data);
     152            1 : }
     153              : 
     154              : static void
     155            1 : test_remove_unavailable (Test *test, gconstpointer unused)
     156              : {
     157              :         GkmDataResult res;
     158              :         GList *items;
     159              :         gchar *data;
     160              :         gsize n_data;
     161              : 
     162            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring", &data, &n_data, NULL))
     163            0 :                 g_assert_not_reached ();
     164            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     165            1 :         g_assert (res == GKM_DATA_SUCCESS);
     166              : 
     167              :         /* Two items from the file */
     168            1 :         items = gkm_secret_collection_get_items (test->collection);
     169            1 :         g_assert_cmpint (g_list_length (items), ==, 2);
     170            1 :         g_list_free (items);
     171              : 
     172              :         /* Fill in some more data */
     173            1 :         test_secret_collection_populate (test->collection, test->sdata);
     174              : 
     175              :         /* Should have added three more */
     176            1 :         items = gkm_secret_collection_get_items (test->collection);
     177            1 :         g_assert_cmpint (g_list_length (items), ==, 5);
     178            1 :         g_list_free (items);
     179              : 
     180              :         /* Re-read the keyring */
     181            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     182            1 :         g_assert (res == GKM_DATA_SUCCESS);
     183              : 
     184              :         /* And we're back to two */
     185            1 :         items = gkm_secret_collection_get_items (test->collection);
     186            1 :         g_assert_cmpint (g_list_length (items), ==, 2);
     187            1 :         g_list_free (items);
     188              : 
     189            1 :         g_free (data);
     190            1 : }
     191              : 
     192              : static void
     193            1 : test_read_with_schema (Test *test,
     194              :                              gconstpointer unused)
     195              : {
     196              :         GkmDataResult res;
     197              :         GkmSecretItem *item;
     198              :         gchar *data;
     199              :         gsize n_data;
     200              : 
     201            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain-with-schema.keyring", &data, &n_data, NULL))
     202            0 :                 g_assert_not_reached ();
     203            1 :         res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
     204            1 :         g_assert (res == GKM_DATA_SUCCESS);
     205              : 
     206            1 :         item = gkm_secret_collection_get_item (test->collection, "1");
     207            1 :         g_assert (item != NULL);
     208              : 
     209            1 :         g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
     210              : 
     211            1 :         g_free (data);
     212            1 : }
     213              : 
     214              : int
     215            1 : main (int argc, char **argv)
     216              : {
     217              : #if !GLIB_CHECK_VERSION(2,35,0)
     218              :         g_type_init ();
     219              : #endif
     220            1 :         g_test_init (&argc, &argv, NULL);
     221              : 
     222            1 :         g_test_add ("/secret-store/search/read", Test, NULL, setup, test_read, teardown);
     223            1 :         g_test_add ("/secret-store/search/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
     224            1 :         g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown);
     225            1 :         g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown);
     226            1 :         g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
     227            1 :         g_test_add ("/secret-store/search/read-with-schema", Test, NULL, setup, test_read_with_schema, teardown);
     228              : 
     229            1 :         return g_test_run ();
     230              : }
        

Generated by: LCOV version 2.0-1