]> git.ozlabs.org Git - ccan/blob - ccan/failtest/_info
failtest: record close events
[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  *              void *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  */
58 int main(int argc, char *argv[])
59 {
60         if (argc != 2)
61                 return 1;
62
63         if (strcmp(argv[1], "depends") == 0) {
64                 printf("ccan/compiler\n");
65                 printf("ccan/read_write_all\n");
66                 printf("ccan/build_assert\n");
67                 return 0;
68         }
69
70         return 1;
71 }