test/kq_connect_client: Make result accurate
[dragonfly.git] / contrib / opie / opietest.c
blob004d2647b948fb413405905f2dfbc5cdfc7ccd37
1 /* opietest.c: Quick, though definitely not complete, regression test for
2 libopie. This is intended to catch two things:
4 (1) when changes break something
5 (2) if some system wierdness (libc, compiler, or CPU/hardware) is
6 not getting along at all with OPIE.
8 It's safe to say that, if tests fail, OPIE isn't going to work right
9 on your system. The converse is not such a safe statement.
11 %%% copyright-cmetz-96
12 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
13 The Inner Net License Version 3 applies to this software.
14 You should have received a copy of the license with this software. If
15 you didn't get a copy, you may request one from <license@inner.net>.
17 History:
19 Modified by cmetz for OPIE 2.4. Use struct opie_key for key blocks.
20 Modified by cmetz for OPIE 2.31. Added a couple of new checks,
21 removed a few commented-out checks for functions that
22 no longer exist, added test-skip capability.
23 Modified by cmetz for OPIE 2.3. Use new calling conventions for
24 opiebtoa8()/atob8(). opiegenerator() outputs hex now.
25 Modified by cmetz for OPIE 2.22. Test opielock()/opieunlock()
26 refcount support.
27 Created by cmetz for OPIE 2.2.
29 #include "opie_cfg.h"
30 #include <stdio.h>
31 #include "opie.h"
33 char buffer[1024];
35 int testatob8()
37 static char testin[] = "0123456789abcdef";
38 static unsigned char testout[sizeof(struct opie_otpkey)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
39 struct opie_otpkey key;
41 if (!opieatob8(&key, testin))
42 return -1;
44 if (memcmp(&key, testout, sizeof(testout)))
45 return -1;
47 return 0;
50 int testbtoa8()
52 static unsigned char testin[sizeof(struct opie_otpkey)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
53 static char testout[] = "0123456789abcdef";
54 struct opie_otpkey testin_aligned;
56 memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
58 if (!opiebtoa8(buffer, &testin_aligned))
59 return -1;
61 if (memcmp(buffer, testout, sizeof(testout)))
62 return -1;
64 return 0;
67 int testbtoe()
69 static unsigned char testin[sizeof(struct opie_otpkey)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
70 static char testout[] = "AIM HEW BLUM FED MITE WARM";
71 struct opie_otpkey testin_aligned;
73 memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
75 if (!opiebtoe(buffer, &testin_aligned))
76 return -1;
78 if (memcmp(buffer, testout, sizeof(testout)))
79 return -1;
81 return 0;
84 int testetob()
86 static char testin[] = "AIM HEW BLUM FED MITE WARM";
87 static unsigned char testout[sizeof(struct opie_otpkey)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
88 struct opie_otpkey key;
90 if (opieetob(&key, testin) != 1)
91 return -1;
93 if (memcmp(&key, testout, sizeof(testout)))
94 return -1;
96 return 0;
99 int testgenerator()
101 static char testin1[] = "otp-md5 123 ke1234";
102 static char testin2[] = "this is a test";
103 /* static char testout[] = "END KERN BALM NICK EROS WAVY"; */
104 static char testout[] = "11D4 C147 E227 C1F1";
106 if (opiegenerator(testin1, testin2, buffer))
107 return -1;
109 if (memcmp(buffer, testout, sizeof(testout)))
110 return -1;
112 return 0;
115 int testgetsequence()
117 struct opie testin;
118 testin.opie_n = 42;
120 if (opiegetsequence(&testin) != 42)
121 return -1;
123 return 0;
126 int testhashmd4()
128 static unsigned char testin[sizeof(struct opie_otpkey)] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
129 static unsigned char testout[sizeof(struct opie_otpkey)] = { 0x9f, 0x40, 0xfb, 0x84, 0xb, 0xf8, 0x7f, 0x4b };
130 struct opie_otpkey testin_aligned;
132 memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
134 opiehash(&testin_aligned, 4);
136 if (memcmp(&testin_aligned, testout, sizeof(struct opie_otpkey)))
137 return -1;
139 return 0;
142 int testhashmd5()
144 static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
145 static unsigned char testout[] = { 0x78, 0xdd, 0x1a, 0x37, 0xf8, 0x91, 0x54, 0xe1 };
146 struct opie_otpkey testin_aligned;
148 memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
150 opiehash(&testin_aligned, 5);
152 if (memcmp(&testin_aligned, testout, sizeof(struct opie_otpkey)))
153 return -1;
155 return 0;
158 int testinsecure()
160 opieinsecure();
162 return 0;
165 int testkeycrunch()
167 static char testin1[] = "ke1234";
168 static char testin2[] = "this is a test";
169 static unsigned char testout[sizeof(struct opie_otpkey)] = { 0x2e, 0xd3, 0x5d, 0x74, 0x3e, 0xa9, 0xe9, 0xe8 };
170 struct opie_otpkey opie_otpkey;
172 if (opiekeycrunch(5, &opie_otpkey, testin1, testin2))
173 return -1;
175 if (memcmp(&opie_otpkey, testout, sizeof(struct opie_otpkey)))
176 return -1;
178 return 0;
181 int testlock()
183 int i;
185 if (getuid())
186 return -2;
188 for (i = 0; i < 3; i++)
189 if (opielock("__opietest"))
190 return -1;
192 return 0;
195 int testpasscheck()
197 static char testin1[] = "abadone";
198 static char testin2[] = "A more reasonable choice.";
200 if (!opiepasscheck(testin1))
201 return -1;
203 if (opiepasscheck(testin2))
204 return -1;
206 return 0;
209 int testrandomchallenge()
211 char buffer[OPIE_CHALLENGE_MAX+1];
213 opierandomchallenge(buffer);
215 if (strncmp(buffer, "otp-", 4))
216 return -1;
218 return 0;
221 int testunlock()
223 int i;
225 if (getuid())
226 return -2;
228 for (i = 0; i < 3; i++)
229 if (opieunlock())
230 return -1;
232 if (opieunlock() != -1)
233 return -1;
235 return 0;
238 struct opietest {
239 int (*f)();
240 char *n;
243 static struct opietest opietests[] = {
244 { testatob8, "atob8" },
245 { testbtoa8, "btoa8" },
246 { testbtoe, "btoe" },
247 { testetob, "etob" },
248 /* { testchallenge, "challenge" }, */
249 { testgenerator, "generator" },
250 { testgetsequence, "getsequence" },
251 { testhashmd4, "hash(MD4)" },
252 { testhashmd5, "hash(MD5)" },
253 { testinsecure, "insecure" },
254 { testkeycrunch, "keycrunch" },
255 { testlock, "lock" },
256 { testrandomchallenge, "randomchallenge" },
257 /* { testreadpass, "readpass" }, */
258 { testunlock, "unlock" },
259 /* { testverify, "verify" }, */
260 { NULL, NULL }
263 int main FUNCTION((argc, argv), int argc AND char *argv[])
265 struct opietest *opietest;
266 int tests_passed = 0;
267 int tests_failed = 0;
268 int tests_skipped = 0;
269 int ntests = 0, testn = 0;
271 if (getuid() != geteuid()) {
272 fprintf(stderr, "opietest: do not make this program setuid!\n");
273 exit(1);
276 for (opietest = opietests; opietest->n; opietest++)
277 ntests++;
279 printf("opietest: executing %d tests\n", ntests);
281 for (opietest = opietests, testn = 1; opietest->n; opietest++) {
282 printf("(%2d/%2d) testing opie%s... ", testn++, ntests, opietest->n);
283 switch(opietest->f()) {
284 case -2:
285 printf("skipped\n");
286 tests_skipped++;
287 opietest->f = NULL;
288 break;
289 case -1:
290 printf("FAILED!\n");
291 tests_failed++;
292 break;
293 case 0:
294 printf("passed\n");
295 tests_passed++;
296 opietest->f = NULL;
297 break;
301 printf("opietest: completed %d tests. %d tests passed, %d tests skipped, %d tests failed.\n", ntests, tests_passed, tests_skipped, tests_failed);
302 if (tests_failed) {
303 printf("opietest: please correct the following failures before attempting to use OPIE:\n");
304 for (opietest = opietests; opietest->n; opietest++)
305 if (opietest->f)
306 printf(" opie%s\n", opietest->n);
307 exit(1);
309 exit(0);