opietest.c revision 22347
122347Spst/* opietest.c: Quick, though definitely not complete, regression test for
222347Spst               libopie. This is intended to catch two things:
322347Spst
422347Spst	(1) when changes break something
522347Spst        (2) if some system wierdness (libc, compiler, or CPU/hardware) is
622347Spst            not getting along at all with OPIE.
722347Spst
822347Spst        It's safe to say that, if tests fail, OPIE isn't going to work right
922347Spston your system. The converse is not such a safe statement.
1022347Spst
1122347Spst%%% copyright-cmetz
1222347SpstThis software is Copyright 1996 by Craig Metz, All Rights Reserved.
1322347SpstThe Inner Net License Version 2 applies to this software.
1422347SpstYou should have received a copy of the license with this software. If
1522347Spstyou didn't get a copy, you may request one from <license@inner.net>.
1622347Spst
1722347Spst        History:
1822347Spst
1922347Spst	Modified by cmetz for OPIE 2.3. Use new calling conventions for
2022347Spst		opiebtoa8()/atob8(). opiegenerator() outputs hex now.
2122347Spst	Modified by cmetz for OPIE 2.22. Test opielock()/opieunlock()
2222347Spst		refcount support.
2322347Spst	Created by cmetz for OPIE 2.2.
2422347Spst*/
2522347Spst#include "opie_cfg.h"
2622347Spst#include <stdio.h>
2722347Spst#include "opie.h"
2822347Spst
2922347Spstchar buffer[1024];
3022347Spstint tests_passed = 0;
3122347Spstint tests_failed = 0;
3222347Spstint ntests = 0, testn = 0;
3322347Spst
3422347Spstint testatob8()
3522347Spst{
3622347Spst  static char testin[] = "0123456789abcdef";
3722347Spst  static unsigned char testout[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
3822347Spst
3922347Spst  if (!opieatob8(buffer, testin))
4022347Spst    return -1;
4122347Spst
4222347Spst  if (memcmp(buffer, testout, sizeof(testout)))
4322347Spst    return -1;
4422347Spst
4522347Spst  return 0;
4622347Spst}
4722347Spst
4822347Spstint testbtoa8()
4922347Spst{
5022347Spst  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
5122347Spst  static char testout[] = "0123456789abcdef";
5222347Spst
5322347Spst  if (!opiebtoa8(buffer, testin))
5422347Spst    return -1;
5522347Spst
5622347Spst  if (memcmp(buffer, testout, sizeof(testout)))
5722347Spst    return -1;
5822347Spst
5922347Spst  return 0;
6022347Spst}
6122347Spst
6222347Spstint testbtoe()
6322347Spst{
6422347Spst  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
6522347Spst  static char testout[] = "AIM HEW BLUM FED MITE WARM";
6622347Spst
6722347Spst  if (!opiebtoe(buffer, testin))
6822347Spst    return -1;
6922347Spst
7022347Spst  if (memcmp(buffer, testout, sizeof(testout)))
7122347Spst    return -1;
7222347Spst
7322347Spst  return 0;
7422347Spst}
7522347Spst
7622347Spstint testetob()
7722347Spst{
7822347Spst  static char testin[] = "AIM HEW BLUM FED MITE WARM";
7922347Spst  static unsigned char testout[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
8022347Spst
8122347Spst  if (opieetob(buffer, testin) != 1)
8222347Spst    return -1;
8322347Spst
8422347Spst  if (memcmp(buffer, testout, sizeof(testout)))
8522347Spst    return -1;
8622347Spst
8722347Spst  return 0;
8822347Spst}
8922347Spst
9022347Spstint testgenerator()
9122347Spst{
9222347Spst  static char testin1[] = "otp-md5 123 ke1234";
9322347Spst  static char testin2[] = "this is a test";
9422347Spst  /*  static char testout[] = "END KERN BALM NICK EROS WAVY"; */
9522347Spst  static char testout[] = "11D4 C147 E227 C1F1";
9622347Spst
9722347Spst  if (opiegenerator(testin1, testin2, buffer))
9822347Spst    return -1;
9922347Spst
10022347Spst  if (memcmp(buffer, testout, sizeof(testout)))
10122347Spst    return -1;
10222347Spst
10322347Spst  return 0;
10422347Spst}
10522347Spst
10622347Spstint testgetsequence()
10722347Spst{
10822347Spst  struct opie testin;
10922347Spst  testin.opie_n = 42;
11022347Spst
11122347Spst  if (opiegetsequence(&testin) != 42)
11222347Spst    return -1;
11322347Spst
11422347Spst  return 0;
11522347Spst}
11622347Spst
11722347Spstint testhashmd4()
11822347Spst{
11922347Spst  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
12022347Spst  static unsigned char testout[] = { 0x9f, 0x40, 0xfb, 0x84, 0xb, 0xf8, 0x7f, 0x4b };
12122347Spst
12222347Spst  opiehash(testin, 4);
12322347Spst
12422347Spst  if (memcmp(testin, testout, sizeof(testout)))
12522347Spst    return -1;
12622347Spst
12722347Spst  return 0;
12822347Spst}
12922347Spst
13022347Spstint testhashmd5()
13122347Spst{
13222347Spst  static unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
13322347Spst  static unsigned char testout[] = { 0x78, 0xdd, 0x1a, 0x37, 0xf8, 0x91, 0x54, 0xe1 };
13422347Spst
13522347Spst  opiehash(testin, 5);
13622347Spst
13722347Spst  if (memcmp(testin, testout, sizeof(testout)))
13822347Spst    return -1;
13922347Spst
14022347Spst  return 0;
14122347Spst}
14222347Spst
14322347Spstint testkeycrunch()
14422347Spst{
14522347Spst  static char testin1[] = "ke1234";
14622347Spst  static char testin2[] = "this is a test";
14722347Spst  static unsigned char testout[] = { 0x2e, 0xd3, 0x5d, 0x74, 0x3e, 0xa9, 0xe9, 0xe8 };
14822347Spst
14922347Spst  if (opiekeycrunch(5, buffer, testin1, testin2))
15022347Spst    return -1;
15122347Spst
15222347Spst  if (memcmp(buffer, testout, sizeof(testout)))
15322347Spst    return -1;
15422347Spst
15522347Spst  return 0;
15622347Spst}
15722347Spst
15822347Spstint testlock()
15922347Spst{
16022347Spst  int i;
16122347Spst
16222347Spst  for (i = 0; i < 3; i++)
16322347Spst    if (opielock("__opietest"))
16422347Spst      return -1;
16522347Spst
16622347Spst  return 0;
16722347Spst}
16822347Spst
16922347Spstint testpasscheck()
17022347Spst{
17122347Spst  static char testin1[] = "abadone";
17222347Spst  static char testin2[] = "A more reasonable choice.";
17322347Spst
17422347Spst  if (!opiepasscheck(testin1))
17522347Spst    return -1;
17622347Spst
17722347Spst  if (opiepasscheck(testin2))
17822347Spst    return -1;
17922347Spst
18022347Spst  return 0;
18122347Spst}
18222347Spst
18322347Spstint testunlock()
18422347Spst{
18522347Spst  int i;
18622347Spst
18722347Spst  for (i = 0; i < 3; i++)
18822347Spst    if (opieunlock())
18922347Spst      return -1;
19022347Spst
19122347Spst  if (opieunlock() != -1)
19222347Spst    return -1;
19322347Spst
19422347Spst  return 0;
19522347Spst}
19622347Spst
19722347Spststruct opietest {
19822347Spst  int (*f)();
19922347Spst  char *n;
20022347Spst};
20122347Spst
20222347Spststatic struct opietest opietests[] = {
20322347Spst  { testatob8, "atob8" },
20422347Spst  { testbtoa8, "btoa8" },
20522347Spst  { testbtoe, "btoe" },
20622347Spst  { testetob, "etob" },
20722347Spst/* { testchallenge, "challenge" }, */
20822347Spst  { testgenerator, "generator" },
20922347Spst  { testgetsequence, "getsequence" },
21022347Spst/* { testgetutmpentry, "getutmpentry" }, */
21122347Spst  { testhashmd4, "hash(MD4)" },
21222347Spst  { testhashmd5, "hash(MD5)" },
21322347Spst/* { testinsecure, "insecure" }, */
21422347Spst  { testkeycrunch, "keycrunch" },
21522347Spst  { testlock, "lock" },
21622347Spst/* { testpututmpentry, "pututmpentry" }, */
21722347Spst/* { testrandomchallenge, "randomchallenge" }, */
21822347Spst/* { testreadpass, "readpass" }, */
21922347Spst  { testunlock, "unlock" },
22022347Spst/* { testverify, "verify" }, */
22122347Spst/*  { testversion, "version" }, */
22222347Spst  { NULL, NULL }
22322347Spst};
22422347Spst
22522347Spstint main FUNCTION((argc, argv), int argc AND char *argv[])
22622347Spst{
22722347Spst  struct opietest *opietest;
22822347Spst
22922347Spst  for (opietest = opietests; opietest->n; opietest++)
23022347Spst    ntests++;
23122347Spst
23222347Spst  printf("opietest: executing %d tests\n", ntests);
23322347Spst
23422347Spst  for (opietest = opietests, testn = 1; opietest->n; opietest++) {
23522347Spst    printf("(%2d/%2d) testing opie%s... ", testn++, ntests, opietest->n);
23622347Spst    if (opietest->f()) {
23722347Spst      printf("FAILED!\n");
23822347Spst      tests_failed++;
23922347Spst    } else {
24022347Spst      printf("passed\n");
24122347Spst      tests_passed++;
24222347Spst      opietest->f = NULL;
24322347Spst    }
24422347Spst  }
24522347Spst
24622347Spst  printf("opietest: completed %d tests. %d tests passed, %d tests failed.\n", ntests, tests_passed, tests_failed);
24722347Spst  if (tests_failed) {
24822347Spst    printf("opietest: please correct the following failures before attempting to use OPIE:\n");
24922347Spst    for (opietest = opietests; opietest->n; opietest++)
25022347Spst      if (opietest->f)
25122347Spst	printf("          opie%s\n", opietest->n);
25222347Spst    exit(1);
25322347Spst  }
25422347Spst  exit(0);
25522347Spst}
256