Disconnect hostapd from building in base
[dragonfly.git] / contrib / opie / opietest.c
1 /* opietest.c: Quick, though definitely not complete, regression test for
2                libopie. This is intended to catch two things:
3
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.
7
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.
10
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>.
16
17         History:
18
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.
28 */
29 #include "opie_cfg.h"
30 #include <stdio.h>
31 #include "opie.h"
32
33 char buffer[1024];
34
35 int testatob8()
36 {
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;
40
41   if (!opieatob8(&key, testin))
42     return -1;
43
44   if (memcmp(&key, testout, sizeof(testout)))
45     return -1;
46   
47   return 0;
48 }
49
50 int testbtoa8()
51 {
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;
55
56   memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
57     
58   if (!opiebtoa8(buffer, &testin_aligned))
59     return -1;
60
61   if (memcmp(buffer, testout, sizeof(testout)))
62     return -1;
63   
64   return 0;  
65 }
66
67 int testbtoe()
68 {
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;
72   
73   memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
74
75   if (!opiebtoe(buffer, &testin_aligned))
76     return -1;
77
78   if (memcmp(buffer, testout, sizeof(testout)))
79     return -1;
80   
81   return 0;  
82 }
83
84 int testetob()
85 {
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;
89
90   if (opieetob(&key, testin) != 1)
91     return -1;
92
93   if (memcmp(&key, testout, sizeof(testout)))
94     return -1;
95   
96   return 0;  
97 }
98
99 int testgenerator()
100 {
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";
105
106   if (opiegenerator(testin1, testin2, buffer))
107     return -1;
108
109   if (memcmp(buffer, testout, sizeof(testout)))
110     return -1;
111   
112   return 0;  
113 }
114
115 int testgetsequence()
116 {
117   struct opie testin;
118   testin.opie_n = 42;
119
120   if (opiegetsequence(&testin) != 42)
121     return -1;
122
123   return 0;
124 }
125
126 int testhashmd4()
127 {
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;
131
132   memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
133
134   opiehash(&testin_aligned, 4);
135
136   if (memcmp(&testin_aligned, testout, sizeof(struct opie_otpkey)))
137     return -1;
138
139   return 0;
140 }
141
142 int testhashmd5()
143 {
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;
147
148   memcpy(&testin_aligned, testin, sizeof(struct opie_otpkey));
149
150   opiehash(&testin_aligned, 5);
151
152   if (memcmp(&testin_aligned, testout, sizeof(struct opie_otpkey)))
153     return -1;
154
155   return 0;
156 }
157
158 int testinsecure()
159 {
160   opieinsecure();
161
162   return 0;
163 }
164
165 int testkeycrunch()
166 {
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;
171
172   if (opiekeycrunch(5, &opie_otpkey, testin1, testin2))
173     return -1;
174
175   if (memcmp(&opie_otpkey, testout, sizeof(struct opie_otpkey)))
176     return -1;
177
178   return 0;
179 }
180
181 int testlock()
182 {
183   int i;
184
185   if (getuid())
186     return -2;
187
188   for (i = 0; i < 3; i++)
189     if (opielock("__opietest"))
190       return -1;
191
192   return 0;
193 }
194
195 int testpasscheck()
196 {
197   static char testin1[] = "abadone";
198   static char testin2[] = "A more reasonable choice.";
199
200   if (!opiepasscheck(testin1))
201     return -1;
202
203   if (opiepasscheck(testin2))
204     return -1;
205
206   return 0;
207 }
208
209 int testrandomchallenge()
210 {
211   char buffer[OPIE_CHALLENGE_MAX+1];
212
213   opierandomchallenge(buffer);
214
215   if (strncmp(buffer, "otp-", 4))
216     return -1;
217
218   return 0;
219 }
220
221 int testunlock()
222 {
223   int i;
224
225   if (getuid())
226     return -2;
227
228   for (i = 0; i < 3; i++)
229     if (opieunlock())
230       return -1;
231
232   if (opieunlock() != -1)
233     return -1;
234
235   return 0;
236 }
237
238 struct opietest {
239   int (*f)();
240   char *n;
241 };
242
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 }
261 };
262
263 int main FUNCTION((argc, argv), int argc AND char *argv[])
264 {
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;
270
271   if (getuid() != geteuid()) {
272     fprintf(stderr, "opietest: do not make this program setuid!\n");
273     exit(1);
274   };
275
276   for (opietest = opietests; opietest->n; opietest++)
277     ntests++;
278
279   printf("opietest: executing %d tests\n", ntests);
280
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;
298     }
299   }
300
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);
308   }
309   exit(0);
310 }