1 /* t-oid.c - Test utility for the OID functions
2 * Copyright (C) 2009 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/>.
28 #include "../src/ksba.h"
31 #define BADOID "1.3.6.1.4.1.11591.2.12242973"
35 read_into_buffer (FILE *fp, size_t *r_length)
39 size_t nread, bufsize = 0;
43 #ifdef HAVE_W32_SYSTEM
44 setmode (fileno(fp), O_BINARY);
51 buffer = realloc (buffer, bufsize);
54 perror ("realloc failed");
58 nread = fread (buffer + buflen, 1, NCHUNK, fp);
59 if (nread < NCHUNK && ferror (fp))
61 perror ("fread failed");
66 while (nread == NCHUNK);
75 test_oid_to_str (void)
83 { 7, "\x02\x82\x06\x01\x0A\x0C\x00",
86 { 7, "\x02\x82\x06\x01\x0A\x0C\x01",
89 { 7, "\x2A\x86\x48\xCE\x38\x04\x01",
92 { 7, "\x2A\x86\x48\xCE\x38\x04\x03",
95 { 10, "\x2B\x06\x01\x04\x01\xDA\x47\x02\x01\x01",
96 "1.3.6.1.4.1.11591.2.1.1"
101 { 9, "\x80\x02\x70\x50\x25\x46\xfd\x0c\xc0",
116 { 2, "\x81\x80", /* legal encoding? */
119 { 2, "\x81\x81\x01", /* legal encoding? */
131 for (tidx=0; tests[tidx].bin; tidx++)
133 str = ksba_oid_to_str (tests[tidx].bin, tests[tidx].binlen);
136 perror ("ksba_oid_to_str failed");
139 if (strcmp (tests[tidx].str, str))
141 fprintf (stderr, "ksba_oid_to_str test %d failed\n", tidx);
142 fprintf (stderr, " got=%s\n", str);
143 fprintf (stderr, " want=%s\n", tests[tidx].str);
152 main (int argc, char **argv)
167 else if (!strcmp (*argv, "--from-str"))
169 unsigned char *buffer;
172 for (argv++,argc-- ; argc; argc--, argv++)
174 err = ksba_oid_from_str (*argv, &buffer, &buflen);
177 fprintf (stderr, "can't convert `%s': %s\n",
178 *argv, gpg_strerror (err));
181 printf ("%s ->", *argv);
182 for (n=0; n < buflen; n++)
183 printf (" %02X", buffer[n]);
189 else if (!strcmp (*argv, "--to-str"))
197 buffer = read_into_buffer (stdin, &buflen);
198 result = ksba_oid_to_str (buffer, buflen);
200 printf ("%s\n", result? result:"[malloc failed]");
205 fputs ("usage: "PGM" [--from-str|--to-str]\n", stderr);