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 ok1(write_sall(sfd1, " 1\n"));
113 /* It can't ask a question yet, since client 2 isn't finished. */
116 /* Make sure that arrives first! */
118 ok1(write_sall(sfd2, " 2\n"));
120 ok1(input_is(sfd1, "While the Oracle ponders,"
121 " please answer the following question:\nQUESTION 2\n"));
122 ok1(input_is(sfd2, "While the Oracle ponders,"
123 " please answer the following question:\nQUESTION 1\n"));
125 ok1(write_sall(sfd1, "ANSWER 2\n"));
126 ok1(write_sall(sfd2, "ANSWER 1"));
128 /* Nothing, until client 2 answers. */
130 ok1(write_sall(sfd2, "\n"));
132 ok1(input_is(sfd1, "The Oracle spake thus:\nANSWER 1\n"));
133 ok1(input_is(sfd2, "The Oracle spake thus:\nANSWER 2\n"));
135 /* Sockets should be dead now. */
136 ok1(read(sfd1, &c, 1) == 0);
137 ok1(read(sfd2, &c, 1) == 0);
139 /* Shut down server. */
140 write(exitfd[1], "", 1);
142 /* This will close once it's shut down, and return. */
143 ok1(read(readyfd[0], &c, 1) == 0);
145 /* This exits depending on whether all tests passed */
146 return exit_status();