1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <assert.h>
5 #include <cmocka.h>
6 /* cmocka < 1.0 didn't support these features we need */
7 #ifndef assert_ptr_equal
8 #define assert_ptr_equal(a, b) \
9 _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
10 cast_ptr_to_largest_integral_type(b), \
11 __FILE__, __LINE__)
12 #define CMUnitTest UnitTest
13 #define cmocka_unit_test unit_test
14 #define cmocka_run_group_tests(t, setup, teardown) run_tests(t)
15 #endif
16
17
18 extern void mock_assert(const int result, const char* const expression,
19 const char * const file, const int line);
20 #undef assert
21 #define assert(expression) \
22 mock_assert((int)(expression), #expression, __FILE__, __LINE__);
23
24 #include "afl-fuzz.h"
25 #include "hash.h"
26
27 /* remap exit -> assert, then use cmocka's mock_assert
28 (compile with `--wrap=exit`) */
29 extern void exit(int status);
30 extern void __real_exit(int status);
31 void __wrap_exit(int status);
__wrap_exit(int status)32 void __wrap_exit(int status) {
33 (void)status;
34 assert(0);
35 }
36
37 /* ignore all printfs */
38 #undef printf
39 extern int printf(const char *format, ...);
40 extern int __real_printf(const char *format, ...);
41 int __wrap_printf(const char *format, ...);
__wrap_printf(const char * format,...)42 int __wrap_printf(const char *format, ...) {
43 (void)format;
44 return 1;
45 }
46
47 /* Rand with 0 seed would broke in the past */
test_hash(void ** state)48 static void test_hash(void **state) {
49 (void)state;
50
51 char bitmap[64] = {0};
52 u64 hash0 = hash64(bitmap, sizeof(bitmap), 0xa5b35705);
53
54 bitmap[10] = 1;
55 u64 hash1 = hash64(bitmap, sizeof(bitmap), 0xa5b35705);
56
57 assert_int_not_equal(hash0, hash1);
58
59 bitmap[10] = 0;
60 assert_int_equal(hash0, hash64(bitmap, sizeof(bitmap), 0xa5b35705));
61
62 bitmap[10] = 1;
63 assert_int_equal(hash1, hash64(bitmap, sizeof(bitmap), 0xa5b35705));
64
65 }
66
main(int argc,char ** argv)67 int main(int argc, char **argv) {
68 (void)argc;
69 (void)argv;
70
71 const struct CMUnitTest tests[] = {
72 cmocka_unit_test(test_hash)
73 };
74
75 //return cmocka_run_group_tests (tests, setup, teardown);
76 __real_exit( cmocka_run_group_tests (tests, NULL, NULL) );
77
78 // fake return for dumb compilers
79 return 0;
80 }
81