Fix line numbering for errors.
[openssl.git] / test / bntest.c
index f9e36869c21469541002c8fc28c78bb1864c3b22..ccedd7a7ec93c9e6c3a9b9716b8b50260767bef3 100644 (file)
@@ -19,7 +19,6 @@
 #include <openssl/err.h>
 #include <openssl/rand.h>
 #include "testutil.h"
-#include "test_main_custom.h"
 
 /*
  * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
@@ -50,7 +49,8 @@ typedef struct pair_st {
 } PAIR;
 
 typedef struct stanza_st {
-    int start;
+    int curr;                           /* Current line in file */
+    int start;                          /* Line where test starts */
     int numpairs;
     PAIR pairs[MAXPAIRS];
 } STANZA;
@@ -68,9 +68,15 @@ typedef struct mpitest_st {
 
 static const int NUM0 = 100;           /* number of tests */
 static const int NUM1 = 50;            /* additional tests for some functions */
-static FILE *fp;
+static BIO *fp;
 static BN_CTX *ctx;
 
+/*
+ * Polynomial coefficients used in GFM tests.
+ */
+static int p0[] = { 163, 7, 6, 3, 0, -1 };
+static int p1[] = { 193, 15, 0, -1 };
+
 
 /*
  * Look for |key| in the stanza and return it or NULL if not found.
@@ -107,13 +113,12 @@ static BIGNUM *getBN(STANZA *s, const char *attribute)
     BIGNUM *ret = NULL;
 
     if ((hex = findattr(s, attribute)) == NULL) {
-        fprintf(stderr, "Can't find %s in test at line %d\n",
-                attribute, s->start);
+        TEST_error("Can't find %s in test at line %d", attribute, s->start);
         return NULL;
     }
 
     if (parseBN(&ret, hex) != (int)strlen(hex)) {
-        fprintf(stderr, "Could not decode '%s'.\n", hex);
+        TEST_error("Could not decode '%s'", hex);
         return NULL;
     }
     return ret;
@@ -121,14 +126,12 @@ static BIGNUM *getBN(STANZA *s, const char *attribute)
 
 static int getint(STANZA *s, int *out, const char *attribute)
 {
-    BIGNUM *ret = getBN(s, attribute);
+    BIGNUM *ret;
     BN_ULONG word;
     int st = 0;
 
-    if (ret == NULL)
-        goto err;
-
-    if ((word = BN_get_word(ret)) > INT_MAX)
+    if (!TEST_ptr(ret = getBN(s, attribute))
+            || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
         goto err;
 
     *out = (int)word;
@@ -140,25 +143,11 @@ err:
 
 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
 {
-    char *exstr = NULL;
-    char *actstr = NULL;
-
     if (BN_cmp(expected, actual) == 0)
         return 1;
 
-    exstr = BN_bn2hex(expected);
-    actstr = BN_bn2hex(actual);
-    if (exstr == NULL || actstr == NULL)
-        goto err;
-
-    fprintf(stderr, "Got %s =\n", op);
-    fprintf(stderr, "\t%s\n", actstr);
-    fprintf(stderr, "wanted:\n");
-    fprintf(stderr, "\t%s\n", exstr);
-
-err:
-    OPENSSL_free(exstr);
-    OPENSSL_free(actstr);
+    TEST_error("unexpected %s value", op);
+    TEST_BN_eq(expected, actual);
     return 0;
 }
 
@@ -177,19 +166,20 @@ static int rand_neg(void)
 
 static int test_sub()
 {
-    BIGNUM *a, *b, *c;
-    int i;
+    BIGNUM *a = NULL, *b = NULL, *c = NULL;
+    int i, st = 0;
 
-    a = BN_new();
-    b = BN_new();
-    c = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(c = BN_new()))
+        goto err;
 
     for (i = 0; i < NUM0 + NUM1; i++) {
         if (i < NUM1) {
             BN_bntest_rand(a, 512, 0, 0);
             BN_copy(b, a);
-            if (BN_set_bit(a, i) == 0)
-                return 0;
+            if (!TEST_int_ne(BN_set_bit(a, i), 0))
+                goto err;
             BN_add_word(b, i);
         } else {
             BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
@@ -199,30 +189,31 @@ static int test_sub()
         BN_sub(c, a, b);
         BN_add(c, c, b);
         BN_sub(c, c, a);
-        if (!BN_is_zero(c)) {
-            printf("Subtract test failed!\n");
-            return 0;
-        }
+        if (!TEST_BN_eq_zero(c))
+            goto err;
     }
+    st = 1;
+err:
     BN_free(a);
     BN_free(b);
     BN_free(c);
-    return 1;
+    return st;
 }
 
 
 static int test_div_recip()
 {
-    BIGNUM *a, *b, *c, *d, *e;
-    BN_RECP_CTX *recp;
-    int i;
+    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+    BN_RECP_CTX *recp = NULL;
+    int st = 0, i;
 
-    recp = BN_RECP_CTX_new();
-    a = BN_new();
-    b = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(recp = BN_RECP_CTX_new()))
+        goto err;
 
     for (i = 0; i < NUM0 + NUM1; i++) {
         if (i < NUM1) {
@@ -239,36 +230,32 @@ static int test_div_recip()
         BN_mul(e, d, b, ctx);
         BN_add(d, e, c);
         BN_sub(d, d, a);
-        if (!BN_is_zero(d)) {
-            printf("Reciprocal division test failed!\n");
-            printf("a=");
-            BN_print_fp(stdout, a);
-            printf("\nb=");
-            BN_print_fp(stdout, b);
-            printf("\n");
-            return 0;
-        }
+        if (!TEST_BN_eq_zero(d))
+            goto err;
     }
+    st = 1;
+err:
     BN_free(a);
     BN_free(b);
     BN_free(c);
     BN_free(d);
     BN_free(e);
     BN_RECP_CTX_free(recp);
-    return 1;
+    return st;
 }
 
 
 static int test_mod()
 {
-    BIGNUM *a, *b, *c, *d, *e;
-    int i;
+    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+    int st = 0, i;
 
-    a = BN_new();
-    b = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new()))
+        goto err;
 
     BN_bntest_rand(a, 1024, 0, 0);
     for (i = 0; i < NUM0; i++) {
@@ -278,17 +265,17 @@ static int test_mod()
         BN_mod(c, a, b, ctx);
         BN_div(d, e, a, b, ctx);
         BN_sub(e, e, c);
-        if (!BN_is_zero(e)) {
-            printf("Modulo test failed!\n");
-            return 0;
-        }
+        if (!TEST_BN_eq_zero(e))
+            goto err;
     }
+    st = 1;
+err:
     BN_free(a);
     BN_free(b);
     BN_free(c);
     BN_free(d);
     BN_free(e);
-    return 1;
+    return st;
 }
 
 static const char *bn1strings[] = {
@@ -339,11 +326,10 @@ static char *glue(const char *list[])
 
     for (i = 0; list[i] != NULL; i++)
         len += strlen(list[i]);
-    p = save = OPENSSL_malloc(len + 1);
-    if (p != NULL) {
-        for (i = 0; list[i] != NULL; i++)
-            p += strlen(strcpy(p, list[i]));
-    }
+    if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
+            return NULL;
+    for (i = 0; list[i] != NULL; i++)
+        p += strlen(strcpy(p, list[i]));
     return save;
 }
 
@@ -353,30 +339,31 @@ static char *glue(const char *list[])
  */
 static int test_modexp_mont5()
 {
-    BIGNUM *a, *p, *m, *d, *e, *b, *n, *c;
-    BN_MONT_CTX *mont;
+    BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
+    BIGNUM *b = NULL, *n = NULL, *c = NULL;
+    BN_MONT_CTX *mont = NULL;
     char *bigstring;
+    int st = 0;
 
-    a = BN_new();
-    p = BN_new();
-    m = BN_new();
-    d = BN_new();
-    e = BN_new();
-    b = BN_new();
-    n = BN_new();
-    c = BN_new();
-    mont = BN_MONT_CTX_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(p = BN_new())
+            || !TEST_ptr(m = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(n = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(mont = BN_MONT_CTX_new()))
+        goto err;
 
     BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
     /* Zero exponent */
     BN_bntest_rand(a, 1024, 0, 0);
     BN_zero(p);
-    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
-        return 0;
-    if (!BN_is_one(d)) {
-        printf("Modular exponentiation test failed!\n");
-        return 0;
-    }
+    if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
+        goto err;
+    if (!TEST_BN_eq_one(d))
+        goto err;
 
     /* Regression test for carry bug in mulx4x_mont */
     BN_hex2bn(&a,
@@ -397,11 +384,8 @@ static int test_modexp_mont5()
     BN_MONT_CTX_set(mont, n, ctx);
     BN_mod_mul_montgomery(c, a, b, mont, ctx);
     BN_mod_mul_montgomery(d, b, a, mont, ctx);
-    if (BN_cmp(c, d)) {
-        fprintf(stderr, "Montgomery multiplication test failed:"
-                        " a*b != b*a.\n");
-        return 0;
-    }
+    if (!TEST_BN_eq(c, d))
+        goto err;
 
     /* Regression test for carry bug in sqr[x]8x_mont */
     bigstring = glue(bn1strings);
@@ -415,21 +399,16 @@ static int test_modexp_mont5()
     BN_MONT_CTX_set(mont, n, ctx);
     BN_mod_mul_montgomery(c, a, a, mont, ctx);
     BN_mod_mul_montgomery(d, a, b, mont, ctx);
-    if (BN_cmp(c, d)) {
-        fprintf(stderr, "Montgomery multiplication test failed:"
-                        " a**2 != a*a.\n");
-        return 0;
-    }
+    if (!TEST_BN_eq(c, d))
+        goto err;
 
     /* Zero input */
     BN_bntest_rand(p, 1024, 0, 0);
     BN_zero(a);
-    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
-        return 0;
-    if (!BN_is_zero(d)) {
-        fprintf(stderr, "Modular exponentiation test failed!\n");
-        return 0;
-    }
+    if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
+            || !TEST_BN_eq_zero(d))
+        goto err;
+
     /*
      * Craft an input whose Montgomery representation is 1, i.e., shorter
      * than the modulus m, in order to test the const time precomputation
@@ -437,26 +416,22 @@ static int test_modexp_mont5()
      */
     BN_one(a);
     BN_MONT_CTX_set(mont, m, ctx);
-    if (!BN_from_montgomery(e, a, mont, ctx))
-        return 0;
-    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
-        return 0;
-    if (!BN_mod_exp_simple(a, e, p, m, ctx))
-        return 0;
-    if (BN_cmp(a, d) != 0) {
-        printf("Modular exponentiation test failed!\n");
-        return 0;
-    }
+    if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
+            || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
+            || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
+            || !TEST_BN_eq(a, d))
+        goto err;
+
     /* Finally, some regular test vectors. */
     BN_bntest_rand(e, 1024, 0, 0);
-    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
-        return 0;
-    if (!BN_mod_exp_simple(a, e, p, m, ctx))
-        return 0;
-    if (BN_cmp(a, d) != 0) {
-        printf("Modular exponentiation test failed!\n");
-        return 0;
-    }
+    if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
+            || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
+            || !TEST_BN_eq(a, d))
+        goto err;
+
+    st = 1;
+
+err:
     BN_MONT_CTX_free(mont);
     BN_free(a);
     BN_free(p);
@@ -466,18 +441,19 @@ static int test_modexp_mont5()
     BN_free(b);
     BN_free(n);
     BN_free(c);
-    return 1;
+    return st;
 }
 
 #ifndef OPENSSL_NO_EC2M
 static int test_gf2m_add()
 {
-    BIGNUM *a, *b, *c;
+    BIGNUM *a = NULL, *b = NULL, *c = NULL;
     int i, st = 0;
 
-    a = BN_new();
-    b = BN_new();
-    c = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(c = BN_new()))
+        goto err;
 
     for (i = 0; i < NUM0; i++) {
         BN_rand(a, 512, 0, 0);
@@ -486,17 +462,13 @@ static int test_gf2m_add()
         b->neg = rand_neg();
         BN_GF2m_add(c, a, b);
         /* Test that two added values have the correct parity. */
-        if ((BN_is_odd(a) && BN_is_odd(c))
-            || (!BN_is_odd(a) && !BN_is_odd(c))) {
-            printf("GF(2^m) addition test (a) failed!\n");
+        if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
+                        || (!BN_is_odd(a) && !BN_is_odd(c))))
             goto err;
