remove gcc34
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / asn1 / check-der.c
blob7cb057749e49f1fbfc4846a6e395dabcda3d8188
1 /*
2 * Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <string.h>
39 #include <err.h>
40 #include <roken.h>
42 #include <asn1-common.h>
43 #include <asn1_err.h>
44 #include <der.h>
46 #include "check-common.h"
48 RCSID("$Id: check-der.c,v 1.9 2003/01/23 10:19:49 lha Exp $");
50 static int
51 cmp_integer (void *a, void *b)
53 int *ia = (int *)a;
54 int *ib = (int *)b;
56 return *ib - *ia;
59 static int
60 test_integer (void)
62 struct test_case tests[] = {
63 {NULL, 3, "\x02\x01\x00"},
64 {NULL, 3, "\x02\x01\x7f"},
65 {NULL, 4, "\x02\x02\x00\x80"},
66 {NULL, 4, "\x02\x02\x01\x00"},
67 {NULL, 3, "\x02\x01\x80"},
68 {NULL, 4, "\x02\x02\xff\x7f"},
69 {NULL, 3, "\x02\x01\xff"},
70 {NULL, 4, "\x02\x02\xff\x01"},
71 {NULL, 4, "\x02\x02\x00\xff"},
72 {NULL, 6, "\x02\x04\x80\x00\x00\x00"},
73 {NULL, 6, "\x02\x04\x7f\xff\xff\xff"}
76 int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
77 0x80000000, 0x7fffffff};
78 int i;
79 int ntests = sizeof(tests) / sizeof(*tests);
81 for (i = 0; i < ntests; ++i) {
82 tests[i].val = &values[i];
83 asprintf (&tests[i].name, "integer %d", values[i]);
86 return generic_test (tests, ntests, sizeof(int),
87 (generic_encode)encode_integer,
88 (generic_length) length_integer,
89 (generic_decode)decode_integer,
90 cmp_integer);
93 static int
94 cmp_octet_string (void *a, void *b)
96 octet_string *oa = (octet_string *)a;
97 octet_string *ob = (octet_string *)b;
99 if (oa->length != ob->length)
100 return ob->length - oa->length;
102 return (memcmp (oa->data, ob->data, oa->length));
105 static int
106 test_octet_string (void)
108 octet_string s1 = {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
110 struct test_case tests[] = {
111 {NULL, 10, "\x04\x08\x01\x23\x45\x67\x89\xab\xcd\xef"}
113 int ntests = sizeof(tests) / sizeof(*tests);
115 tests[0].val = &s1;
116 asprintf (&tests[0].name, "a octet string");
118 return generic_test (tests, ntests, sizeof(octet_string),
119 (generic_encode)encode_octet_string,
120 (generic_length)length_octet_string,
121 (generic_decode)decode_octet_string,
122 cmp_octet_string);
125 static int
126 cmp_general_string (void *a, void *b)
128 unsigned char **sa = (unsigned char **)a;
129 unsigned char **sb = (unsigned char **)b;
131 return strcmp (*sa, *sb);
134 static int
135 test_general_string (void)
137 unsigned char *s1 = "Test User 1";
139 struct test_case tests[] = {
140 {NULL, 13, "\x1b\x0b\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
142 int ntests = sizeof(tests) / sizeof(*tests);
144 tests[0].val = &s1;
145 asprintf (&tests[0].name, "the string \"%s\"", s1);
147 return generic_test (tests, ntests, sizeof(unsigned char *),
148 (generic_encode)encode_general_string,
149 (generic_length)length_general_string,
150 (generic_decode)decode_general_string,
151 cmp_general_string);
154 static int
155 cmp_generalized_time (void *a, void *b)
157 time_t *ta = (time_t *)a;
158 time_t *tb = (time_t *)b;
160 return *tb - *ta;
163 static int
164 test_generalized_time (void)
166 struct test_case tests[] = {
167 {NULL, 17, "\x18\x0f""19700101000000Z"},
168 {NULL, 17, "\x18\x0f""19851106210627Z"}
170 time_t values[] = {0, 500159187};
171 int i;
172 int ntests = sizeof(tests) / sizeof(*tests);
174 for (i = 0; i < ntests; ++i) {
175 tests[i].val = &values[i];
176 asprintf (&tests[i].name, "time %d", (int)values[i]);
179 return generic_test (tests, ntests, sizeof(time_t),
180 (generic_encode)encode_generalized_time,
181 (generic_length)length_generalized_time,
182 (generic_decode)decode_generalized_time,
183 cmp_generalized_time);
187 main(int argc, char **argv)
189 int ret = 0;
191 ret += test_integer ();
192 ret += test_octet_string ();
193 ret += test_general_string ();
194 ret += test_generalized_time ();
196 return ret;