Add documentation for running unit tests under Valgrind
[openssl.git] / test / md2test.c
index 5d94e5f88ad5ab750d9b6d970578abd3242a863d..47f55b75e39f8936e554cec50d85ae79a4c78b34 100644 (file)
@@ -1,25 +1,21 @@
 /*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
-#include "../e_os.h"
+#include <openssl/provider.h>
+#include "internal/nelem.h"
+#include "testutil.h"
 
-#ifdef OPENSSL_NO_MD2
-int main(int argc, char *argv[])
-{
-    printf("No MD2 support\n");
-    return (0);
-}
-#else
+static OSSL_PROVIDER *prov = NULL;
+
+#ifndef OPENSSL_NO_MD2
 # include <openssl/evp.h>
 # include <openssl/md2.h>
 
@@ -35,7 +31,6 @@ static char *test[] = {
     "abcdefghijklmnopqrstuvwxyz",
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
     "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
-    NULL,
 };
 
 static char *ret[] = {
@@ -48,42 +43,39 @@ static char *ret[] = {
     "d5976f79d83d3a0dc9806c3c66f3efd8",
 };
 
-static char *pt(unsigned char *md);
-int main(int argc, char *argv[])
+static int test_md2(int n)
 {
-    int i, err = 0;
-    char **P, **R;
-    char *p;
+    char buf[80];
     unsigned char md[MD2_DIGEST_LENGTH];
+    int i;
 
-    P = test;
-    R = ret;
-    i = 1;
-    while (*P != NULL) {
-        EVP_Digest((unsigned char *)*P, strlen(*P), md, NULL, EVP_md2(),
-                   NULL);
-        p = pt(md);
-        if (strcmp(p, *R) != 0) {
-            printf("error calculating MD2 on '%s'\n", *P);
-            printf("got %s instead of %s\n", p, *R);
-            err++;
-        } else
-            printf("test %d ok\n", i);
-        i++;
-        R++;
-        P++;
-    }
-    EXIT(err);
-    return err;
+    if (!TEST_true(EVP_Digest((unsigned char *)test[n], strlen(test[n]),
+                                 md, NULL, EVP_md2(), NULL)))
+        return 0;
+
+    for (i = 0; i < MD2_DIGEST_LENGTH; i++)
+        sprintf(&(buf[i * 2]), "%02x", md[i]);
+    if (!TEST_str_eq(buf, ret[n]))
+        return 0;
+    return 1;
 }
+#endif
 
-static char *pt(unsigned char *md)
+int global_init(void)
 {
-    int i;
-    static char buf[80];
+    prov = OSSL_PROVIDER_load(NULL, "legacy");
 
-    for (i = 0; i < MD2_DIGEST_LENGTH; i++)
-        sprintf(&(buf[i * 2]), "%02x", md[i]);
-    return (buf);
+    return prov != NULL;
+}
+void cleanup_tests(void)
+{
+    OSSL_PROVIDER_unload(prov);
 }
+
+int setup_tests(void)
+{
+#ifndef OPENSSL_NO_MD2
+    ADD_ALL_TESTS(test_md2, OSSL_NELEM(test));
 #endif
+    return 1;
+}