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

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-secret-fields.c: Test secret fields
       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 "secret-store/gkm-secret-fields.h"
      26              : 
      27              : #include "pkcs11/pkcs11i.h"
      28              : 
      29              : #include <glib.h>
      30              : 
      31              : #include <stdlib.h>
      32              : #include <stdio.h>
      33              : #include <string.h>
      34              : 
      35              : static void
      36            1 : test_new (void)
      37              : {
      38            1 :         GHashTable *fields = gkm_secret_fields_new ();
      39            1 :         g_hash_table_unref (fields);
      40            1 : }
      41              : 
      42              : static void
      43            1 : test_boxed (void)
      44              : {
      45            1 :         GType boxed = gkm_secret_fields_boxed_type ();
      46            1 :         GType check = gkm_secret_fields_boxed_type ();
      47            1 :         g_assert (boxed == check);
      48            1 : }
      49              : 
      50              : static void
      51            1 : test_add_get_values (void)
      52              : {
      53            1 :         GHashTable *fields = gkm_secret_fields_new ();
      54              :         const gchar *value;
      55              : 
      56            1 :         gkm_secret_fields_add (fields, "one", "value1");
      57            1 :         gkm_secret_fields_take (fields, g_strdup ("two"), g_strdup ("value2"));
      58            1 :         gkm_secret_fields_add (fields, "three", NULL);
      59              : 
      60            1 :         value = gkm_secret_fields_get (fields, "one");
      61            1 :         g_assert_cmpstr (value, ==, "value1");
      62              : 
      63            1 :         value = gkm_secret_fields_get (fields, "two");
      64            1 :         g_assert_cmpstr (value, ==, "value2");
      65              : 
      66            1 :         value = gkm_secret_fields_get (fields, "three");
      67            1 :         g_assert_cmpstr (value, ==, "");
      68              : 
      69            1 :         g_hash_table_unref (fields);
      70            1 : }
      71              : 
      72              : static void
      73            1 : test_parse (void)
      74              : {
      75            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value1\0two\0value2\0three\0value3\0", 35 };
      76              :         gchar *schema_name;
      77              :         GHashTable *fields;
      78              :         const gchar *value;
      79              :         CK_RV rv;
      80              : 
      81            1 :         rv = gkm_secret_fields_parse (&attr, &fields, &schema_name);
      82            1 :         g_assert (rv == CKR_OK);
      83              : 
      84            1 :         g_assert_cmpuint (g_hash_table_size (fields), ==, 3);
      85            1 :         value = g_hash_table_lookup (fields, "one");
      86            1 :         g_assert_cmpstr (value, ==, "value1");
      87            1 :         value = g_hash_table_lookup (fields, "two");
      88            1 :         g_assert_cmpstr (value, ==, "value2");
      89            1 :         value = g_hash_table_lookup (fields, "three");
      90            1 :         g_assert_cmpstr (value, ==, "value3");
      91              : 
      92            1 :         g_assert (schema_name == NULL);
      93              : 
      94            1 :         g_hash_table_unref (fields);
      95            1 : }
      96              : 
      97              : static void
      98            1 : test_parse_schema (void)
      99              : {
     100            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value1\0two\0valu\0xdg:schema\0xxx\0", 35 };
     101              :         gchar *schema_name;
     102              :         GHashTable *fields;
     103              :         const gchar *value;
     104              :         CK_RV rv;
     105              : 
     106            1 :         rv = gkm_secret_fields_parse (&attr, &fields, &schema_name);
     107            1 :         g_assert (rv == CKR_OK);
     108              : 
     109            1 :         g_assert_cmpuint (g_hash_table_size (fields), ==, 3);
     110            1 :         value = g_hash_table_lookup (fields, "one");
     111            1 :         g_assert_cmpstr (value, ==, "value1");
     112            1 :         value = g_hash_table_lookup (fields, "two");
     113            1 :         g_assert_cmpstr (value, ==, "valu");
     114            1 :         value = g_hash_table_lookup (fields, GKM_SECRET_FIELD_SCHEMA);
     115            1 :         g_assert_cmpstr (value, ==, "xxx");
     116              : 
     117            1 :         g_assert_cmpstr (schema_name, ==, "xxx");
     118              : 
     119            1 :         g_free (schema_name);
     120            1 :         g_hash_table_unref (fields);
     121            1 : }
     122              : 
     123              : static void
     124            1 : test_parse_empty (void)
     125              : {
     126            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "", 0 };
     127              :         GHashTable *fields;
     128              :         CK_RV rv;
     129              : 
     130            1 :         rv = gkm_secret_fields_parse (&attr, &fields, NULL);
     131            1 :         g_assert (rv == CKR_OK);
     132              : 
     133            1 :         g_assert_cmpuint (g_hash_table_size (fields), == , 0);
     134              : 
     135            1 :         g_hash_table_unref (fields);
     136            1 : }
     137              : 
     138              : static void
     139            1 : test_parse_null_invalid (void)
     140              : {
     141            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 5 };
     142              :         GHashTable *fields;
     143              :         CK_RV rv;
     144              : 
     145            1 :         rv = gkm_secret_fields_parse (&attr, &fields, NULL);
     146            1 :         g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
     147            1 : }
     148              : 
     149              : static void
     150            1 : test_parse_missing_value (void)
     151              : {
     152            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one", 3 };
     153              :         GHashTable *fields;
     154              :         CK_RV rv;
     155              : 
     156            1 :         rv = gkm_secret_fields_parse (&attr, &fields, NULL);
     157            1 :         g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
     158            1 : }
     159              : 
     160              : static void
     161            1 : test_parse_missing_terminator (void)
     162              : {
     163            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value", 9 };
     164              :         GHashTable *fields;
     165              :         CK_RV rv;
     166              : 
     167            1 :         rv = gkm_secret_fields_parse (&attr, &fields, NULL);
     168            1 :         g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
     169            1 : }
     170              : 
     171              : static void
     172            1 : test_parse_not_utf8 (void)
     173              : {
     174            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0not\234utf8\0", 13 };
     175              :         GHashTable *fields;
     176              :         CK_RV rv;
     177              : 
     178            1 :         rv = gkm_secret_fields_parse (&attr, &fields, NULL);
     179            1 :         g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
     180            1 : }
     181              : 
     182              : static void
     183            1 : test_serialize (void)
     184              : {
     185              :         gchar buffer[32];
     186            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, buffer, 32 };
     187              :         GHashTable *fields;
     188              :         CK_RV rv;
     189              : 
     190            1 :         fields = gkm_secret_fields_new ();
     191            1 :         gkm_secret_fields_add (fields, "one", "value1");
     192              : 
     193            1 :         rv = gkm_secret_fields_serialize (&attr, fields, NULL);
     194            1 :         g_assert (rv == CKR_OK);
     195            1 :         g_assert (attr.ulValueLen == 11);
     196            1 :         g_assert (memcmp (buffer, "one\0value1\0", 11) == 0);
     197              : 
     198            1 :         g_hash_table_unref (fields);
     199            1 : }
     200              : 
     201              : static void
     202            1 : test_serialize_schema (void)
     203              : {
     204              :         gchar buffer[32];
     205            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, buffer, 32 };
     206              :         GHashTable *fields;
     207              :         CK_RV rv;
     208              : 
     209            1 :         fields = gkm_secret_fields_new ();
     210            1 :         gkm_secret_fields_add (fields, "one", "value1");
     211              : 
     212            1 :         rv = gkm_secret_fields_serialize (&attr, fields, "xxx");
     213            1 :         g_assert (rv == CKR_OK);
     214            1 :         g_assert_cmpint (attr.ulValueLen, ==, 26);
     215            1 :         g_assert (memcmp (buffer, "one\0value1\0xdg:schema\0xxx\0", 26) == 0);
     216              : 
     217            1 :         g_hash_table_unref (fields);
     218            1 : }
     219              : 
     220              : static void
     221            1 : test_serialize_schema_already (void)
     222              : {
     223              :         gchar buffer[32];
     224            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, buffer, 32 };
     225              :         GHashTable *fields;
     226              :         CK_RV rv;
     227              : 
     228            1 :         fields = gkm_secret_fields_new ();
     229            1 :         gkm_secret_fields_add (fields, GKM_SECRET_FIELD_SCHEMA, "yyy");
     230              : 
     231            1 :         rv = gkm_secret_fields_serialize (&attr, fields, "xxx");
     232            1 :         g_assert (rv == CKR_OK);
     233            1 :         g_assert (attr.ulValueLen == 15);
     234            1 :         g_assert (memcmp (buffer, "xdg:schema\0yyy\0", 15) == 0);
     235              : 
     236            1 :         g_hash_table_unref (fields);
     237            1 : }
     238              : 
     239              : static void
     240            1 : test_serialize_length (void)
     241              : {
     242            1 :         CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 0 };
     243              :         GHashTable *fields;
     244              :         CK_RV rv;
     245              : 
     246            1 :         fields = gkm_secret_fields_new ();
     247            1 :         gkm_secret_fields_add (fields, "one", "value1");
     248              : 
     249            1 :         rv = gkm_secret_fields_serialize (&attr, fields, NULL);
     250            1 :         g_assert (rv == CKR_OK);
     251            1 :         g_assert (attr.ulValueLen == 11);
     252              : 
     253            1 :         g_hash_table_unref (fields);
     254            1 : }
     255              : 
     256              : static void
     257            1 : test_add_get_compat_uint32 (void)
     258              : {
     259              :         GHashTable *fields;
     260              :         gboolean ret;
     261              :         guint32 value;
     262              : 
     263            1 :         fields = gkm_secret_fields_new ();
     264            1 :         gkm_secret_fields_add_compat_uint32 (fields, "field", 992);
     265              : 
     266            1 :         ret = gkm_secret_fields_get_compat_uint32 (fields, "field", &value);
     267            1 :         g_assert (ret == TRUE);
     268            1 :         g_assert_cmpuint (value, ==, 992);
     269              : 
     270            1 :         g_hash_table_unref (fields);
     271            1 : }
     272              : 
     273              : static void
     274            1 : test_get_compat_uint32_fail (void)
     275              : {
     276              :         GHashTable *fields;
     277              :         gboolean ret;
     278              :         guint32 value;
     279              : 
     280            1 :         fields = gkm_secret_fields_new ();
     281            1 :         gkm_secret_fields_add (fields, "test", "value");
     282              : 
     283            1 :         ret = gkm_secret_fields_get_compat_uint32 (fields, "test", &value);
     284            1 :         g_assert (ret == FALSE);
     285              : 
     286            1 :         g_hash_table_unref (fields);
     287            1 : }
     288              : 
     289              : static void
     290            1 : test_get_compat_hashed_string (void)
     291              : {
     292              :         GHashTable *fields;
     293              :         gboolean ret;
     294              :         gchar *value;
     295              : 
     296            1 :         fields = gkm_secret_fields_new ();
     297            1 :         gkm_secret_fields_add (fields, "test", "value");
     298              : 
     299            1 :         ret = gkm_secret_fields_get_compat_hashed_string (fields, "test", &value);
     300            1 :         g_assert (ret == TRUE); /* Must be the same as the old gnome-keyring code */
     301            1 :         g_assert_cmpstr (value, ==, "2063c1608d6e0baf80249c42e2be5804");
     302            1 :         g_free (value);
     303              : 
     304            1 :         g_hash_table_unref (fields);
     305            1 : }
     306              : 
     307              : static void
     308            1 : test_get_compat_hashed_already (void)
     309              : {
     310              :         GHashTable *fields;
     311              :         gboolean ret;
     312              :         gchar *value;
     313              : 
     314            1 :         fields = gkm_secret_fields_new ();
     315            1 :         gkm_secret_fields_add_compat_hashed_string (fields, "test", "e991664529cd0caeb6e9fce8fac3d611");
     316              : 
     317            1 :         ret = gkm_secret_fields_get_compat_hashed_string (fields, "test", &value);
     318            1 :         g_assert (ret == TRUE); /* What went in comes out */
     319            1 :         g_assert_cmpstr (value, ==, "e991664529cd0caeb6e9fce8fac3d611");
     320            1 :         g_free (value);
     321              : 
     322            1 :         g_hash_table_unref (fields);
     323            1 : }
     324              : 
     325              : static void
     326            1 : test_get_compat_hashed_uint32 (void)
     327              : {
     328              :         GHashTable *fields;
     329              :         gboolean ret;
     330              :         guint32 value;
     331            1 :         guint32 val = 992;
     332              : 
     333            1 :         fields = gkm_secret_fields_new ();
     334            1 :         gkm_secret_fields_add_compat_uint32 (fields, "test", val);
     335              : 
     336            1 :         ret = gkm_secret_fields_get_compat_hashed_uint32 (fields, "test", &value);
     337            1 :         g_assert (ret == TRUE); /* Must be the same as the old gnome-keyring code */
     338            1 :         g_assert_cmpuint (value, ==, 0x18273645 ^ val ^ (val << 16 | val >> 16));
     339              : 
     340            1 :         g_hash_table_unref (fields);
     341            1 : }
     342              : 
     343              : static void
     344            1 : test_get_compat_hashed_uint32_already (void)
     345              : {
     346              :         GHashTable *fields;
     347              :         gboolean ret;
     348              :         guint32 value;
     349            1 :         guint32 val = 0x1bc735a5;
     350              : 
     351            1 :         fields = gkm_secret_fields_new ();
     352            1 :         gkm_secret_fields_add_compat_hashed_uint32 (fields, "test", val);
     353              : 
     354            1 :         ret = gkm_secret_fields_get_compat_hashed_uint32 (fields, "test", &value);
     355            1 :         g_assert (ret == TRUE); /* What went in comes out */
     356            1 :         g_assert_cmpuint (value, ==, val);
     357              : 
     358            1 :         g_hash_table_unref (fields);
     359            1 : }
     360              : 
     361              : static void
     362            1 : test_get_names (void)
     363              : {
     364              :         GHashTable *fields;
     365              :         GList *names, *l;
     366              : 
     367            1 :         fields = gkm_secret_fields_new ();
     368              : 
     369            1 :         gkm_secret_fields_add (fields, "one", "11111");
     370            1 :         gkm_secret_fields_add_compat_uint32 (fields, "two", 2);
     371            1 :         gkm_secret_fields_add_compat_hashed_string (fields, "test", "2063c1608d6e0baf80249c42e2be5804");
     372              : 
     373            1 :         names = gkm_secret_fields_get_names (fields);
     374            1 :         g_assert_cmpuint (g_list_length (names), ==, 3);
     375              : 
     376            4 :         for (l = names; l; l = g_list_next (l)) {
     377            3 :                 g_assert (l->data);
     378            3 :                 if (!g_str_equal (l->data, "one") &&
     379            2 :                     !g_str_equal (l->data, "two") &&
     380            1 :                     !g_str_equal (l->data, "test"))
     381            0 :                         g_assert_not_reached ();
     382              :         }
     383              : 
     384            1 :         g_list_free (names);
     385            1 :         g_hash_table_unref (fields);
     386            1 : }
     387              : 
     388              : static void
     389            1 : test_match (void)
     390              : {
     391              :         GHashTable *haystack;
     392              :         GHashTable *needle;
     393              :         gboolean ret;
     394              : 
     395            1 :         haystack = gkm_secret_fields_new ();
     396            1 :         gkm_secret_fields_add (haystack, "one", "11111");
     397            1 :         gkm_secret_fields_add_compat_uint32 (haystack, "two", 2);
     398            1 :         gkm_secret_fields_add_compat_hashed_string (haystack, "test", "2063c1608d6e0baf80249c42e2be5804"); /* Hashed 'value' */
     399            1 :         gkm_secret_fields_add_compat_hashed_uint32 (haystack, "number", 0x1bc735a5); /* Hashed 992 */
     400            1 :         gkm_secret_fields_add (haystack, "extra", "an extra value");
     401              : 
     402            1 :         needle = gkm_secret_fields_new ();
     403            1 :         gkm_secret_fields_add (needle, "one", "11111");
     404            1 :         gkm_secret_fields_add (needle, "two", "2");
     405            1 :         gkm_secret_fields_add (needle, "test", "value");
     406            1 :         gkm_secret_fields_add (needle, "number", "992");
     407              : 
     408            1 :         ret = gkm_secret_fields_match (haystack, needle);
     409            1 :         g_assert (ret == TRUE);
     410              : 
     411            1 :         g_hash_table_unref (haystack);
     412            1 :         g_hash_table_unref (needle);
     413            1 : }
     414              : 
     415              : static void
     416            1 : test_match_mismatch_value (void)
     417              : {
     418              :         GHashTable *haystack;
     419              :         GHashTable *needle;
     420              :         gboolean ret;
     421              : 
     422            1 :         haystack = gkm_secret_fields_new ();
     423            1 :         gkm_secret_fields_add (haystack, "field", "value");
     424              : 
     425            1 :         needle = gkm_secret_fields_new ();
     426            1 :         gkm_secret_fields_add (needle, "field", "another");
     427              : 
     428            1 :         ret = gkm_secret_fields_match (haystack, needle);
     429            1 :         g_assert (ret == FALSE);
     430              : 
     431            1 :         g_hash_table_unref (haystack);
     432            1 :         g_hash_table_unref (needle);
     433            1 : }
     434              : 
     435              : static void
     436            1 : test_match_mismatch_field (void)
     437              : {
     438              :         GHashTable *haystack;
     439              :         GHashTable *needle;
     440              :         gboolean ret;
     441              : 
     442            1 :         haystack = gkm_secret_fields_new ();
     443            1 :         gkm_secret_fields_add (haystack, "test", "value");
     444              : 
     445            1 :         needle = gkm_secret_fields_new ();
     446            1 :         gkm_secret_fields_add (needle, "field", "value");
     447              : 
     448            1 :         ret = gkm_secret_fields_match (haystack, needle);
     449            1 :         g_assert (ret == FALSE);
     450              : 
     451            1 :         g_hash_table_unref (haystack);
     452            1 :         g_hash_table_unref (needle);
     453            1 : }
     454              : 
     455              : static void
     456            1 : test_match_wrong_hashed (void)
     457              : {
     458              :         GHashTable *haystack;
     459              :         GHashTable *needle;
     460              :         gboolean ret;
     461              : 
     462            1 :         haystack = gkm_secret_fields_new ();
     463            1 :         gkm_secret_fields_add_compat_hashed_uint32 (haystack, "number", 0x1bc735a5); /* Hashed 992 */
     464              : 
     465            1 :         needle = gkm_secret_fields_new ();
     466            1 :         gkm_secret_fields_add (needle, "number", "1000");
     467              : 
     468            1 :         ret = gkm_secret_fields_match (haystack, needle);
     469            1 :         g_assert (ret == FALSE);
     470              : 
     471            1 :         g_hash_table_unref (haystack);
     472            1 :         g_hash_table_unref (needle);
     473            1 : }
     474              : 
     475              : int
     476            1 : main (int argc, char **argv)
     477              : {
     478              : #if !GLIB_CHECK_VERSION(2,35,0)
     479              :         g_type_init ();
     480              : #endif
     481            1 :         g_test_init (&argc, &argv, NULL);
     482              : 
     483            1 :         g_test_add_func ("/secret-store/fields/new", test_new);
     484            1 :         g_test_add_func ("/secret-store/fields/boxed", test_boxed);
     485            1 :         g_test_add_func ("/secret-store/fields/add_get_values", test_add_get_values);
     486            1 :         g_test_add_func ("/secret-store/fields/parse", test_parse);
     487            1 :         g_test_add_func ("/secret-store/fields/parse_schema", test_parse_schema);
     488            1 :         g_test_add_func ("/secret-store/fields/parse_empty", test_parse_empty);
     489            1 :         g_test_add_func ("/secret-store/fields/parse_null_invalid", test_parse_null_invalid);
     490            1 :         g_test_add_func ("/secret-store/fields/parse_missing_value", test_parse_missing_value);
     491            1 :         g_test_add_func ("/secret-store/fields/parse_missing_terminator", test_parse_missing_terminator);
     492            1 :         g_test_add_func ("/secret-store/fields/parse_not_utf8", test_parse_not_utf8);
     493            1 :         g_test_add_func ("/secret-store/fields/serialize", test_serialize);
     494            1 :         g_test_add_func ("/secret-store/fields/serialize_schema", test_serialize_schema);
     495            1 :         g_test_add_func ("/secret-store/fields/serialize_schema_already", test_serialize_schema_already);
     496            1 :         g_test_add_func ("/secret-store/fields/serialize_length", test_serialize_length);
     497            1 :         g_test_add_func ("/secret-store/fields/add_get_compat_uint32", test_add_get_compat_uint32);
     498            1 :         g_test_add_func ("/secret-store/fields/get_compat_uint32_fail", test_get_compat_uint32_fail);
     499            1 :         g_test_add_func ("/secret-store/fields/get_compat_hashed_string", test_get_compat_hashed_string);
     500            1 :         g_test_add_func ("/secret-store/fields/get_compat_hashed_already", test_get_compat_hashed_already);
     501            1 :         g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32", test_get_compat_hashed_uint32);
     502            1 :         g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32_already", test_get_compat_hashed_uint32_already);
     503            1 :         g_test_add_func ("/secret-store/fields/get_names", test_get_names);
     504            1 :         g_test_add_func ("/secret-store/fields/match", test_match);
     505            1 :         g_test_add_func ("/secret-store/fields/match_mismatch_value", test_match_mismatch_value);
     506            1 :         g_test_add_func ("/secret-store/fields/match_mismatch_field", test_match_mismatch_field);
     507            1 :         g_test_add_func ("/secret-store/fields/match_wrong_hashed", test_match_wrong_hashed);
     508              : 
     509            1 :         return g_test_run ();
     510              : }
        

Generated by: LCOV version 2.0-1