-        }
         BN_GF2m_add(c, c, c);
         /* Test that c + c = 0. */
-        if (!BN_is_zero(c)) {
-            printf("GF(2^m) addition test (b) failed!\n");
+        if (!TEST_BN_eq_zero(c))
             goto err;
-        }
     }
     st = 1;
  err:
@@ -508,17 +480,16 @@ static int test_gf2m_add()
 
 static int test_gf2m_mod()
 {
-    static int p0[] = { 163, 7, 6, 3, 0, -1 };
-    static int p1[] = { 193, 15, 0, -1 };
-    BIGNUM *a, *b[2], *c, *d, *e;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
     int i, j, st = 0;
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -530,10 +501,8 @@ static int test_gf2m_mod()
             BN_GF2m_add(d, a, c);
             BN_GF2m_mod(e, d, b[j]);
             /* Test that a + (a mod p) mod p == 0. */
-            if (!BN_is_zero(e)) {
-                printf("GF(2^m) modulo test failed!\n");
+            if (!TEST_BN_eq_zero(e))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -549,20 +518,20 @@ static int test_gf2m_mod()
 
 static int test_gf2m_mul()
 {
-    BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
+    BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
+    BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
-
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
-    f = BN_new();
-    g = BN_new();
-    h = BN_new();
+
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(f = BN_new())
+            || !TEST_ptr(g = BN_new())
+            || !TEST_ptr(h = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -579,13 +548,12 @@ static int test_gf2m_mul()
             BN_GF2m_add(f, e, g);
             BN_GF2m_add(f, f, h);
             /* Test that (a+d)*c = a*c + d*c. */
-            if (!BN_is_zero(f)) {
-                printf("GF(2^m) modular multiplication test failed!\n");
+            if (!TEST_BN_eq_zero(f))
                 goto err;
-            }
         }
     }
     st = 1;
+
  err:
     BN_free(a);
     BN_free(b[0]);
@@ -601,16 +569,15 @@ static int test_gf2m_mul()
 
 static int test_gf2m_sqr()
 {
-    BIGNUM *a, *b[2], *c, *d;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -623,10 +590,8 @@ static int test_gf2m_sqr()
             BN_GF2m_mod_mul(d, a, d, b[j], ctx);
             BN_GF2m_add(d, c, d);
             /* Test that a*a = a^2. */
-            if (!BN_is_zero(d)) {
-                printf("GF(2^m) modular squaring test failed!\n");
+            if (!TEST_BN_eq_zero(d))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -641,16 +606,15 @@ static int test_gf2m_sqr()
 
 static int test_gf2m_modinv()
 {
-    BIGNUM *a, *b[2], *c, *d;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -661,10 +625,8 @@ static int test_gf2m_modinv()
             BN_GF2m_mod_inv(c, a, b[j], ctx);
             BN_GF2m_mod_mul(d, a, c, b[j], ctx);
             /* Test that ((1/a)*a) = 1. */
-            if (!BN_is_one(d)) {
-                printf("GF(2^m) modular inversion test failed!\n");
+            if (!TEST_BN_eq_one(d))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -679,18 +641,18 @@ static int test_gf2m_modinv()
 
 static int test_gf2m_moddiv()
 {
-    BIGNUM *a, *b[2], *c, *d, *e, *f;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
+    BIGNUM *e = NULL, *f = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
-    f = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(f = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -703,10 +665,8 @@ static int test_gf2m_moddiv()
             BN_GF2m_mod_mul(e, d, c, b[j], ctx);
             BN_GF2m_mod_div(f, a, e, b[j], ctx);
             /* Test that ((a/c)*c)/a = 1. */
-            if (!BN_is_one(f)) {
-                printf("GF(2^m) modular division test failed!\n");
+            if (!TEST_BN_eq_one(f))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -723,18 +683,18 @@ static int test_gf2m_moddiv()
 
 static int test_gf2m_modexp()
 {
-    BIGNUM *a, *b[2], *c, *d, *e, *f;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
+    BIGNUM *e = NULL, *f = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
-    f = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(f = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -751,10 +711,8 @@ static int test_gf2m_modexp()
             BN_GF2m_mod_exp(f, a, f, b[j], ctx);
             BN_GF2m_add(f, e, f);
             /* Test that a^(c+d)=a^c*a^d. */
-            if (!BN_is_zero(f)) {
-                printf("GF(2^m) modular exponentiation test failed!\n");
+            if (!TEST_BN_eq_zero(f))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -771,18 +729,18 @@ static int test_gf2m_modexp()
 
 static int test_gf2m_modsqrt()
 {
-    BIGNUM *a, *b[2], *c, *d, *e, *f;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
+    BIGNUM *e = NULL, *f = NULL;
     int i, j, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
-    f = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new())
+            || !TEST_ptr(f = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -795,10 +753,8 @@ static int test_gf2m_modsqrt()
             BN_GF2m_mod_sqr(e, d, b[j], ctx);
             BN_GF2m_add(f, c, e);
             /* Test that d^2 = a, where d = sqrt(a). */
-            if (!BN_is_zero(f)) {
-                printf("GF(2^m) modular square root test failed!\n");
+            if (!TEST_BN_eq_zero(f))
                 goto err;
-            }
         }
     }
     st = 1;
@@ -815,17 +771,17 @@ static int test_gf2m_modsqrt()
 
 static int test_gf2m_modsolvequad()
 {
-    BIGNUM *a, *b[2], *c, *d, *e;
+    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
+    BIGNUM *e = NULL;
     int i, j, s = 0, t, st = 0;
-    int p0[] = { 163, 7, 6, 3, 0, -1 };
-    int p1[] = { 193, 15, 0, -1 };
 
-    a = BN_new();
-    b[0] = BN_new();
-    b[1] = BN_new();
-    c = BN_new();
-    d = BN_new();
-    e = BN_new();
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b[0] = BN_new())
+            || !TEST_ptr(b[1] = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new())
+            || !TEST_ptr(e = BN_new()))
+        goto err;
 
     BN_GF2m_arr2poly(p0, b[0]);
     BN_GF2m_arr2poly(p1, b[1]);
@@ -843,18 +799,13 @@ static int test_gf2m_modsolvequad()
                 /*
                  * Test that solution of quadratic c satisfies c^2 + c = a.
                  */
-                if (!BN_is_zero(e)) {
-                    printf("GF(2^m) modular solve quadratic test failed!\n");
+                if (!TEST_BN_eq_zero(e))
                     goto err;
-                }
-
             }
         }
     }
-    if (s == 0) {
-        printf("All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
-                NUM0);
-        printf("this is very unlikely and probably indicates an error.\n");
+    if (!TEST_int_ge(s, 0)) {
+        TEST_info("%d tests found no roots; probably an error", NUM0);
         goto err;
     }
     st = 1;
@@ -871,16 +822,13 @@ static int test_gf2m_modsolvequad()
 
 static int test_kronecker()
 {
-    BIGNUM *a, *b, *r, *t;
-    int i;
-    int legendre, kronecker;
-    int st = 0;
+    BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
+    int i, legendre, kronecker, st = 0;
 
-    a = BN_new();
-    b = BN_new();
-    r = BN_new();
-    t = BN_new();
-    if (a == NULL || b == NULL || r == NULL || t == NULL)
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(r = BN_new())
+            || !TEST_ptr(t = BN_new()))
         goto err;
 
     /*
@@ -893,27 +841,27 @@ static int test_kronecker()
      * is prime but whether BN_kronecker works.)
      */
 
-    if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL))
+    if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
         goto err;
     b->neg = rand_neg();
 
     for (i = 0; i < NUM0; i++) {
-        if (!BN_bntest_rand(a, 512, 0, 0))
+        if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
             goto err;
         a->neg = rand_neg();
 
         /* t := (|b|-1)/2  (note that b is odd) */
-        if (!BN_copy(t, b))
+        if (!TEST_true(BN_copy(t, b)))
             goto err;
         t->neg = 0;
-        if (!BN_sub_word(t, 1))
+        if (!TEST_true(BN_sub_word(t, 1)))
             goto err;
-        if (!BN_rshift1(t, t))
+        if (!TEST_true(BN_rshift1(t, t)))
             goto err;
         /* r := a^t mod b */
         b->neg = 0;
 
-        if (!BN_mod_exp_recp(r, a, t, b, ctx))
+        if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
             goto err;
         b->neg = 1;
 
@@ -922,30 +870,23 @@ static int test_kronecker()
         else if (BN_is_zero(r))
             legendre = 0;
         else {
-            if (!BN_add_word(r, 1))
+            if (!TEST_true(BN_add_word(r, 1)))
                 goto err;
-            if (0 != BN_ucmp(r, b)) {
-                printf("Legendre symbol computation failed\n");
+            if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
+                TEST_info("Legendre symbol computation failed");
                 goto err;
             }
             legendre = -1;
         }
 
-        kronecker = BN_kronecker(a, b, ctx);
-        if (kronecker < -1)
+        if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
             goto err;
         /* we actually need BN_kronecker(a, |b|) */
         if (a->neg && b->neg)
             kronecker = -kronecker;
 
-        if (legendre != kronecker) {
-            printf("legendre != kronecker; a = ");
-            BN_print_fp(stdout, a);
-            printf(", b = ");
-            BN_print_fp(stdout, b);
-            printf("\n");
+        if (!TEST_int_eq(legendre, kronecker))
             goto err;
-        }
     }
 
     st = 1;
@@ -959,21 +900,21 @@ static int test_kronecker()
 
 static int file_sum(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *b = getBN(s, "B");
-    BIGNUM *sum = getBN(s, "Sum");
-    BIGNUM *ret = BN_new();
+    BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
     BN_ULONG b_word;
     int st = 0;
 
-    if (a == NULL || b == NULL || sum == NULL || ret == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(b = getBN(s, "B"))
+            || !TEST_ptr(sum = getBN(s, "Sum"))
+            || !TEST_ptr(ret = BN_new()))
         goto err;
 
-    if (!BN_add(ret, a, b)
+    if (!TEST_true(BN_add(ret, a, b))
             || !equalBN("A + B", sum, ret)
-            || !BN_sub(ret, sum, a)
+            || !TEST_true(BN_sub(ret, sum, a))
             || !equalBN("Sum - A", b, ret)
-            || !BN_sub(ret, sum, b)
+            || !TEST_true(BN_sub(ret, sum, b))
             || !equalBN("Sum - B", a, ret))
         goto err;
 
@@ -982,23 +923,23 @@ static int file_sum(STANZA *s)
      * or when |r| and |b| point to the same BIGNUM.
      * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
      */
-    if (!BN_copy(ret, a)
-            || !BN_add(ret, ret, b)
+    if (!TEST_true(BN_copy(ret, a))
+            || !TEST_true(BN_add(ret, ret, b))
             || !equalBN("A + B (r is a)", sum, ret)
-            || !BN_copy(ret, b)
-            || !BN_add(ret, a, ret)
+            || !TEST_true(BN_copy(ret, b))
+            || !TEST_true(BN_add(ret, a, ret))
             || !equalBN("A + B (r is b)", sum, ret)
-            || !BN_copy(ret, sum)
-            || !BN_sub(ret, ret, a)
+            || !TEST_true(BN_copy(ret, sum))
+            || !TEST_true(BN_sub(ret, ret, a))
             || !equalBN("Sum - A (r is a)", b, ret)
-            || !BN_copy(ret, a)
-            || !BN_sub(ret, sum, ret)
+            || !TEST_true(BN_copy(ret, a))
+            || !TEST_true(BN_sub(ret, sum, ret))
             || !equalBN("Sum - A (r is b)", b, ret)
-            || !BN_copy(ret, sum)
-            || !BN_sub(ret, ret, b)
+            || !TEST_true(BN_copy(ret, sum))
+            || !TEST_true(BN_sub(ret, ret, b))
             || !equalBN("Sum - B (r is a)", a, ret)
-            || !BN_copy(ret, b)
-            || !BN_sub(ret, sum, ret)
+            || !TEST_true(BN_copy(ret, b))
+            || !TEST_true(BN_sub(ret, sum, ret))
             || !equalBN("Sum - B (r is b)", a, ret))
         goto err;
 
@@ -1010,11 +951,11 @@ static int file_sum(STANZA *s)
      * TODO: test that.
      */
     if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
-        if (!BN_uadd(ret, a, b)
+        if (!TEST_true(BN_uadd(ret, a, b))
                 || !equalBN("A +u B", sum, ret)
-                || !BN_usub(ret, sum, a)
+                || !TEST_true(BN_usub(ret, sum, a))
                 || !equalBN("Sum -u A", b, ret)
-                || !BN_usub(ret, sum, b)
+                || !TEST_true(BN_usub(ret, sum, b))
                 || !equalBN("Sum -u B", a, ret))
             goto err;
         /*
@@ -1022,23 +963,23 @@ static int file_sum(STANZA *s)
          * BIGNUM, or when |r| and |b| point to the same BIGNUM.
          * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
          */
-        if (!BN_copy(ret, a)
-                || !BN_uadd(ret, ret, b)
+        if (!TEST_true(BN_copy(ret, a))
+                || !TEST_true(BN_uadd(ret, ret, b))
                 || !equalBN("A +u B (r is a)", sum, ret)
-                || !BN_copy(ret, b)
-                || !BN_uadd(ret, a, ret)
+                || !TEST_true(BN_copy(ret, b))
+                || !TEST_true(BN_uadd(ret, a, ret))
                 || !equalBN("A +u B (r is b)", sum, ret)
-                || !BN_copy(ret, sum)
-                || !BN_usub(ret, ret, a)
+                || !TEST_true(BN_copy(ret, sum))
+                || !TEST_true(BN_usub(ret, ret, a))
                 || !equalBN("Sum -u A (r is a)", b, ret)
-                || !BN_copy(ret, a)
-                || !BN_usub(ret, sum, ret)
+                || !TEST_true(BN_copy(ret, a))
+                || !TEST_true(BN_usub(ret, sum, ret))
                 || !equalBN("Sum -u A (r is b)", b, ret)
-                || !BN_copy(ret, sum)
-                || !BN_usub(ret, ret, b)
+                || !TEST_true(BN_copy(ret, sum))
+                || !TEST_true(BN_usub(ret, ret, b))
                 || !equalBN("Sum -u B (r is a)", a, ret)
-                || !BN_copy(ret, b)
-                || !BN_usub(ret, sum, ret)
+                || !TEST_true(BN_copy(ret, b))
+                || !TEST_true(BN_usub(ret, sum, ret))
                 || !equalBN("Sum -u B (r is b)", a, ret))
             goto err;
     }
@@ -1048,11 +989,11 @@ static int file_sum(STANZA *s)
      */
     b_word = BN_get_word(b);
     if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
-        if (!BN_copy(ret, a)
-                || !BN_add_word(ret, b_word)
+        if (!TEST_true(BN_copy(ret, a))
+                || !TEST_true(BN_add_word(ret, b_word))
                 || !equalBN("A + B (word)", sum, ret)
-                || !BN_copy(ret, sum)
-                || !BN_sub_word(ret, b_word)
+                || !TEST_true(BN_copy(ret, sum))
+                || !TEST_true(BN_sub_word(ret, b_word))
                 || !equalBN("Sum - B (word)", a, ret))
             goto err;
     }
@@ -1068,41 +1009,41 @@ err:
 
 static int file_lshift1(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *lshift1 = getBN(s, "LShift1");
-    BIGNUM *zero = BN_new();
-    BIGNUM *ret = BN_new();
-    BIGNUM *two = BN_new();
-    BIGNUM *remainder = BN_new();
+    BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
+    BIGNUM *two = NULL, *remainder = NULL;
     int st = 0;
 
-    if (a == NULL || lshift1 == NULL || zero == NULL
-            || ret == NULL || two == NULL || remainder == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
+            || !TEST_ptr(zero = BN_new())
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(two = BN_new())
+            || !TEST_ptr(remainder = BN_new()))
         goto err;
 
     BN_zero(zero);
 
-    if (!BN_set_word(two, 2)
-            || !BN_add(ret, a, a)
+    if (!TEST_true(BN_set_word(two, 2))
+            || !TEST_true(BN_add(ret, a, a))
             || !equalBN("A + A", lshift1, ret)
-            || !BN_mul(ret, a, two, ctx)
+            || !TEST_true(BN_mul(ret, a, two, ctx))
             || !equalBN("A * 2", lshift1, ret)
-            || !BN_div(ret, remainder, lshift1, two, ctx)
+            || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
             || !equalBN("LShift1 / 2", a, ret)
             || !equalBN("LShift1 % 2", zero, remainder)
-            || !BN_lshift1(ret, a)
+            || !TEST_true(BN_lshift1(ret, a))
             || !equalBN("A << 1", lshift1, ret)
-            || !BN_rshift1(ret, lshift1)
+            || !TEST_true(BN_rshift1(ret, lshift1))
             || !equalBN("LShift >> 1", a, ret)
-            || !BN_rshift1(ret, lshift1)
+            || !TEST_true(BN_rshift1(ret, lshift1))
             || !equalBN("LShift >> 1", a, ret))
         goto err;
 
     /* Set the LSB to 1 and test rshift1 again. */
-    if (!BN_set_bit(lshift1, 0)
-            || !BN_div(ret, NULL /* rem */ , lshift1, two, ctx)
+    if (!TEST_true(BN_set_bit(lshift1, 0))
+            || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
             || !equalBN("(LShift1 | 1) / 2", a, ret)
-            || !BN_rshift1(ret, lshift1)
+            || !TEST_true(BN_rshift1(ret, lshift1))
             || !equalBN("(LShift | 1) >> 1", a, ret))
         goto err;
 
@@ -1120,18 +1061,16 @@ err:
 
 static int file_lshift(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *lshift = getBN(s, "LShift");
-    BIGNUM *ret = BN_new();
-    int n = 0;
-    int st = 0;
+    BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
+    int n = 0, st = 0;
 
-    if (a == NULL || lshift == NULL || ret == NULL || !getint(s, &n, "N"))
-        goto err;
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(lshift = getBN(s, "LShift"))
+            || !TEST_ptr(ret = BN_new()))
 
-    if (!BN_lshift(ret, a, n)
+    if (!TEST_true(BN_lshift(ret, a, n))
             || !equalBN("A << N", lshift, ret)
-            || !BN_rshift(ret, lshift, n)
+            || !TEST_true(BN_rshift(ret, lshift, n))
             || !equalBN("A >> N", a, ret))
         goto err;
 
@@ -1145,20 +1084,27 @@ err:
 
 static int file_rshift(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *rshift = getBN(s, "RShift");
-    BIGNUM *ret = BN_new();
-    int n = 0;
-    int st = 0;
+    BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
+    int n = 0, st = 0;
 
-    if (a == NULL || rshift == NULL || ret == NULL || !getint(s, &n, "N"))
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(rshift = getBN(s, "RShift"))
+            || !TEST_ptr(ret = BN_new())
+            || !getint(s, &n, "N"))
         goto err;
 
-    if (!BN_rshift(ret, a, n)
+    if (!TEST_true(BN_rshift(ret, a, n))
             || !equalBN("A >> N", rshift, ret))
         goto err;
 
+    /* If N == 1, try with rshift1 as well */
+    if (n == 1) {
+        if (!TEST_true(BN_rshift1(ret, a))
+                || !equalBN("A >> 1 (rshift1)", rshift, ret))
+            goto err;
+    }
     st = 1;
+
 err:
     BN_free(a);
     BN_free(rshift);
@@ -1168,55 +1114,49 @@ err:
 
 static int file_square(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *square = getBN(s, "Square");
-    BIGNUM *zero = BN_new();
-    BIGNUM *ret = BN_new();
-    BIGNUM *remainder = BN_new();
-    BIGNUM *tmp = NULL;
+    BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
+    BIGNUM *remainder = NULL, *tmp = NULL;
     int st = 0;
 
-    if (a == NULL || square == NULL || zero == NULL || ret == NULL
-            || remainder == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(square = getBN(s, "Square"))
+            || !TEST_ptr(zero = BN_new())
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(remainder = BN_new()))
         goto err;
 
     BN_zero(zero);
-
-    if (!BN_sqr(ret, a, ctx)
+    if (!TEST_true(BN_sqr(ret, a, ctx))
             || !equalBN("A^2", square, ret)
-            || !BN_mul(ret, a, a, ctx)
+            || !TEST_true(BN_mul(ret, a, a, ctx))
             || !equalBN("A * A", square, ret)
-            || !BN_div(ret, remainder, square, a, ctx)
+            || !TEST_true(BN_div(ret, remainder, square, a, ctx))
             || !equalBN("Square / A", a, ret)
             || !equalBN("Square % A", zero, remainder))
         goto err;
 
 #if HAVE_BN_SQRT
     BN_set_negative(a, 0);
-    if (!BN_sqrt(ret, square, ctx)
+    if (!TEST_true(BN_sqrt(ret, square, ctx))
             || !equalBN("sqrt(Square)", a, ret))
         goto err;
 
     /* BN_sqrt should fail on non-squares and negative numbers. */
-    if (!BN_is_zero(square)) {
-        tmp = BN_new();
-        if (tmp == NULL || !BN_copy(tmp, square))
+    if (!TEST_BN_eq_zero(square)) {
+        if (!TEST_ptr(tmp = BN_new())
+                || !TEST_true(BN_copy(tmp, square)))
             goto err;
         BN_set_negative(tmp, 1);
 
-        if (BN_sqrt(ret, tmp, ctx)) {
-            fprintf(stderr, "BN_sqrt succeeded on a negative number");
+        if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
             goto err;
-        }
         ERR_clear_error();
 
         BN_set_negative(tmp, 0);
         if (BN_add(tmp, tmp, BN_value_one()))
             goto err;
-        if (BN_sqrt(ret, tmp, ctx)) {
-            fprintf(stderr, "BN_sqrt succeeded on a non-square");
+        if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
             goto err;
-        }
         ERR_clear_error();
     }
 #endif
@@ -1234,26 +1174,26 @@ err:
 
 static int file_product(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *b = getBN(s, "B");
-    BIGNUM *product = getBN(s, "Product");
-    BIGNUM *ret = BN_new();
-    BIGNUM *remainder = BN_new();
-    BIGNUM *zero = BN_new();
+    BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
+    BIGNUM *remainder = NULL, *zero = NULL;
     int st = 0;
 
-    if (a == NULL || b == NULL || product == NULL || ret == NULL
-            || remainder == NULL || zero == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(b = getBN(s, "B"))
+            || !TEST_ptr(product = getBN(s, "Product"))
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(remainder = BN_new())
+            || !TEST_ptr(zero = BN_new()))
         goto err;
 
     BN_zero(zero);
 
-    if (!BN_mul(ret, a, b, ctx)
+    if (!TEST_true(BN_mul(ret, a, b, ctx))
             || !equalBN("A * B", product, ret)
-            || !BN_div(ret, remainder, product, a, ctx)
+            || !TEST_true(BN_div(ret, remainder, product, a, ctx))
             || !equalBN("Product / A", b, ret)
             || !equalBN("Product % A", zero, remainder)
-            || !BN_div(ret, remainder, product, b, ctx)
+            || !TEST_true(BN_div(ret, remainder, product, b, ctx))
             || !equalBN("Product / B", a, ret)
             || !equalBN("Product % B", zero, remainder))
         goto err;
@@ -1271,25 +1211,25 @@ err:
 
 static int file_quotient(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *b = getBN(s, "B");
-    BIGNUM *quotient = getBN(s, "Quotient");
-    BIGNUM *remainder = getBN(s, "Remainder");
-    BIGNUM *ret = BN_new();
-    BIGNUM *ret2 = BN_new();
-    BIGNUM *nnmod = BN_new();
+    BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
+    BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
     BN_ULONG b_word, ret_word;
     int st = 0;
 
-    if (a == NULL || b == NULL || quotient == NULL || remainder == NULL
-            || ret == NULL || ret2 == NULL || nnmod == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(b = getBN(s, "B"))
+            || !TEST_ptr(quotient = getBN(s, "Quotient"))
+            || !TEST_ptr(remainder = getBN(s, "Remainder"))
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(ret2 = BN_new())
+            || !TEST_ptr(nnmod = BN_new()))
         goto err;
 
-    if (!BN_div(ret, ret2, a, b, ctx)
+    if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
             || !equalBN("A / B", quotient, ret)
             || !equalBN("A % B", remainder, ret2)
-            || !BN_mul(ret, quotient, b, ctx)
-            || !BN_add(ret, ret, remainder)
+            || !TEST_true(BN_mul(ret, quotient, b, ctx))
+            || !TEST_true(BN_add(ret, ret, remainder))
             || !equalBN("Quotient * B + Remainder", a, ret))
         goto err;
 
@@ -1302,16 +1242,16 @@ static int file_quotient(STANZA *s)
         BN_ULONG remainder_word = BN_get_word(remainder);
 
         assert(remainder_word != (BN_ULONG)-1);
-        if (!BN_copy(ret, a))
+        if (!TEST_ptr(BN_copy(ret, a)))
             goto err;
         ret_word = BN_div_word(ret, b_word);
         if (ret_word != remainder_word) {
 #ifdef BN_DEC_FMT1
-            fprintf(stderr,
-                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\n",
+            TEST_error(
+                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
                     ret_word, remainder_word);
 #else
-            fprintf(stderr, "Got A %% B (word) mismatch\n");
+            TEST_error("Got A %% B (word) mismatch");
 #endif
             goto err;
         }
@@ -1321,11 +1261,11 @@ static int file_quotient(STANZA *s)
         ret_word = BN_mod_word(a, b_word);
         if (ret_word != remainder_word) {
 #ifdef BN_DEC_FMT1
-            fprintf(stderr,
-                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\n",
+            TEST_error(
+                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
                     ret_word, remainder_word);
 #else
-            fprintf(stderr, "Got A %% B (word) mismatch\n");
+            TEST_error("Got A %% B (word) mismatch");
 #endif
             goto err;
         }
@@ -1333,9 +1273,10 @@ static int file_quotient(STANZA *s)
 
     /* Test BN_nnmod. */
     if (!BN_is_negative(b)) {
-        if (!BN_copy(nnmod, remainder)
-                || (BN_is_negative(nnmod) && !BN_add(nnmod, nnmod, b))
-                || !BN_nnmod(ret, a, b, ctx)
+        if (!TEST_true(BN_copy(nnmod, remainder))
+                || (BN_is_negative(nnmod)
+                        && !TEST_true(BN_add(nnmod, nnmod, b)))
+                || !TEST_true(BN_nnmod(ret, a, b, ctx))
                 || !equalBN("A % B (non-negative)", nnmod, ret))
             goto err;
     }
@@ -1354,17 +1295,17 @@ err:
 
 static int file_modmul(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *b = getBN(s, "B");
-    BIGNUM *m = getBN(s, "M");
-    BIGNUM *mod_mul = getBN(s, "ModMul");
-    BIGNUM *ret = BN_new();
+    BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
     int st = 0;
 
-    if (a == NULL || b == NULL || m == NULL || mod_mul == NULL || ret == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(b = getBN(s, "B"))
+            || !TEST_ptr(m = getBN(s, "M"))
+            || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
+            || !TEST_ptr(ret = BN_new()))
         goto err;
 
-    if (!BN_mod_mul(ret, a, b, m, ctx)
+    if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
             || !equalBN("A * B (mod M)", mod_mul, ret))
         goto err;
 
@@ -1373,19 +1314,20 @@ static int file_modmul(STANZA *s)
         BN_MONT_CTX *mont = BN_MONT_CTX_new();
         BIGNUM *a_tmp = BN_new();
         BIGNUM *b_tmp = BN_new();
+
         if (mont == NULL || a_tmp == NULL || b_tmp == NULL
-                || !BN_MONT_CTX_set(mont, m, ctx)
-                || !BN_nnmod(a_tmp, a, m, ctx)
-                || !BN_nnmod(b_tmp, b, m, ctx)
-                || !BN_to_montgomery(a_tmp, a_tmp, mont, ctx)
-                || !BN_to_montgomery(b_tmp, b_tmp, mont, ctx)
-                || !BN_mod_mul_montgomery(ret, a_tmp, b_tmp, mont, ctx)
-                || !BN_from_montgomery(ret, ret, mont, ctx)
-                || !equalBN("A * B (mod M) (mont)", mod_mul, ret)) {
+                || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
+                || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
+                || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
+                || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
+                || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
+                || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
+                                                    mont, ctx))
+                || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
+                || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
             st = 0;
-        } else {
+        else
             st = 1;
-        }
         BN_MONT_CTX_free(mont);
         BN_free(a_tmp);
         BN_free(b_tmp);
@@ -1405,25 +1347,27 @@ err:
 
 static int file_modexp(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *e = getBN(s, "E");
-    BIGNUM *m = getBN(s, "M");
-    BIGNUM *mod_exp = getBN(s, "ModExp");
-    BIGNUM *ret = BN_new();
-    BIGNUM *b = NULL, *c = NULL, *d = BN_new();
+    BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
+    BIGNUM *b = NULL, *c = NULL, *d = NULL;
     int st = 0;
 
-    if (a == NULL || e == NULL || m == NULL || mod_exp == NULL || ret == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(e = getBN(s, "E"))
+            || !TEST_ptr(m = getBN(s, "M"))
+            || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(d = BN_new()))
         goto err;
 
-    if (!BN_mod_exp(ret, a, e, m, ctx)
+    if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
             || !equalBN("A ^ E (mod M)", mod_exp, ret))
         goto err;
 
     if (BN_is_odd(m)) {
-        if (!BN_mod_exp_mont(ret, a, e, m, ctx, NULL)
+        if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
                 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
-                || !BN_mod_exp_mont_consttime(ret, a, e, m, ctx, NULL)
+                || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
+                                                        ctx, NULL))
                 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
             goto err;
     }
@@ -1440,10 +1384,8 @@ static int file_modexp(STANZA *s)
         "0000000000000000000000000000000000000000000000000000000001");
     BN_mod_exp(d, a, b, c, ctx);
     BN_mul(e, a, a, ctx);
-    if (BN_cmp(d, e)) {
-        fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
+    if (!TEST_BN_eq(d, e))
         goto err;
-    }
 
     st = 1;
 err:
@@ -1460,16 +1402,16 @@ err:
 
 static int file_exp(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *e = getBN(s, "E");
-    BIGNUM *exp = getBN(s, "Exp");
-    BIGNUM *ret = BN_new();
+    BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
     int st = 0;
 
-    if (a == NULL || e == NULL || exp == NULL || ret == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(e = getBN(s, "E"))
+            || !TEST_ptr(exp = getBN(s, "Exp"))
+            || !TEST_ptr(ret = BN_new()))
         goto err;
 
-    if (!BN_exp(ret, a, e, ctx)
+    if (!TEST_true(BN_exp(ret, a, e, ctx))
             || !equalBN("A ^ E", exp, ret))
         goto err;
 
@@ -1484,21 +1426,22 @@ err:
 
 static int file_modsqrt(STANZA *s)
 {
-    BIGNUM *a = getBN(s, "A");
-    BIGNUM *p = getBN(s, "P");
-    BIGNUM *mod_sqrt = getBN(s, "ModSqrt");
-    BIGNUM *ret = BN_new();
-    BIGNUM *ret2 = BN_new();
+    BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
     int st = 0;
 
-    if (a == NULL || p == NULL || mod_sqrt == NULL
-            || ret == NULL || ret2 == NULL)
+    if (!TEST_ptr(a = getBN(s, "A"))
+            || !TEST_ptr(p = getBN(s, "P"))
+            || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
+            || !TEST_ptr(ret = BN_new())
+            || !TEST_ptr(ret2 = BN_new()))
         goto err;
 
     /* There are two possible answers. */
-    if (!BN_mod_sqrt(ret, a, p, ctx) || !BN_sub(ret2, p, ret))
+    if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
+            || !TEST_true(BN_sub(ret2, p, ret)))
         goto err;
 
+    /* The first condition should NOT be a test. */
     if (BN_cmp(ret2, mod_sqrt) != 0
             && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
         goto err;
@@ -1523,71 +1466,46 @@ static int test_bn2padded()
     /* Test edge case at 0. */
     if (n == NULL)
         goto err;
-    if (!BN_bn2bin_padded(NULL, 0, n)) {
-        fprintf(stderr,
-                "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
+    if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
         goto err;
-    }
     memset(out, -1, sizeof(out));
-    if (!BN_bn2bin_padded(out, sizeof(out), n)) {
-        fprintf(stderr,
-                "BN_bn2bin_padded failed to encode 0 in a non-empty buffer.\n");
+    if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
         goto err;
-    }
     memset(zeros, 0, sizeof(zeros));
-    if (memcmp(zeros, out, sizeof(out))) {
-        fprintf(stderr, "BN_bn2bin_padded did not zero buffer.\n");
+    if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
         goto err;
-    }
 
     /* Test a random numbers at various byte lengths. */
     for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
 #define TOP_BIT_ON 0
 #define BOTTOM_BIT_NOTOUCH 0
-        if (!BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)) {
-            ERR_print_errors_fp(stderr);
+        if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
             goto err;
-        }
-        if (BN_num_bytes(n) != bytes
-                || BN_bn2bin(n, reference) != bytes) {
-            fprintf(stderr, "Bad result from BN_rand; bytes.\n");
+        if (!TEST_int_eq(BN_num_bytes(n),A) bytes
+                || TEST_int_eq(BN_bn2bin(n, reference), bytes))
             goto err;
-        }
         /* Empty buffer should fail. */
-        if (BN_bn2bin_padded(NULL, 0, n)) {
-            fprintf(stderr,
-                    "BN_bn2bin_padded incorrectly succeeded on empty buffer.\n");
+        if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
             goto err;
-        }
         /* One byte short should fail. */
-        if (BN_bn2bin_padded(out, bytes - 1, n)) {
-            fprintf(stderr,
-                    "BN_bn2bin_padded incorrectly succeeded on short.\n");
+        if (BN_bn2bin_padded(out, bytes - 1, n))
             goto err;
-        }
         /* Exactly right size should encode. */
-        if (!BN_bn2bin_padded(out, bytes, n)
-                || memcmp(out, reference, bytes) != 0) {
-            fprintf(stderr,
-                    "BN_bn2bin_padded gave a bad result.\n");
+        if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
+                || TEST_mem_eq(out, bytes, reference, bytes))
             goto err;
-        }
         /* Pad up one byte extra. */
-        if (!BN_bn2bin_padded(out, bytes + 1, n)
-                || memcmp(out + 1, reference, bytes)
-                || memcmp(out, zeros, 1)) {
-            fprintf(stderr,
-                    "BN_bn2bin_padded gave a bad result.\n");
+        if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
+                || !TEST_mem_eq(out + 1, bytes, reference, bytes)
+                || !TEST_mem_eq(out, 1, zeros, 1))
             goto err;
-        }
         /* Pad up to 256. */
-        if (!BN_bn2bin_padded(out, sizeof(out), n)
-                || memcmp(out + sizeof(out) - bytes, reference, bytes)
-                || memcmp(out, zeros, sizeof(out) - bytes)) {
-            fprintf(stderr,
-                    "BN_bn2bin_padded gave a bad result.\n");
+        if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
+                || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
+                                reference, bytes)
+                || !TEST_mem_eq(out, sizseof(out) - bytes,
+                                zeros, sizeof(out) - bytes))
             goto err;
-        }
     }
 
     st = 1;
@@ -1604,40 +1522,63 @@ static int test_dec2bn()
     BIGNUM *bn = NULL;
     int st = 0;
 
-    int ret = parsedecBN(&bn, "0");
-    if (ret != 1 || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_dec2bn(0) gave a bad result.\n");
+    if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
+            || !TEST_BN_eq_word(bn, 0)
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parsedecBN(&bn, "256");
-    if (ret != 3 || !BN_is_word(bn, 256) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_dec2bn(256) gave a bad result.\n");
+    if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
+            || !TEST_BN_eq_word(bn, 256)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parsedecBN(&bn, "-42");
-    if (ret != 3 || !BN_abs_is_word(bn, 42) || !BN_is_negative(bn)) {
-        fprintf(stderr, "BN_dec2bn(42) gave a bad result.\n");
+    if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_lt_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parsedecBN(&bn, "-0");
-    if (ret != 2 || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_dec2bn(-0) gave a bad result.\n");
+    if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
+            || !TEST_BN_eq_word(bn, 1)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_eq_one(bn)
+            || !TEST_BN_odd(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parsedecBN(&bn, "42trailing garbage is ignored");
-    if (ret != 2 || !BN_abs_is_word(bn, 42)
-            || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_dec2bn(42trailing...) gave a bad result.\n");
+    if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_even(bn))
+        goto err;
+    BN_free(bn);
+    bn = NULL;
+
+    if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
 
     st = 1;
 err:
@@ -1648,41 +1589,62 @@ err:
 static int test_hex2bn()
 {
     BIGNUM *bn = NULL;
-    int ret, st = 0;
+    int st = 0;
 
-    ret = parseBN(&bn, "0");
-    if (ret != 1 || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_hex2bn(0) gave a bad result.\n");
+    if (!TEST_int_eq(parseBN(&bn, "0"), 1)
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parseBN(&bn, "256");
-    if (ret != 3 || !BN_is_word(bn, 0x256) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_hex2bn(256) gave a bad result.\n");
+    if (!TEST_int_eq(parseBN(&bn, "256"), 3)
+            || !TEST_BN_eq_word(bn, 0x256)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parseBN(&bn, "-42");
-    if (ret != 3 || !BN_abs_is_word(bn, 0x42) || !BN_is_negative(bn)) {
-        fprintf(stderr, "BN_hex2bn(-42) gave a bad result.\n");
+    if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
+            || !TEST_BN_abs_eq_word(bn, 0x42)
+            || !TEST_BN_lt_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parseBN(&bn, "-0");
-    if (ret != 2 || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_hex2bn(-0) gave a bad result.\n");
+    if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
+            || !TEST_BN_eq_word(bn, 0xCB)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_odd(bn))
         goto err;
-    }
     BN_free(bn);
+    bn = NULL;
 
-    ret = parseBN(&bn, "abctrailing garbage is ignored");
-    if (ret != 3 || !BN_is_word(bn, 0xabc) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_hex2bn(abctrail...) gave a bad result.\n");
+    if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_even(bn))
+        goto err;
+    BN_free(bn);
+    bn = NULL;
+
+    if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
+            || !TEST_BN_eq_word(bn, 0xabc)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
-    }
     st = 1;
 
 err:
@@ -1692,53 +1654,51 @@ err:
 
 static int test_asc2bn()
 {
-    BIGNUM *bn = BN_new();
+    BIGNUM *bn = NULL;
     int st = 0;
 
-    if (!BN_asc2bn(&bn, "0") || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(0) gave a bad result.\n");
+    if (!TEST_ptr(bn = BN_new()))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "256") || !BN_is_word(bn, 256) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(256) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "0"))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "-42")
-            || !BN_abs_is_word(bn, 42) || !BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(-42) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "256"))
+            || !TEST_BN_eq_word(bn, 256)
+            || !TEST_BN_ge_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "0x1234")
-            || !BN_is_word(bn, 0x1234) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(0x1234) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "-42"))
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_lt_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "0X1234")
-            || !BN_is_word(bn, 0x1234) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(0X1234) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
+            || !TEST_BN_eq_word(bn, 0x1234)
+            || !TEST_BN_ge_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "-0xabcd")
-            || !BN_abs_is_word(bn, 0xabcd) || !BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(-0xabcd) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
+            || !TEST_BN_eq_word(bn, 0x1234)
+            || !TEST_BN_ge_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "-0") || !BN_is_zero(bn) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(-0) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
+            || !TEST_BN_abs_eq_word(bn, 0xabcd)
+            || !TEST_BN_lt_zero(bn))
         goto err;
-    }
 
-    if (!BN_asc2bn(&bn, "123trailing garbage is ignored")
-            || !BN_is_word(bn, 123) || BN_is_negative(bn)) {
-        fprintf(stderr, "BN_asc2bn(123trail...) gave a bad result.\n");
+    if (!TEST_true(BN_asc2bn(&bn, "-0"))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn))
+        goto err;
+
+    if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
+            || !TEST_BN_eq_word(bn, 123)
+            || !TEST_BN_ge_zero(bn))
         goto err;
-    }
 
     st = 1;
 err:
@@ -1755,57 +1715,34 @@ static const MPITEST kMPITests[] = {
     {"-256", "\x00\x00\x00\x02\x81\x00", 6},
 };
 
-static int test_mpi()
+static int test_mpi(int i)
 {
     uint8_t scratch[8];
-    int i = (int)sizeof(kMPITests) / sizeof(kMPITests[0]);
-    const MPITEST *test = kMPITests;
+    const MPITEST *test = &kMPITests[i];
     size_t mpi_len, mpi_len2;
-    BIGNUM *bn = BN_new();
+    BIGNUM *bn = NULL;
     BIGNUM *bn2 = NULL;
     int st = 0;
 
-    for ( ; --i >= 0; test++) {
-        if (!BN_asc2bn(&bn, test->base10)) {
-            fprintf(stderr, "Can't convert %s\n", test->base10);
-            goto err;
-        }
-        mpi_len = BN_bn2mpi(bn, NULL);
-        if (mpi_len > sizeof (scratch)) {
-            fprintf(stderr,
-                    "MPI test #%u: MPI size is too large to test.\n",
-                    (unsigned)i);
-            goto err;
-        }
-
-        mpi_len2 = BN_bn2mpi(bn, scratch);
-        if (mpi_len != mpi_len2) {
-            fprintf(stderr, "MPI test #%u: length changes.\n",
-                    (unsigned)i);
-            goto err;
-        }
+    if (!TEST_ptr(bn = BN_new())
+            || !TEST_true(BN_asc2bn(&bn, test->base10)))
+        goto err;
+    mpi_len = BN_bn2mpi(bn, NULL);
+    if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
+        goto err;
 
-        if (mpi_len != test->mpi_len
-                || memcmp(test->mpi, scratch, mpi_len) != 0) {
-            fprintf(stderr, "MPI test #%u failed:\n", (unsigned)i);
-            goto err;
-        }
+    if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
+            || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
+        goto err;
 
-        bn2 = BN_mpi2bn(scratch, mpi_len, NULL);
-        if (bn2 == NULL) {
-            fprintf(stderr, "MPI test #%u: failed to parse\n",
-                    (unsigned)i);
-            goto err;
-        }
+    if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
+        goto err;
 
-        if (BN_cmp(bn, bn2) != 0) {
-            fprintf(stderr, "MPI test #%u: wrong result\n",
-                    (unsigned)i);
-            BN_free(bn2);
-            goto err;
-        }
+    if (!TEST_BN_eq(bn, bn2)) {
         BN_free(bn2);
+        goto err;
     }
+    BN_free(bn2);
 
     st = 1;
 err:
@@ -1815,41 +1752,23 @@ err:
 
 static int test_rand()
 {
-    BIGNUM *bn = BN_new();
+    BIGNUM *bn = NULL;
     int st = 0;
 
-    if (bn == NULL)
+    if (!TEST_ptr(bn = BN_new()))
         return 0;
 
-    /*
-     * Test BN_rand for degenerate cases with |top| and |bottom| parameters.
-     */
-    if (BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ )) {
-        fprintf(stderr, "BN_rand1 gave a bad result.\n");
-        goto err;
-    }
-    if (BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ )) {
-        fprintf(stderr, "BN_rand2 gave a bad result.\n");
-        goto err;
-    }
-
-    if (!BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ) || !BN_is_word(bn, 1)) {
-        fprintf(stderr, "BN_rand3 gave a bad result.\n");
+    /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
+    if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
+            || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
+            || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
+            || !TEST_BN_eq_one(bn)
+            || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
+            || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
+            || !TEST_BN_eq_one(bn)
+            || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
+            || !TEST_BN_eq_word(bn, 3))
         goto err;
-    }
-    if (BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ )) {
-        fprintf(stderr, "BN_rand4 gave a bad result.\n");
-        goto err;
-    }
-    if (!BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ) || !BN_is_word(bn, 1)) {
-        fprintf(stderr, "BN_rand5 gave a bad result.\n");
-        goto err;
-    }
-
-    if (!BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ) || !BN_is_word(bn, 3)) {
-        fprintf(stderr, "BN_rand6 gave a bad result.\n");
-        goto err;
-    }
 
     st = 1;
 err:
@@ -1859,59 +1778,51 @@ err:
 
 static int test_negzero()
 {
-    BIGNUM *a = BN_new();
-    BIGNUM *b = BN_new();
-    BIGNUM *c = BN_new();
-    BIGNUM *d = BN_new();
+    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
     BIGNUM *numerator = NULL, *denominator = NULL;
     int consttime, st = 0;
 
-    if (a == NULL || b == NULL || c == NULL || d == NULL)
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(c = BN_new())
+            || !TEST_ptr(d = BN_new()))
         goto err;
 
     /* Test that BN_mul never gives negative zero. */
-    if (!BN_set_word(a, 1))
+    if (!TEST_true(BN_set_word(a, 1)))
         goto err;
     BN_set_negative(a, 1);
     BN_zero(b);
-    if (!BN_mul(c, a, b, ctx))
+    if (!TEST_true(BN_mul(c, a, b, ctx)))
         goto err;
-    if (!BN_is_zero(c) || BN_is_negative(c)) {
-        fprintf(stderr, "Multiplication test failed!\n");
+    if (!TEST_BN_eq_zero(c)
+            || !TEST_BN_ge_zero(c))
         goto err;
-    }
 
     for (consttime = 0; consttime < 2; consttime++) {
-        numerator = BN_new();
-        denominator = BN_new();
-        if (numerator == NULL || denominator == NULL)
+        if (!TEST_ptr(numerator = BN_new())
+                || !TEST_ptr(denominator = BN_new()))
             goto err;
         if (consttime) {
             BN_set_flags(numerator, BN_FLG_CONSTTIME);
             BN_set_flags(denominator, BN_FLG_CONSTTIME);
         }
         /* Test that BN_div never gives negative zero in the quotient. */
-        if (!BN_set_word(numerator, 1) || !BN_set_word(denominator, 2))
+        if (!TEST_true(BN_set_word(numerator, 1))
+                || !TEST_true(BN_set_word(denominator, 2)))
             goto err;
         BN_set_negative(numerator, 1);
-        if (!BN_div(a, b, numerator, denominator, ctx))
-            goto err;
-        if (!BN_is_zero(a) || BN_is_negative(a)) {
-            fprintf(stderr, "Incorrect quotient (consttime = %d).\n",
-                    consttime);
+        if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
+                || !TEST_BN_eq_zero(a)
+                || !TEST_BN_ge_zero(a))
             goto err;
-        }
 
         /* Test that BN_div never gives negative zero in the remainder. */
-        if (!BN_set_word(denominator, 1))
-            goto err;
-        if (!BN_div(a, b, numerator, denominator, ctx))
+        if (!TEST_true(BN_set_word(denominator, 1))
+                || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
+                || !TEST_BN_eq_zero(b)
+                || !TEST_BN_ge_zero(b))
             goto err;
-        if (!BN_is_zero(b) || BN_is_negative(b)) {
-            fprintf(stderr, "Incorrect remainder (consttime = %d).\n",
-                    consttime);
-            goto err;
-        }
         BN_free(numerator);
         BN_free(denominator);
         numerator = denominator = NULL;
@@ -1920,12 +1831,10 @@ static int test_negzero()
     /* Test that BN_set_negative will not produce a negative zero. */
     BN_zero(a);
     BN_set_negative(a, 1);
-    if (BN_is_negative(a)) {
-        fprintf(stderr, "BN_set_negative produced a negative zero.\n");
+    if (BN_is_negative(a))
         goto err;
-    }
-
     st = 1;
+
 err:
     BN_free(a);
     BN_free(b);
@@ -1938,78 +1847,59 @@ err:
 
 static int test_badmod()
 {
-    BIGNUM *a = BN_new();
-    BIGNUM *b = BN_new();
-    BIGNUM *zero = BN_new();
-    BN_MONT_CTX *mont = BN_MONT_CTX_new();
+    BIGNUM *a = NULL, *b = NULL, *zero = NULL;
+    BN_MONT_CTX *mont = NULL;
     int st = 0;
 
-    if (a == NULL || b == NULL || zero == NULL || mont == NULL)
+    if (!TEST_ptr(a = BN_new())
+            || !TEST_ptr(b = BN_new())
+            || !TEST_ptr(zero = BN_new())
+            || !TEST_ptr(mont = BN_MONT_CTX_new()))
         goto err;
     BN_zero(zero);
 
-    if (BN_div(a, b, BN_value_one(), zero, ctx)) {
-        fprintf(stderr, "Division by zero succeeded!\n");
+    if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)) {
-        fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
+    if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)) {
-        fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
+    if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), zero, ctx, NULL)) {
-        fprintf(stderr, "BN_mod_exp_mont with zero modulus succeeded!\n");
+    if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
+                                    zero, ctx, NULL)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
-                                  zero, ctx, NULL)) {
-        fprintf(stderr,
-                "BN_mod_exp_mont_consttime with zero modulus succeeded!\n");
+    if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
+                                             zero, ctx, NULL)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_MONT_CTX_set(mont, zero, ctx)) {
-        fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
+    if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
         goto err;
-    }
     ERR_clear_error();
 
     /* Some operations also may not be used with an even modulus. */
-    if (!BN_set_word(b, 16))
+    if (!TEST_true(BN_set_word(b, 16)))
         goto err;
 
-    if (BN_MONT_CTX_set(mont, b, ctx)) {
-        fprintf(stderr,
-                "BN_MONT_CTX_set succeeded for even modulus!\n");
+    if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), b, ctx, NULL)) {
-        fprintf(stderr,
-                "BN_mod_exp_mont with even modulus succeeded!\n");
+    if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
+                                    b, ctx, NULL)))
         goto err;
-    }
     ERR_clear_error();
 
-    if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
-                                  b, ctx, NULL)) {
-        fprintf(stderr,
-                "BN_mod_exp_mont_consttime with even modulus succeeded!\n");
+    if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
+                                  b, ctx, NULL)))
         goto err;
