_info 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * failtest - unit test helpers for testing malloc and other failures.
  6. *
  7. * The failtest module overrides various standard functions, and forks
  8. * your unit test at those points to test failure paths. The failing
  9. * child are expected to fail (eg. when malloc fails), but should not
  10. * leak memory or crash. After including failtest_override.h, you can
  11. * include failtest_restore.h to return to non-failing versions.
  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. * char *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. * Ccanlint:
  57. * // valgrind seems to mess up rlimit.
  58. * tests_pass_valgrind test/run-with-fdlimit.c:FAIL
  59. */
  60. int main(int argc, char *argv[])
  61. {
  62. if (argc != 2)
  63. return 1;
  64. if (strcmp(argv[1], "depends") == 0) {
  65. printf("ccan/build_assert\n");
  66. printf("ccan/compiler\n");
  67. printf("ccan/hash\n");
  68. printf("ccan/htable\n");
  69. printf("ccan/read_write_all\n");
  70. printf("ccan/str\n");
  71. printf("ccan/time\n");
  72. printf("ccan/tlist\n");
  73. return 0;
  74. }
  75. return 1;
  76. }