From cecbbb88fc585e5fd5691b741c8dc0d29655f96c Mon Sep 17 00:00:00 2001 From: Patrick Pelletier Date: Sun, 25 Sep 2011 18:04:05 -0700 Subject: [PATCH] When a test fails, print the algorithm that it failed on. As near as I can tell, LibTomCrypt doesn't provide any way to tell which cipher failed when it reports a cipher test failure. For example, I was getting: Algorithm failed test vectors. (5) cipher_hash_test.c:14:cipher_descriptor[x].test() But there's no way to tell what value x has, and even if there was, it would take a bit of digging to determine which algorithm that corresponds to. So, I added a variant of the DO() macro, DOX(), which takes an additional string argument which is displayed on failure. So now I get: Algorithm failed test vectors. (5) - camellia cipher_hash_test.c:14:cipher_descriptor[x].test() --- testprof/cipher_hash_test.c | 18 +++++++++--------- testprof/test_driver.c | 7 +++++-- testprof/tomcrypt_test.h | 8 +++++--- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/testprof/cipher_hash_test.c b/testprof/cipher_hash_test.c index 666d913..dba7d9a 100644 --- a/testprof/cipher_hash_test.c +++ b/testprof/cipher_hash_test.c @@ -11,25 +11,25 @@ int cipher_hash_test(void) /* test ciphers */ for (x = 0; cipher_descriptor[x].name != NULL; x++) { - DO(cipher_descriptor[x].test()); + DOX(cipher_descriptor[x].test(), cipher_descriptor[x].name); } /* test hashes */ for (x = 0; hash_descriptor[x].name != NULL; x++) { - DO(hash_descriptor[x].test()); + DOX(hash_descriptor[x].test(), hash_descriptor[x].name); } /* test prngs (test, import/export */ for (x = 0; prng_descriptor[x].name != NULL; x++) { - DO(prng_descriptor[x].test()); - DO(prng_descriptor[x].start(&nprng)); - DO(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng)); - DO(prng_descriptor[x].ready(&nprng)); + DOX(prng_descriptor[x].test(), prng_descriptor[x].name); + DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name); + DOX(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng), prng_descriptor[x].name); + DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name); n = sizeof(buf); - DO(prng_descriptor[x].pexport(buf, &n, &nprng)); + DOX(prng_descriptor[x].pexport(buf, &n, &nprng), prng_descriptor[x].name); prng_descriptor[x].done(&nprng); - DO(prng_descriptor[x].pimport(buf, n, &nprng)); - DO(prng_descriptor[x].ready(&nprng)); + DOX(prng_descriptor[x].pimport(buf, n, &nprng), prng_descriptor[x].name); + DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name); if (prng_descriptor[x].read(buf, 100, &nprng) != 100) { fprintf(stderr, "Error reading from imported PRNG!\n"); exit(EXIT_FAILURE); diff --git a/testprof/test_driver.c b/testprof/test_driver.c index 6e54668..b728f91 100644 --- a/testprof/test_driver.c +++ b/testprof/test_driver.c @@ -1,9 +1,12 @@ #include -void run_cmd(int res, int line, char *file, char *cmd) +void run_cmd(int res, int line, char *file, char *cmd, const char *algorithm) { if (res != CRYPT_OK) { - fprintf(stderr, "%s (%d)\n%s:%d:%s\n", error_to_string(res), res, file, line, cmd); + fprintf(stderr, "%s (%d)%s%s\n%s:%d:%s\n", + error_to_string(res), res, + (algorithm ? " - " : ""), (algorithm ? algorithm : ""), + file, line, cmd); if (res != CRYPT_NOP) { exit(EXIT_FAILURE); } diff --git a/testprof/tomcrypt_test.h b/testprof/tomcrypt_test.h index 237eee3..84e173c 100644 --- a/testprof/tomcrypt_test.h +++ b/testprof/tomcrypt_test.h @@ -36,12 +36,14 @@ typedef struct { extern prng_state yarrow_prng; -void run_cmd(int res, int line, char *file, char *cmd); +void run_cmd(int res, int line, char *file, char *cmd, const char *algorithm); #ifdef LTC_VERBOSE -#define DO(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x), __LINE__, __FILE__, #x); } while (0); +#define DO(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0); +#define DOX(x, str) do { fprintf(stderr, "%s - %s:\n", #x, (str)); run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0); #else -#define DO(x) do { run_cmd((x), __LINE__, __FILE__, #x); } while (0); +#define DO(x) do { run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0); +#define DOX(x, str) do { run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0); #endif /* TESTS */