LCOV - code coverage report
Current view: top level - gcr - test-ssh-agent-util.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 29 30 96.7 %
Date: 2022-09-04 10:20:22 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 18 61.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * gnome-keyring
       3                 :            :  *
       4                 :            :  * Copyright (C) 2008 Stefan Walter
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU Lesser General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2.1 of
       9                 :            :  * the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful, but
      12                 :            :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General Public
      17                 :            :  * License along with this program; if not, see
      18                 :            :  * <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: Stef Walter <stef@thewalter.net>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include "gcr-ssh-agent-util.h"
      26                 :            : 
      27                 :            : #include <glib.h>
      28                 :            : 
      29                 :            : #include <stdlib.h>
      30                 :            : #include <stdio.h>
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : static struct {
      34                 :            :         const char *filename;
      35                 :            :         const char *encoded;
      36                 :            : } PUBLIC_FILES[] = {
      37                 :            :         { SRCDIR "/gcr/fixtures/ssh-agent/id_rsa_test.pub",
      38                 :            :           "AAAAB3NzaC1yc2EAAAABIwAAAQEAoD6VKqkhay6pKHSRjAGWWfFPU8xfsi2gnOwP/B1UHDoztx3czhO+py/fTlhCnSP1jsjkrVIZcnzah2fUNFFRgS4+jROBtvbgHsS72V1E6+ZogV+mBJWWAhw0iPrmQ3Kvm38D3PByo5Y7yKO5kIG2LloYLjosJ5F4sx2xh0uz2wXNtnY1b5xhe2+VEksm9OB+FXaUkZC2fQrTNo8ZGFJQSFd8kUhIfbUDJmlYuZ+vvHM+A3Lc9rHyW4IPaRyxFQciRmb+ZQqU2uSdOXAhg17lskuX/q8yCI5Hy5eDicC222oUMdJTtYgwX4dQCU8TICWhxb3x4RCV+g7D99+tkIvv+w==" },
      39                 :            :         { SRCDIR "/gcr/fixtures/ssh-agent/id_dsa_test.pub",
      40                 :            :           "AAAAB3NzaC1kc3MAAACBANHNmw2YHEodUj4Ae27i8Rm8uoLnpS68QEiCJx8bv9P1o0AaD0w55sH+TBzlo7vtAEDlAzIOBY3PMpy5WarELTIeXmFPzKfHL8tuxMbOPaN/wDkDZNnJZsqlyRwlQKStPcAlvLBNuMjA53u2ndMTVghtUHXETQzwxKhXf7TmvfLBAAAAFQDnF/Y8MgFCP0PpRC5ZAQo1dyDEwwAAAIEAr4iOpTeZx8i1QgQpRl+dmbBAtHTXbPiophzNJBge9lixqF0T3egN2B9wGGnumIXmnst9RPPjuu+cHCLfxhXHzLlW8MLwoiF6ZQOx9M8WcfWIl5oiGyr2e969woRf5OcMGQPOQBdws6MEtemRqq5gu6dqDqVl3xfhSZSP9LpqAI8AAACAUjiuQ3qGErsCz++qd0qrR++QA185XGXAPZqQEHcr4iKSlO17hSUYA03kOWtDaeRtJOlxjIjl9iLo3juKGFgxUfo2StScOSO2saTWFGjA4MybHCK1+mIYXRcYrq314yK2Tmbql/UGDWpcCCGXLWpSFHTaXTbJjPd6VL+TO9/8tFk=" },
      41                 :            :         { SRCDIR "/gcr/fixtures/ssh-agent/identity.pub",
      42                 :            :           NULL }
      43                 :            : };
      44                 :            : 
      45                 :            : #define COMMENT "A public key comment"
      46                 :            : 
      47                 :            : static void
      48                 :          1 : test_parse_public (void)
      49                 :            : {
      50                 :            :         GBytes *input_bytes, *output_bytes;
      51                 :            :         gchar *comment;
      52                 :            :         guchar *data;
      53                 :            :         const guchar *blob;
      54                 :            :         gsize n_data;
      55                 :            :         gchar *encoded;
      56                 :            :         gsize i;
      57                 :            : 
      58         [ +  + ]:          4 :         for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) {
      59         [ -  + ]:          3 :                 if (!g_file_get_contents (PUBLIC_FILES[i].filename, (gchar **)&data, &n_data, NULL))
      60                 :          0 :                         g_assert_not_reached ();
      61                 :            : 
      62                 :          3 :                 input_bytes = g_bytes_new_take (data, n_data);
      63                 :          3 :                 output_bytes = _gcr_ssh_agent_parse_public_key (input_bytes, &comment);
      64                 :          3 :                 g_bytes_unref (input_bytes);
      65         [ +  + ]:          3 :                 if (PUBLIC_FILES[i].encoded == NULL) {
      66         [ -  + ]:          1 :                         g_assert (output_bytes == NULL);
      67                 :            :                 } else {
      68         [ -  + ]:          2 :                         g_assert (output_bytes);
      69                 :            : 
      70                 :          2 :                         blob = g_bytes_get_data (output_bytes, &n_data);
      71                 :          2 :                         encoded = g_base64_encode (blob, n_data);
      72                 :          2 :                         g_bytes_unref (output_bytes);
      73         [ -  + ]:          2 :                         g_assert_cmpstr (encoded, ==, PUBLIC_FILES[i].encoded);
      74                 :          2 :                         g_free (encoded);
      75                 :            : 
      76         [ -  + ]:          2 :                         g_assert_cmpstr (comment, ==, COMMENT);
      77                 :          2 :                         g_free (comment);
      78                 :            :                 }
      79                 :            :         }
      80                 :          1 : }
      81                 :            : 
      82                 :            : static void
      83                 :          1 : test_canon_error (void)
      84                 :            : {
      85                 :            :         static const gchar input[] =
      86                 :            :                 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
      87                 :            :                 "@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @\r\n"
      88                 :            :                 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
      89                 :            :                 "Permissions 0620 for '/home/foo/.ssh/id_rsa' are too open.\r\n"
      90                 :            :                 "It is required that your private key files are NOT accessible by others.\r\n"
      91                 :            :                 "This private key will be ignored.\r\n";
      92                 :            :         static const gchar expected[] =
      93                 :            :                 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
      94                 :            :                 "@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @\n"
      95                 :            :                 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
      96                 :            :                 "Permissions 0620 for '/home/foo/.ssh/id_rsa' are too open.\n"
      97                 :            :                 "It is required that your private key files are NOT accessible by others.\n"
      98                 :            :                 "This private key will be ignored.\n";
      99                 :            :         gchar *p, *output;
     100                 :            : 
     101                 :          1 :         p = g_strdup (input);
     102                 :          1 :         output = _gcr_ssh_agent_canon_error (p);
     103                 :            : 
     104         [ -  + ]:          1 :         g_assert (output == p);
     105         [ -  + ]:          1 :         g_assert_cmpstr (expected, ==, output);
     106                 :            : 
     107                 :          1 :         g_free (p);
     108                 :          1 : }
     109                 :            : 
     110                 :            : int
     111                 :          1 : main (int argc, char **argv)
     112                 :            : {
     113                 :          1 :         g_test_init (&argc, &argv, NULL);
     114                 :            : 
     115                 :          1 :         g_test_add_func ("/ssh-agent/util/parse_public", test_parse_public);
     116                 :          1 :         g_test_add_func ("/ssh-agent/util/canon_error", test_canon_error);
     117                 :            : 
     118                 :          1 :         return g_test_run ();
     119                 :            : }

Generated by: LCOV version 1.14