Commit 2180ba81 authored by Davide Galassi's avatar Davide Galassi

Malloc tests code shared between tests

parent d6b1d9a2
Pipeline #57974016 passed with stages
in 1 minute and 32 seconds
......@@ -3,7 +3,7 @@
#include <stdio.h>
int g_malloc_mock_count = -1;
int g_malloc_mock_state = MALLOC_MOCK_IDLE;
int g_malloc_mock_state = MALLOC_MOCK_STOPPED;
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
......
#ifndef MALLOC_MOCK_H_
#define MALLOC_MOCK_H_
#define MALLOC_MOCK_IDLE 0
#define MALLOC_MOCK_ACTIVE 1
#define MALLOC_MOCK_FAILED 2
#define MALLOC_MOCK_STOPPED 0
#define MALLOC_MOCK_READY 1
#define MALLOC_MOCK_ACTIVE 2
#define MALLOC_MOCK_FAILED 3
extern int g_malloc_mock_count;
extern int g_malloc_mock_state;
......
......@@ -146,9 +146,7 @@ static void mpi_sqr(int argc, char *argv[])
static void check(int res, cry_mpi *num, char *res_str)
{
if (strcmp(res_str, "OOM") == 0) {
ASSERT ((res == -1 && g_malloc_mock_state == MALLOC_MOCK_FAILED) || res == 0);
} else if (*res_str != ERROR_FLAG) {
if (*res_str != ERROR_FLAG) {
ASSERT(res == 0);
ASSERT(cry_mpi_store_str(num, 16, (char *)g_buf) == 0);
ASSERT(strcmp((char *)g_buf, res_str) == 0);
......@@ -157,6 +155,17 @@ static void check(int res, cry_mpi *num, char *res_str)
}
}
#define RUNM(test) do { \
if (g_malloc_mock_state == MALLOC_MOCK_READY) \
g_malloc_mock_state = MALLOC_MOCK_ACTIVE; \
test; \
if (g_malloc_mock_state == MALLOC_MOCK_FAILED) { \
ASSERT(res == -1); \
return; \
} \
} while (0)
typedef int (* binary_op_f)(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
......@@ -168,9 +177,7 @@ static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
ASSERT(cry_mpi_load_str(g_mpi0, 16, argv[0]) == 0);
ASSERT(cry_mpi_load_str(g_mpi1, 16, argv[1]) == 0);
if (strcmp(argv[2], "OOM") == 0)
g_malloc_mock_state = MALLOC_MOCK_ACTIVE;
res = op(g_mpi2, g_mpi0, g_mpi1);
RUNM(res = op(g_mpi2, g_mpi0, g_mpi1));
check(res, g_mpi2, argv[2]);
}
......@@ -188,9 +195,7 @@ static void mpi_binary_mod_op(int argc, char *argv[], binary_mod_op_f op)
ASSERT(cry_mpi_load_str(g_mpi1, 16, argv[1]) == 0);
ASSERT(cry_mpi_load_str(g_mpi2, 16, argv[2]) == 0);
if (strcmp(argv[2], "OOM") == 0)
g_malloc_mock_state = MALLOC_MOCK_ACTIVE;
res = op(g_mpi3, g_mpi0, g_mpi1, g_mpi2);
RUNM(res = op(g_mpi3, g_mpi0, g_mpi1, g_mpi2));
check(res, g_mpi3, argv[3]);
}
......@@ -245,41 +250,19 @@ static void mpi_dispatch(int argc, char *argv[])
mpi_teardown();
}
struct args {
char *name;
int argc;
char *argv[10];
};
static struct args g_malloc_fail_tests[] = {
{ "Comba", 4, { "mpi_mul_comba", "12345678", "12345678", "OOM" }},
{ "Karatsuba", 4, { "mpi_mul_karatsuba", "12345678", "12345678", "OOM" }},
{ "Toom-3", 4, { "mpi_mul_toom3", "12345678", "12345678", "OOM" }},
static struct malloc_fail_args g_malloc_fail_tests[] = {
{ 5, { "Comba", "mpi_mul_comba", "12345678", "12345678", "14b66dc1df4d840" }},
{ 5, { "Karatsuba", "mpi_mul_karatsuba", "12345678", "12345678", "14b66dc1df4d840" }},
{ 5, { "Toom-3", "mpi_mul_toom3", "12345678", "12345678", "14b66dc1df4d840" }},
};
static void malloc_fails(void)
{
size_t t, i;
printf(" Malloc fails\n");
for (t = 0; t < ARLEN(g_malloc_fail_tests); t++) {
printf(" %s \n", g_malloc_fail_tests[t].name);
i = 0;
do {
g_malloc_mock_state = MALLOC_MOCK_IDLE;
g_malloc_mock_count = i++;
mpi_dispatch(g_malloc_fail_tests[t].argc, g_malloc_fail_tests[t].argv);
} while (g_malloc_mock_state == MALLOC_MOCK_FAILED);
printf(" count: %u\n", (unsigned)i);
g_malloc_mock_state = MALLOC_MOCK_IDLE; /* restore normal behavior */
}
}
void mpi_test(void)
{
printf("* MPI\n");
func_test("mpi_test.data", mpi_dispatch);
malloc_fails();
malloc_fail_tests(g_malloc_fail_tests, ARLEN(g_malloc_fail_tests),
mpi_dispatch);
printf("\n");
}
#include "test.h"
#include "malloc_mock.h"
void version_test(void);
void memxor_test(void);
......@@ -146,6 +147,24 @@ void run(const char *name, void (* test)(void),
teardown();
}
void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
dispatch_func_t dispatch)
{
size_t i, fail_after;
printf(" Malloc fails\n");
for (i = 0; i < num; i++) {
printf(" %s \n", args[i].argv[0]);
fail_after = 0;
do {
g_malloc_mock_state = MALLOC_MOCK_READY;
g_malloc_mock_count = fail_after++;
dispatch(args[i].argc - 1, args[i].argv + 1);
} while (g_malloc_mock_state == MALLOC_MOCK_FAILED);
printf(" count: %u\n", (unsigned)fail_after);
}
g_malloc_mock_state = MALLOC_MOCK_STOPPED;
}
void asc_to_raw(const char *asc, size_t size, unsigned char *raw)
{
......
......@@ -16,11 +16,20 @@ struct test_case {
test_func_t func;
};
typedef void (* dispatch_func_t)(int argc, char *argv[]);
typedef void (* dispatch_func_t)(int argc, char *argv[]);
void func_test(const char *datafile, dispatch_func_t dispatch);
struct malloc_fail_args {
int argc;
char *argv[10];
};
void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
dispatch_func_t dispatch);
void asc_to_raw(const char *asc, size_t size, unsigned char *raw);
int raw_init(unsigned char *raw, size_t rawlen, const char *asc);
......
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