LCOV - code coverage report
Current view: top level - gcr - test-openssh.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 71 71 100.0 %
Date: 2022-09-04 10:20:22 Functions: 10 10 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 22 59.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.h"
      25                 :            : #include "gcr/gcr-openssh.h"
      26                 :            : 
      27                 :            : #include "egg/egg-testing.h"
      28                 :            : 
      29                 :            : #include <gcrypt.h>
      30                 :            : #include <glib.h>
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : typedef struct {
      34                 :            :         const gchar *expected_label;
      35                 :            :         const gchar *expected_options;
      36                 :            : } Test;
      37                 :            : 
      38                 :            : #define OPENSSH_PUBLIC_RSA1 \
      39                 :            :         "2048 65537 19574029774826276058535216798260123376543523095248321838931" \
      40                 :            :         "8476099051534660565418100376122247153936738716140984293302866595208305" \
      41                 :            :         "7124376564328644357957081508003798389808113087527047927841196160520784" \
      42                 :            :         "3971799891833860159372766201922902824211581515042106928142039998651198" \
      43                 :            :         "7806024885997262427984841536983221992403267030558391252672804492615887" \
      44                 :            :         "9294713324466630490990131504557923061505441555447586185019409756877006" \
      45                 :            :         "5871190731807718592844942425524851665039303855329966512492845780563670" \
      46                 :            :         "0617451083369174928502647995734856960603065454655489558179113130210712" \
      47                 :            :         "74638931037011169213563881172297734240201883475566393175838117784693 r" \
      48                 :            :         "sa-key@example.com\n"
      49                 :            : 
      50                 :            : #define OPENSSH_PUBLIC_RSA2 \
      51                 :            :         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCs8z2y0cCPYRAPkq8tAt6FC/kdfnR/p" \
      52                 :            :         "8B2ZoY0oiLNt7kQEwJfexgwLqTxWYd2fSDUSSDPrsqAxZAwLLS/eF04kXiJO2VfqAWFpTL" \
      53                 :            :         "NToERHpFF1yZQe26ELTlNNfna7LqfCRvpNDwu6AqndsT3eFt7DWvBDXbbEiTLW21Z2OFAA" \
      54                 :            :         "H/J2iCFn4c0a8Myf7IaMYcy5GG3mpk39kEO4aNV/67U7kfooek24ObwD0vlXzlsi5VZIUF" \
      55                 :            :         "OIUi0UdkNEMCtUWpfkZ1STUlmwp9HVM7xb7/9PESQKDnZdxpB09S9cIjdpDecpDlMDDEbE" \
      56                 :            :         "UECM1PIas3ndhB7gAN1i2JsPHTcXZ1 rsa-key@example.com\r\n" \
      57                 :            :         "# Comment\n"
      58                 :            : 
      59                 :            : #define OPENSSH_PUBLIC_DSA2 \
      60                 :            :         "ssh-dss AAAAB3NzaC1kc3MAAACBAL4z+ad0ZJYzMOQuGp00UJ+AijKhrPVUEYLcxBmFQo" \
      61                 :            :         "nb/KIlLSWJua4Rl9DB4tDj30Y9c/oApqC4n+FIYlUZMSnxmpvcLF6aeXOiHHPvm0EDYjjy" \
      62                 :            :         "VubyYQWI7CROrrzSc+x++ha3TuJEvF3PlKlZmTKKVYEkZNjwFqYysGyPxPalAAAAFQDtDS" \
      63                 :            :         "EF9Gvnv5fQtSbbsp7j78uVBwAAAIAtNpAg/Mbd/E2241enedB9AxAbJWZ5QYnoPe6/zx5d" \
      64                 :            :         "OmU7+qz8mG6tgvF8F7IgXPabuAKslzTDGS3zgaEhWicDS3CIYik2UR8hXdxfovIEqZKZe7" \
      65                 :            :         "u02FCEoXYCEiFUAdzDGzjI7PswgtEJWWNqKeNis3HmDDha9lMkqz/3fLZGXwAAAIEAiaRP" \
      66                 :            :         "YKZDMoJG+aVZ5A3R/m2gl+mYE2MsjPKXuBKcrZ6ItA9BMe4G/An0/+E3A+DuoGxdeNNMF8" \
      67                 :            :         "U9Dy2N8Sch/Ngtg2E/FBo5geljWobJXd1jxmPtF2WAliYJXDdIt6RBVPGL9H/KSjDmBMsV" \
      68                 :            :         "d42wxVJywawzypklVZjSUuWuBMI= dsa-key@example.com \n"
      69                 :            : 
      70                 :            : #define OPENSSH_PUBLIC_ECDSA \
      71                 :            :         "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA" \
      72                 :            :         "BBBMKWn4nDF3IVAB2XKK8MdlMV0r1PkwHWemNuNkKnDSLy1CA17IEBXzEFX0yDEaC/8cFG" \
      73                 :            :         "Bc0VblrySYCYKvJc+is= ecdsa-key@example.com \n"
      74                 :            : 
      75                 :            : #define EXTRA_LINES_WITHOUT_KEY \
      76                 :            :         "\n# Comment\n\n" \
      77                 :            :         "20aa3\n" \
      78                 :            :         "not a key\n"
      79                 :            : 
      80                 :            : static void
      81                 :          6 : setup (Test *test,
      82                 :            :        gconstpointer unused)
      83                 :            : {
      84                 :            : 
      85                 :          6 : }
      86                 :            : 
      87                 :            : static void
      88                 :          6 : teardown (Test *test,
      89                 :            :           gconstpointer unused)
      90                 :            : {
      91                 :            : 
      92                 :          6 : }
      93                 :            : 
      94                 :            : static void
      95                 :          6 : on_openssh_pub_parse (GckAttributes *attrs,
      96                 :            :                       const gchar *label,
      97                 :            :                       const gchar *options,
      98                 :            :                       GBytes *outer,
      99                 :            :                       gpointer user_data)
     100                 :            : {
     101                 :          6 :         Test *test = user_data;
     102                 :            :         guint keys;
     103                 :            : 
     104         [ +  + ]:          6 :         if (test->expected_label)
     105         [ -  + ]:          4 :                 g_assert_cmpstr (label, ==, test->expected_label);
     106         [ +  + ]:          6 :         if (test->expected_options)
     107         [ -  + ]:          2 :                 g_assert_cmpstr (options, ==, test->expected_options);
     108                 :            : 
     109                 :            :         /* The block should parse properly */
     110                 :          6 :         keys = _gcr_openssh_pub_parse (outer, NULL, NULL);
     111         [ -  + ]:          6 :         g_assert_cmpuint (keys, ==, 1);
     112                 :          6 : }
     113                 :            : 
     114                 :            : static void
     115                 :          1 : test_parse_v1_rsa (Test *test,
     116                 :            :                    gconstpointer unused)
     117                 :            : {
     118                 :          1 :         const gchar *data = OPENSSH_PUBLIC_RSA1 EXTRA_LINES_WITHOUT_KEY;
     119                 :            :         GBytes *bytes;
     120                 :            :         gint keys;
     121                 :            : 
     122                 :          1 :         test->expected_label = "rsa-key@example.com";
     123                 :            : 
     124                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     125                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     126         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     127                 :            : 
     128                 :          1 :         g_bytes_unref (bytes);
     129                 :          1 : }
     130                 :            : 
     131                 :            : static void
     132                 :          1 : test_parse_v2_rsa (Test *test,
     133                 :            :                    gconstpointer unused)
     134                 :            : {
     135                 :          1 :         const gchar *data = OPENSSH_PUBLIC_RSA2 EXTRA_LINES_WITHOUT_KEY;
     136                 :            :         GBytes *bytes;
     137                 :            :         gint keys;
     138                 :            : 
     139                 :          1 :         test->expected_label = "rsa-key@example.com";
     140                 :            : 
     141                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     142                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     143         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     144                 :            : 
     145                 :          1 :         g_bytes_unref (bytes);
     146                 :          1 : }
     147                 :            : 
     148                 :            : static void
     149                 :          1 : test_parse_v2_dsa (Test *test,
     150                 :            :                gconstpointer unused)
     151                 :            : {
     152                 :          1 :         const gchar *data = OPENSSH_PUBLIC_DSA2 EXTRA_LINES_WITHOUT_KEY;
     153                 :            :         GBytes *bytes;
     154                 :            :         gint keys;
     155                 :            : 
     156                 :          1 :         test->expected_label = "dsa-key@example.com";
     157                 :            : 
     158                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     159                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     160         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     161                 :            : 
     162                 :          1 :         g_bytes_unref (bytes);
     163                 :          1 : }
     164                 :            : 
     165                 :            : static void
     166                 :          1 : test_parse_v2_ecdsa (Test *test,
     167                 :            :                      gconstpointer unused)
     168                 :            : {
     169                 :          1 :         const gchar *data = OPENSSH_PUBLIC_ECDSA EXTRA_LINES_WITHOUT_KEY;
     170                 :            :         GBytes *bytes;
     171                 :            :         gint keys;
     172                 :            : 
     173                 :          1 :         test->expected_label = "ecdsa-key@example.com";
     174                 :            : 
     175                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     176                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     177         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     178                 :            : 
     179                 :          1 :         g_bytes_unref (bytes);
     180                 :          1 : }
     181                 :            : 
     182                 :            : static void
     183                 :          1 : test_parse_v1_options (Test *test,
     184                 :            :                        gconstpointer unused)
     185                 :            : {
     186                 :          1 :         const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA1;
     187                 :            :         GBytes *bytes;
     188                 :            :         gint keys;
     189                 :            : 
     190                 :          1 :         test->expected_options = "option1,option2=\"value 2\",option3";
     191                 :            : 
     192                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     193                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     194         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     195                 :            : 
     196                 :          1 :         g_bytes_unref (bytes);
     197                 :          1 : }
     198                 :            : 
     199                 :            : static void
     200                 :          1 : test_parse_v2_options (Test *test,
     201                 :            :                        gconstpointer unused)
     202                 :            : {
     203                 :          1 :         const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA2;
     204                 :            :         GBytes *bytes;
     205                 :            :         gint keys;
     206                 :            : 
     207                 :          1 :         test->expected_options = "option1,option2=\"value 2\",option3";
     208                 :            : 
     209                 :          1 :         bytes = g_bytes_new_static (data, strlen (data));
     210                 :          1 :         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
     211         [ -  + ]:          1 :         g_assert_cmpint (keys, ==, 1);
     212                 :            : 
     213                 :          1 :         g_bytes_unref (bytes);
     214                 :          1 : }
     215                 :            : 
     216                 :            : int
     217                 :          1 : main (int argc, char **argv)
     218                 :            : {
     219                 :          1 :         g_test_init (&argc, &argv, NULL);
     220                 :          1 :         g_set_prgname ("test-openssh");
     221                 :            : 
     222                 :          1 :         g_test_add ("/gcr/openssh/parse_v1_rsa", Test, NULL, setup, test_parse_v1_rsa, teardown);
     223                 :          1 :         g_test_add ("/gcr/openssh/parse_v2_rsa", Test, NULL, setup, test_parse_v2_rsa, teardown);
     224                 :          1 :         g_test_add ("/gcr/openssh/parse_v2_dsa", Test, NULL, setup, test_parse_v2_dsa, teardown);
     225                 :          1 :         g_test_add ("/gcr/openssh/parse_v2_ecdsa", Test, NULL, setup, test_parse_v2_ecdsa, teardown);
     226                 :          1 :         g_test_add ("/gcr/openssh/parse_v1_options", Test, NULL, setup, test_parse_v1_options, teardown);
     227                 :          1 :         g_test_add ("/gcr/openssh/parse_v2_options", Test, NULL, setup, test_parse_v2_options, teardown);
     228                 :            : 
     229                 :          1 :         return egg_tests_run_with_loop ();
     230                 :            : }

Generated by: LCOV version 1.14