Add.
[shishi.git] / tests / authenticator.c
blob6e7eecfa883932b78b31157fc03b221b706c08f5
1 /* authenticator.c Shishi authenticator self tests.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi 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 2 of the License, or
9 * (at your option) any later version.
11 * Shishi 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 Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "utils.c"
24 static const char authenticator[] = {
25 '\x62', '\x4b', '\x30', '\x49', '\xa0', '\x03', '\x02', '\x01',
26 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
27 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
28 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
29 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
30 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
31 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
32 '\x33', '\x31', '\x5a', '\xa8', '\x10', '\x30', '\x0e', '\x30',
33 '\x0c', '\xa0', '\x03', '\x02', '\x01', '\x2a', '\xa1', '\x05',
34 '\x04', '\x03', '\x62', '\x61', '\x7a'
37 static const char authenticator2[] = {
38 '\x62', '\x39', '\x30', '\x37', '\xa0', '\x03', '\x02', '\x01',
39 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
40 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
41 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
42 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
43 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
44 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
45 '\x33', '\x31', '\x5a'
48 #define AUTHENTICATOR_LEN 77
49 #define AUTHENTICATOR2_LEN 59
51 void
52 test (Shishi * handle)
54 Shishi_asn1 a;
55 char *p, *buf, *buf2;
56 int res;
57 size_t n, m;
58 int32_t t;
59 uint32_t s;
61 /* shishi_authenticator */
62 a = shishi_authenticator (handle);
63 if (debug)
64 printf ("shishi_authenticator () => `%p'.\n", a);
65 if (a)
66 success ("shishi_authenticator() OK\n");
67 else
68 fail ("shishi_authenticator() failed\n");
70 if (debug)
71 shishi_authenticator_print (handle, stdout, a);
73 res = shishi_authenticator_remove_subkey (handle, a);
74 if (res == SHISHI_OK)
75 success ("shishi_authenticator_remove_subkey() OK\n");
76 else
77 fail ("shishi_authenticator_remove_subkey() failed\n");
79 /* shishi_authenticator_seqnumber_get */
80 res = shishi_authenticator_seqnumber_get (handle, a, &s);
81 if (res == SHISHI_OK)
82 success ("shishi_authenticator_seqnumber_get() OK\n");
83 else
84 fail ("shishi_authenticator_seqnumber_get() failed\n");
86 /* shishi_authenticator_seqnumber_set */
87 res = shishi_authenticator_seqnumber_set (handle, a, 42);
88 if (res == SHISHI_OK)
89 success ("shishi_authenticator_seqnumber_set() OK\n");
90 else
91 fail ("shishi_authenticator_seqnumber_set() failed\n");
93 /* shishi_authenticator_seqnumber_get */
94 res = shishi_authenticator_seqnumber_get (handle, a, &s);
95 if (res == SHISHI_OK && s == 42)
96 success ("shishi_authenticator_seqnumber_get() OK\n");
97 else
98 fail ("shishi_authenticator_seqnumber_get() failed\n");
100 /* shishi_authenticator_seqnumber_remove */
101 res = shishi_authenticator_seqnumber_remove (handle, a);
102 if (res == SHISHI_OK)
103 success ("shishi_authenticator_seqnumber_remove() OK\n");
104 else
105 fail ("shishi_authenticator_seqnumber_remove() failed\n");
107 /* shishi_authenticator_set_crealm */
108 res = shishi_authenticator_set_crealm (handle, a, "foo");
109 if (res == SHISHI_OK)
110 success ("shishi_authenticator_set_crealm() OK\n");
111 else
112 fail ("shishi_authenticator_set_crealm() failed\n");
114 /* shishi_authenticator_client_set */
115 res = shishi_authenticator_client_set (handle, a, "foo/bar/baz");
116 if (res == SHISHI_OK)
117 success ("shishi_authenticator_client_set() OK\n");
118 else
119 fail ("shishi_authenticator_client_set() failed\n");
121 /* shishi_authenticator_client */
122 res = shishi_authenticator_client (handle, a, &buf, &n);
123 if (debug)
124 escapeprint (buf, n);
125 if (res == SHISHI_OK &&
126 n == strlen ("foo/bar/baz") && memcmp (buf, "foo/bar/baz", n) == 0)
127 success ("shishi_authenticator_client() OK\n");
128 else
129 fail ("shishi_authenticator_client() failed\n");
130 if (res == SHISHI_OK)
131 free (buf);
133 /* shishi_authenticator_client_set */
134 res = shishi_authenticator_client_set (handle, a, "foo");
135 if (res == SHISHI_OK)
136 success ("shishi_authenticator_client_set() OK\n");
137 else
138 fail ("shishi_authenticator_client_set() failed\n");
140 /* shishi_authenticator_client */
141 res = shishi_authenticator_client (handle, a, &buf, &n);
142 if (debug)
143 escapeprint (buf, n);
144 if (res == SHISHI_OK && n == strlen ("foo") && memcmp (buf, "foo", n) == 0)
145 success ("shishi_authenticator_client() OK\n");
146 else
147 fail ("shishi_authenticator_client() failed\n");
148 if (res == SHISHI_OK)
149 free (buf);
151 /* shishi_authenticator_set_crealm */
152 res = shishi_authenticator_set_crealm (handle, a, "bar");
153 if (res == SHISHI_OK)
154 success ("shishi_authenticator_set_crealm() OK\n");
155 else
156 fail ("shishi_authenticator_set_crealm() failed\n");
158 /* shishi_authenticator_clientrealm */
159 res = shishi_authenticator_clientrealm (handle, a, &buf, &n);
160 if (debug)
161 escapeprint (buf, n);
162 if (res == SHISHI_OK &&
163 n == strlen ("foo@bar") && memcmp (buf, "foo@bar", n) == 0)
164 success ("shishi_authenticator_clientrealm() OK\n");
165 else
166 fail ("shishi_authenticator_clientrealm() failed\n");
167 if (res == SHISHI_OK)
168 free (buf);
170 /* shishi_authenticator_add_authorizationdata */
171 res = shishi_authenticator_add_authorizationdata (handle, a, 42, "baz", 3);
172 if (res == SHISHI_OK)
173 success ("shishi_authenticator_add_authorizationdata() OK\n");
174 else
175 fail ("shishi_authenticator_add_authorizationdata() failed\n");
177 /* shishi_authenticator_authorizationdata */
178 res = shishi_authenticator_authorizationdata (handle, a, &t, &buf, &m, 1);
179 if (debug)
180 escapeprint (buf, m);
181 if (res == SHISHI_OK && t == 42 && m == 3 && memcmp (buf, "baz", 3) == 0)
182 success ("shishi_authenticator_authorizationdata() OK\n");
183 else
184 fail ("shishi_authenticator_authorizationdata() failed\n");
185 if (res == SHISHI_OK)
186 free (buf);
188 /* shishi_authenticator_authorizationdata */
189 res = shishi_authenticator_authorizationdata (handle, a, &t, &buf, &m, 2);
190 if (res == SHISHI_OK)
191 free (buf);
192 if (res == SHISHI_OUT_OF_RANGE)
193 success ("shishi_authenticator_authorizationdata() OK\n");
194 else
195 fail ("shishi_authenticator_authorizationdata() failed\n");
197 /* shishi_authenticator_remove_cksum */
198 res = shishi_authenticator_remove_cksum (handle, a);
199 if (res == SHISHI_OK)
200 success ("shishi_authenticator_remove_cksum() OK\n");
201 else
202 fail ("shishi_authenticator_remove_cksum() failed\n");
204 /* shishi_asn1_to_der */
205 res = shishi_asn1_to_der (handle, a, &buf, &n);
206 if (res == SHISHI_OK)
207 success ("shishi_asn1_to_der() OK\n");
208 else
209 n = 0, fail ("shishi_asn1_to_der() failed\n");
211 /* shishi_authenticator_to_file */
212 res = shishi_authenticator_to_file (handle, a, SHISHI_FILETYPE_TEXT,
213 "authenticator.tmp");
214 if (res == SHISHI_OK)
215 success ("shishi_authenticator_to_file() OK\n");
216 else
217 fail ("shishi_authenticator_to_file() failed\n");
219 /* shishi_asn1_done */
220 shishi_asn1_done (handle, a);
221 success ("shishi_asn1_done() OK\n");
223 a = NULL;
225 /* shishi_authenticator_from_file */
226 res = shishi_authenticator_from_file (handle, &a, SHISHI_FILETYPE_TEXT,
227 "authenticator.tmp");
228 if (res == SHISHI_OK)
229 success ("shishi_authenticator_from_file() OK\n");
230 else
231 fail ("shishi_authenticator_from_file() failed\n");
233 if (debug)
235 /* shishi_authenticator_print */
236 res = shishi_authenticator_print (handle, stdout, a);
237 if (res == SHISHI_OK)
238 success ("shishi_authenticator_print() OK\n");
239 else
240 fail ("shishi_authenticator_print() failed\n");
243 /* shishi_asn1_to_der */
244 res = shishi_asn1_to_der (handle, a, &buf2, &m);
245 if (res == SHISHI_OK)
246 success ("shishi_asn1_to_der() OK\n");
247 else
248 n = 0, fail ("shishi_asn1_to_der() failed\n");
250 /* Compare DER encodings of authenticators */
251 if (n > 0 && m > 0 && n == m && memcmp (buf, buf2, n) == 0)
252 success ("DER comparison OK\n");
253 else
254 fail ("DER comparison failed\n");
256 free (buf);
257 free (buf2);
259 /* shishi_authenticator_cusec_set */
260 res = shishi_authenticator_cusec_set (handle, a, 4711);
261 if (res == SHISHI_OK)
262 success ("shishi_authenticator_cusec_set() OK\n");
263 else
264 fail ("shishi_authenticator_cusec_set() failed\n");
266 /* shishi_authenticator_cusec_get */
267 res = shishi_authenticator_cusec_get (handle, a, &s);
268 if (debug)
269 printf ("shishi_authenticator_cusec_get () => `%d'.\n", t);
270 if (res == SHISHI_OK && s == 4711)
271 success ("shishi_authenticator_cusec_get() OK\n");
272 else
273 fail ("shishi_authenticator_cusec_get() failed\n");
275 /* shishi_authenticator_ctime_set */
276 res = shishi_authenticator_ctime_set (handle, a, "19700101011831Z");
277 if (res == SHISHI_OK)
278 success ("shishi_authenticator_ctime_set() OK\n");
279 else
280 fail ("shishi_authenticator_ctime_set() failed\n");
282 /* shishi_authenticator_ctime */
283 res = shishi_authenticator_ctime (handle, a, &p);
284 if (debug)
285 escapeprint (p, strlen (p));
286 if (res == SHISHI_OK && memcmp (p, "19700101011831Z", 15) == 0)
287 success ("shishi_authenticator_ctime() OK\n");
288 else
289 fail ("shishi_authenticator_ctime() failed\n");
290 if (res == SHISHI_OK)
291 free (p);
293 /* shishi_asn1_to_der */
294 res = shishi_asn1_to_der (handle, a, &buf, &n);
295 if (res == SHISHI_OK)
296 success ("shishi_asn1_to_der() OK\n");
297 else
298 n = 0, fail ("shishi_asn1_to_der() failed\n");
299 if (debug)
301 shishi_authenticator_print (handle, stdout, a);
302 hexprint (buf, n);
303 puts ("");
304 hexprint (authenticator, sizeof (authenticator));
305 puts ("");
307 if (n == sizeof (authenticator) &&
308 n == AUTHENTICATOR_LEN && memcmp (authenticator, buf, n) == 0)
309 success ("DER comparison OK\n");
310 else
311 fail ("DER comparison failed\n");
313 free (buf);
315 /* shishi_authenticator_clear_authorizationdata */
316 res = shishi_authenticator_clear_authorizationdata (handle, a);
317 if (res == SHISHI_OK)
318 success ("shishi_authenticator_clear_authorizationdata() OK\n");
319 else
320 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
322 /* shishi_asn1_to_der */
323 res = shishi_asn1_to_der (handle, a, &buf, &n);
324 if (res == SHISHI_OK)
325 success ("shishi_asn1_to_der() OK\n");
326 else
327 n = 0, fail ("shishi_asn1_to_der() failed\n");
328 if (debug)
330 shishi_authenticator_print (handle, stdout, a);
331 hexprint (buf, n);
332 puts ("");
333 hexprint (authenticator2, sizeof (authenticator2));
334 puts ("");
336 if (n == sizeof (authenticator2) &&
337 n == AUTHENTICATOR2_LEN && memcmp (authenticator2, buf, n) == 0)
338 success ("DER comparison OK\n");
339 else
340 fail ("DER comparison failed\n");
342 free (buf);
344 /* unlink */
345 res = unlink ("authenticator.tmp");
346 if (res == 0)
347 success ("unlink() OK\n");
348 else
349 fail ("unlink() failed\n");