]> git.ozlabs.org Git - ppp.git/commitdiff
Adding in unit-tests for chap_ms.c in pppd
authorEivind Næss <eivnaes@yahoo.com>
Thu, 29 Jul 2021 14:52:17 +0000 (07:52 -0700)
committerEivind Næss <eivnaes@yahoo.com>
Thu, 29 Jul 2021 14:52:17 +0000 (07:52 -0700)
This would have uncovered the change that broke MD4

Signed-off-by: Eivind Næss <eivnaes@yahoo.com>
.gitignore
pppd/Makefile.am
pppd/chap_ms.c
pppd/utils.c

index 052d5fd6ee19bcff34cd9f2c47e7568fbe4b8587..133a3dfea3cdf9a61e0302896bb27c78c10628e4 100644 (file)
@@ -27,6 +27,7 @@ autom4te.cache
 /configure.scan
 /depcomp
 /install-sh
+/test-driver
 /missing
 /INSTALL
 /pppd/stamp-h1
@@ -50,5 +51,9 @@ m4/lt~obsolete.m4
 Makefile
 Makefile.in
 
+# Test related stuff
+utest_*
+test-suite.log
+
 # Distfiles
 /ppp-*.tar.gz
index c15ea2efdf13a283dd952748a54ddf84a12f35f9..22445072f78b8e3add93bd7aefb8dd6ed6b47e4e 100644 (file)
@@ -1,5 +1,14 @@
 sbin_PROGRAMS = pppd
 dist_man8_MANS = pppd.8
+check_PROGRAMS = \
+    utest_chap
+
+utest_chap_SOURCES = chap_ms.c pppcrypt.c utils.c
+utest_chap_CPPFLAGS = -DUNIT_TEST
+utest_chap_LDFLAGS =
+
+TESTS = $(check_PROGRAMS)
+
 if WITH_SRP
 sbin_PROGRAMS += srp-entry
 dist_man8_MANS += srp-entry.8
@@ -129,18 +138,27 @@ endif
 
 if !WITH_OPENSSL
 pppd_SOURCES += md5.c md4.c sha1.c
+utest_chap_SOURCES += md5.c md4.c sha1.c
 else
 pppd_CPPFLAGS += $(OPENSSL_INCLUDES)
 pppd_LDFLAGS += $(OPENSSL_LDFLAGS)
+
+utest_chap_CPPFLAGS += $(OPENSSL_INCLUDES)
+utest_chap_LDFLAGS += $(OPENSSL_LDFLAGS)
+utest_chap_LDADD = $(OPENSSL_LIBS)
+
 pppd_LIBS += $(OPENSSL_LIBS)
 if !OPENSSL_HAVE_SHA
 pppd_SOURCES += sha1.c
+utest_chap_SOURCES += sha1.c
 endif
 if !OPENSSL_HAVE_MD4
 pppd_SOURCES += md4.c
+utest_chap_SOURCES += md4.c
 endif
 if !OPENSSL_HAVE_MD5
 pppd_SOURCES += md5.c
+utest_chap_SOURCES += md5.c
 endif
 endif
 
index 2f31bb5da5f11dfe4ec2ab238334f0c231c75595..c6e4b34bd60001ab30b05189726f3550c95a5fcb 100644 (file)
 #include "magic.h"
 #include "mppe.h"
 
+#ifdef UNIT_TEST
+#undef MPPE
+#endif
+
 static void    ascii2unicode (char[], int, u_char[]);
 static void    NTPasswordHash (u_char *, int, u_char[MD4_SIGNATURE_SIZE]);
 static void    ChallengeResponse (u_char *, u_char *, u_char[24]);
@@ -841,6 +845,7 @@ static struct chap_digest_type chapms2_digest = {
        chapms_handle_failure,
 };
 
+#ifndef UNIT_TEST
 void
 chapms_init(void)
 {
@@ -848,5 +853,96 @@ chapms_init(void)
        chap_register_digest(&chapms2_digest);
        add_options(chapms_option_list);
 }
