]> git.ozlabs.org Git - ccan/blob - ccan/list/test/run-check-corrupt.c
ilog: credit Tim Terriberry as author in ccan/ilog/_info
[ccan] / ccan / list / test / run-check-corrupt.c
1 #include <setjmp.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <err.h>
7
8 /* We don't actually want it to exit... */
9 static jmp_buf aborted;
10 #define abort() longjmp(aborted, 1)
11
12 #define fprintf my_fprintf
13 static char printf_buffer[1000];
14
15 static int my_fprintf(FILE *stream, const char *format, ...)
16 {
17         va_list ap;
18         int ret;
19         va_start(ap, format);
20         ret = vsprintf(printf_buffer, format, ap);
21         va_end(ap);
22         return ret;
23 }
24
25 #include <ccan/list/list.h>
26 #include <ccan/tap/tap.h>
27 #include <ccan/list/list.c>
28
29 int main(int argc, char *argv[])
30 {
31         struct list_head list;
32         struct list_node n1;
33         char expect[100];
34
35         plan_tests(9);
36         /* Empty list. */
37         list.n.next = &list.n;
38         list.n.prev = &list.n;
39         ok1(list_check(&list, NULL) == &list);
40
41         /* Bad back ptr */
42         list.n.prev = &n1;
43         /* Non-aborting version. */
44         ok1(list_check(&list, NULL) == NULL);
45
46         /* Aborting version. */
47         sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
48                 &list, &list);
49         if (setjmp(aborted) == 0) {
50                 list_check(&list, "test message");
51                 fail("list_check on empty with bad back ptr didn't fail!");
52         } else {
53                 ok1(strcmp(printf_buffer, expect) == 0);
54         }
55
56         /* n1 in list. */
57         list.n.next = &n1;
58         list.n.prev = &n1;
59         n1.prev = &list.n;
60         n1.next = &list.n;
61         ok1(list_check(&list, NULL) == &list);
62         ok1(list_check_node(&n1, NULL) == &n1);
63
64         /* Bad back ptr */
65         n1.prev = &n1;
66         ok1(list_check(&list, NULL) == NULL);
67         ok1(list_check_node(&n1, NULL) == NULL);
68
69         /* Aborting version. */
70         sprintf(expect, "test message: prev corrupt in node %p (1) of %p\n",
71                 &n1, &list);
72         if (setjmp(aborted) == 0) {
73                 list_check(&list, "test message");
74                 fail("list_check on n1 bad back ptr didn't fail!");
75         } else {
76                 ok1(strcmp(printf_buffer, expect) == 0);
77         }
78
79         sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
80                 &n1, &n1);
81         if (setjmp(aborted) == 0) {
82                 list_check_node(&n1, "test message");
83                 fail("list_check_node on n1 bad back ptr didn't fail!");
84         } else {
85                 ok1(strcmp(printf_buffer, expect) == 0);
86         }
87
88         return exit_status();
89 }