4 #include <ccan/tap/tap.h>
6 #define printf saved_printf
7 static int saved_printf(const char *fmt, ...);
9 #define fprintf saved_fprintf
10 static int saved_fprintf(FILE *ignored, const char *fmt, ...);
12 /* Include the C files directly. */
13 #include <ccan/failtest/failtest.c>
15 static char *output = NULL;
17 static int saved_vprintf(const char *fmt, va_list ap)
19 int ret = vsnprintf(NULL, 0, fmt, ap);
25 output = realloc(output, len + ret + 1);
26 return vsprintf(output + len, fmt, ap);
29 static int saved_printf(const char *fmt, ...)
35 ret = saved_vprintf(fmt, ap);
40 static int saved_fprintf(FILE *ignored, const char *fmt, ...)
46 ret = saved_vprintf(fmt, ap);
53 struct failtest_call *call;
54 struct calloc_call calloc_call;
55 struct malloc_call malloc_call;
56 struct realloc_call realloc_call;
57 struct open_call open_call;
58 struct pipe_call pipe_call;
59 struct read_call read_call;
60 struct write_call write_call;
64 /* This is how many tests you plan to run */
67 calloc_call.ret = calloc(1, 2);
68 calloc_call.nmemb = 1;
70 call = add_history(FAILTEST_CALLOC, "run-history.c", 1, &calloc_call);
71 ok1(call->type == FAILTEST_CALLOC);
72 ok1(strcmp(call->file, "run-history.c") == 0);
74 ok1(call->u.calloc.ret == calloc_call.ret);
75 ok1(call->u.calloc.nmemb == calloc_call.nmemb);
76 ok1(call->u.calloc.size == calloc_call.size);
78 malloc_call.ret = malloc(2);
80 call = add_history(FAILTEST_MALLOC, "run-history.c", 2, &malloc_call);
81 ok1(call->type == FAILTEST_MALLOC);
82 ok1(strcmp(call->file, "run-history.c") == 0);
84 ok1(call->u.malloc.ret == malloc_call.ret);
85 ok1(call->u.malloc.size == malloc_call.size);
87 realloc_call.ret = realloc(malloc_call.ret, 3);
88 realloc_call.ptr = malloc_call.ret;
89 realloc_call.size = 3;
90 call = add_history(FAILTEST_REALLOC, "run-history.c",
92 ok1(call->type == FAILTEST_REALLOC);
93 ok1(strcmp(call->file, "run-history.c") == 0);
95 ok1(call->u.realloc.ret == realloc_call.ret);
96 ok1(call->u.realloc.ptr == realloc_call.ptr);
97 ok1(call->u.realloc.size == realloc_call.size);
99 open_call.ret = open("test/run_history.c", O_RDONLY);
100 open_call.pathname = "test/run_history.c";
101 open_call.flags = O_RDONLY;
103 call = add_history(FAILTEST_OPEN, "run-history.c", 4, &open_call);
104 ok1(call->type == FAILTEST_OPEN);
105 ok1(strcmp(call->file, "run-history.c") == 0);
106 ok1(call->line == 4);
107 ok1(call->u.open.ret == open_call.ret);
108 ok1(strcmp(call->u.open.pathname, open_call.pathname) == 0);
109 ok1(call->u.open.flags == open_call.flags);
110 ok1(call->u.open.mode == open_call.mode);
112 pipe_call.ret = pipe(pipe_call.fds);
113 call = add_history(FAILTEST_PIPE, "run-history.c", 5, &pipe_call);
114 ok1(call->type == FAILTEST_PIPE);
115 ok1(strcmp(call->file, "run-history.c") == 0);
116 ok1(call->line == 5);
117 ok1(call->u.pipe.ret == pipe_call.ret);
118 ok1(call->u.pipe.fds[0] == pipe_call.fds[0]);
119 ok1(call->u.pipe.fds[1] == pipe_call.fds[1]);
121 read_call.ret = read(open_call.ret, buf, 20);
123 read_call.fd = open_call.ret;
124 read_call.count = 20;
125 call = add_history(FAILTEST_READ, "run-history.c", 6, &read_call);
126 ok1(call->type == FAILTEST_READ);
127 ok1(strcmp(call->file, "run-history.c") == 0);
128 ok1(call->line == 6);
129 ok1(call->u.read.ret == read_call.ret);
130 ok1(call->u.read.buf == read_call.buf);
131 ok1(call->u.read.fd == read_call.fd);
132 ok1(call->u.read.count == read_call.count);
135 write_call.buf = buf;
136 write_call.fd = open_call.ret;
137 write_call.count = 20;
138 call = add_history(FAILTEST_WRITE, "run-history.c", 7, &write_call);
139 ok1(call->type == FAILTEST_WRITE);
140 ok1(strcmp(call->file, "run-history.c") == 0);
141 ok1(call->line == 7);
142 ok1(call->u.write.ret == write_call.ret);
143 ok1(call->u.write.buf == write_call.buf);
144 ok1(call->u.write.fd == write_call.fd);
145 ok1(call->u.write.count == write_call.count);
147 ok1(history_num == 7);
149 for (i = 0; i < history_num; i++)
150 history[i].fail = false;
153 ok1(strcmp(output, "To reproduce: --failpath=cmeoprw\n") == 0);
157 for (i = 0; i < history_num; i++)
158 history[i].fail = true;
161 ok1(strcmp(output, "To reproduce: --failpath=CMEOPRW\n") == 0);
165 return exit_status();