mirror of
https://github.com/AFLplusplus/AFLplusplus.git
synced 2025-06-10 01:01:33 +00:00
added (broken) list test
This commit is contained in:
parent
48fc106905
commit
dab429521b
13
Makefile
13
Makefile
@ -322,12 +322,21 @@ unit_maybe_alloc: test/unittests/unit_maybe_alloc.o
|
|||||||
$(CC) $(CFLAGS) -Wl,--wrap=exit -Wl,--wrap=printf test/unittests/unit_maybe_alloc.o -o test/unittests/unit_maybe_alloc $(LDFLAGS) -lcmocka
|
$(CC) $(CFLAGS) -Wl,--wrap=exit -Wl,--wrap=printf test/unittests/unit_maybe_alloc.o -o test/unittests/unit_maybe_alloc $(LDFLAGS) -lcmocka
|
||||||
./test/unittests/unit_maybe_alloc
|
./test/unittests/unit_maybe_alloc
|
||||||
|
|
||||||
|
test/unittests/unit_list.o : $(COMM_HDR) include/list.h test/unittests/unit_list.c $(AFL_FUZZ_FILES)
|
||||||
|
$(CC) $(CFLAGS) $(CFLAGS_FLTO) -c test/unittests/unit_list.c -o test/unittests/unit_list.o
|
||||||
|
|
||||||
|
unit_list: test/unittests/unit_list.o
|
||||||
|
$(CC) $(CFLAGS) -Wl,--wrap=exit -Wl,--wrap=printf $(LDFLAGS) test/unittests/unit_list.o -o test/unittests/unit_list -ldl -lcmocka
|
||||||
|
./test/unittests/unit_list
|
||||||
|
|
||||||
|
test/unittests/preallocable.o : $(COMM_HDR) include/afl-prealloc.h test/unittests/preallocable.c $(AFL_FUZZ_FILES)
|
||||||
|
$(CC) $(CFLAGS) $(CFLAGS_FLTO) -c test/unittests/preallocable.c -o test/unittests/preallocable.o
|
||||||
|
|
||||||
unit_preallocable: test/unittests/unit_preallocable.o
|
unit_preallocable: test/unittests/unit_preallocable.o
|
||||||
$(CC) $(CFLAGS) -Wl,--wrap=exit -Wl,--wrap=printf test/unittests/unit_preallocable.o -o test/unittests/unit_preallocable $(LDFLAGS) -lcmocka
|
$(CC) $(CFLAGS) -Wl,--wrap=exit -Wl,--wrap=printf test/unittests/unit_preallocable.o -o test/unittests/unit_preallocable $(LDFLAGS) -lcmocka
|
||||||
./test/unittests/unit_preallocable
|
./test/unittests/unit_preallocable
|
||||||
|
|
||||||
|
unit: unit_maybe_alloc unit_preallocable unit_list
|
||||||
unit: unit_maybe_alloc unit_preallocable
|
|
||||||
|
|
||||||
code-format:
|
code-format:
|
||||||
./.custom-format.py -i src/*.c
|
./.custom-format.py -i src/*.c
|
||||||
|
127
test/unittests/unit_list.c
Normal file
127
test/unittests/unit_list.c
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
/* cmocka < 1.0 didn't support these features we need */
|
||||||
|
#ifndef assert_ptr_equal
|
||||||
|
#define assert_ptr_equal(a, b) \
|
||||||
|
_assert_int_equal(cast_ptr_to_largest_integral_type(a), \
|
||||||
|
cast_ptr_to_largest_integral_type(b), \
|
||||||
|
__FILE__, __LINE__)
|
||||||
|
#define CMUnitTest UnitTest
|
||||||
|
#define cmocka_unit_test unit_test
|
||||||
|
#define cmocka_run_group_tests(t, setup, teardown) run_tests(t)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
extern void mock_assert(const int result, const char* const expression,
|
||||||
|
const char * const file, const int line);
|
||||||
|
#undef assert
|
||||||
|
#define assert(expression) \
|
||||||
|
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
/* remap exit -> assert, then use cmocka's mock_assert
|
||||||
|
(compile with `--wrap=exit`) */
|
||||||
|
extern void exit(int status);
|
||||||
|
extern void __real_exit(int status);
|
||||||
|
void __wrap_exit(int status) {
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ignore all printfs */
|
||||||
|
extern int printf(const char *format, ...);
|
||||||
|
extern int __real_printf(const char *format, ...);
|
||||||
|
int __wrap_printf(const char *format, ...) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
list_t testlist;
|
||||||
|
|
||||||
|
static void test_contains(void **state) {
|
||||||
|
|
||||||
|
u32 one = 1;
|
||||||
|
u32 two = 2;
|
||||||
|
|
||||||
|
list_append(&testlist, &one);
|
||||||
|
assert_true(list_contains(&testlist, &one));
|
||||||
|
assert_false(list_contains(&testlist, &two));
|
||||||
|
list_remove(&testlist, &one);
|
||||||
|
assert_false(list_contains(&testlist, &one));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_foreach(void **state) {
|
||||||
|
|
||||||
|
u32 one = 1;
|
||||||
|
u32 two = 2;
|
||||||
|
u32 result = 0;
|
||||||
|
|
||||||
|
list_append(&testlist, &one);
|
||||||
|
list_append(&testlist, &two);
|
||||||
|
list_append(&testlist, &one);
|
||||||
|
|
||||||
|
/* The list is for pointers, so int doesn't work as type directly */
|
||||||
|
LIST_FOREACH(&testlist, u32, {
|
||||||
|
result += *el;
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_int_equal(result, 4);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_long_list(void **state) {
|
||||||
|
|
||||||
|
u32 result1 = 0;
|
||||||
|
u32 result2 = 0;
|
||||||
|
u32 i;
|
||||||
|
|
||||||
|
u32 vals[100];
|
||||||
|
|
||||||
|
for (i = 0; i < 100; i++) {
|
||||||
|
vals[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < 100; i++) {
|
||||||
|
list_append(&testlist, &vals[i]);
|
||||||
|
}
|
||||||
|
LIST_FOREACH(&testlist, u32, {
|
||||||
|
result1 += *el;
|
||||||
|
});
|
||||||
|
printf("removing %d\n", vals[50]);
|
||||||
|
list_remove(&testlist, &vals[50]);
|
||||||
|
|
||||||
|
LIST_FOREACH(&testlist, u32, {
|
||||||
|
printf("var: %d\n", *el);
|
||||||
|
result2 += *el;
|
||||||
|
});
|
||||||
|
assert_int_not_equal(result1, result2);
|
||||||
|
assert_int_equal(result1 + 50, result2);
|
||||||
|
|
||||||
|
result1 = 0;
|
||||||
|
LIST_FOREACH_CLEAR(&testlist, u32, {
|
||||||
|
result1 += *el;
|
||||||
|
});
|
||||||
|
assert_int_equal(result1, result2);
|
||||||
|
|
||||||
|
result1 = 0;
|
||||||
|
LIST_FOREACH(&testlist, u32, {
|
||||||
|
result1 += *el;
|
||||||
|
});
|
||||||
|
assert_int_equal(result1, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_contains),
|
||||||
|
cmocka_unit_test(test_foreach),
|
||||||
|
cmocka_unit_test(test_long_list),
|
||||||
|
};
|
||||||
|
|
||||||
|
//return cmocka_run_group_tests (tests, setup, teardown);
|
||||||
|
return cmocka_run_group_tests (tests, NULL, NULL);
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user