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

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-secret-binary.c: Test binary keyring read and write
       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-binary.h"
      28              : #include "secret-store/gkm-secret-collection.h"
      29              : #include "secret-store/gkm-secret-data.h"
      30              : #include "secret-store/gkm-secret-fields.h"
      31              : #include "secret-store/gkm-secret-item.h"
      32              : 
      33              : #include "gkm/gkm-secret.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              :         GkmSecretData *sdata;
      48              : } Test;
      49              : 
      50              : static void
      51            9 : setup (Test *test, gconstpointer unused)
      52              : {
      53              :         GkmSecret *master;
      54              : 
      55            9 :         test->module = test_secret_module_initialize_and_enter ();
      56            9 :         test->session = test_secret_module_open_session (TRUE);
      57              : 
      58            9 :         test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
      59              :                                    "module", test->module,
      60              :                                    "identifier", "test",
      61              :                                    "label", "brigadooooooooooooon",
      62              :                                    NULL);
      63              : 
      64            9 :         test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
      65            9 :         master = gkm_secret_new_from_password ("my-keyring-password");
      66            9 :         gkm_secret_data_set_master (test->sdata, master);
      67            9 :         g_object_unref (master);
      68              : 
      69            9 :         g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
      70              : 
      71            9 : }
      72              : 
      73              : static void
      74            9 : teardown (Test *test, gconstpointer unused)
      75              : {
      76            9 :         g_object_unref (test->collection);
      77            9 :         g_object_unref (test->sdata);
      78            9 :         test_secret_module_leave_and_finalize ();
      79            9 : }
      80              : 
      81              : static GkmDataResult
      82            7 : check_read_keyring_file (Test *test, const gchar *path)
      83              : {
      84              :         GkmDataResult res;
      85              :         gchar *data;
      86              :         gsize n_data;
      87              : 
      88            7 :         if (!g_file_get_contents (path, &data, &n_data, NULL))
      89            0 :                 g_assert_not_reached ();
      90            7 :         res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
      91            7 :         g_free (data);
      92              : 
      93            7 :         return res;
      94              : }
      95              : 
      96              : 
      97              : static void
      98            1 : test_read_encrypted (Test *test, gconstpointer unused)
      99              : {
     100              :         GkmDataResult res;
     101              : 
     102            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
     103            1 :         g_assert (res == GKM_DATA_SUCCESS);
     104              : 
     105            1 :         test_secret_collection_validate (test->collection, test->sdata);
     106            1 : }
     107              : 
     108              : static void
     109            1 : test_read_wrong_format (Test *test, gconstpointer unused)
     110              : {
     111              :         GkmDataResult res;
     112              : 
     113            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring");
     114            1 :         g_assert (res == GKM_DATA_UNRECOGNIZED);
     115            1 : }
     116              : 
     117              : static void
     118            1 : test_read_wrong_master (Test *test, gconstpointer unused)
     119              : {
     120              :         GkmDataResult res;
     121              :         GkmSecret *master;
     122              : 
     123            1 :         master = gkm_secret_new_from_password ("wrong");
     124            1 :         gkm_secret_data_set_master (test->sdata, master);
     125            1 :         g_object_unref (master);
     126              : 
     127            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
     128            1 :         g_assert (res == GKM_DATA_LOCKED);
     129            1 : }
     130              : 
     131              : static void
     132            1 : test_read_sdata_but_no_master (Test *test, gconstpointer unused)
     133              : {
     134              :         GkmDataResult res;
     135              : 
     136            1 :         gkm_secret_data_set_master (test->sdata, NULL);
     137              : 
     138            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
     139            1 :         g_assert (res == GKM_DATA_LOCKED);
     140            1 : }
     141              : 
     142              : static void
     143            1 : test_write (Test *test, gconstpointer unused)
     144              : {
     145              :         GkmDataResult res;
     146              :         gpointer data;
     147              :         gsize n_data;
     148              : 
     149            1 :         test_secret_collection_populate (test->collection, test->sdata);
     150              : 
     151            1 :         res = gkm_secret_binary_write (test->collection, test->sdata, &data, &n_data);
     152            1 :         g_assert (res == GKM_DATA_SUCCESS);
     153            1 :         g_assert (data);
     154            1 :         g_assert (n_data);
     155              : 
     156              :         /* Try parsing it again */
     157            1 :         res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
     158            1 :         g_assert (res == GKM_DATA_SUCCESS);
     159            1 :         g_free (data);
     160            1 : }
     161              : 
     162              : static void
     163            1 : test_remove_unavailable (Test *test, gconstpointer unused)
     164              : {
     165              :         GkmDataResult res;
     166              :         GList *items;
     167              :         gchar *data;
     168              :         gsize n_data;
     169              : 
     170            1 :         if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring", &data, &n_data, NULL))
     171            0 :                 g_assert_not_reached ();
     172            1 :         res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
     173            1 :         g_assert (res == GKM_DATA_SUCCESS);
     174              : 
     175              :         /* Two items from the file */
     176            1 :         items = gkm_secret_collection_get_items (test->collection);
     177            1 :         g_assert_cmpint (g_list_length (items), ==, 2);
     178            1 :         g_list_free (items);
     179              : 
     180              :         /* Fill in some more data */
     181            1 :         test_secret_collection_populate (test->collection, test->sdata);
     182              : 
     183              :         /* Should have added three more */
     184            1 :         items = gkm_secret_collection_get_items (test->collection);
     185            1 :         g_assert_cmpint (g_list_length (items), ==, 5);
     186            1 :         g_list_free (items);
     187              : 
     188              :         /* Re-read the keyring */
     189            1 :         res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
     190            1 :         g_assert (res == GKM_DATA_SUCCESS);
     191              : 
     192              :         /* And we're back to two */
     193            1 :         items = gkm_secret_collection_get_items (test->collection);
     194            1 :         g_assert_cmpint (g_list_length (items), ==, 2);
     195            1 :         g_list_free (items);
     196              : 
     197            1 :         g_free (data);
     198            1 : }
     199              : 
     200              : static void
     201            1 : test_read_created_on_solaris_opencsw (Test *test, gconstpointer unused)
     202              : {
     203              :         GkmDataResult res;
     204              :         GkmSecret *master;
     205              : 
     206            1 :         master = gkm_secret_new_from_password ("test");
     207            1 :         gkm_secret_data_set_master (test->sdata, master);
     208            1 :         g_object_unref (master);
     209              : 
     210            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/created-on-solaris-opencsw.keyring");
     211            1 :         g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
     212            1 : }
     213              : 
     214              : static void
     215            1 : test_read_created_on_rhel (Test *test, gconstpointer unused)
     216              : {
     217              :         GkmDataResult res;
     218              :         GkmSecret *master;
     219              : 
     220            1 :         master = gkm_secret_new_from_password ("test");
     221            1 :         gkm_secret_data_set_master (test->sdata, master);
     222            1 :         g_object_unref (master);
     223              : 
     224            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/created-on-rhel.keyring");
     225            1 :         g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
     226            1 : }
     227              : 
     228              : static void
     229            1 : test_read_with_schema (Test *test,
     230              :                        gconstpointer unused)
     231              : {
     232              :         GkmDataResult res;
     233              :         GkmSecret *master;
     234              :         GkmSecretItem *item;
     235              : 
     236            1 :         master = gkm_secret_new_from_password ("test");
     237            1 :         gkm_secret_data_set_master (test->sdata, master);
     238            1 :         g_object_unref (master);
     239            1 :         res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted-with-schema.keyring");
     240            1 :         g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
     241              : 
     242            1 :         item = gkm_secret_collection_get_item (test->collection, "1");
     243            1 :         g_assert (item != NULL);
     244              : 
     245            1 :         g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
     246            1 : }
     247              : 
     248              : int
     249            1 : main (int argc, char **argv)
     250              : {
     251              : #if !GLIB_CHECK_VERSION(2,35,0)
     252              :         g_type_init ();
     253              : #endif
     254            1 :         g_test_init (&argc, &argv, NULL);
     255              : 
     256            1 :         g_test_add ("/secret-store/binary/read_encrypted", Test, NULL, setup, test_read_encrypted, teardown);
     257            1 :         g_test_add ("/secret-store/binary/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
     258            1 :         g_test_add ("/secret-store/binary/read_wrong_master", Test, NULL, setup, test_read_wrong_master, teardown);
     259            1 :         g_test_add ("/secret-store/binary/read_sdata_but_no_master", Test, NULL, setup, test_read_sdata_but_no_master, teardown);
     260            1 :         g_test_add ("/secret-store/binary/write", Test, NULL, setup, test_write, teardown);
     261            1 :         g_test_add ("/secret-store/binary/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
     262            1 :         g_test_add ("/secret-store/binary/created_on_rhel", Test, NULL, setup, test_read_created_on_rhel, teardown);
     263            1 :         g_test_add ("/secret-store/binary/created_on_solaris_opencsw", Test, NULL, setup, test_read_created_on_solaris_opencsw, teardown);
     264            1 :         g_test_add ("/secret-store/binary/read_with_schema", Test, NULL, setup, test_read_with_schema, teardown);
     265              : 
     266            1 :         return g_test_run ();
     267              : }
        

Generated by: LCOV version 2.0-1