New evp_test updates.

Print usage message.

Print expected and got values if mismatch.
Reviewed-by: Andy Polyakov <appro@openssl.org>
This commit is contained in:
Dr. Stephen Henson 2015-02-09 23:24:10 +00:00
parent 7303b472f1
commit b033e5d5ab

View file

@ -161,6 +161,10 @@ struct evp_test {
int ntests; int ntests;
/* Error count */ /* Error count */
int errors; int errors;
/* If output mismatch expected and got value */
unsigned char *out_got;
unsigned char *out_expected;
size_t out_len;
/* test specific data */ /* test specific data */
void *data; void *data;
}; };
@ -197,6 +201,27 @@ static const struct evp_test_method *evp_find_test(const char *name)
return NULL; return NULL;
} }
static void hex_print(const char *name, const unsigned char *buf, size_t len)
{
size_t i;
fprintf(stderr, "%s ", name);
for (i = 0; i < len; i++)
fprintf(stderr, "%02X", buf[i]);
fputs("\n", stderr);
}
static void print_expected(struct evp_test *t)
{
if (t->out_expected == NULL)
return;
hex_print("Expected:", t->out_expected, t->out_len);
hex_print("Got: ", t->out_got, t->out_len);
OPENSSL_free(t->out_expected);
OPENSSL_free(t->out_got);
t->out_expected = NULL;
t->out_got = NULL;
}
static int check_test_error(struct evp_test *t) static int check_test_error(struct evp_test *t)
{ {
if (!t->err && !t->expected_err) if (!t->err && !t->expected_err)
@ -204,6 +229,7 @@ static int check_test_error(struct evp_test *t)
if (t->err && !t->expected_err) { if (t->err && !t->expected_err) {
fprintf(stderr, "Test line %d: unexpected error %s\n", fprintf(stderr, "Test line %d: unexpected error %s\n",
t->start_line, t->err); t->start_line, t->err);
print_expected(t);
return 0; return 0;
} }
if (!t->err && t->expected_err) { if (!t->err && t->expected_err) {
@ -299,12 +325,32 @@ static int process_test(struct evp_test *t, char *buf, int verbose)
return 1; return 1;
} }
static int check_output(struct evp_test *t, const unsigned char *expected,
const unsigned char *got, size_t len)
{
if (!memcmp(expected, got, len))
return 0;
t->out_expected = BUF_memdup(expected, len);
t->out_got = BUF_memdup(got, len);
t->out_len = len;
if (t->out_expected == NULL || t->out_got == NULL) {
fprintf(stderr, "Memory allocation error!\n");
exit(1);
}
return 1;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *in = NULL; FILE *in = NULL;
char buf[10240]; char buf[10240];
struct evp_test t; struct evp_test t;
if (argc != 2) {
fprintf(stderr, "usage: evp_test testfile.txt\n");
return 1;
}
ERR_load_crypto_strings(); ERR_load_crypto_strings();
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
t.meth = NULL; t.meth = NULL;
@ -313,6 +359,9 @@ int main(int argc, char **argv)
t.start_line = -1; t.start_line = -1;
t.errors = 0; t.errors = 0;
t.ntests = 0; t.ntests = 0;
t.out_expected = NULL;
t.out_got = NULL;
t.out_len = 0;
in = fopen(argv[1], "r"); in = fopen(argv[1], "r");
while (fgets(buf, sizeof(buf), in)) { while (fgets(buf, sizeof(buf), in)) {
t.line++; t.line++;
@ -403,14 +452,14 @@ static int digest_test_run(struct evp_test *t)
if (md_len != mdata->output_len) if (md_len != mdata->output_len)
goto err; goto err;
err = "DIGEST_MISMATCH"; err = "DIGEST_MISMATCH";
if (memcmp(mdata->output, md, md_len)) if (check_output(t, mdata->output, md, md_len))
goto err; goto err;
err = NULL; err = NULL;
err: err:
if (mctx) if (mctx)
EVP_MD_CTX_destroy(mctx); EVP_MD_CTX_destroy(mctx);
t->err = err; t->err = err;
return err ? 0 : 1; return 1;
} }
static const struct evp_test_method digest_test_method = { static const struct evp_test_method digest_test_method = {
@ -611,7 +660,7 @@ static int cipher_test_enc(struct evp_test *t, int enc)
if (out_len != (size_t)(tmplen + tmpflen)) if (out_len != (size_t)(tmplen + tmpflen))
goto err; goto err;
err = "VALUE_MISMATCH"; err = "VALUE_MISMATCH";
if (memcmp(out, tmp, out_len)) if (check_output(t, out, tmp, out_len))
goto err; goto err;
if (enc && cdat->aead) { if (enc && cdat->aead) {
unsigned char rtag[16]; unsigned char rtag[16];
@ -625,7 +674,7 @@ static int cipher_test_enc(struct evp_test *t, int enc)
err = "TAG_RETRIEVE_ERROR"; err = "TAG_RETRIEVE_ERROR";
goto err; goto err;
} }
if (memcmp(cdat->tag, rtag, cdat->tag_len)) { if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
err = "TAG_VALUE_MISMATCH"; err = "TAG_VALUE_MISMATCH";
goto err; goto err;
} }