Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-colomar committed Dec 5, 2023
1 parent ed66515 commit 7aca7b4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
2 changes: 2 additions & 0 deletions tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ test_strtcpy_LDADD = \
$(NULL)

test_x_SOURCES = \
../../lib/shadowlog.c \
test_x.c \
$(NULL)
test_x_CFLAGS = \
$(AM_FLAGS) \
$(NULL)
test_x_LDFLAGS = \
-Wl,-wrap,vasprintf \
-Wl,-wrap,exit \
$(NULL)
test_x_LDADD = \
Expand Down
104 changes: 65 additions & 39 deletions tests/unit/test_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
#include <stdint.h> // Required by <cmocka.h>
#include <cmocka.h>

#include "attr.h"
#include "x.h"


#define assert_unreachable() assert_true(0)

#define eval(e) e

#define X_CALLED (-36)
#define EXIT_CALLED (42)
#define TEST_OK (-6)
Expand All @@ -28,18 +31,26 @@
static jmp_buf jmpb;


/**********************
* WRAPPERS
**********************/
int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
void __wrap_exit(int status);

static void test_x_1(unused void **state);
static void test_x_null(unused void **state);
static void test_x_ok(unused void **state);

static int ret_n(int n);
static void *ret_p(void *p);


int
__wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap)
main(void)
{
return mock() == -1 ? -1 : __real_vasprintf(p, fmt, ap);
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_x_1),
cmocka_unit_test(test_x_null),
cmocka_unit_test(test_x_ok),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}


Expand All @@ -50,65 +61,80 @@ __wrap_exit(int status)
}


/**********************
* TEST
**********************/
static void test_x_exit(void **state);
static void test_x_ok(void **state);
static void
test_x_1(void **state)
{
volatile int n;

n = 0;

switch (setjmp(jmpb)) {
case 0:
n = X_CALLED;
n = x(ret_n(-1));
assert_unreachable();
break;
case EXIT_CALLED:
assert_true(n == X_CALLED);
n = TEST_OK;
break;
default:
assert_unreachable();
break;
}

assert_true(n == TEST_OK);
}


static void
test_x_exit(void **state)
test_x_null(void **state)
{
volatile int len;
char *volatile p;
volatile void *p;

will_return(__wrap_vasprintf, -1);

len = 0;
p = &setjmp;

switch (setjmp(jmpb)) {
case 0:
len = X_CALLED;
len = x(asprintf(&p, "foo%s", "bar"));
p = &ret_p;
p = x(ret_p(NULL));
assert_unreachable();
break;
case EXIT_CALLED:
assert_int_equal(len, X_CALLED);
len = TEST_OK;
assert_true(p == &ret_p);
p = &exit;
break;
default:
assert_unreachable();
break;
}

assert_int_equal(len, TEST_OK);
assert_true(p == &exit);
}


static void
test_x_ok(void **state)
{
int len;
char *p;
assert_true(x(ret_n(0)) == 0);
assert_true(x(ret_n(42)) == 42);
assert_true(x(ret_n(-6)) == -6);
assert_true(x(eval(36)) == 36);
assert_true(x(ret_p(&ret_n)) == ret_n);
assert_true(x(ret_p(&jmp_buf)) == &jmp_buf);
assert_true(x(eval(&jmp_buf)) == &jmp_buf);
}

// Trick: it will actually return the length, not 0.
will_return(__wrap_vasprintf, 0);

len = x(asprintf(&p, "foo%d%s", 1, "bar"));
assert_int_equal(len, strlen("foo1bar"));
assert_string_equal(p, "foo1bar");
free(p);
static int
ret_n(int n)
{
return n;
}


int
main(void)
static void *
ret_p(void *p)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_x_exit),
cmocka_unit_test(test_x_ok),
};

return cmocka_run_group_tests(tests, NULL, NULL);
return p;
}

0 comments on commit 7aca7b4

Please sign in to comment.