+#else
+
+#include <time.h>
+
+int debug = 1;
+int error_count = 0;
+int unsuccess = 0;
+
+void random_bytes(unsigned char *bytes, int len)
+{
+    int i = 0;
+    srand(time(NULL));
+    while (i < len) {
+        bytes[i++] = (unsigned char) rand();
+    }
+}
+
+
+int test_chap_v1(void) {
+    char *secret = "TestPassword";
+
+    unsigned char challenge[8] = {
+        0x6c, 0x8d, 0x4b, 0xa1, 0x2b, 0x5c, 0x13, 0xc3
+    };
+    unsigned char response[MS_CHAP_RESPONSE_LEN] = {
+    };
+    unsigned char result[MS_CHAP_RESPONSE_LEN] = {
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+        0x91, 0x09, 0x61, 0x5a, 0x0c, 0xac, 0xac, 0x55,
+        0x1f, 0x60, 0xe2, 0x9c, 0x00, 0xac, 0x24, 0xda,
+        0x6e, 0xa5, 0x7b, 0xdb, 0x1d, 0x6a, 0x17, 0xc5,
+        0x01
+    };
+
+    ChapMS(challenge, secret, strlen(secret), response);
+    return memcmp(response, result, MS_CHAP_RESPONSE_LEN);
+}
+
+int test_chap_v2(void) {
+    char *secret = "clientPass";
+    char *name = "User";
+
+    char saresponse[MS_AUTH_RESPONSE_LENGTH+1];
+    char *saresult = "407A5589115FD0D6209F510FE9C04566932CDA56";
+
+    unsigned char authenticator[16] = {
+        0x5B, 0x5D, 0x7C, 0x7D, 0x7B, 0x3F, 0x2F, 0x3E,
+        0x3C, 0x2C, 0x60, 0x21, 0x32, 0x26, 0x26, 0x28
+    };
+    unsigned char peerchallenge[16] = {
+        0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, 0x26, 0x2A,
+        0x28, 0x29, 0x5F, 0x2B, 0x3A, 0x33, 0x7C, 0x7E
+    };
+    unsigned char result[MS_CHAP_NTRESP_LEN] = {
+        0x82, 0x30, 0x9E, 0xCD, 0x8D, 0x70, 0x8B, 0x5E,
+        0xA0, 0x8F, 0xAA, 0x39, 0x81, 0xCD, 0x83, 0x54,
+        0x42, 0x33, 0x11, 0x4A, 0x3D, 0x85, 0xD6, 0xDF
+    };
+
+    unsigned char response[MS_CHAP2_RESPONSE_LEN] = {
+    };
+
+       ChapMS2(authenticator, peerchallenge, name,
+               secret, strlen(secret), response,
+               (unsigned char *)saresponse, MS_CHAP2_AUTHENTICATOR);
+
+    return memcmp(&response[MS_CHAP2_NTRESP], result, MS_CHAP2_NTRESP_LEN) ||
+        strncmp(saresponse, saresult, MS_AUTH_RESPONSE_LENGTH);
+}
+
+int main(int argc, char *argv[]) {
+
+    if (test_chap_v1()) {
+        printf("CHAPv1 failed\n");
+        return -1;
+    }
+
+    if (test_chap_v2()) {
+        printf("CHAPv2 failed\n");
+        return -1;
+    }
+
+    printf("Success\n");
+    return 0;
+}
+
+#endif  /* UNIT_TEST */
+
 
 #endif /* CHAPMS */
index d66df1ff5bf88f7c04d1333b039c35b906033b6b..fd213c2044725993565f8352d9e6e05959d70292 100644 (file)
@@ -329,6 +329,7 @@ vslprintf(char *buf, int buflen, char *fmt, va_list args)
                    OUTCHAR(c);
            }
            continue;
+#ifndef UNIT_TEST
        case 'P':               /* print PPP packet */
            bufinfo.ptr = buf;
            bufinfo.len = buflen + 1;
@@ -338,6 +339,7 @@ vslprintf(char *buf, int buflen, char *fmt, va_list args)
            buf = bufinfo.ptr;
            buflen = bufinfo.len - 1;
            continue;
+#endif
        case 'B':
            p = va_arg(args, unsigned char *);
            for (n = prec; n > 0; --n) {
@@ -432,6 +434,7 @@ log_packet(u_char *p, int len, char *prefix, int level)
 }
 #endif /* unused */
 
+#ifndef UNIT_TEST
 /*
  * format_packet - make a readable representation of a packet,
  * calling `printer(arg, format, ...)' to output it.
@@ -477,6 +480,7 @@ format_packet(u_char *p, int len, printer_func printer, void *arg)
     else
        printer(arg, "%.*B", len, p);
 }
+#endif  /* UNIT_TEST */
 
 /*
  * init_pr_log, end_pr_log - initialize and finish use of pr_log.
@@ -603,6 +607,7 @@ logit(int level, char *fmt, va_list args)
     log_write(level, buf);
 }
 
+#ifndef UNIT_TEST
 static void
 log_write(int level, char *buf)
 {
@@ -617,6 +622,13 @@ log_write(int level, char *buf)
            log_to_fd = -1;
     }
 }
+#else
+static void
+log_write(int level, char *buf)
+{
+    printf("<%d>: %s\n", level, buf);
+}
+#endif
 
 /*
  * fatal - log an error message and die horribly.
@@ -631,7 +643,11 @@ fatal(char *fmt, ...)
     logit(LOG_ERR, fmt, pvar);
     va_end(pvar);
 
+#ifndef UNIT_TEST
     die(1);                    /* as promised */
+#else
+    exit(-1);
+#endif
 }
 
 /*
@@ -735,6 +751,8 @@ dump_packet(const char *tag, unsigned char *p, int len)
     dbglog("%s %P", tag, p, len);
 }
 
+
+#ifndef UNIT_TEST
 /*
  * complete_read - read a full `count' bytes from fd,
  * unless end-of-file or an error other than EINTR is encountered.
@@ -760,6 +778,7 @@ complete_read(int fd, void *buf, size_t count)
        }
        return done;
 }
+#endif
 
 /* Procedures for locking the serial device using a lock file. */
 #ifndef LOCK_DIR