]> git.ozlabs.org Git - ccan/blob - ccan/failtest/_info
failtest: new module.
[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.
12  *
13  * The unit test is a normal CCAN tap-style test, except it should
14  * start by calling failtest_init() and end by calling
15  * failtest_exit().  
16  *
17  * You can control what functions fail: see failtest_hook.
18  *
19  * Example:
20  *      #include <stdio.h>
21  *      #include <stdlib.h>
22  *      #include <string.h>
23  *      #include <ccan/tap/tap.h>
24  *      #include <ccan/failtest/failtest_override.h>
25  *      #include <ccan/failtest/failtest.h>
26  *
27  *      int main(int argc, char *argv[])
28  *      {
29  *              void *a, *b;
30  *
31  *              failtest_init(argc, argv);
32  *              plan_tests(3);
33  *
34  *              // Simple malloc test.
35  *              a = malloc(100);
36  *              if (ok1(a)) {
37  *                      // Fill the memory.
38  *                      memset(a, 'x', 100);
39  *                      b = realloc(a, 200);
40  *                      if (ok1(b)) {
41  *                              // Fill the rest of the memory.
42  *                              memset(b + 100, 'y', 100);
43  *                              // Check it got a copy of a as expected.
44  *                              ok1(strspn(b, "x") == 100);
45  *                              free(b);
46  *                      } else {
47  *                              // Easy to miss: free a on realloc failure!
48  *                              free(a);
49  *                      }
50  *              }
51  *              failtest_exit(exit_status());
52  *      }
53  *
54  * License: LGPL
55  * Author: Rusty Russell <rusty@rustcorp.com.au>
56  */
57 int main(int argc, char *argv[])
58 {
59         if (argc != 2)
60                 return 1;
61
62         if (strcmp(argv[1], "depends") == 0) {
63                 printf("ccan/compiler\n");
64                 printf("ccan/read_write_all\n");
65                 return 0;
66         }
67
68         return 1;
69 }