LCOV - code coverage report
Current view: top level - gcr - test-record.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 143 146 97.9 %
Date: 2022-09-04 10:20:22 Functions: 20 20 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 49 94 52.1 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /*
       3                 :            :    Copyright (C) 2011 Collabora Ltd.
       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                 :            :    see <http://www.gnu.org/licenses/>.
      18                 :            : 
      19                 :            :    Author: Stef Walter <stefw@collabora.co.uk>
      20                 :            : */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : 
      24                 :            : #include "gcr/gcr-record.h"
      25                 :            : 
      26                 :            : #include "egg/egg-testing.h"
      27                 :            : 
      28                 :            : #include <glib.h>
      29                 :            : 
      30                 :            : typedef struct {
      31                 :            :         GcrRecord *record;
      32                 :            : } Test;
      33                 :            : 
      34                 :            : static void
      35                 :         11 : setup (Test *test, gconstpointer unused)
      36                 :            : {
      37                 :         11 :         test->record = _gcr_record_parse_colons ("one:tab\\there::YW9ldTM4Mzg=:f\xfc""nf:3533333:-88", -1);
      38                 :         11 : }
      39                 :            : 
      40                 :            : static void
      41                 :         11 : teardown (Test *test, gconstpointer unused)
      42                 :            : {
      43                 :         11 :         _gcr_record_free (test->record);
      44                 :         11 : }
      45                 :            : 
      46                 :            : static void
      47                 :          1 : test_count (Test *test, gconstpointer unused)
      48                 :            : {
      49         [ -  + ]:          1 :         g_assert_cmpuint (_gcr_record_get_count (test->record), ==, 7);
      50                 :          1 : }
      51                 :            : 
      52                 :            : static void
      53                 :          1 : test_parse_colons (void)
      54                 :            : {
      55                 :            :         GcrRecord *record;
      56                 :            : 
      57                 :          1 :         record = _gcr_record_parse_colons ("one:two::four::six", -1);
      58         [ -  + ]:          1 :         g_assert (record);
      59                 :            : 
      60         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
      61         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
      62         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "");
      63         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 3), ==, "four");
      64         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 4), ==, "");
      65         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 5), ==, "six");
      66         [ -  + ]:          1 :         g_assert (_gcr_record_get_raw (record, 6) == NULL);
      67                 :            : 
      68                 :          1 :         _gcr_record_free (record);
      69                 :          1 : }
      70                 :            : 
      71                 :            : static void
      72                 :          1 : test_parse_spaces (void)
      73                 :            : {
      74                 :            :         GcrRecord *record;
      75                 :            : 
      76                 :          1 :         record = _gcr_record_parse_spaces (" one two  four six   ", -1);
      77         [ -  + ]:          1 :         g_assert (record);
      78                 :            : 
      79         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
      80         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
      81         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "four");
      82         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 3), ==, "six");
      83         [ -  + ]:          1 :         g_assert (_gcr_record_get_raw (record, 4) == NULL);
      84         [ -  + ]:          1 :         g_assert_cmpuint (_gcr_record_get_count (record), ==, 4);
      85                 :            : 
      86                 :          1 :         _gcr_record_free (record);
      87                 :          1 : }
      88                 :            : 
      89                 :            : static void
      90                 :          1 : test_parse_part (void)
      91                 :            : {
      92                 :            :         GcrRecord *record;
      93                 :            : 
      94                 :          1 :         record = _gcr_record_parse_colons ("one:two::four::six", 8);
      95         [ -  + ]:          1 :         g_assert (record);
      96                 :            : 
      97         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
      98         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
      99         [ -  + ]:          1 :         g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "");
     100         [ -  + ]:          1 :         g_assert (_gcr_record_get_raw (record, 3) == NULL);
     101                 :            : 
     102                 :          1 :         _gcr_record_free (record);
     103                 :          1 : }
     104                 :            : 
     105                 :            : static void
     106                 :          1 : test_parse_too_long (void)
     107                 :            : {
     108                 :            :         GcrRecord *record;
     109                 :            : 
     110                 :            :         /* Too many columns */
     111                 :          1 :         record = _gcr_record_parse_colons (":::::::::::::::::::::::::::::::::::::::::::::::::::::", -1);
     112         [ -  + ]:          1 :         g_assert (record == NULL);
     113                 :          1 : }
     114                 :            : 
     115                 :            : static void
     116                 :          1 : test_find (void)
     117                 :            : {
     118                 :            :         GcrRecord *uid, *pub, *one, *check;
     119                 :            :         GPtrArray *records;
     120                 :            : 
     121                 :          1 :         records = g_ptr_array_new_with_free_func (_gcr_record_free);
     122                 :            : 
     123                 :          1 :         one = _gcr_record_parse_colons ("one:two::four::six", -1);
     124                 :          1 :         g_ptr_array_add (records, one);
     125                 :          1 :         pub = _gcr_record_parse_colons ("pub:two", -1);
     126                 :          1 :         g_ptr_array_add (records, pub);
     127                 :          1 :         uid = _gcr_record_parse_colons ("uid:two", -1);
     128                 :          1 :         g_ptr_array_add (records, uid);
     129                 :            : 
     130                 :          1 :         check = _gcr_records_find (records, GCR_RECORD_SCHEMA_PUB);
     131         [ -  + ]:          1 :         g_assert (check == pub);
     132                 :            : 
     133                 :          1 :         check = _gcr_records_find (records, GCR_RECORD_SCHEMA_UID);
     134         [ -  + ]:          1 :         g_assert (check == uid);
     135                 :            : 
     136                 :          1 :         g_ptr_array_unref (records);
     137                 :          1 : }
     138                 :            : 
     139                 :            : static void
     140                 :          1 : test_get_string (Test *test, gconstpointer unused)
     141                 :            : {
     142                 :          1 :         gchar *value = _gcr_record_get_string (test->record, 1);
     143         [ -  + ]:          1 :         g_assert (value);
     144                 :            : 
     145         [ -  + ]:          1 :         g_assert_cmpstr (value, ==, "tab\there");
     146                 :          1 :         g_free (value);
     147                 :          1 : }
     148                 :            : 
     149                 :            : static void
     150                 :          1 : test_get_string_null (Test *test, gconstpointer unused)
     151                 :            : {
     152                 :          1 :         gchar *value = _gcr_record_get_string (test->record, 35);
     153         [ -  + ]:          1 :         g_assert (value == NULL);
     154                 :          1 : }
     155                 :            : 
     156                 :            : static void
     157                 :          1 : test_get_string_latin1 (Test *test, gconstpointer unused)
     158                 :            : {
     159                 :          1 :         gchar *value = _gcr_record_get_string (test->record, 4);
     160         [ -  + ]:          1 :         g_assert (value);
     161                 :            : 
     162         [ -  + ]:          1 :         g_assert_cmpstr (value, ==, "f\xc3\xbc""nf");
     163         [ -  + ]:          1 :         g_assert (g_utf8_validate (value, -1, NULL));
     164                 :          1 :         g_free (value);
     165                 :          1 : }
     166                 :            : 
     167                 :            : static void
     168                 :          1 : test_get_uint (Test *test, gconstpointer unused)
     169                 :            : {
     170                 :          1 :         guint value = 0;
     171                 :            : 
     172         [ -  + ]:          1 :         if (!_gcr_record_get_uint (test->record, 5, &value))
     173                 :          0 :                 g_assert_not_reached ();
     174         [ -  + ]:          1 :         g_assert_cmpuint (value, ==, 3533333);
     175                 :          1 : }
     176                 :            : 
     177                 :            : static void
     178                 :          1 : test_get_uint_range (Test *test, gconstpointer unused)
     179                 :            : {
     180                 :          1 :         guint value = 0;
     181                 :            : 
     182         [ -  + ]:          1 :         if (_gcr_record_get_uint (test->record, 6, &value))
     183                 :          0 :                 g_assert_not_reached ();
     184                 :          1 : }
     185                 :            : 
     186                 :            : static void
     187                 :          1 : test_get_uint_invalid (Test *test, gconstpointer unused)
     188                 :            : {
     189                 :          1 :         guint value = 0;
     190                 :            : 
     191         [ -  + ]:          1 :         if (_gcr_record_get_uint (test->record, 0, &value))
     192                 :          0 :                 g_assert_not_reached ();
     193                 :          1 : }
     194                 :            : 
     195                 :            : static void
     196                 :          1 : test_get_base64 (Test *test, gconstpointer unused)
     197                 :            : {
     198                 :            :         gchar *value;
     199                 :            :         gsize n_value;
     200                 :            : 
     201                 :          1 :         value = _gcr_record_get_base64 (test->record, 3, &n_value);
     202         [ -  + ]:          1 :         g_assert (value);
     203         [ -  + ]:          1 :         egg_assert_cmpsize (n_value, ==, 8);
     204         [ -  + ]:          1 :         g_assert (memcmp (value, "aoeu3838", n_value) == 0);
     205                 :            : 
     206                 :          1 :         g_free (value);
     207                 :          1 : }
     208                 :            : 
     209                 :            : 
     210                 :            : static void
     211                 :          1 : test_free_null (void)
     212                 :            : {
     213                 :          1 :         _gcr_record_free (NULL);
     214                 :          1 : }
     215                 :            : 
     216                 :            : static void
     217                 :          1 : test_get_schema (Test *test, gconstpointer unused)
     218                 :            : {
     219                 :            :         GQuark schema;
     220                 :            :         GQuark check;
     221                 :            : 
     222                 :            :         /* Initialize this quark */
     223                 :          1 :         check = g_quark_from_static_string ("one");
     224                 :            : 
     225                 :          1 :         schema = _gcr_record_get_schema (test->record);
     226         [ -  + ]:          1 :         g_assert (check == schema);
     227         [ -  + ]:          1 :         g_assert_cmpstr (g_quark_to_string (schema), ==, "one");
     228                 :          1 : }
     229                 :            : 
     230                 :            : static void
     231                 :          1 : test_copy (Test *test, gconstpointer unused)
     232                 :            : {
     233                 :            :         GcrRecord *copy;
     234                 :            :         guint count, i;
     235                 :            : 
     236                 :          1 :         copy = _gcr_record_copy (test->record);
     237                 :            : 
     238                 :          1 :         count = _gcr_record_get_count (test->record);
     239         [ -  + ]:          1 :         g_assert_cmpuint (_gcr_record_get_count (copy), ==, count);
     240         [ +  + ]:          8 :         for (i = 0; i < count; i++) {
     241         [ -  + ]:          7 :                 g_assert_cmpstr (_gcr_record_get_raw (copy, i), ==,
     242                 :            :                                  _gcr_record_get_raw (test->record, i));
     243                 :            :         }
     244                 :            : 
     245         [ -  + ]:          1 :         g_assert (_gcr_record_get_raw (copy, count) == NULL);
     246                 :          1 :         _gcr_record_free (copy);
     247                 :          1 : }
     248                 :            : 
     249                 :            : static void
     250                 :          1 : test_boxed (Test *test, gconstpointer unused)
     251                 :            : {
     252                 :            :         GcrRecord *copy;
     253                 :            :         guint count, i;
     254                 :            : 
     255                 :          1 :         copy = g_boxed_copy (GCR_TYPE_RECORD, test->record);
     256                 :            : 
     257                 :          1 :         count = _gcr_record_get_count (test->record);
     258         [ -  + ]:          1 :         g_assert_cmpuint (_gcr_record_get_count (copy), ==, count);
     259         [ +  + ]:          8 :         for (i = 0; i < count; i++) {
     260         [ -  + ]:          7 :                 g_assert_cmpstr (_gcr_record_get_raw (copy, i), ==,
     261                 :            :                                  _gcr_record_get_raw (test->record, i));
     262                 :            :         }
     263                 :            : 
     264         [ -  + ]:          1 :         g_assert (_gcr_record_get_raw (copy, count) == NULL);
     265                 :          1 :         g_boxed_free (GCR_TYPE_RECORD, copy);
     266                 :          1 : }
     267                 :            : 
     268                 :            : int
     269                 :          1 : main (int argc, char **argv)
     270                 :            : {
     271                 :          1 :         g_test_init (&argc, &argv, NULL);
     272                 :            : 
     273                 :          1 :         g_test_add_func ("/gcr/record/parse-colons", test_parse_colons);
     274                 :          1 :         g_test_add_func ("/gcr/record/parse-spaces", test_parse_spaces);
     275                 :          1 :         g_test_add_func ("/gcr/record/parse_part", test_parse_part);
     276                 :          1 :         g_test_add_func ("/gcr/record/parse_too_long", test_parse_too_long);
     277                 :          1 :         g_test_add_func ("/gcr/record/free_null", test_free_null);
     278                 :          1 :         g_test_add_func ("/gcr/record/find", test_find);
     279                 :          1 :         g_test_add ("/gcr/record/count", Test, NULL, setup, test_count, teardown);
     280                 :          1 :         g_test_add ("/gcr/record/copy", Test, NULL, setup, test_copy, teardown);
     281                 :          1 :         g_test_add ("/gcr/record/boxed", Test, NULL, setup, test_boxed, teardown);
     282                 :          1 :         g_test_add ("/gcr/record/get_string", Test, NULL, setup, test_get_string, teardown);
     283                 :          1 :         g_test_add ("/gcr/record/get_string_null", Test, NULL, setup, test_get_string_null, teardown);
     284                 :          1 :         g_test_add ("/gcr/record/get_string_latin1", Test, NULL, setup, test_get_string_latin1, teardown);
     285                 :          1 :         g_test_add ("/gcr/record/get_uint", Test, NULL, setup, test_get_uint, teardown);
     286                 :          1 :         g_test_add ("/gcr/record/get_uint_invalid", Test, NULL, setup, test_get_uint_invalid, teardown);
     287                 :          1 :         g_test_add ("/gcr/record/get_uint_range", Test, NULL, setup, test_get_uint_range, teardown);
     288                 :          1 :         g_test_add ("/gcr/record/get_base64", Test, NULL, setup, test_get_base64, teardown);
     289                 :          1 :         g_test_add ("/gcr/record/get_schema", Test, NULL, setup, test_get_schema, teardown);
     290                 :            : 
     291                 :          1 :         return g_test_run ();
     292                 :            : }

Generated by: LCOV version 1.14