LCOV - code coverage report
Current view: top level - egg - test-dn.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.3 % 107 102
Test Date: 2024-09-14 11:48:09 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* unit-test-pkix-parser.c: Test PKIX parser
       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              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "egg/egg-asn1-defs.h"
      26              : #include "egg/egg-asn1x.h"
      27              : #include "egg/egg-dn.h"
      28              : #include "egg/egg-oid.h"
      29              : #include "egg/egg-testing.h"
      30              : 
      31              : #include <glib.h>
      32              : #include <gcrypt.h>
      33              : 
      34              : #include <stdlib.h>
      35              : #include <stdio.h>
      36              : #include <string.h>
      37              : 
      38              : typedef struct {
      39              :         GNode* asn1;
      40              :         guchar *data;
      41              :         gsize n_data;
      42              : } Test;
      43              : 
      44              : static void
      45            5 : setup (Test *test, gconstpointer unused)
      46              : {
      47              :         GBytes *bytes;
      48              : 
      49            5 :         if (!g_file_get_contents (SRCDIR "/egg/fixtures/test-certificate-1.der",
      50            5 :                                   (gchar**)&test->data, &test->n_data, NULL))
      51            0 :                 g_assert_not_reached ();
      52              : 
      53            5 :         test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate");
      54            5 :         g_assert (test->asn1 != NULL);
      55              : 
      56            5 :         bytes = g_bytes_new_static (test->data, test->n_data);
      57            5 :         if (!egg_asn1x_decode (test->asn1, bytes))
      58            0 :                 g_assert_not_reached ();
      59            5 :         g_bytes_unref (bytes);
      60            5 : }
      61              : 
      62              : static void
      63            5 : teardown (Test *test, gconstpointer unused)
      64              : {
      65            5 :         egg_asn1x_destroy (test->asn1);
      66            5 :         g_free (test->data);
      67            5 : }
      68              : 
      69              : static void
      70            1 : test_read_dn (Test* test, gconstpointer unused)
      71              : {
      72              :         gchar *dn;
      73              : 
      74            1 :         dn = egg_dn_read (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL));
      75            1 :         g_assert (dn != NULL);
      76            1 :         g_assert_cmpstr (dn, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com");
      77              : 
      78            1 :         g_free (dn);
      79            1 : }
      80              : 
      81              : static void
      82            1 : test_dn_value (Test* test, gconstpointer unused)
      83              : {
      84            1 :         const guchar value[] = { 0x13, 0x1a, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x50, 0x72, 0x65, 0x6d, 0x69, 0x75, 0x6d, 0x20, 0x43, 0x41 };
      85            1 :         gsize n_value = 28;
      86              :         GBytes *bytes;
      87              :         GNode *asn;
      88              :         GQuark oid;
      89              :         gchar *text;
      90              : 
      91            1 :         bytes = g_bytes_new_static (value, n_value);
      92              : 
      93            1 :         asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "AttributeValue", bytes);
      94            1 :         g_assert (asn != NULL);
      95              : 
      96              :         /* Some printable strings */
      97            1 :         oid = g_quark_from_static_string ("2.5.4.3");
      98            1 :         text = egg_dn_print_value (oid, asn);
      99            1 :         g_assert_cmpstr (text, ==, "Thawte Personal Premium CA");
     100            1 :         g_free (text);
     101            1 :         g_bytes_unref (bytes);
     102              : 
     103              :         /* Unknown oid */
     104            1 :         oid = g_quark_from_static_string ("1.1.1.1.1.1");
     105            1 :         bytes = g_bytes_new_static (value, n_value);
     106            1 :         text = egg_dn_print_value (oid, asn);
     107            1 :         g_assert_cmpstr (text, ==, "#131A54686177746520506572736F6E616C205072656D69756D204341");
     108            1 :         g_free (text);
     109              : 
     110            1 :         egg_asn1x_destroy (asn);
     111            1 :         g_bytes_unref (bytes);
     112            1 : }
     113              : 
     114              : static int last_index = 0;
     115              : 
     116              : static void
     117            7 : concatenate_dn (guint index,
     118              :                 GQuark oid,
     119              :                 GNode *value,
     120              :                 gpointer user_data)
     121              : {
     122            7 :         GString *dn = user_data;
     123              :         gchar *text;
     124              : 
     125            7 :         g_assert (oid);
     126            7 :         g_assert (value != NULL);
     127              : 
     128            7 :         g_assert (index == last_index);
     129            7 :         ++last_index;
     130              : 
     131            7 :         if (index != 1) {
     132           12 :                 g_string_append (dn, ", ");
     133              :         }
     134              : 
     135            7 :         g_string_append (dn, egg_oid_get_name (oid));
     136              :         g_string_append_c (dn, '=');
     137              : 
     138            7 :         text = egg_dn_print_value (oid, value);
     139              :         g_string_append (dn, text);
     140            7 :         g_free (text);
     141            7 : }
     142              : 
     143              : static void
     144            1 : test_parse_dn (Test* test, gconstpointer unused)
     145              : {
     146            1 :         GString *dn = g_string_new ("");
     147            1 :         last_index = 1;
     148              : 
     149            1 :         if (!egg_dn_parse (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL), concatenate_dn, dn))
     150            0 :                 g_assert_not_reached ();
     151              : 
     152            1 :         g_assert_cmpstr (dn->str, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com");
     153            1 :         g_string_free (dn, TRUE);
     154            1 : }
     155              : 
     156              : static void
     157            1 : test_read_dn_part (Test* test, gconstpointer unused)
     158              : {
     159              :         GNode *node;
     160              :         gchar *value;
     161              : 
     162            1 :         node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
     163              : 
     164            1 :         value = egg_dn_read_part (node, "CN");
     165            1 :         g_assert (value != NULL);
     166            1 :         g_assert_cmpstr (value, ==, "Thawte Personal Premium CA");
     167            1 :         g_free (value);
     168              : 
     169            1 :         value = egg_dn_read_part (node, "2.5.4.8");
     170            1 :         g_assert (value != NULL);
     171            1 :         g_assert_cmpstr (value, ==, "Western Cape");
     172            1 :         g_free (value);
     173              : 
     174            1 :         value = egg_dn_read_part (node, "DC");
     175            1 :         g_assert (value == NULL);
     176              : 
     177            1 :         value = egg_dn_read_part (node, "0.0.0.0");
     178            1 :         g_assert (value == NULL);
     179              : 
     180            1 :         value = egg_dn_read_part (node, "2.5.4.9");
     181            1 :         g_assert (value == NULL);
     182            1 : }
     183              : 
     184              : static void
     185            1 : test_add_dn_part (Test *test,
     186              :                   gconstpointer unused)
     187              : {
     188              :         GBytes *check;
     189              :         GBytes *dn;
     190              :         GNode *check_dn;
     191              :         GNode *asn;
     192              :         GNode *node;
     193              : 
     194            1 :         asn = egg_asn1x_create (pkix_asn1_tab, "Name");
     195            1 :         node = egg_asn1x_node (asn, "rdnSequence", NULL);
     196            1 :         egg_asn1x_set_choice (asn, node);
     197            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.6"), "ZA");
     198            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.8"), "Western Cape");
     199            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.7"), "Cape Town");
     200            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.10"), "Thawte Consulting");
     201            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.11"), "Certification Services Division");
     202            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.3"), "Thawte Personal Premium CA");
     203            1 :         egg_dn_add_string_part (node, g_quark_from_static_string ("1.2.840.113549.1.9.1"), "personal-premium@thawte.com");
     204              : 
     205            1 :         dn = egg_asn1x_encode (asn, NULL);
     206            1 :         if (dn == NULL) {
     207            0 :                 g_warning ("couldn't encode dn: %s", egg_asn1x_message (asn));
     208            0 :                 g_assert_not_reached ();
     209              :         }
     210              : 
     211            1 :         check_dn = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
     212            1 :         check = egg_asn1x_encode (check_dn, NULL);
     213            1 :         egg_asn1x_destroy (asn);
     214              : 
     215            1 :         egg_assert_cmpbytes (dn, ==, g_bytes_get_data (check, NULL), g_bytes_get_size (check));
     216              : 
     217            1 :         g_bytes_unref (dn);
     218            1 :         g_bytes_unref (check);
     219            1 : }
     220              : 
     221              : int
     222            1 : main (int argc, char **argv)
     223              : {
     224            1 :         g_test_init (&argc, &argv, NULL);
     225              : 
     226            1 :         g_test_add ("/dn/read_dn", Test, NULL, setup, test_read_dn, teardown);
     227            1 :         g_test_add ("/dn/dn_value", Test, NULL, setup, test_dn_value, teardown);
     228            1 :         g_test_add ("/dn/parse_dn", Test, NULL, setup, test_parse_dn, teardown);
     229            1 :         g_test_add ("/dn/read_dn_part", Test, NULL, setup, test_read_dn_part, teardown);
     230            1 :         g_test_add ("/dn/add_dn_part", Test, NULL, setup, test_add_dn_part, teardown);
     231              : 
     232            1 :         return g_test_run ();
     233              : }
        

Generated by: LCOV version 2.0-1