LCOV - code coverage report
Current view: top level - egg - test-hex.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 47 47 100.0 %
Date: 2024-02-08 14:44:34 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 17 34 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2                 :            : /* unit-test-util.c: Test gck-util.c
       3                 :            : 
       4                 :            :    Copyright (C) 2007 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                 :            :    see <http://www.gnu.org/licenses/>.
      19                 :            : 
      20                 :            :    Author: Stef Walter <stef@memberwebs.com>
      21                 :            : */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #undef G_DISABLE_ASSERT
      26                 :            : 
      27                 :            : #include "egg/egg-hex.h"
      28                 :            : 
      29                 :            : #include <stdlib.h>
      30                 :            : #include <stdio.h>
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 };
      34                 :            : static const gchar *TEST_HEX = "05D695961012AE35";
      35                 :            : static const gchar *TEST_HEX_DELIM = "05  D6  95  96  10  12  AE  35";
      36                 :            : 
      37                 :            : static void
      38                 :          1 : test_encode (void)
      39                 :            : {
      40                 :            :         gchar *hex;
      41                 :            : 
      42                 :          1 :         hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
      43         [ -  + ]:          1 :         g_assert_nonnull (hex);
      44         [ -  + ]:          1 :         g_assert_cmpstr (hex, ==, TEST_HEX);
      45                 :            : 
      46                 :          1 :         g_free (hex);
      47                 :          1 : }
      48                 :            : 
      49                 :            : static void
      50                 :          1 : test_encode_spaces (void)
      51                 :            : {
      52                 :            :         gchar *hex;
      53                 :            : 
      54                 :            :         /* Encode without spaces */
      55                 :          1 :         hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0);
      56         [ -  + ]:          1 :         g_assert_nonnull (hex);
      57         [ -  + ]:          1 :         g_assert_cmpstr (hex, ==, TEST_HEX);
      58                 :            : 
      59                 :          1 :         g_free (hex);
      60                 :            : 
      61                 :            :         /* Encode with spaces */
      62                 :          1 :         hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, "  ", 1);
      63         [ -  + ]:          1 :         g_assert_nonnull (hex);
      64         [ -  + ]:          1 :         g_assert_cmpstr (hex, ==, TEST_HEX_DELIM);
      65                 :            : 
      66                 :          1 :         g_free (hex);
      67                 :          1 : }
      68                 :            : 
      69                 :            : static void
      70                 :          1 : test_decode (void)
      71                 :            : {
      72                 :            :         guchar *data;
      73                 :            :         gsize n_data;
      74                 :            : 
      75                 :          1 :         data = egg_hex_decode (TEST_HEX, -1, &n_data);
      76         [ -  + ]:          1 :         g_assert_nonnull (data);
      77         [ -  + ]:          1 :         g_assert_cmpuint (n_data, ==, sizeof (TEST_DATA));
      78         [ -  + ]:          1 :         g_assert_true (memcmp (data, TEST_DATA, n_data) == 0);
      79                 :          1 :         g_free (data);
      80                 :            : 
      81                 :            :         /* Nothing in, empty out */
      82                 :          1 :         data = egg_hex_decode ("AB", 0, &n_data);
      83         [ -  + ]:          1 :         g_assert_nonnull (data);
      84         [ -  + ]:          1 :         g_assert_cmpuint (n_data, ==, 0);
      85                 :          1 :         g_free (data);
      86                 :            : 
      87                 :            :         /* Delimited*/
      88                 :          1 :         data = egg_hex_decode_full (TEST_HEX_DELIM, -1, "  ", 1, &n_data);
      89         [ -  + ]:          1 :         g_assert_nonnull (data);
      90         [ -  + ]:          1 :         g_assert_cmpuint (n_data, ==, sizeof (TEST_DATA));
      91         [ -  + ]:          1 :         g_assert_true (memcmp (data, TEST_DATA, n_data) == 0);
      92                 :          1 :         g_free (data);
      93                 :          1 : }
      94                 :            : 
      95                 :            : static void
      96                 :          1 : test_decode_fail (void)
      97                 :            : {
      98                 :            :         guchar *data;
      99                 :            :         gsize n_data;
     100                 :            : 
     101                 :            :         /* Invalid input, null out */
     102                 :          1 :         data = egg_hex_decode ("AB", 1, &n_data);
     103         [ -  + ]:          1 :         g_assert_null (data);
     104                 :            : 
     105                 :            :         /* Bad characters, null out */
     106                 :          1 :         data = egg_hex_decode ("ABXX", -1, &n_data);
     107         [ -  + ]:          1 :         g_assert_null (data);
     108                 :            : 
     109                 :            :         /* Not Delimited, null out*/
     110                 :          1 :         data = egg_hex_decode_full ("ABABAB", -1, ":", 1, &n_data);
     111         [ -  + ]:          1 :         g_assert_null (data);
     112                 :          1 : }
     113                 :            : 
     114                 :            : int
     115                 :          1 : main (int argc, char **argv)
     116                 :            : {
     117                 :          1 :         g_test_init (&argc, &argv, NULL);
     118                 :            : 
     119                 :          1 :         g_test_add_func ("/hex/encode", test_encode);
     120                 :          1 :         g_test_add_func ("/hex/encode_spaces", test_encode_spaces);
     121                 :          1 :         g_test_add_func ("/hex/decode", test_decode);
     122                 :          1 :         g_test_add_func ("/hex/decode_fail", test_decode_fail);
     123                 :            : 
     124                 :          1 :         return g_test_run ();
     125                 :            : }

Generated by: LCOV version 1.14