]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/test/run.c
dc6031b9de547e715a4e6f54c80389afcc62fc4c
[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/foreach/foreach.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;
15         int status;
16         char buf[200];
17         const char *input;
18
19         /* This is how many tests you plan to run */
20         plan_tests(4 * 6);
21
22         foreach_ptr(input,
23                     "This is a test\n",
24                     "This is a test\r",
25                     "This is a test\r\n",
26                     "This is a test\nWith extra",
27                     "This is a test\rWith extra",
28                     "This is a test\r\nWith extra") {
29                 fd = open("run-fd", O_RDWR|O_CREAT|O_TRUNC, 0600);
30
31                 write(fd, input, strlen(input));
32                 lseek(fd, 0, SEEK_SET);
33
34                 if (fork() == 0)
35                         oserver_serve(fd);
36
37                 wait(&status);
38
39                 ok1(WIFEXITED(status));
40                 ok1(WEXITSTATUS(status) == 0);
41
42                 lseek(fd, 0, SEEK_SET);
43                 buf[read(fd, buf, sizeof(buf)-1)] = '\0';
44
45                 ok1(strncmp(buf, input, strlen("This is a test")) == 0);
46                 ok1(streq(buf + strlen("This is a test") + 1,
47                           "Louder, like this: 'THIS IS A TEST'\r\n"));
48         }
49
50         /* This exits depending on whether all tests passed */
51         return exit_status();
52 }