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