-    }
     ERR_clear_error();
 
     st = 1;
@@ -2023,23 +1913,27 @@ err:
 
 static int test_expmodzero()
 {
-    BIGNUM *zero = BN_new();
-    BIGNUM *a = BN_new();
-    BIGNUM *r = BN_new();
+    BIGNUM *a = NULL, *r = NULL, *zero = NULL;
     int st = 0;
 
-    if (zero == NULL || a == NULL || r == NULL || !BN_rand(a, 1024, 0, 0))
+    if (!TEST_ptr(zero = BN_new())
+            || !TEST_ptr(a = BN_new())
+            || !TEST_ptr(r = BN_new()))
         goto err;
     BN_zero(zero);
 
-    if (!BN_mod_exp(r, a, zero, BN_value_one(), NULL)
-            || !BN_is_zero(r)
-            || !BN_mod_exp_mont(r, a, zero, BN_value_one(), NULL, NULL)
-            || !BN_is_zero(r)
-            || !BN_mod_exp_mont_consttime(r, a, zero, BN_value_one(), NULL, NULL)
-            || !BN_is_zero(r)
-            || !BN_mod_exp_mont_word(r, 42, zero, BN_value_one(), NULL, NULL)
-            || !BN_is_zero(r))
+    if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
+            || !TEST_BN_eq_zero(r)
+            || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
+                                          NULL, NULL))
+            || !TEST_BN_eq_zero(r)
+            || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
+                                                    BN_value_one(),
+                                                    NULL, NULL))
+            || !TEST_BN_eq_zero(r)
+            || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
+                                               BN_value_one(), NULL, NULL))
+            || !TEST_BN_eq_zero(r))
         goto err;
 
     st = 1;
