helper.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ccan/failtest/failtest_override.h>
  4. #include <ccan/failtest/failtest.h>
  5. #include <ccan/rfc822/rfc822.h>
  6. #include "helper.h"
  7. /* failtest limitations mean we need these wrappers to test talloc
  8. * failure paths. */
  9. #ifndef TAL_USE_TALLOC
  10. static void *malloc_wrapper(size_t size)
  11. {
  12. return malloc(size);
  13. }
  14. static void free_wrapper(void *ptr)
  15. {
  16. free(ptr);
  17. }
  18. static void *realloc_wrapper(void *ptr, size_t size)
  19. {
  20. return realloc(ptr, size);
  21. }
  22. #endif
  23. #if 0
  24. static void allocation_failure_exit(const char *s)
  25. {
  26. fprintf(stderr, "Allocation failure: %s", s);
  27. exit(0);
  28. }
  29. #endif
  30. static bool allocation_failed = false;
  31. static void allocation_failure_continue(const char *s)
  32. {
  33. fprintf(stderr, "Allocation failure: %s", s);
  34. allocation_failed = true;
  35. }
  36. void allocation_failure_check(void)
  37. {
  38. if (allocation_failed) {
  39. fprintf(stderr, "Exiting due to earlier failed allocation\n");
  40. exit(0);
  41. }
  42. }
  43. #ifdef TAL_USE_TALLOC
  44. #include <ccan/tal/talloc/talloc.h>
  45. #else
  46. #include <ccan/tal/tal.h>
  47. #endif
  48. /* Don't abort on allocation failures! */
  49. static void noabort_wrapper(const char *why)
  50. {
  51. return;
  52. }
  53. void failtest_setup(int argc, char *argv[])
  54. {
  55. failtest_init(argc, argv);
  56. rfc822_set_allocation_failure_handler(allocation_failure_continue);
  57. #ifdef TAL_USE_TALLOC
  58. /* FIXME: we can't inject allocation failures in talloc! */
  59. tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
  60. #else
  61. tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
  62. noabort_wrapper);
  63. #endif
  64. }
  65. void check_header(struct rfc822_msg *msg,
  66. struct rfc822_header *h,
  67. const char *name, const char *val,
  68. enum rfc822_header_errors experr, int crlf)
  69. {
  70. enum rfc822_header_errors errs;
  71. struct bytestring hname, hvalue, hfull;
  72. size_t namelen = strlen(name);
  73. size_t valuelen = strlen(val);
  74. size_t nln = crlf ? 2 : 1;
  75. size_t fulllen = namelen + valuelen + 1 + nln;
  76. errs = rfc822_header_errors(msg, h);
  77. ok(errs == experr, "Header errors 0x%x != 0x%x", errs, experr);
  78. allocation_failure_check();
  79. hname = rfc822_header_raw_name(msg, h);
  80. allocation_failure_check();
  81. ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
  82. "Header name \"%.*s\"", (int)hname.len, hname.ptr);
  83. hvalue = rfc822_header_raw_value(msg, h);
  84. allocation_failure_check();
  85. ok(hvalue.ptr && ((valuelen + nln) == hvalue.len)
  86. && (memcmp(val, hvalue.ptr, valuelen) == 0)
  87. && (!crlf || (hvalue.ptr[hvalue.len - 2] == '\r'))
  88. && (hvalue.ptr[hvalue.len - 1] == '\n'),
  89. "Header value");
  90. hfull = rfc822_header_raw_content(msg, h);
  91. allocation_failure_check();
  92. ok(hfull.ptr && (fulllen == hfull.len)
  93. && (memcmp(name, hfull.ptr, namelen) == 0)
  94. && (hfull.ptr[namelen] == ':')
  95. && (memcmp(val, hfull.ptr + namelen + 1, valuelen) == 0)
  96. && (!crlf || (hfull.ptr[fulllen-2] == '\r'))
  97. && (hfull.ptr[fulllen-1] == '\n'),
  98. "Full header");
  99. }