Imported Upstream version 1.6.5
[platform/upstream/libksba.git] / tests / t-oid.c
1 /* t-oid.c - Test utility for the OID functions
2  *      Copyright (C) 2009 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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <time.h>
26 #include <errno.h>
27
28 #include "../src/ksba.h"
29
30 #define PGM "t-oid"
31 #define BADOID "1.3.6.1.4.1.11591.2.12242973"
32
33
34 static void *
35 read_into_buffer (FILE *fp, size_t *r_length)
36 {
37   char *buffer;
38   size_t buflen;
39   size_t nread, bufsize = 0;
40
41   *r_length = 0;
42 #define NCHUNK 8192
43 #ifdef HAVE_W32_SYSTEM
44   setmode (fileno(fp), O_BINARY);
45 #endif
46   buffer = NULL;
47   buflen = 0;
48   do
49     {
50       bufsize += NCHUNK;
51       buffer = realloc (buffer, bufsize);
52       if (!buffer)
53         {
54           perror ("realloc failed");
55           exit (1);
56         }
57
58       nread = fread (buffer + buflen, 1, NCHUNK, fp);
59       if (nread < NCHUNK && ferror (fp))
60         {
61           perror ("fread failed");
62           exit (1);
63         }
64       buflen += nread;
65     }
66   while (nread == NCHUNK);
67 #undef NCHUNK
68
69   *r_length = buflen;
70   return buffer;
71 }
72
73
74 static void
75 test_oid_to_str (void)
76 {
77   struct {
78     unsigned int binlen;
79     unsigned char *bin;
80     char *str;
81   } tests[] = {
82
83     {  7, "\x02\x82\x06\x01\x0A\x0C\x00",
84        "0.2.262.1.10.12.0"
85     },
86     {  7, "\x02\x82\x06\x01\x0A\x0C\x01",
87        "0.2.262.1.10.12.1"
88     },
89     {  7, "\x2A\x86\x48\xCE\x38\x04\x01",
90        "1.2.840.10040.4.1"
91     },
92     {  7, "\x2A\x86\x48\xCE\x38\x04\x03",
93        "1.2.840.10040.4.3"
94     },
95     { 10, "\x2B\x06\x01\x04\x01\xDA\x47\x02\x01\x01",
96       "1.3.6.1.4.1.11591.2.1.1"
97     },
98     {  3, "\x55\x1D\x0E",
99        "2.5.29.14"
100     },
101     {  9, "\x80\x02\x70\x50\x25\x46\xfd\x0c\xc0",
102        BADOID
103     },
104     {  1, "\x80",
105        BADOID
106     },
107     {  2, "\x81\x00",
108        "2.48"
109     },
110     {  2, "\x81\x01",
111        "2.49"
112     },
113     {  2, "\x81\x7f",
114        "2.175"
115     },
116     {  2, "\x81\x80",  /* legal encoding? */
117        "2.48"
118     },
119     {  2, "\x81\x81\x01",  /* legal encoding? */
120        "2.49"
121     },
122     {  0, "",
123        ""
124     },
125
126     { 0, NULL, NULL }
127   };
128   int tidx;
129   char *str;
130
131   for (tidx=0; tests[tidx].bin; tidx++)
132     {
133       str = ksba_oid_to_str (tests[tidx].bin, tests[tidx].binlen);
134       if (!str)
135         {
136           perror ("ksba_oid_to_str failed");
137           exit (1);
138         }
139       if (strcmp (tests[tidx].str, str))
140         {
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);
144           exit (1);
145         }
146       ksba_free (str);
147     }
148 }
149
150
151 int
152 main (int argc, char **argv)
153 {
154   gpg_error_t err;
155
156   if (argc)
157     {
158       argc--;
159       argv++;
160     }
161
162
163   if (!argc)
164     {
165       test_oid_to_str ();
166     }
167   else if (!strcmp (*argv, "--from-str"))
168     {
169       unsigned char *buffer;
170       size_t n, buflen;
171
172       for (argv++,argc-- ; argc; argc--, argv++)
173         {
174           err = ksba_oid_from_str (*argv, &buffer, &buflen);
175           if (err)
176             {
177               fprintf (stderr, "can't convert `%s': %s\n",
178                        *argv, gpg_strerror (err));
179               return 1;
180             }
181           printf ("%s ->", *argv);
182           for (n=0; n < buflen; n++)
183             printf (" %02X", buffer[n]);
184           putchar ('\n');
185           free (buffer);
186           buffer = NULL;
187         }
188     }
189   else if (!strcmp (*argv, "--to-str"))
190     {
191       char *buffer;
192       size_t buflen;
193       char *result;
194
195       argv++;argc--;
196
197       buffer = read_into_buffer (stdin, &buflen);
198       result = ksba_oid_to_str (buffer, buflen);
199       free (buffer);
200       printf ("%s\n", result? result:"[malloc failed]");
201       free (result);
202     }
203   else
204     {
205       fputs ("usage: "PGM" [--from-str|--to-str]\n", stderr);
206       return 1;
207     }
208
209   return 0;
210 }