]> git.ozlabs.org Git - ccan/blobdiff - ccan/rfc822/test/run-default-alloc-failure.c
rfc822: switch to ccan/tal.
[ccan] / ccan / rfc822 / test / run-default-alloc-failure.c
index 7fc0393e2079eaae1507d8d18a915c304ca868e0..b81fd4c0c9644c4f89132d3713dbe54199740aa7 100644 (file)
@@ -1,6 +1,7 @@
-#include <ccan/tap/tap.h>
+#include "config.h"
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include <signal.h>
 #include <errno.h>
 
 #include <ccan/rfc822/rfc822.h>
 
-#include <ccan/rfc822/rfc822.c>
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
 
-#include "testdata.h"
+static bool should_fail = false;
 
-static void *failing_malloc(size_t size)
+static void *mayfail_alloc(const void *ctx, size_t size)
 {
-       return NULL;
+       if (should_fail)
+               return NULL;
+       return tal_arrz(ctx, char, size);
 }
 
+/* Override various tallocation functions. */
+#undef tal
+#undef talz
+#undef tal_arr
+#define tal(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define talz(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define tal_arr(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
+
+#include <ccan/rfc822/rfc822.c>
+
+#include "testdata.h"
+
 static void abort_handler(int signum)
 {
-       ok(1, "Aborted");
+       printf("Aborted");
        exit(0);
 }
 
@@ -34,23 +53,15 @@ int main(int argc, char *argv[])
        };
        int ret;
 
-       plan_tests(2);
-
        ret = sigaction(SIGABRT, &sa, NULL);
-       ok(ret, "Couldn't install signal handler: %s", strerror(errno));
+       assert(ret == 0);
 
        buf = assemble_msg(&test_msg_1, &len, 0);
 
        msg = rfc822_start(NULL, buf, len);
-
-       talloc_set_allocator(failing_malloc, free, realloc);
-
+       should_fail = true;
        (void) rfc822_next_header(msg, NULL);
 
-       ok(0, "Didn't get SIGABRT");
-
-       rfc822_free(msg);
-       talloc_free(buf);
-
-       exit(exit_status());
+       /* We should never get here! */
+       abort();
 }