opietest.c revision 59118
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
9on your system. The converse is not such a safe statement.
10
11%%% copyright-cmetz-96
12This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved.
13The Inner Net License Version 2 applies to this software.
14You should have received a copy of the license with this software. If
15you didn't get a copy, you may request one from <license@inner.net>.
16
17        History:
18
19	Modified by cmetz for OPIE 2.31. Added a couple of new checks,
20		removed a few commented-out checks for functions that
21		no longer exist, added test-skip capability.
22	Modified by cmetz for OPIE 2.3. Use new calling conventions for
23		opiebtoa8()/atob8(). opiegenerator() outputs hex now.
24	Modified by cmetz for OPIE 2.22. Test opielock()/opieunlock()
25		refcount support.
26	Created by cmetz for OPIE 2.2.
27*/
28#include "opie_cfg.h"
29#include <stdio.h>
30#include "opie.h"
31
32char buffer[1024];
33
34int testatob8()
35{
36  static char testin[] = "0123456789abcdef";
37  static unsigned char testout[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
38
39  if (!opieatob8(buffer, testin))
40    return -1;
41
42  if (memcmp(buffer, testout, sizeof(testout)))
43    return -1;
44
45  return 0;
46}
47
48int testbtoa8()
49{
50  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
51  static char testout[] = "0123456789abcdef";
52
53  if (!opiebtoa8(buffer, testin))
54    return -1;
55
56  if (memcmp(buffer, testout, sizeof(testout)))
57    return -1;
58
59  return 0;
60}
61
62int testbtoe()
63{
64  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
65  static char testout[] = "AIM HEW BLUM FED MITE WARM";
66
67  if (!opiebtoe(buffer, testin))
68    return -1;
69
70  if (memcmp(buffer, testout, sizeof(testout)))
71    return -1;
72
73  return 0;
74}
75
76int testetob()
77{
78  static char testin[] = "AIM HEW BLUM FED MITE WARM";
79  static unsigned char testout[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
80
81  if (opieetob(buffer, testin) != 1)
82    return -1;
83
84  if (memcmp(buffer, testout, sizeof(testout)))
85    return -1;
86
87  return 0;
88}
89
90int testgenerator()
91{
92  static char testin1[] = "otp-md5 123 ke1234";
93  static char testin2[] = "this is a test";
94  /*  static char testout[] = "END KERN BALM NICK EROS WAVY"; */
95  static char testout[] = "11D4 C147 E227 C1F1";
96
97  if (opiegenerator(testin1, testin2, buffer))
98    return -1;
99
100  if (memcmp(buffer, testout, sizeof(testout)))
101    return -1;
102
103  return 0;
104}
105
106int testgetsequence()
107{
108  struct opie testin;
109  testin.opie_n = 42;
110
111  if (opiegetsequence(&testin) != 42)
112    return -1;
113
114  return 0;
115}
116
117int testhashmd4()
118{
119  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
120  static unsigned char testout[] = { 0x9f, 0x40, 0xfb, 0x84, 0xb, 0xf8, 0x7f, 0x4b };
121
122  opiehash(testin, 4);
123
124  if (memcmp(testin, testout, sizeof(testout)))
125    return -1;
126
127  return 0;
128}
129
130int testhashmd5()
131{
132  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
133  static unsigned char testout[] = { 0x78, 0xdd, 0x1a, 0x37, 0xf8, 0x91, 0x54, 0xe1 };
134
135  opiehash(testin, 5);
136
137  if (memcmp(testin, testout, sizeof(testout)))
138    return -1;
139
140  return 0;
141}
142
143int testinsecure()
144{
145  opieinsecure();
146
147  return 0;
148}
149
150int testkeycrunch()
151{
152  static char testin1[] = "ke1234";
153  static char testin2[] = "this is a test";
154  static unsigned char testout[] = { 0x2e, 0xd3, 0x5d, 0x74, 0x3e, 0xa9, 0xe9, 0xe8 };
155
156  if (opiekeycrunch(5, buffer, testin1, testin2))
157    return -1;
158
159  if (memcmp(buffer, testout, sizeof(testout)))
160    return -1;
161
162  return 0;
163}
164
165int testlock()
166{
167  int i;
168
169  if (getuid())
170    return -2;
171
172  for (i = 0; i < 3; i++)
173    if (opielock("__opietest"))
174      return -1;
175
176  return 0;
177}
178
179int testpasscheck()
180{
181  static char testin1[] = "abadone";
182  static char testin2[] = "A more reasonable choice.";
183
184  if (!opiepasscheck(testin1))
185    return -1;
186
187  if (opiepasscheck(testin2))
188    return -1;
189
190  return 0;
191}
192
193int testrandomchallenge()
194{
195  char buffer[OPIE_CHALLENGE_MAX+1];
196
197  opierandomchallenge(buffer);
198
199  if (strncmp(buffer, "otp-", 4))
200    return -1;
201
202  return 0;
203}
204
205int testunlock()
206{
207  int i;
208
209  if (getuid())
210    return -2;
211
212  for (i = 0; i < 3; i++)
213    if (opieunlock())
214      return -1;
215
216  if (opieunlock() != -1)
217    return -1;
218
219  return 0;
220}
221
222struct opietest {
223  int (*f)();
224  char *n;
225};
226
227static struct opietest opietests[] = {
228  { testatob8, "atob8" },
229  { testbtoa8, "btoa8" },
230  { testbtoe, "btoe" },
231  { testetob, "etob" },
232/*  { testchallenge, "challenge" }, */
233  { testgenerator, "generator" },
234  { testgetsequence, "getsequence" },
235  { testhashmd4, "hash(MD4)" },
236  { testhashmd5, "hash(MD5)" },
237  { testinsecure, "insecure" },
238  { testkeycrunch, "keycrunch" },
239  { testlock, "lock" },
240  { testrandomchallenge, "randomchallenge" },
241/* { testreadpass, "readpass" }, */
242  { testunlock, "unlock" },
243/*  { testverify, "verify" }, */
244  { NULL, NULL }
245};
246
247int main FUNCTION((argc, argv), int argc AND char *argv[])
248{
249  struct opietest *opietest;
250  int tests_passed = 0;
251  int tests_failed = 0;
252  int tests_skipped = 0;
253  int ntests = 0, testn = 0;
254
255  if (getuid() != geteuid()) {
256    fprintf(stderr, "opietest: do not make this program setuid!\n");
257    exit(1);
258  };
259
260  for (opietest = opietests; opietest->n; opietest++)
261    ntests++;
262
263  printf("opietest: executing %d tests\n", ntests);
264
265  for (opietest = opietests, testn = 1; opietest->n; opietest++) {
266    printf("(%2d/%2d) testing opie%s... ", testn++, ntests, opietest->n);
267    switch(opietest->f()) {
268      case -2:
269        printf("skipped\n");
270        tests_skipped++;
271        opietest->f = NULL;
272        break;
273      case -1:
274        printf("FAILED!\n");
275        tests_failed++;
276        break;
277      case 0:
278        printf("passed\n");
279        tests_passed++;
280        opietest->f = NULL;
281        break;
282    }
283  }
284
285  printf("opietest: completed %d tests. %d tests passed, %d tests skipped, %d tests failed.\n", ntests, tests_passed, tests_skipped, tests_failed);
286  if (tests_failed) {
287    printf("opietest: please correct the following failures before attempting to use OPIE:\n");
288    for (opietest = opietests; opietest->n; opietest++)
289      if (opietest->f)
290	printf("          opie%s\n", opietest->n);
291    exit(1);
292  }
293  exit(0);
294}
295