]> git.ozlabs.org Git - ccan/blob - ccan/failtest/_info
Merge branch 'io'
[ccan] / ccan / failtest / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * failtest - unit test helpers for testing malloc and other failures.
7  *
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.
13  *
14  * The unit test is a normal CCAN tap-style test, except it should
15  * start by calling failtest_init() and end by calling
16  * failtest_exit().  
17  *
18  * You can control what functions fail: see failtest_hook.
19  *
20  * Example:
21  *      #include <stdio.h>
22  *      #include <stdlib.h>
23  *      #include <string.h>
24  *      #include <ccan/tap/tap.h>
25  *      #include <ccan/failtest/failtest_override.h>
26  *      #include <ccan/failtest/failtest.h>
27  *
28  *      int main(int argc, char *argv[])
29  *      {
30  *              char *a, *b;
31  *
32  *              failtest_init(argc, argv);
33  *              plan_tests(3);
34  *
35  *              // Simple malloc test.
36  *              a = malloc(100);
37  *              if (ok1(a)) {
38  *                      // Fill the memory.
39  *                      memset(a, 'x', 100);
40  *                      b = realloc(a, 200);
41  *                      if (ok1(b)) {
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);
46  *                              free(b);
47  *                      } else {
48  *                              // Easy to miss: free a on realloc failure!
49  *                              free(a);
50  *                      }
51  *              }
52  *              failtest_exit(exit_status());
53  *      }
54  *
55  * License: LGPL
56  * Author: Rusty Russell <rusty@rustcorp.com.au>
57  * Ccanlint:
58  *      // valgrind seems to mess up rlimit.
59  *      tests_pass_valgrind test/run-with-fdlimit.c:FAIL
60  */
61 int main(int argc, char *argv[])
62 {
63         if (argc != 2)
64                 return 1;
65
66         if (strcmp(argv[1], "depends") == 0) {
67                 printf("ccan/build_assert\n");
68                 printf("ccan/compiler\n");
69                 printf("ccan/err\n");
70                 printf("ccan/hash\n");
71                 printf("ccan/htable\n");
72                 printf("ccan/read_write_all\n");
73                 printf("ccan/str\n");
74                 printf("ccan/time\n");
75                 printf("ccan/tlist\n");
76                 return 0;
77         }
78
79         return 1;
80 }