summaryrefslogtreecommitdiff
path: root/lib/ccan/list/test/run-check-corrupt.c
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-02-22 14:59:32 +1030
committerAmitay Isaacs <amitay@gmail.com>2012-03-07 13:16:16 +1100
commit361f3ea9ee577c5a3e2fed687a0b417b257c31de (patch)
tree6d356c3aa64317c609ff4e208be76e18996a55f8 /lib/ccan/list/test/run-check-corrupt.c
parent4f5412dda687c3ff76b426842bf284d01d56a997 (diff)
downloadsamba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.gz
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.bz2
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.zip
lib/ccan: import failtest and required ccan modules for TDB2 unit tests.
New modules: failtest, list, time, read_write_all and tlist. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/ccan/list/test/run-check-corrupt.c')
-rw-r--r--lib/ccan/list/test/run-check-corrupt.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/ccan/list/test/run-check-corrupt.c b/lib/ccan/list/test/run-check-corrupt.c
new file mode 100644
index 0000000000..5dd9f9cc83
--- /dev/null
+++ b/lib/ccan/list/test/run-check-corrupt.c
@@ -0,0 +1,89 @@
+#include <setjmp.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <err.h>
+
+/* We don't actually want it to exit... */
+static jmp_buf aborted;
+#define abort() longjmp(aborted, 1)
+
+#define fprintf my_fprintf
+static char printf_buffer[1000];
+
+static int my_fprintf(FILE *stream, const char *format, ...)
+{
+ va_list ap;
+ int ret;
+ va_start(ap, format);
+ ret = vsprintf(printf_buffer, format, ap);
+ va_end(ap);
+ return ret;
+}
+
+#include <ccan/list/list.h>
+#include <ccan/tap/tap.h>
+#include <ccan/list/list.c>
+
+int main(int argc, char *argv[])
+{
+ struct list_head list;
+ struct list_node n1;
+ char expect[100];
+
+ plan_tests(9);
+ /* Empty list. */
+ list.n.next = &list.n;
+ list.n.prev = &list.n;
+ ok1(list_check(&list, NULL) == &list);
+
+ /* Bad back ptr */
+ list.n.prev = &n1;
+ /* Non-aborting version. */
+ ok1(list_check(&list, NULL) == NULL);
+
+ /* Aborting version. */
+ sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
+ &list, &list);
+ if (setjmp(aborted) == 0) {
+ list_check(&list, "test message");
+ fail("list_check on empty with bad back ptr didn't fail!");
+ } else {
+ ok1(strcmp(printf_buffer, expect) == 0);
+ }
+
+ /* n1 in list. */
+ list.n.next = &n1;
+ list.n.prev = &n1;
+ n1.prev = &list.n;
+ n1.next = &list.n;
+ ok1(list_check(&list, NULL) == &list);
+ ok1(list_check_node(&n1, NULL) == &n1);
+
+ /* Bad back ptr */
+ n1.prev = &n1;
+ ok1(list_check(&list, NULL) == NULL);
+ ok1(list_check_node(&n1, NULL) == NULL);
+
+ /* Aborting version. */
+ sprintf(expect, "test message: prev corrupt in node %p (1) of %p\n",
+ &n1, &list);
+ if (setjmp(aborted) == 0) {
+ list_check(&list, "test message");
+ fail("list_check on n1 bad back ptr didn't fail!");
+ } else {
+ ok1(strcmp(printf_buffer, expect) == 0);
+ }
+
+ sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
+ &n1, &n1);
+ if (setjmp(aborted) == 0) {
+ list_check_node(&n1, "test message");
+ fail("list_check_node on n1 bad back ptr didn't fail!");
+ } else {
+ ok1(strcmp(printf_buffer, expect) == 0);
+ }
+
+ return exit_status();
+}