Debian Codegraph @

tests/t-dnparser.c

raw
1 /* t-dnparser.c - basic test for the DN parser
2 * Copyright (C) 2002, 2006 g10 Code GmbH
3 *
4 * This file is part of KSBA.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <time.h>
25 #include <errno.h>
26
27 #include "../src/ksba.h"
28 #include "t-common.h"
29
30 static int quiet;
31 static int verbose;
32
33
34 static void
35 test_0 (void)
36 {
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",
40 "CN=www.gnupg.org",
41 " CN=www.gnupg.org ",
42 "C=fr,L=Paris,CN=Julien Duvivier,[email protected]",
43 NULL
44 };
45 gpg_error_t err;
46 int i;
47 unsigned char *buf;
48 size_t off, len;
49
50 for (i=0; good_strings[i]; i++)
51 {
52 err = ksba_dn_str2der (good_strings[i], &buf, &len);
53 if (err)
54 {
55 fprintf (stderr, "%s:%d: ksba_dn_str2der failed for `%s': %s\n",
56 __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
57 exit (1);
58 }
59 err = ksba_dn_teststr (good_strings[i], 0, &off, &len);
60 if (err)
61 {
62 fprintf (stderr, "%s:%d: ksba_dn_teststr failed for `%s': %s\n",
63 __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
64 exit (1);
65 }
66 xfree (buf);
67 }
68 }
69
70
71 static void
72 test_1 (void)
73 {
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",
78 "C=de,O=foo,OU=",
79 "C=de,O=foo,OU= ",
80 "C=,O=foo,OU=bar ",
81 "C = ,O=foo,OU=bar ",
82 "C=",
83 NULL
84 };
85 gpg_error_t err;
86 int i;
87 unsigned char *buf;
88 size_t off, len;
89
90 for (i=0; empty_elements[i]; i++)
91 {
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);
96 if (!err)
97 fail ("ksba_dn_teststr returned no error");
98 if (!quiet)
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);
102 xfree (buf);
103 }
104 }
105
106 static void
107 test_2 (void)
108 {
109 static char *invalid_labels[] = {
110 "C=de,FOO=something,O=bar",
111 "Y=foo, C=baz",
112 NULL
113 };
114 gpg_error_t err;
115 int i;
116 unsigned char *buf;
117 size_t off, len;
118
119 for (i=0; invalid_labels[i]; i++)
120 {
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);
125 if (!err)
126 fail ("ksba_dn_test_str returned no error");
127 if (!quiet)
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);
131 xfree (buf);
132 }
133 }
134
135
136
137 int
138 main (int argc, char **argv)
139 {
140 char inputbuf[4096];
141 unsigned char *buf;
142 size_t len;
143 gpg_error_t err;
144 char *string;
145
146 if (argc)
147 {
148 argc--; argv++;
149 }
150 if (argc && !strcmp (*argv, "--verbose"))
151 {
152 verbose = 1;
153 argc--; argv++;
154 }
155
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);
159 if (!feof (stdin))
160 fail ("read error or input too large");
161
162 err = ksba_dn_der2str (inputbuf, len, &string);
163 fail_if_err (err);
164 fputs (string, stdout);
165 ksba_free (string);
166 }
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);
170 if (!feof (stdin))
171 fail ("read error or input too large");
172
173 err = ksba_dn_str2der (inputbuf, &buf, &len);
174 fail_if_err (err);
175 fwrite (buf, len, 1, stdout);
176 }
177 else if (!argc)
178 {
179 if (!verbose)
180 quiet = 1;
181 test_0 ();
182 test_1 ();
183 test_2 ();
184 }
185 else
186 {
187 fprintf (stderr, "usage: t-dnparser [--to-str|--to-der]\n");
188 return 1;
189 }
190
191 return 0;
192 }