]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/test/run.c
f6c5b45762ff30a413fd840baaa1577e1321c671
[ccan-lca-2011.git] / ccan / oserver / test / run.c
1 #include <ccan/oserver/oserver.h>
2 #include <ccan/oserver/oserver.c>
3 #include <ccan/str/str.h>
4 #include <ccan/array_size/array_size.h>
5 #include <ccan/tap/tap.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <sys/wait.h>
11
12 int main(void)
13 {
14         int fd, i;
15         int status;
16         char buf[200];
17         const char *input[] = { "This is a test\n",
18                                 "This is a test\r",
19                                 "This is a test\r\n",
20                                 "This is a test\nWith extra",
21                                 "This is a test\rWith extra",
22                                 "This is a test\r\nWith extra" };
23
24         /* This is how many tests you plan to run */
25         plan_tests(4 * ARRAY_SIZE(input));
26
27         for (i = 0; i < ARRAY_SIZE(input); i++) {
28                 fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600);
29
30                 write(fd, input[i], strlen(input[i]));
31                 lseek(fd, 0, SEEK_SET);
32
33                 if (fork() == 0)
34                         oserver_serve(fd);
35
36                 wait(&status);
37
38                 ok1(WIFEXITED(status));
39                 ok1(WEXITSTATUS(status) == 0);
40
41                 lseek(fd, 0, SEEK_SET);
42                 buf[read(fd, buf, sizeof(buf)-1)] = '\0';
43
44                 ok1(strncmp(buf, input[i], strlen("This is a test")) == 0);
45                 ok1(streq(buf + strlen("This is a test") + 1,
46                           "Louder, like this: 'THIS IS A TEST'\r\n"));
47         }
48                 
49         /* This exits depending on whether all tests passed */
50         return exit_status();
51 }