@@ -2053,17 +1947,14 @@ err:
 static int test_smallprime()
 {
     static const int kBits = 10;
-    BIGNUM *r = BN_new();
+    BIGNUM *r;
     int st = 0;
 
-    if (r == NULL
-            || !BN_generate_prime_ex(r, (int)kBits, 0, NULL, NULL, NULL))
-        goto err;
-    if (BN_num_bits(r) != kBits) {
-        fprintf(stderr, "Expected %u bit prime, got %u bit number\n",
-                kBits, BN_num_bits(r));
+    if (!TEST_ptr(r = BN_new())
+            || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
+                                               NULL, NULL, NULL))
+            || !TEST_int_eq(BN_num_bits(r), kBits))
         goto err;
-    }
 
     st = 1;
 err:
@@ -2071,6 +1962,30 @@ err:
     return st;
 }
 
+static int test_3_is_prime()
+{
+    int ret = 0;
+    BIGNUM *r = NULL;
+
+    /*
+     * For a long time, small primes were not considered prime when
+     * do_trial_division was set.
+     */
+    if (!TEST_ptr(r = BN_new())
+            || !TEST_true(BN_set_word(r, 3))
+            || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
+                                0 /* do_trial_division */, NULL), 1)
+            || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
+                                1 /* do_trial_division */, NULL), 1))
+        goto err;
+
+    ret = 1;
+
+err:
+    BN_free(r);
+    return ret;
+}
+
 
 /* Delete leading and trailing spaces from a string */
 static char *strip_spaces(char *p)
