1 #include <ccan/oserver/oserver.c>
2 #include <ccan/oserver/oserver_cdump.c>
3 #include <ccan/oserver/oserver.h>
4 #include <ccan/str/str.h>
5 #include <ccan/tap/tap.h>
7 #include <sys/select.h>
12 static void exit_quietly(struct tevent_context *ev,
13 struct tevent_fd *fde, uint16_t flags, void *unused)
19 static void run_server(int readyfd, int exitfd)
21 struct tevent_context *ev = tevent_context_init(NULL);
23 if (oserver_setup(ev, OSERVER_PORT, NULL) == NULL)
26 tevent_add_fd(ev, ev, exitfd, TEVENT_FD_READ, exit_quietly, NULL);
27 /* Tell parent we are ready to go. */
28 write(readyfd, "", 1);
30 while (tevent_loop_wait(ev) == 0);
33 static bool write_sall(int fd, const char *str)
36 ssize_t len = write(fd, str, strlen(str));
44 static bool input_is(int fd, const char *str)
48 ssize_t len = read(fd, buffer, strlen(str));
51 if (strncmp(str, buffer, len) != 0)
58 static bool no_input(int fd)
61 struct timeval t = { 0, 0 };
65 return (select(fd+1, &set, NULL, NULL, &t) == 0);
68 int main(int argc, char *argv[])
70 int readyfd[2], exitfd[2];
73 struct sockaddr_in in;
78 /* This is how many tests you plan to run */
86 run_server(readyfd[1], exitfd[0]);
87 err(1, "Event loop failed");
92 sfd1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
93 sfd2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
95 u.in.sin_family = AF_INET;
96 u.in.sin_port = htons(OSERVER_PORT);
97 u.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
99 /* Wait until child is ready... */
100 if (read(readyfd[0], &c, 1) != 1)
101 errx(1, "Child problems");
104 ok1(connect(sfd1, &u.addr, sizeof(u.in)) == 0);
105 ok1(connect(sfd2, &u.addr, sizeof(u.in)) == 0);
107 ok1(input_is(sfd1, "Welcome. Please ask your question.\n"));
108 ok1(input_is(sfd2, "Welcome. Please ask your question.\n"));
110 ok1(write_sall(sfd1, "QUESTION"));
111 ok1(write_sall(sfd2, "QUESTION"));
112 ok1(write_sall(sfd1, " 1\n"));
114 /* It can't ask a question yet, since client 2 isn't finished. */
117 /* Make sure that arrives first! */
119 ok1(write_sall(sfd2, " 2\n"));
121 ok1(input_is(sfd1, "While the Oracle ponders,"
122 " please answer the following question:\nQUESTION 2\n"));
123 ok1(input_is(sfd2, "While the Oracle ponders,"
124 " please answer the following question:\nQUESTION 1\n"));
126 ok1(write_sall(sfd1, "ANSWER 2\n"));
127 ok1(write_sall(sfd2, "ANSWER 1"));
129 /* Nothing, until client 2 answers. */
131 ok1(write_sall(sfd2, "\n"));
133 ok1(input_is(sfd1, "The Oracle spake thus:\nANSWER 1\n"));
134 ok1(input_is(sfd2, "The Oracle spake thus:\nANSWER 2\n"));
136 /* Sockets should be dead now. */
137 ok1(read(sfd1, &c, 1) == 0);
138 ok1(read(sfd2, &c, 1) == 0);
140 /* Shut down server. */
141 write(exitfd[1], "", 1);
143 /* This will close once it's shut down, and return. */
144 ok1(read(readyfd[0], &c, 1) == 0);
146 /* This exits depending on whether all tests passed */
147 return exit_status();