1 #include <ccan/oserver/oserver.c>
2 #include <ccan/oserver/oserver.h>
3 #include <ccan/str/str.h>
4 #include <ccan/tap/tap.h>
6 #include <sys/select.h>
11 static void exit_quietly(struct tevent_context *ev,
12 struct tevent_fd *fde, uint16_t flags, void *unused)
18 static void run_server(int readyfd, int exitfd)
20 struct tevent_context *ev = tevent_context_init(NULL);
22 if (oserver_setup(ev, OSERVER_PORT) == NULL)
25 tevent_add_fd(ev, ev, exitfd, TEVENT_FD_READ, exit_quietly, NULL);
26 /* Tell parent we are ready to go. */
27 write(readyfd, "", 1);
29 while (tevent_loop_wait(ev) == 0);
32 static bool write_sall(int fd, const char *str)
35 ssize_t len = write(fd, str, strlen(str));
43 static bool input_is(int fd, const char *str)
47 ssize_t len = read(fd, buffer, strlen(str));
50 if (strncmp(str, buffer, len) != 0)
57 static bool no_input(int fd)
60 struct timeval t = { 0, 0 };
64 return (select(fd+1, &set, NULL, NULL, &t) == 0);
67 int main(int argc, char *argv[])
69 int readyfd[2], exitfd[2];
72 struct sockaddr_in in;
77 /* This is how many tests you plan to run */
85 run_server(readyfd[1], exitfd[0]);
86 err(1, "Event loop failed");
91 sfd1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
92 sfd2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
94 u.in.sin_family = AF_INET;
95 u.in.sin_port = htons(OSERVER_PORT);
96 u.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
98 /* Wait until child is ready... */
99 if (read(readyfd[0], &c, 1) != 1)
100 errx(1, "Child problems");
103 ok1(connect(sfd1, &u.addr, sizeof(u.in)) == 0);
104 ok1(connect(sfd2, &u.addr, sizeof(u.in)) == 0);
106 ok1(input_is(sfd1, "Welcome. Please ask your question.\n"));
107 ok1(input_is(sfd2, "Welcome. Please ask your question.\n"));
109 ok1(write_sall(sfd1, "question"));
110 ok1(write_sall(sfd2, "question"));
111 /* It shouldn't say anything until we've finished! */
115 ok1(write_sall(sfd1, " 1\n"));
116 /* Make sure that arrives first! */
118 ok1(write_sall(sfd2, " 2\n"));
120 ok1(input_is(sfd1, "I believe a better question is how many manly men mendaciously mention mending mansions?\n"));
121 ok1(input_is(sfd2, "I believe a better question is question 1\n"));
123 /* Sockets should be dead now. */
124 ok1(read(sfd1, &c, 1) == 0);
125 ok1(read(sfd2, &c, 1) == 0);
127 /* Shut down server. */
128 write(exitfd[1], "", 1);
130 /* This will close once it's shut down, and return. */
131 ok1(read(readyfd[0], &c, 1) == 0);
133 /* This exits depending on whether all tests passed */
134 return exit_status();