failtest: fix tracepath bug.
[ccan] / ccan / failtest / test / run-history.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <ccan/tap/tap.h>
5 /* Include the C files directly. */
6 #include <ccan/failtest/failtest.c>
7
8 int main(void)
9 {
10         struct failtest_call *call;
11         struct calloc_call calloc_call;
12         struct malloc_call malloc_call;
13         struct realloc_call realloc_call;
14         struct open_call open_call;
15         struct pipe_call pipe_call;
16         struct read_call read_call;
17         struct write_call write_call;
18         char buf[20];
19         unsigned int i;
20         char *path;
21
22         /* This is how many tests you plan to run */
23         plan_tests(47);
24
25         calloc_call.ret = calloc(1, 2);
26         calloc_call.nmemb = 1;
27         calloc_call.size = 2;
28         call = add_history(FAILTEST_CALLOC, "run-history.c", 1, &calloc_call);
29         ok1(call->type == FAILTEST_CALLOC);
30         ok1(strcmp(call->file, "run-history.c") == 0);
31         ok1(call->line == 1);
32         ok1(call->u.calloc.ret == calloc_call.ret);
33         ok1(call->u.calloc.nmemb == calloc_call.nmemb);
34         ok1(call->u.calloc.size == calloc_call.size);
35
36         malloc_call.ret = malloc(2);
37         malloc_call.size = 2;
38         call = add_history(FAILTEST_MALLOC, "run-history.c", 2, &malloc_call);
39         ok1(call->type == FAILTEST_MALLOC);
40         ok1(strcmp(call->file, "run-history.c") == 0);
41         ok1(call->line == 2);
42         ok1(call->u.malloc.ret == malloc_call.ret);
43         ok1(call->u.malloc.size == malloc_call.size);
44
45         realloc_call.ret = realloc(malloc_call.ret, 3);
46         realloc_call.ptr = malloc_call.ret;
47         realloc_call.size = 3;
48         call = add_history(FAILTEST_REALLOC, "run-history.c", 3,
49                            &realloc_call);
50         ok1(call->type == FAILTEST_REALLOC);
51         ok1(strcmp(call->file, "run-history.c") == 0);
52         ok1(call->line == 3);
53         ok1(call->u.realloc.ret == realloc_call.ret);
54         ok1(call->u.realloc.ptr == realloc_call.ptr);
55         ok1(call->u.realloc.size == realloc_call.size);
56
57         open_call.ret = open("test/run_history.c", O_RDONLY);
58         open_call.pathname = "test/run_history.c";
59         open_call.flags = O_RDONLY;
60         open_call.mode = 0;
61         call = add_history(FAILTEST_OPEN, "run-history.c", 4, &open_call);
62         ok1(call->type == FAILTEST_OPEN);
63         ok1(strcmp(call->file, "run-history.c") == 0);
64         ok1(call->line == 4);
65         ok1(call->u.open.ret == open_call.ret);
66         ok1(strcmp(call->u.open.pathname, open_call.pathname) == 0);
67         ok1(call->u.open.flags == open_call.flags);
68         ok1(call->u.open.mode == open_call.mode);
69
70         pipe_call.ret = pipe(pipe_call.fds);
71         call = add_history(FAILTEST_PIPE, "run-history.c", 5, &pipe_call);
72         ok1(call->type == FAILTEST_PIPE);
73         ok1(strcmp(call->file, "run-history.c") == 0);
74         ok1(call->line == 5);
75         ok1(call->u.pipe.ret == pipe_call.ret);
76         ok1(call->u.pipe.fds[0] == pipe_call.fds[0]);
77         ok1(call->u.pipe.fds[1] == pipe_call.fds[1]);
78
79         read_call.ret = read(open_call.ret, buf, 20);
80         read_call.buf = buf;
81         read_call.fd = open_call.ret;
82         read_call.count = 20;
83         call = add_history(FAILTEST_READ, "run-history.c", 6, &read_call);
84         ok1(call->type == FAILTEST_READ);
85         ok1(strcmp(call->file, "run-history.c") == 0);
86         ok1(call->line == 6);
87         ok1(call->u.read.ret == read_call.ret);
88         ok1(call->u.read.buf == read_call.buf);
89         ok1(call->u.read.fd == read_call.fd);
90         ok1(call->u.read.count == read_call.count);
91
92         write_call.ret = 20;
93         write_call.buf = buf;
94         write_call.fd = open_call.ret;
95         write_call.count = 20;
96         call = add_history(FAILTEST_WRITE, "run-history.c", 7, &write_call);
97         ok1(call->type == FAILTEST_WRITE);
98         ok1(strcmp(call->file, "run-history.c") == 0);
99         ok1(call->line == 7);
100         ok1(call->u.write.ret == write_call.ret);
101         ok1(call->u.write.buf == write_call.buf);
102         ok1(call->u.write.fd == write_call.fd);
103         ok1(call->u.write.count == write_call.count);
104
105         ok1(history_num == 7);
106
107         for (i = 0; i < history_num; i++)
108                 history[i].fail = false;
109
110         path = failpath_string();
111         ok1(strcmp(path, "cmeoprw") == 0);
112         free(path);
113
114         for (i = 0; i < history_num; i++)
115                 history[i].fail = true;
116
117         path = failpath_string();
118         ok1(strcmp(path, "CMEOPRW") == 0);
119         free(path);
120
121         return exit_status();
122 }