1 /* t-dnparser.c - basic test for the DN parser
2 * Copyright (C) 2002, 2006 g10 Code GmbH
4 * This file is part of KSBA.
6 * KSBA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * KSBA 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "../src/ksba.h"
37 static char *good_strings[] = {
38 "C=de,O=g10 Code,OU=qa,CN=Pépé le Moko",
39 "C= de, O=g10 Code , OU=qa ,CN=Pépé le Moko",
42 "C=fr,L=Paris,CN=Julien Duvivier,EMAIL=julien@example.org",
50 for (i=0; good_strings[i]; i++)
52 err = ksba_dn_str2der (good_strings[i], &buf, &len);
55 fprintf (stderr, "%s:%d: ksba_dn_str2der failed for `%s': %s\n",
56 __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
59 err = ksba_dn_teststr (good_strings[i], 0, &off, &len);
62 fprintf (stderr, "%s:%d: ksba_dn_teststr failed for `%s': %s\n",
63 __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
74 static char *empty_elements[] = {
75 "C=de,O=foo,OU=,CN=joe",
76 "C=de,O=foo,OU= ,CN=joe",
77 "C=de,O=foo,OU=\"\" ,CN=joe",
90 for (i=0; empty_elements[i]; i++)
92 err = ksba_dn_str2der (empty_elements[i], &buf, &len);
93 if (gpg_err_code (err) != GPG_ERR_SYNTAX)
94 fail ("empty element not detected");
95 err = ksba_dn_teststr (empty_elements[i], 0, &off, &len);
97 fail ("ksba_dn_teststr returned no error");
99 printf ("string ->%s<- error at %lu.%lu (%.*s)\n",
100 empty_elements[i], (unsigned long)off, (unsigned long)len,
101 (int)len, empty_elements[i]+off);
109 static char *invalid_labels[] = {
110 "C=de,FOO=something,O=bar",
119 for (i=0; invalid_labels[i]; i++)
121 err = ksba_dn_str2der (invalid_labels[i], &buf, &len);
122 if (gpg_err_code (err) != GPG_ERR_UNKNOWN_NAME)
123 fail ("invalid label not detected");
124 err = ksba_dn_teststr (invalid_labels[i], 0, &off, &len);
126 fail ("ksba_dn_test_str returned no error");
128 printf ("string ->%s<- error at %lu.%lu (%.*s)\n",
129 invalid_labels[i], (unsigned long)off, (unsigned long)len,
130 (int)len, invalid_labels[i]+off);
138 main (int argc, char **argv)
150 if (argc && !strcmp (*argv, "--verbose"))
156 if (argc == 1 && !strcmp (argv[0], "--to-str") )
157 { /* Read the DER encoded DN from stdin write the string to stdout */
158 len = fread (inputbuf, 1, sizeof inputbuf, stdin);
160 fail ("read error or input too large");
162 err = ksba_dn_der2str (inputbuf, len, &string);
164 fputs (string, stdout);
167 else if (argc == 1 && !strcmp (argv[0], "--to-der") )
168 { /* Read the String from stdin write the DER encoding to stdout */
169 len = fread (inputbuf, 1, sizeof inputbuf, stdin);
171 fail ("read error or input too large");
173 err = ksba_dn_str2der (inputbuf, &buf, &len);
175 fwrite (buf, len, 1, stdout);
187 fprintf (stderr, "usage: t-dnparser [--to-str|--to-der]\n");