@@ -2091,16 +2006,16 @@ static char *strip_spaces(char *p)
 /*
  * Read next test stanza; return 1 if found, 0 on EOF or error.
  */
-static int readstanza(STANZA *s, int *linesread)
+static int readstanza(STANZA *s)
 {
     PAIR *pp = s->pairs;
     char *p, *equals, *key, *value;
     char buff[1024];
 
-    while (fgets(buff, sizeof(buff), fp) != NULL) {
-        (*linesread)++;
-        if ((p = strchr(buff, '\n')) == NULL) {
-            fprintf(stderr, "Line %d too long.\n", s->start);
+    while (BIO_gets(fp, buff, sizeof(buff))) {
+        s->curr++;
+        if (!TEST_ptr(p = strchr(buff, '\n'))) {
+            TEST_info("Line %d too long", s->curr);
             return 0;
         }
         *p = '\0';
@@ -2113,25 +2028,20 @@ static int readstanza(STANZA *s, int *linesread)
         if (buff[0] == '#')
             continue;
 
-        if ((equals = strchr(buff, '=')) == NULL) {
-            fprintf(stderr, "Line %d missing equals.\n", s->start);
+        if (!TEST_ptr(equals = strchr(buff, '='))) {
+            TEST_info("Missing = at line %d\n", s->curr);
             return 0;
         }
         *equals++ = '\0';
-
-        key = strip_spaces(buff);
-        value = strip_spaces(equals);
-        if (key == NULL || value == NULL) {
-            fprintf(stderr, "Line %d missing field.\n", s->start);
+        if (s->numpairs == 0)
+            s->start = s->curr;
+
+        if (!TEST_ptr(key = strip_spaces(buff))
+                || !TEST_ptr(value = strip_spaces(equals))
+                || !TEST_int_lt(s->numpairs++, MAXPAIRS)
+                || !TEST_ptr(pp->key = OPENSSL_strdup(key))
+                || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
             return 0;
-        }
-        s->numpairs++;
-        if (s->numpairs >= MAXPAIRS) {
-            fprintf(stderr, "Line %d too many lines\n", s->start);
-            return 0;
-        }
-        pp->key = OPENSSL_strdup(key);
-        pp->value = OPENSSL_strdup(value);
         pp++;
     }
 
@@ -2144,12 +2054,14 @@ static void clearstanza(STANZA *s)
     PAIR *pp = s->pairs;
     int i = s->numpairs;
     int start = s->start;
+    int curr = s->curr;
 
     for ( ; --i >= 0; pp++) {
         OPENSSL_free(pp->key);
         OPENSSL_free(pp->value);
     }
     memset(s, 0, sizeof(*s));
+    s->curr = curr;
     s->start = start;
 }
 
@@ -2172,85 +2084,92 @@ static int file_test_run(STANZA *s)
     const FILETEST *tp = filetests;
 
     for ( ; --numtests >= 0; tp++) {
-        if (findattr(s, tp->name) != NULL)
-            return tp->func(s);
+        if (findattr(s, tp->name) != NULL) {
+            if (!tp->func(s)) {
+                TEST_info("Failed %s test at %d", tp->name, s->start);
+                return 0;
+            }
+            return 1;
+        }
     }
-    fprintf(stderr, "Unknown test at %d\n", s->start);
+    TEST_info("Unknown test at %d", s->start);
     return 0;
 }
 
-static int file_tests()
+static char * const *testfiles;
+
+static int run_file_tests(int i)
 {
     STANZA s;
-    int linesread = 0, result = 0;
+    int errcnt = 0;
+
+    if (!TEST_ptr(fp = BIO_new_file(testfiles[i], "rb")))
+        return 0;
 
     /* Read test file. */
+    set_test_title(testfiles[i]);
     memset(&s, 0, sizeof(s));
-    while (!feof(fp) && readstanza(&s, &linesread)) {
+    while (!BIO_eof(fp) && readstanza(&s)) {
         if (s.numpairs == 0)
             continue;
         if (!file_test_run(&s)) {
-            if (result == 0)
-                fprintf(stderr, "Test at %d failed\n", s.start);
-            goto err;
+            errcnt++;
         }
         clearstanza(&s);
-        s.start = linesread;
     }
-    result = 1;
+    BIO_free(fp);
 
-err:
-    return result;
+    return errcnt == 0;
 }
 
+
 int test_main(int argc, char *argv[])
 {
     static const char rnd_seed[] =
         "If not seeded, BN_generate_prime might fail";
-    int result = 0;
+    int result = EXIT_FAILURE;
 
-    if (argc != 2) {
-        fprintf(stderr, "%s TEST_FILE\n", argv[0]);
-        return 1;
-    }
 
-    ADD_TEST(test_sub);
-    ADD_TEST(test_div_recip);
-    ADD_TEST(test_mod);
-    ADD_TEST(test_modexp_mont5);
-    ADD_TEST(test_kronecker);
-    ADD_TEST(test_rand);
-    ADD_TEST(test_bn2padded);
-    ADD_TEST(test_dec2bn);
-    ADD_TEST(test_hex2bn);
-    ADD_TEST(test_asc2bn);
-    ADD_TEST(test_mpi);
-    ADD_TEST(test_negzero);
-    ADD_TEST(test_badmod);
-    ADD_TEST(test_expmodzero);
-    ADD_TEST(test_smallprime);
+    RAND_seed(rnd_seed, sizeof rnd_seed);
+    if (!TEST_ptr(ctx = BN_CTX_new()))
+        goto end;
+
+    if (argc < 2) {
+        ADD_TEST(test_sub);
+        ADD_TEST(test_div_recip);
+        ADD_TEST(test_mod);
+        ADD_TEST(test_modexp_mont5);
+        ADD_TEST(test_kronecker);
+        ADD_TEST(test_rand);
+        ADD_TEST(test_bn2padded);
+        ADD_TEST(test_dec2bn);
+        ADD_TEST(test_hex2bn);
+        ADD_TEST(test_asc2bn);
+        ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
+        ADD_TEST(test_negzero);
+        ADD_TEST(test_badmod);
+        ADD_TEST(test_expmodzero);
+        ADD_TEST(test_smallprime);
 #ifndef OPENSSL_NO_EC2M
-    ADD_TEST(test_gf2m_add);
-    ADD_TEST(test_gf2m_mod);
-    ADD_TEST(test_gf2m_mul);
-    ADD_TEST(test_gf2m_sqr);
-    ADD_TEST(test_gf2m_modinv);
-    ADD_TEST(test_gf2m_moddiv);
-    ADD_TEST(test_gf2m_modexp);
-    ADD_TEST(test_gf2m_modsqrt);
-    ADD_TEST(test_gf2m_modsolvequad);
+        ADD_TEST(test_gf2m_add);
+        ADD_TEST(test_gf2m_mod);
+        ADD_TEST(test_gf2m_mul);
+        ADD_TEST(test_gf2m_sqr);
+        ADD_TEST(test_gf2m_modinv);
+        ADD_TEST(test_gf2m_moddiv);
+        ADD_TEST(test_gf2m_modexp);
+        ADD_TEST(test_gf2m_modsqrt);
+        ADD_TEST(test_gf2m_modsolvequad);
 #endif
-    ADD_TEST(file_tests);
-
-    RAND_seed(rnd_seed, sizeof rnd_seed);
-    ctx = BN_CTX_new();
-    TEST_check(ctx != NULL);
+        ADD_TEST(test_3_is_prime);
+    } else {
+        testfiles = &argv[1];
+        ADD_ALL_TESTS(run_file_tests, argc - 1);
+    }
 
-    fp = fopen(argv[1], "r");
-    TEST_check(fp != NULL);
     result = run_tests(argv[0]);
-    fclose(fp);
 
+end:
     BN_CTX_free(ctx);
     return result;
 }