Merge branch 'unit-testing-framework' of https://github.com/JonathonReinhart/unicorn into JonathonReinhart-unit-testing-framework

This commit is contained in:
Nguyen Anh Quynh 2015-09-21 21:08:00 +08:00
commit d8c3b44cb6
4 changed files with 100 additions and 7 deletions

View file

@ -1,2 +1,3 @@
test_x86
test_mem_map
test_sanity

View file

@ -4,7 +4,7 @@ CFLAGS += -L ../../
CFLAGS += -lcmocka -lunicorn
CFLAGS += -I ../../include
ALL_TESTS = test_x86 test_mem_map
ALL_TESTS = test_sanity test_x86 test_mem_map
.PHONY: all
all: ${ALL_TESTS}
@ -16,10 +16,11 @@ clean:
.PHONY: test
test: export LD_LIBRARY_PATH=../../
test: ${ALL_TESTS}
./test_sanity
./test_x86
./test_mem_map
test_sanity: test_sanity.c
test_x86: test_x86.c
test_mem_map: test_mem_map.c

88
test/unit/test_sanity.c Normal file
View file

@ -0,0 +1,88 @@
#include "unicorn_test.h"
/* Make sure the uc_assert macros work with constants */
static void test_uc_assert_macros_constants(void **state)
{
const uc_err nomem = UC_ERR_NOMEM;
uc_assert_success(UC_ERR_OK);
uc_assert_err(UC_ERR_NOMEM, nomem);
uc_assert_fail(UC_ERR_VERSION);
}
/******************************************************************************/
static uc_err feedback(uc_err err, int *callcount)
{
assert_int_equal(++(*callcount), 1);
return err;
}
/**
* Make sure the uc_assert macros work with function calls
* and only evaluate them once!
*/
static void test_uc_assert_macros_func_calls(void **state)
{
int callcount;
callcount = 0;
uc_assert_success(feedback(UC_ERR_OK, &callcount));
callcount = 0;
uc_assert_err(UC_ERR_NOMEM, feedback(UC_ERR_NOMEM, &callcount));
callcount = 0;
uc_assert_fail(feedback(UC_ERR_VERSION, &callcount));
}
/******************************************************************************/
static void fail_uc_assert_success(void **state)
{
uc_assert_success(UC_ERR_NOMEM);
}
static void fail_uc_assert_err(void **state)
{
const uc_err ok = UC_ERR_OK;
uc_assert_err(UC_ERR_VERSION, ok);
}
static void fail_uc_assert_fail(void **state)
{
uc_assert_fail(UC_ERR_OK);
}
static void test_uc_assert_macros_fail(void **state)
{
/* A test-inside-a-test */
const struct CMUnitTest tests[] = {
/* these should all fail */
cmocka_unit_test(fail_uc_assert_success),
cmocka_unit_test(fail_uc_assert_err),
cmocka_unit_test(fail_uc_assert_fail),
};
print_message("\n\n--------------------------------------------------------------------------------\n");
print_message("START: Failure of the following tests is expected.\n\n");
assert_int_not_equal(0, cmocka_run_group_tests(tests, NULL, NULL));
print_message("\n\nEND: Failure of the preceding tests was expected.\n");
print_message("--------------------------------------------------------------------------------\n\n");
}
/******************************************************************************/
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_uc_assert_macros_constants),
cmocka_unit_test(test_uc_assert_macros_func_calls),
cmocka_unit_test(test_uc_assert_macros_fail),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View file

@ -7,16 +7,13 @@
#include <cmocka.h>
#include <unicorn/unicorn.h>
#define UC_ASSERT_ERR_ANY 0xDEADBEEF
/**
* Assert that err matches expect
*/
#define uc_assert_err(expect, err) \
do { \
uc_err __err = err; \
if ((__err != expect) \
|| (expect == UC_ASSERT_ERR_ANY && __err == UC_ERR_OK)) { \
if (__err != expect) { \
fail_msg("%s", uc_strerror(__err)); \
} \
} while (0)
@ -33,7 +30,13 @@ do { \
* as this serves to document which errors a function will return
* in various scenarios.
*/
#define uc_assert_fail(err) uc_assert_err(UC_ASSERT_ERR_ANY, err)
#define uc_assert_fail(err) \
do { \
uc_err __err = err; \
if (__err == UC_ERR_OK) { \
fail_msg("%s", uc_strerror(__err)); \
} \
} while (0)
#endif /* UNICORN_TEST_H */