failtest: failtest_restore.h as an antidote to function overload.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 18 Jan 2011 00:38:34 +0000 (11:08 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 18 Jan 2011 00:38:34 +0000 (11:08 +1030)
This makes some cases simpler, where you no longer want malloc etc.
to fail.

ccan/failtest/_info
ccan/failtest/failtest.c
ccan/failtest/failtest_undo.h [new file with mode: 0644]

index 73aa6a2daef21d92d48afd68833f5e5f40ad191f..4b43246fff06973433a288ed385595f557259ec0 100644 (file)
@@ -8,7 +8,8 @@
  * The failtest module overrides various standard functions, and forks
  * your unit test at those points to test failure paths.  The failing
  * child are expected to fail (eg. when malloc fails), but should not
- * leak memory or crash.
+ * leak memory or crash.  After including failtest_override.h, you can
+ * include failtest_restore.h to return to non-failing versions.
  *
  * The unit test is a normal CCAN tap-style test, except it should
  * start by calling failtest_init() and end by calling
index 092d2091b137bf3ea8b4e193931c1b239019c7ad..1127e4fdd7c385973e4d91b3050507c4086968fa 100644 (file)
@@ -66,12 +66,19 @@ static unsigned int fd_orig_num = 0;
 
 static const char info_to_arg[] = "mceoprw";
 
+/* Dummy call used for failtest_undo wrappers. */
+static struct failtest_call unrecorded_call;
+
 static struct failtest_call *add_history_(enum failtest_call_type type,
                                          const char *file,
                                          unsigned int line,
                                          const void *elem,
                                          size_t elem_size)
 {
+       /* NULL file is how we suppress failure. */
+       if (!file)
+               return &unrecorded_call;
+
        history = realloc(history, (history_num + 1) * sizeof(*history));
        history[history_num].type = type;
        history[history_num].file = file;
@@ -172,6 +179,9 @@ static bool should_fail(struct failtest_call *call)
        char *out = NULL;
        size_t outlen = 0;
 
+       if (call == &unrecorded_call)
+               return false;
+
        if (failpath) {
                if (tolower(*failpath) != info_to_arg[call->type])
                        errx(1, "Failpath expected '%c' got '%c'\n",
@@ -341,6 +351,9 @@ int failtest_open(const char *pathname, int flags,
                va_end(ap);
        }
        p = add_history(FAILTEST_OPEN, file, line, &call);
+       /* Avoid memory leak! */
+       if (p == &unrecorded_call)
+               free((char *)call.pathname);
        if (should_fail(p)) {
                p->u.open.ret = -1;
                /* FIXME: Play with error codes? */
diff --git a/ccan/failtest/failtest_undo.h b/ccan/failtest/failtest_undo.h
new file mode 100644 (file)
index 0000000..17bf356
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef CCAN_FAILTEST_RESTORE_H
+#define CCAN_FAILTEST_RESTORE_H
+/* This file undoes the effect of failtest_override.h. */
+
+#undef calloc
+#define calloc(nmemb, size)    \
+       failtest_calloc((nmemb), (size), NULL, 0)
+
+#undef malloc
+#define malloc(size)   \
+       failtest_malloc((size), NULL, 0)
+
+#undef realloc
+#define realloc(ptr, size)                                     \
+       failtest_realloc((ptr), (size), NULL, 0)
+
+#undef open
+#define open(pathname, flags, ...) \
+       failtest_open((pathname), (flags), NULL, 0, __VA_ARGS__)
+
+#undef pipe
+#define pipe(pipefd) \
+       failtest_pipe((pipefd), NULL, 0)
+
+#undef read
+#define read(fd, buf, count) \
+       failtest_read((fd), (buf), (count), NULL, 0)
+
+#undef write
+#define write(fd, buf, count) \
+       failtest_write((fd), (buf), (count), NULL, 0)
+
+#undef close
+#define close(fd) failtest_close(fd)
+
+#endif /* CCAN_FAILTEST_RESTORE_H */