Commit c450b9a2 authored by Davide Galassi's avatar Davide Galassi

Tests are now using TRACE macro

parent 8183b5f1
Pipeline #65256332 passed with stages
in 2 minutes and 46 seconds
......@@ -274,14 +274,14 @@ static void dispatch(int argc, char *argv[])
else if (strcmp(test, "aes_gcm_decrypt") == 0)
aes_gcm_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void aes_test(void)
{
printf("* AES NIST AESAVS KAT\n");
TRACE("* AES NIST AESAVS KAT\n");
func_test("aes_kat_test.data", dispatch);
printf("* AES GCM NIST Validation\n");
TRACE("* AES GCM NIST Validation\n");
func_test("aes_gcm_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -65,7 +65,7 @@ static void dispatch(int argc, char *argv[])
else if (strcmp(test, "affine_decrypt") == 0)
affine_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void affine_test(void)
......@@ -80,7 +80,7 @@ void affine_test(void)
cry_affine_encrypt(&ctx, ciphertext, (unsigned char *)"HelloWorld", 10);
cry_affine_decrypt(&ctx, cleartext, ciphertext, 10);
printf("* Affine Test\n");
TRACE("* Affine Test\n");
func_test("affine_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -38,14 +38,14 @@ static void base64_dispatch(int argc, char *argv[])
else if (strcmp(test, "decode") == 0)
decode(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void base64_test(void)
{
printf("* Base-64\n");
TRACE("* Base-64\n");
func_test("base64_test.data", base64_dispatch);
printf("\n");
TRACE("\n");
}
......@@ -55,7 +55,7 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "cmac_aes_digest") == 0)
cmac_aes_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
......@@ -151,7 +151,7 @@ static void cmac_aes_last_incomplete(void)
void cmac_test(void)
{
printf("* CMAC Tests\n");
TRACE("* CMAC Tests\n");
run("CMAC-AES First Sliced #1", cmac_aes_first_sliced1, NULL, NULL);
run("CMAC-AES First Sliced #2", cmac_aes_first_sliced2, NULL, NULL);
run("CMAC-AES Mid Sliced #1", cmac_aes_mid_sliced, NULL, NULL);
......@@ -159,5 +159,5 @@ void cmac_test(void)
run("CMAC-AES Last Incomplete", cmac_aes_last_incomplete, NULL, NULL);
func_test("cmac_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -202,17 +202,17 @@ void crc_test(void)
{
int i;
printf("* CRC16-CCITT\n");
TRACE("* CRC16-CCITT\n");
for (i = 0; i < ARLEN(crc16_ccitt_tests); i++)
run(crc16_ccitt_tests[i].name, crc16_ccitt_tests[i].func, NULL, NULL);
printf("* CRC16-IBM\n");
TRACE("* CRC16-IBM\n");
for (i = 0; i < ARLEN(crc16_ibm_tests); i++)
run(crc16_ibm_tests[i].name, crc16_ibm_tests[i].func, NULL, NULL);
printf("* CRC16-DNP\n");
TRACE("* CRC16-DNP\n");
for (i = 0; i < ARLEN(crc16_dnp_tests); i++)
run(crc16_dnp_tests[i].name, crc16_dnp_tests[i].func, NULL, NULL);
printf("* CRC32-ETH\n");
TRACE("* CRC32-ETH\n");
for (i = 0; i < ARLEN(crc32_eth_tests); i++)
run(crc32_eth_tests[i].name, crc32_eth_tests[i].func, NULL, NULL);
printf("\n");
TRACE("\n");
}
......@@ -222,12 +222,12 @@ static void dispatch(int argc, char *argv[])
else if (strcmp(test, "des_gcm_decrypt") == 0)
des_gcm_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void des_test(void)
{
printf("* DES OpenSSL Vectors\n");
TRACE("* DES OpenSSL Vectors\n");
func_test("des_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -48,12 +48,12 @@ static void add_test(void)
simple_curve_init(&grp);
cry_ecp_init(&p);
do {
//printf("n = %d\n", i);
//TRACE("n = %d\n", i);
//cry_mpi_print(&p.x, 10);
//cry_mpi_print(&p.y, 10);
//cry_mpi_print(&p.z, 10);
point_check(&p, &grp);
//printf("--------------------\n");
//TRACE("--------------------\n");
cry_ecp_add(&p, &p, &grp.g, &grp);
i++;
} while (!cry_ecp_is_zero(&p));
......@@ -218,7 +218,7 @@ static void curve_params_load(void)
void ecp_test(void)
{
printf("* ECP load curves parameters\n");
TRACE("* ECP load curves parameters\n");
curve_params_load();
add_test();
mul_test();
......
......@@ -72,13 +72,13 @@ static void dispatch(int argc, char *argv[])
else if (strcmp(test, "hill_decrypt") == 0)
hill_decrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void hill_test(void)
{
printf("* HILL Test\n");
TRACE("* HILL Test\n");
func_test("hill_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -57,12 +57,12 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "hmac_digest") == 0)
hmac_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void hmac_test(void)
{
printf("* HMAC NIST CAVS 11.0\n");
TRACE("* HMAC NIST CAVS 11.0\n");
func_test("hmac_test.data", dispatch);
printf("\n");
TRACE("\n");
}
#include "malloc_mock.h"
#include "test.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
......@@ -62,7 +63,7 @@ void *__wrap_realloc(void *ptr, size_t size)
head = ((mem_head_t *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: realloc of a not alloced buffer\n");
TRACE(">>> Warning: realloc of a not alloced buffer\n");
return NULL;
}
if (size < head->size)
......@@ -96,7 +97,7 @@ void __wrap_free(void *ptr)
return;
head = ((mem_head_t *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: free of a not alloced buffer\n");
TRACE(">>> Warning: free of a not alloced buffer\n");
return;
}
memset(head, 0xFF, HEAD_SIZE + head->size);
......
......@@ -38,12 +38,12 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "md5_digest") == 0)
md5_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void md5_test(void)
{
printf("* MD5 RFC 1321 Vectors\n");
TRACE("* MD5 RFC 1321 Vectors\n");
func_test("md5_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -135,9 +135,9 @@ void memxor_test(void)
{
int i;
printf("* XOR\n");
TRACE("* XOR\n");
ASSERT(USED_MAX < BUFSIZ);
for (i = 0; i < NTESTS; i++)
run(tests[i].name, tests[i].func, setup, NULL);
printf("\n");
TRACE("\n");
}
......@@ -267,7 +267,7 @@ static void mpi_dispatch(int argc, char *argv[])
else if (strcmp(test, "mpi_sqr") == 0)
mpi_unary_op(argc, argv, cry_mpi_sqr);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
mpi_teardown();
}
......@@ -283,9 +283,9 @@ static struct malloc_fail_args g_malloc_fail_tests[] = {
void mpi_test(void)
{
printf("* MPI\n");
TRACE("* MPI\n");
func_test("mpi_test.data", mpi_dispatch);
malloc_fail_tests(g_malloc_fail_tests,
ARLEN(g_malloc_fail_tests), mpi_dispatch);
printf("\n");
TRACE("\n");
}
......@@ -193,15 +193,15 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "rsa_pkcs1_encrypt") == 0)
rsa_pkcs1_encrypt(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void rsa_test(void)
{
printf("* RSA Test\n");
TRACE("* RSA Test\n");
run("Keygen 512", keygen, NULL, NULL);
run("Encrypt-Decrypt", encrypt_decrypt, NULL, NULL);
run("Sign-Verify", sign_verify, NULL, NULL);
func_test("rsa_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -38,12 +38,12 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "sha1_digest") == 0)
sha1_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void sha1_test(void)
{
printf("* SHA1 NIST CAVS Vectors\n");
TRACE("* SHA1 NIST CAVS Vectors\n");
func_test("sha1_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -38,12 +38,12 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "sha256_digest") == 0)
sha256_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void sha256_test(void)
{
printf("* SHA256 NIST CAVS Vectors\n");
TRACE("* SHA256 NIST CAVS Vectors\n");
func_test("sha256_test.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -93,7 +93,7 @@ void func_test(const char *datafile, dispatch_func_t dispatch)
file = fopen(datafile, "r");
if (file == NULL) {
printf("Error: data file \"%s\" not found\n", datafile);
TRACE("Error: data file \"%s\" not found\n", datafile);
return;
}
......@@ -105,7 +105,7 @@ void func_test(const char *datafile, dispatch_func_t dispatch)
continue;
left = sizeof(argbuf);
if (g_verbose != 0)
printf(" %s\n", argbuf);
TRACE(" %s\n", argbuf);
/* Collect test function name and parameters */
cnt = strlen(argbuf) + 1;
curr = argbuf + cnt;
......@@ -117,7 +117,7 @@ void func_test(const char *datafile, dispatch_func_t dispatch)
params[i] = curr;
cnt = strlen(params[i]) + 1;
if (cnt > left) {
printf("Args buffer memory exhausted... skip test set\n");
TRACE("Args buffer memory exhausted... skip test set\n");
fclose(file);
return;
}
......@@ -129,7 +129,7 @@ void func_test(const char *datafile, dispatch_func_t dispatch)
dispatch(i, params);
g_runs++;
if (g_fails != fails)
printf(" %s\n", argbuf);
TRACE(" %s\n", argbuf);
}
fclose(file);
}
......@@ -142,13 +142,13 @@ void run(const char *name, void (* test)(void),
g_runs++;
if (g_verbose != 0)
printf(" %s\n", name);
TRACE(" %s\n", name);
if (setup != NULL)
setup();
fails = g_fails;
test();
if (g_fails != fails)
printf(" %s\n", name);
TRACE(" %s\n", name);
if (teardown != NULL)
teardown();
}
......@@ -159,10 +159,10 @@ void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
size_t i, fail_after;
if (g_verbose != 0)
printf(" Malloc fails\n");
TRACE(" Malloc fails\n");
for (i = 0; i < num; i++) {
if (g_verbose != 0)
printf(" %s \n", args[i].argv[0]);
TRACE(" %s \n", args[i].argv[0]);
fail_after = 0;
do {
g_malloc_mock_state = MALLOC_MOCK_READY;
......@@ -170,7 +170,7 @@ void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
dispatch(args[i].argc - 1, args[i].argv + 1);
} while (g_malloc_mock_state == MALLOC_MOCK_FAILED);
if (g_verbose != 0)
printf(" fail-counter: %u\n", (unsigned)fail_after);
TRACE(" fail-counter: %u\n", (unsigned)fail_after);
}
g_malloc_mock_state = MALLOC_MOCK_STOPPED;
}
......@@ -229,12 +229,12 @@ int raw_init(unsigned char *raw, size_t rawlen, const char *asc)
static void help(const char *arg)
{
if (arg != NULL)
printf("Error: unsupported option '%s'\n", arg);
printf("\nUsage: test [options] [tests]\n");
printf(" -h help\n");
printf(" -l show cases\n");
printf(" -v verbose\n");
printf("\n");
TRACE("Error: unsupported option '%s'\n", arg);
TRACE("\nUsage: test [options] [tests]\n");
TRACE(" -h help\n");
TRACE(" -l show cases\n");
TRACE(" -v verbose\n");
TRACE("\n");
exit(0);
}
......@@ -242,10 +242,10 @@ static void show_cases(void)
{
int i;
printf("\nTest cases:\n");
TRACE("\nTest cases:\n");
for (i = 0; i < NTESTS; i++)
printf(" %s\n", g_tests[i].name);
printf("\n");
TRACE(" %s\n", g_tests[i].name);
TRACE("\n");
exit(0);
}
......@@ -279,6 +279,12 @@ static void parse_args(int argc, char *argv[])
}
}
void cry_contract_fail(const char *cond, const char *file, int line)
{
TRACE("Contract fail: %s @ %s:%d\n", cond, file, line);
g_fails++;
}
int main(int argc, char *argv[])
{
int i;
......@@ -291,17 +297,17 @@ int main(int argc, char *argv[])
/* Parse arguments */
parse_args(argc, argv);
printf("\nC R Y T E S T\n\n");
TRACE("\nC R Y T E S T\n\n");
for (i = 0; i < NTESTS; i++) {
if (g_test_skip[i] == 0)
g_tests[i].func();
}
printf("\n");
printf("|| Tests: %d\n", g_runs);
printf("|| Fails: %d\n", g_fails);
printf("\n-------------------------------------\n\n");
TRACE("\n");
TRACE("|| Tests: %d\n", g_runs);
TRACE("|| Fails: %d\n", g_fails);
TRACE("\n-------------------------------------\n\n");
return (g_fails != 0);
}
......@@ -45,10 +45,17 @@ void run(const char *name, void (* test)(void),
#define RUN(test) run(#test, test, NULL, NULL)
#define TRACE(...) do { \
fprintf(stdout, __VA_ARGS__); \
fflush(stdout); \
} while(0)
#define ASSERT(test) do { \
if ((test) == 0) { \
g_fails++; \
printf("!!! ASSERTION FAIL (%s:%d): %s\n", \
TRACE("!!! ASSERTION FAIL (%s:%d): %s\n", \
__FILE__, __LINE__, #test); \
return; \
} \
......@@ -80,16 +87,16 @@ void run(const char *name, void (* test)(void),
#define PRINT_HEX(msg, buf, siz) do { \
size_t __siz = siz; \
unsigned char *__p = buf; \
if (msg) printf("%s: ", msg); \
while (__siz-- > 0) printf("%02x", *__p++); \
printf("\n"); \
if (msg) TRACE("%s: ", msg); \
while (__siz-- > 0) TRACE("%02x", *__p++); \
TRACE("\n"); \
} while(0)
#define PRINT_ASC(msg, buf, siz) \
printf("%s: %.*s\n", msg, (int)(siz), buf)
TRACE("%s: %.*s\n", msg, (int)(siz), buf)
#define PRINT_MPI(msg, mpi, rad) do { \
printf("%s:\t", msg); \
TRACE("%s:\t", msg); \
cry_mpi_print(mpi, rad); \
} while(0)
......
......@@ -49,12 +49,12 @@ static void dispatch(int argc, char *argv[])
if (strcmp(test, "trivium_operate") == 0)
trivium_operate(argc, argv);
else
printf("Test '%s' not defined\n", test);
TRACE("Test '%s' not defined\n", test);
}
void trivium_test(void)
{
printf("* TRIVIUM ECRYPT Vectors\n");
TRACE("* TRIVIUM ECRYPT Vectors\n");
func_test("trivium_ecrypt.data", dispatch);
printf("\n");
TRACE("\n");
}
......@@ -3,7 +3,7 @@
void version_test(void)
{
printf("* Version: %d.%d.%d\n", CRY_MAJOR, CRY_MINOR, CRY_PATCH);
TRACE("* Version: %d.%d.%d\n", CRY_MAJOR, CRY_MINOR, CRY_PATCH);
ASSERT(cry_version() == CRY_VERSION);
printf("\n");
TRACE("\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment