6 * failtest - unit test helpers for testing malloc and other failures.
8 * The failtest module overrides various standard functions, and forks
9 * your unit test at those points to test failure paths. The failing
10 * child are expected to fail (eg. when malloc fails), but should not
11 * leak memory or crash. After including failtest_override.h, you can
12 * include failtest_restore.h to return to non-failing versions.
14 * The unit test is a normal CCAN tap-style test, except it should
15 * start by calling failtest_init() and end by calling
18 * You can control what functions fail: see failtest_hook.
24 * #include <ccan/tap/tap.h>
25 * #include <ccan/failtest/failtest_override.h>
26 * #include <ccan/failtest/failtest.h>
28 * int main(int argc, char *argv[])
32 * failtest_init(argc, argv);
35 * // Simple malloc test.
39 * memset(a, 'x', 100);
40 * b = realloc(a, 200);
42 * // Fill the rest of the memory.
43 * memset(b + 100, 'y', 100);
44 * // Check it got a copy of a as expected.
45 * ok1(strspn(b, "x") == 100);
48 * // Easy to miss: free a on realloc failure!
52 * failtest_exit(exit_status());
56 * Author: Rusty Russell <rusty@rustcorp.com.au>
58 * // valgrind seems to mess up rlimit.
59 * tests_pass_valgrind test/run-with-fdlimit.c:FAIL
61 int main(int argc, char *argv[])
66 if (strcmp(argv[1], "depends") == 0) {
67 printf("ccan/build_assert\n");
68 printf("ccan/compiler\n");
70 printf("ccan/hash\n");
71 printf("ccan/htable\n");
72 printf("ccan/list\n");
73 printf("ccan/read_write_all\n");
75 printf("ccan/time\n");
76 printf("ccan/tlist\n");