summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPauli <paul.dale@oracle.com>2017-06-12 10:01:17 +1000
committerRich Salz <rsalz@openssl.org>2017-06-16 16:15:31 -0400
commit3791646202bb4da21992b0aecae253d394507a9e (patch)
treecc1f33ab05144ec2442df45366fac1175fcaca16
parent5511101ad86fdd5bc3ad4f27143e93ae14737bfe (diff)
Add output routines to allow consistent formatting of memory, strings
and bignums. These have been refactored into their own file, along with their error displays. The formatting follows the output format used on error, except that bignums of sixty four bits or less are displayed in a more compact one line form. Added a TEST_note function for producing output without file and line information. Update the three tests that call BN_print so they use the new test infrastructure instead. Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Rich Salz <rsalz@openssl.org> (Merged from https://github.com/openssl/openssl/pull/3655)
-rw-r--r--test/build.info1
-rw-r--r--test/ectest.c199
-rw-r--r--test/exptest.c16
-rw-r--r--test/igetest.c33
-rw-r--r--test/srptest.c50
-rw-r--r--test/test_test.c34
-rw-r--r--test/testutil.h10
-rw-r--r--test/testutil/driver.c1
-rw-r--r--test/testutil/format_output.c528
-rw-r--r--test/testutil/tests.c450
-rw-r--r--test/testutil/tu_local.h30
11 files changed, 745 insertions, 607 deletions
diff --git a/test/build.info b/test/build.info
index fc09fcbd67..eba52cc336 100644
--- a/test/build.info
+++ b/test/build.info
@@ -11,6 +11,7 @@ IF[{- !$disabled{tests} -}]
LIBS_NO_INST=libtestutil.a
SOURCE[libtestutil.a]=testutil/basic_output.c testutil/output_helpers.c \
testutil/driver.c testutil/tests.c testutil/cb.c testutil/stanza.c \
+ testutil/format_output.c \
{- rebase_files("../apps", $target{apps_aux_src}) -} \
testutil/test_main.c testutil/main.c
INCLUDE[libtestutil.a]=.. ../include
diff --git a/test/ectest.c b/test/ectest.c
index 8e311e5d71..37bffc65e1 100644
--- a/test/ectest.c
+++ b/test/ectest.c
@@ -161,7 +161,7 @@ static int prime_field_tests(void)
const EC_POINT *points[4];
const BIGNUM *scalars[4];
unsigned char buf[100];
- size_t i, len, r = 0;
+ size_t len, r = 0;
int k;
if (!TEST_ptr(ctx = BN_CTX_new())
@@ -187,15 +187,11 @@ static int prime_field_tests(void)
if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx)))
goto err;
- BIO_printf(bio_out,
- "Curve defined by Weierstrass equation\n"
- " y^2 = x^3 + a*x + b (mod 0x");
- BN_print(bio_out, p);
- BIO_printf(bio_out, ")\n a = 0x");
- BN_print(bio_out, a);
- BIO_printf(bio_out, "\n b = 0x");
- BN_print(bio_out, b);
- BIO_printf(bio_out, "\n");
+ TEST_info("Curve defined by Weierstrass equation");
+ TEST_note(" y^2 = x^3 + a*x + b (mod p)");
+ test_output_bignum("a", a);
+ test_output_bignum("b", b);
+ test_output_bignum("p", p);
buf[0] = 0;
if (!TEST_ptr(P = EC_POINT_new(group))
@@ -219,32 +215,27 @@ static int prime_field_tests(void)
if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y,
ctx)))
goto err;
- BIO_printf(bio_err, "Point is not on curve: x = 0x");
- BN_print_fp(stderr, x);
- BIO_printf(bio_err, ", y = 0x");
- BN_print_fp(stderr, y);
- BIO_printf(bio_err, "\n");
+ TEST_info("Point is not on curve");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
goto err;
}
- BIO_printf(bio_out, "A cyclic subgroup:\n");
+ TEST_note("A cyclic subgroup:");
k = 100;
do {
if (!TEST_int_ne(k--, 0))
goto err;
if (EC_POINT_is_at_infinity(group, P)) {
- BIO_printf(bio_out, " point at infinity\n");
+ TEST_note(" point at infinity");
} else {
if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, " x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
}
if (!TEST_true(EC_POINT_copy(R, P))
@@ -264,9 +255,8 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "Generator as octet string, compressed form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, compressed form:",
+ buf, len);
len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
buf, sizeof buf, ctx);
@@ -274,10 +264,8 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
- " ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, uncompressed form:",
+ buf, len);
len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
buf, sizeof buf, ctx);
@@ -285,23 +273,17 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, hybrid form:",
+ buf, len);
if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
ctx)))
goto err;
- BIO_printf(bio_out,
- "\nA representation of the inverse of that generator in\n"
- "Jacobian projective coordinates:\n"
- " X = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", Y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, ", Z = 0x");
- BN_print(bio_out, z);
- BIO_printf(bio_out, "\n");
+ TEST_info("A representation of the inverse of that generator in");
+ TEST_note("Jacobian projective coordinates");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
+ test_output_bignum("z", z);
if (!TEST_true(EC_POINT_invert(group, P, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
@@ -337,11 +319,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("SEC2 curve secp160r1 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "23a62855"
"3168947d59dcc912042351377ac5fb32"))
@@ -372,11 +352,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-192 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-192 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
"631011ED6B24CDD573F977A11E794811"))
@@ -414,11 +392,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-224 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-224 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
"CD4375A05A07476444D5819985007E34"))
@@ -457,11 +433,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-256 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-256 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
"2BCE33576B315ECECBB6406837BF51F5"))
@@ -505,11 +479,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-384 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-384 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
"F8F41DBD289A147CE9DA3113B5F0B8C0"
@@ -562,11 +534,9 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-521 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-521 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "118"
"39296A789A3BC0045C8A5FB42C7D1BD9"
@@ -613,7 +583,7 @@ static int prime_field_tests(void)
scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
scalars[1] = y;
- BIO_printf(bio_out, "combined multiplication ...");
+ TEST_note("combined multiplication ...");
/* z is still the group order */
if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
@@ -646,7 +616,7 @@ static int prime_field_tests(void)
|| !TEST_true(EC_POINT_is_at_infinity(group, P)))
goto err;
- BIO_printf(bio_out, " ok\n\n");
+ TEST_note(" ok\n");
r = 1;
@@ -898,11 +868,9 @@ static int char2_curve_test(int n)
|| !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name);
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("%s -- Generator", test->name);
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, test->y))
|| !TEST_BN_eq(y, z))
@@ -920,11 +888,9 @@ static int char2_curve_test(int n)
|| !TEST_true(BN_hex2bn(&cof, test->cof))
|| !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
goto err;
- BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name); \
- BN_print(bio_out, x); \
- BIO_printf(bio_out, "\n y = 0x"); \
- BN_print(bio_out, y); \
- BIO_printf(bio_out, "\n");
+ TEST_info("%s -- Generator:", test->name);
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
# endif
if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
@@ -959,7 +925,7 @@ static int char2_curve_test(int n)
scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
scalars[1] = y;
- BIO_printf(bio_out, "combined multiplication ...");
+ TEST_note("combined multiplication ...");
/* z is still the group order */
if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
@@ -1019,7 +985,7 @@ static int char2_field_tests(void)
EC_POINT *P = NULL, *Q = NULL, *R = NULL;
BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
unsigned char buf[100];
- size_t i, len;
+ size_t len;
int k, r = 0;
if (!TEST_ptr(ctx = BN_CTX_new())
@@ -1047,15 +1013,11 @@ static int char2_field_tests(void)
if (!TEST_true(EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)))
goto err;
- BIO_printf(bio_out,
- "Curve defined by Weierstrass equation\n"
- " y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
- BN_print(bio_out, p);
- BIO_printf(bio_out, ")\n a = 0x");
- BN_print(bio_out, a);
- BIO_printf(bio_out, "\n b = 0x");
- BN_print(bio_out, b);
- BIO_printf(bio_out, "\n(0x... means binary polynomial)\n");
+ TEST_info("Curve defined by Weierstrass equation");
+ TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
+ test_output_bignum("a", a);
+ test_output_bignum("b", b);
+ test_output_bignum("p", p);
if (!TEST_ptr(P = EC_POINT_new(group))
|| !TEST_ptr(Q = EC_POINT_new(group))
@@ -1091,32 +1053,27 @@ static int char2_field_tests(void)
ctx)))
goto err;
# endif
- BIO_printf(bio_err, "Point is not on curve: x = 0x");
- BN_print_fp(stderr, x);
- BIO_printf(bio_err, ", y = 0x");
- BN_print_fp(stderr, y);
- BIO_printf(bio_err, "\n");
+ TEST_info("Point is not on curve");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
goto err;
}
- BIO_printf(bio_out, "A cyclic subgroup:\n");
+ TEST_note("A cyclic subgroup:");
k = 100;
do {
if (!TEST_int_ne(k--, 0))
goto err;
if (EC_POINT_is_at_infinity(group, P))
- BIO_printf(bio_out, " point at infinity\n");
+ TEST_note(" point at infinity");
else {
if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, " x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
}
if (!TEST_true(EC_POINT_copy(R, P))
@@ -1137,9 +1094,8 @@ static int char2_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "Generator as octet string, compressed form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, compressed form:",
+ buf, len);
# endif
len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
@@ -1148,10 +1104,8 @@ static int char2_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
- " ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, uncompressed form:",
+ buf, len);
/* Change test based on whether binary point compression is enabled or not. */
# ifdef OPENSSL_EC_BIN_PT_COMP
@@ -1162,20 +1116,15 @@ static int char2_field_tests(void)
|| !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, hybrid form:",
+ buf, len);
# endif
- BIO_printf(bio_out, "\n");
if (!TEST_true(EC_POINT_invert(group, P, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
goto err;
-
-#if 0
-#endif
- BIO_printf(bio_out, "\n\n");
+ TEST_note("\n");
r = 1;
err:
@@ -1352,8 +1301,8 @@ static int nistp_single_test(int idx)
EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
int r = 0;
- BIO_printf(bio_out, "\nNIST curve P-%d (optimised implementation):\n",
- test->degree);
+ TEST_note("NIST curve P-%d (optimised implementation):",
+ test->degree);
if (!TEST_ptr(ctx = BN_CTX_new())
|| !TEST_ptr(p = BN_new())
|| !TEST_ptr(a = BN_new())
@@ -1394,7 +1343,7 @@ static int nistp_single_test(int idx)
|| !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
goto err;
- BIO_printf(bio_out, "NIST test vectors ... ");
+ TEST_note("NIST test vectors ... ");
if (!TEST_true(BN_hex2bn(&n, test->d)))
goto err;
/* fixed point multiplication */
diff --git a/test/exptest.c b/test/exptest.c
index ee1bff168c..e6f52139a1 100644
--- a/test/exptest.c
+++ b/test/exptest.c
@@ -22,14 +22,7 @@
#define NUM_BITS (BN_BITS2 * 4)
-#define BN_print_var(v) bn_print_var(#v, v)
-
-static void bn_print_var(const char *var, const BIGNUM *bn)
-{
- fprintf(stderr, "%s (%3d) = ", var, BN_num_bits(bn));
- BN_print_fp(stderr, bn);
- fprintf(stderr, "\n");
-}
+#define BN_print_var(v) test_output_bignum(#v, v)
/*
* Test that r == 0 in test_exp_mod_zero(). Returns one on success,
@@ -39,8 +32,7 @@ static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
const BIGNUM *a)
{
if (!BN_is_zero(r)) {
- fprintf(stderr, "%s failed:\n", method);
- fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
+ TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method);
BN_print_var(a);
BN_print_var(r);
return 0;
@@ -110,8 +102,8 @@ static int test_mod_exp_zero()
goto err;
if (!TEST_BN_eq_zero(r)) {
- fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
- fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
+ TEST_error("BN_mod_exp_mont_word failed: "
+ "1 ** 0 mod 1 = r (should be 0)");
BN_print_var(r);
goto err;
}
diff --git a/test/igetest.c b/test/igetest.c
index f6972b6b0a..a0fc623a64 100644
--- a/test/igetest.c
+++ b/test/igetest.c
@@ -27,19 +27,6 @@ static unsigned char rkey2[16];
static unsigned char plaintext[BIG_TEST_SIZE];
static unsigned char saved_iv[AES_BLOCK_SIZE * 4];
-static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
-{
- int n = 0;
-
- fprintf(f, "%s", title);
- for (; n < l; ++n) {
- if ((n % 16) == 0)
- fprintf(f, "\n%04x", n);
- fprintf(f, " %02x", s[n]);
- }
- fprintf(f, "\n");
-}
-
#define MAX_VECTOR_SIZE 64
struct ige_test {
@@ -174,9 +161,9 @@ static int test_ige_vectors(int n)
if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
TEST_info("IGE test vector %d failed", n);
- hexdump(stderr, "key", v->key, sizeof v->key);
- hexdump(stderr, "iv", v->iv, sizeof v->iv);
- hexdump(stderr, "in", v->in, v->length);
+ test_output_memory("key", v->key, sizeof v->key);
+ test_output_memory("iv", v->iv, sizeof v->iv);
+ test_output_memory("in", v->in, v->length);
testresult = 0;
}
@@ -187,9 +174,9 @@ static int test_ige_vectors(int n)
if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
TEST_info("IGE test vector %d failed (with in == out)", n);
- hexdump(stderr, "key", v->key, sizeof v->key);
- hexdump(stderr, "iv", v->iv, sizeof v->iv);
- hexdump(stderr, "in", v->in, v->length);
+ test_output_memory("key", v->key, sizeof v->key);
+ test_output_memory("iv", v->iv, sizeof v->iv);
+ test_output_memory("in", v->in, v->length);
testresult = 0;
}
@@ -218,10 +205,10 @@ static int test_bi_ige_vectors(int n)
v->encrypt);
if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
- hexdump(stderr, "key 1", v->key1, sizeof v->key1);
- hexdump(stderr, "key 2", v->key2, sizeof v->key2);
- hexdump(stderr, "iv", v->iv, sizeof v->iv);
- hexdump(stderr, "in", v->in, v->length);
+ test_output_memory("key 1", v->key1, sizeof v->key1);
+ test_output_memory("key 2", v->key2, sizeof v->key2);
+ test_output_memory("iv", v->iv, sizeof v->iv);
+ test_output_memory("in", v->in, v->length);
return 0;
}
diff --git a/test/srptest.c b/test/srptest.c
index 3e3b589926..ed5bf611a6 100644
--- a/test/srptest.c
+++ b/test/srptest.c
@@ -18,22 +18,6 @@
# include <openssl/rand.h>
# include <openssl/err.h>
-static void showbn(const char *name, const BIGNUM *bn)
-{
- BIO *b;
- const char *text;
-
- if (!TEST_ptr(b = BIO_new(BIO_s_mem())))
- return;
- BIO_write(b, name, strlen(name));
- BIO_write(b, " = ", 3);
- BN_print(b, bn);
- BIO_write(b, "\0", 1);
- BIO_get_mem_data(b, &text);
- TEST_info("%s", text);
- BIO_free(b);
-}
-
# define RANDOM_SIZE 32 /* use 256 bits on each side */
static int run_srp(const char *username, const char *client_pass,
@@ -62,21 +46,21 @@ static int run_srp(const char *username, const char *client_pass,
&s, &v, GN->N, GN->g)))
goto end;
- showbn("N", GN->N);
- showbn("g", GN->g);
- showbn("Salt", s);
- showbn("Verifier", v);
+ test_output_bignum("N", GN->N);
+ test_output_bignum("g", GN->g);
+ test_output_bignum("Salt", s);
+ test_output_bignum("Verifier", v);
/* Server random */
RAND_bytes(rand_tmp, sizeof(rand_tmp));
b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
if (!TEST_BN_ne_zero(b))
goto end;
- showbn("b", b);
+ test_output_bignum("b", b);
/* Server's first message */
Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
- showbn("B", Bpub);
+ test_output_bignum("B", Bpub);
if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
goto end;
@@ -86,11 +70,11 @@ static int run_srp(const char *username, const char *client_pass,
a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
if (!TEST_BN_ne_zero(a))
goto end;
- showbn("a", a);
+ test_output_bignum("a", a);
/* Client's response */
Apub = SRP_Calc_A(a, GN->N, GN->g);
- showbn("A", Apub);
+ test_output_bignum("A", Apub);
if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
goto end;
@@ -101,11 +85,11 @@ static int run_srp(const char *username, const char *client_pass,
/* Client's key */
x = SRP_Calc_x(s, username, client_pass);
Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
- showbn("Client's key", Kclient);
+ test_output_bignum("Client's key", Kclient);
/* Server's key */
Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
- showbn("Server's key", Kserver);
+ test_output_bignum("Server's key", Kserver);
if (!TEST_BN_eq(Kclient, Kserver))
goto end;
@@ -164,9 +148,10 @@ static int run_srp_kat(void)
BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
/* Set up server's password entry */
if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
- GN->g)))
+ GN->g)))
goto err;
+ TEST_info("checking v");
if (!TEST_true(check_bn("v", v,
"7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
"9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
@@ -174,6 +159,7 @@ static int run_srp_kat(void)
"EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
"E955A5E29E7AB245DB2BE315E2099AFB")))
goto err;
+ TEST_note(" okay");
/* Server random */
BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
@@ -184,6 +170,7 @@ static int run_srp_kat(void)
if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
goto err;
+ TEST_info("checking B");
if (!TEST_true(check_bn("B", Bpub,
"BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
"BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
@@ -191,6 +178,7 @@ static int run_srp_kat(void)
"37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
"EB4012B7D7665238A8E3FB004B117B58")))
goto err;
+ TEST_note(" okay");
/* Client random */
BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
@@ -201,6 +189,7 @@ static int run_srp_kat(void)
if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
goto err;
+ TEST_info("checking A");
if (!TEST_true(check_bn("A", Apub,
"61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
"4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
@@ -208,6 +197,7 @@ static int run_srp_kat(void)
"BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
"B349EF5D76988A3672FAC47B0769447B")))
goto err;
+ TEST_note(" okay");
/* Both sides calculate u */
u = SRP_Calc_u(Apub, Bpub, GN->N);
@@ -219,6 +209,7 @@ static int run_srp_kat(void)
/* Client's key */
x = SRP_Calc_x(s, "alice", "password123");
Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
+ TEST_info("checking client's key");
if (!TEST_true(check_bn("Client's key", Kclient,
"B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
"233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
@@ -226,9 +217,11 @@ static int run_srp_kat(void)
"3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
"C346D7E474B29EDE8A469FFECA686E5A")))
goto err;
+ TEST_note(" okay");
/* Server's key */
Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
+ TEST_info("checking server's key");
if (!TEST_true(check_bn("Server's key", Kserver,
"B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
"233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
@@ -236,6 +229,7 @@ static int run_srp_kat(void)
"3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
"C346D7E474B29EDE8A469FFECA686E5A")))
goto err;
+ TEST_note(" okay");
ret = 1;
@@ -257,10 +251,12 @@ err:
static int run_srp_tests(void)
{
/* "Negative" test, expect a mismatch */
+ TEST_info("run_srp: expecting a mismatch");
if (!TEST_false(run_srp("alice", "password1", "password2")))
return 0;
/* "Positive" test, should pass */
+ TEST_info("run_srp: expecting a match");
if (!TEST_true(run_srp("alice", "password", "password")))
return 0;
diff --git a/test/test_test.c b/test/test_test.c
index a1b04561b3..051058fffe 100644
--- a/test/test_test.c
+++ b/test/test_test.c
@@ -477,6 +477,38 @@ static int test_single_eval(void)
&& TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456"));
}
+static int test_output(void)
+{
+ const char s[] = "1234567890123456789012345678901234567890123456789012"
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ test_output_string("test", s, sizeof(s) - 1);
+ test_output_memory("test", (const unsigned char *)s, sizeof(s));
+ return 1;
+}
+
+static const char *bn_output_tests[] = {
+ NULL,
+ "0",
+ "-12345678",
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789013"
+ "987657"
+};
+
+static int test_bn_output(int n)
+{
+ BIGNUM *b = NULL;
+
+ if (bn_output_tests[n] != NULL
+ && !TEST_true(BN_hex2bn(&b, bn_output_tests[n])))
+ return 0;
+ test_output_bignum(bn_output_tests[n], b);
+ BN_free(b);
+ return 1;
+}
+
+
void register_tests(void)
{
ADD_TEST(test_int);
@@ -496,4 +528,6 @@ void register_tests(void)
ADD_TEST(test_long_output);
ADD_TEST(test_messages);
ADD_TEST(test_single_eval);
+ ADD_TEST(test_output);
+ ADD_ALL_TESTS(test_bn_output, OSSL_NELEM(bn_output_tests));
}
diff --git a/test/testutil.h b/test/testutil.h
index 0975e2b779..027c7063b5 100644
--- a/test/testutil.h
+++ b/test/testutil.h
@@ -271,6 +271,7 @@ void test_error_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
void test_info(const char *file, int line, const char *desc, ...)
PRINTF_FORMAT(3, 4);
void test_info_c90(const char *desc, ...) PRINTF_FORMAT(1, 2);
+void test_note(const char *desc, ...) PRINTF_FORMAT(1, 2);
void test_openssl_errors(void);
/*
@@ -384,6 +385,7 @@ void test_openssl_errors(void);
# define TEST_error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
# define TEST_info(...) test_info(__FILE__, __LINE__, __VA_ARGS__)
# endif
+# define TEST_note test_note
# define TEST_openssl_errors test_openssl_errors
/*
@@ -403,6 +405,14 @@ extern BIO *bio_out;
extern BIO *bio_err;
/*
+ * Formatted output for strings, memory and bignums.
+ */
+void test_output_string(const char *name, const char *m, size_t l);
+void test_output_bignum(const char *name, const BIGNUM *bn);
+void test_output_memory(const char *name, const unsigned char *m, size_t l);
+
+
+/*
* Utilities to parse a test file.
*/
#define TESTMAXPAIRS 20
diff --git a/test/testutil/driver.c b/test/testutil/driver.c
index 3b62e59554..dc236b7d6b 100644
--- a/test/testutil/driver.c
+++ b/test/testutil/driver.c
@@ -196,6 +196,7 @@ int run_tests(const char *test_prog_name)
test_printf_stdout("%*s%s %d - %s\n", level, "", verdict, ii + 1,
test_title);
test_flush_stdout();
+ test_flush_stderr();
finalize(ret);
} else {
int num_failed_inner = 0;
diff --git a/test/testutil/format_output.c b/test/testutil/format_output.c
new file mode 100644
index 0000000000..ae5fdc99a9
--- /dev/null
+++ b/test/testutil/format_output.c
@@ -0,0 +1,528 @@
+#include "../testutil.h"
+#include "output.h"
+#include "tu_local.h"
+
+#include <string.h>
+#include <ctype.h>
+#include "../../e_os.h"
+
+/* The size of memory buffers to display on failure */
+#define MEM_BUFFER_SIZE (2000)
+#define MAX_STRING_WIDTH (80)
+#define BN_OUTPUT_SIZE (8)
+
+/* Output a diff header */
+static void test_diff_header(const char *left, const char *right)
+{
+ test_printf_stderr("%*s# --- %s\n", subtest_level(), "", left);
+ test_printf_stderr("%*s# +++ %s\n", subtest_level(), "", right);
+}
+
+/* Formatted string output routines */
+static void test_string_null_empty(const char *m, char c)
+{
+ if (m == NULL)
+ test_printf_stderr("%*s# % 4s %c NULL\n", subtest_level(), "", "", c);
+ else
+ test_printf_stderr("%*s# % 4u:%c ''\n", subtest_level(), "", 0u, c);
+}
+
+static void test_fail_string_common(const char *prefix, const char *file,
+ int line, const char *type,
+ const char *left, const char *right,
+ const char *op, const char *m1, size_t l1,
+ const char *m2, size_t l2)
+{
+ const int indent = subtest_level();
+ const size_t width = (MAX_STRING_WIDTH - indent - 12) / 16 * 16;
+ char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
+ char bdiff[MAX_STRING_WIDTH + 1];
+ size_t n1, n2, i;
+ unsigned int cnt = 0, diff;
+
+ test_fail_message_prefix(prefix, file, line, type, left, right, op);
+ if (m1 == NULL)
+ l1 = 0;
+ if (m2 == NULL)
+ l2 = 0;
+ if (l1 == 0 && l2 == 0) {
+ if ((m1 == NULL) == (m2 == NULL)) {
+ test_string_null_empty(m1, ' ');
+ } else {
+ test_diff_header(left, right);
+ test_string_null_empty(m1, '-');
+ test_string_null_empty(m2, '+');
+ }
+ goto fin;
+ }
+
+ if (l1 != l2 || strcmp(m1, m2) != 0)
+ test_diff_header(left, right);
+
+ while (l1 > 0 || l2 > 0) {
+ n1 = n2 = 0;
+ if (l1 > 0) {
+ b1[n1 = l1 > width ? width : l1] = 0;
<