--- sa_test.c 2002/10/30 09:41:51 1.18
+++ sa_test.c 2002/10/30 18:46:22 1.19
@@ -154,11 +154,6 @@
} \
} while (0)
-#define MSG_SRV_WELCOME "Welcome client\n"
-#define MSG_CLT_WELCOME "Welcome server\n"
-#define MSG_SRV_GOODBYE "Goodbye client\n"
-#define MSG_CLT_GOODBYE "Goodbye server\n"
-
/* test: client/server stream communication */
TS_TEST(test_sa_stream)
{
@@ -173,6 +168,11 @@
char buf_clt[1024];
size_t l;
+#define MSG_TCP_SRV_WELCOME "Welcome client\n"
+#define MSG_TCP_CLT_WELCOME "Welcome server\n"
+#define MSG_TCP_SRV_GOODBYE "Goodbye client\n"
+#define MSG_TCP_CLT_GOODBYE "Goodbye server\n"
+
if ((pid_srv = fork()) == 0) {
/*
** SERVER
@@ -195,17 +195,17 @@
ex(SRV, sa_accept(sa_srv, &saa_cld, &sa_cld));
/* communicate with client */
- ex(SRV, sa_writef(sa_cld, "%s", MSG_SRV_WELCOME));
+ ex(SRV, sa_writef(sa_cld, "%s", MSG_TCP_SRV_WELCOME));
ex(SRV, sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l));
- if (strcmp(buf_srv, MSG_CLT_WELCOME) != 0)
+ if (strcmp(buf_srv, MSG_TCP_CLT_WELCOME) != 0)
ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",
- buf_srv, MSG_CLT_WELCOME);
- ex(SRV, sa_writef(sa_cld, "%s", MSG_SRV_GOODBYE));
+ buf_srv, MSG_TCP_CLT_WELCOME);
+ ex(SRV, sa_writef(sa_cld, "%s", MSG_TCP_SRV_GOODBYE));
ex(SRV, sa_shutdown(sa_cld, "w"));
ex(SRV, sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l));
- if (strcmp(buf_srv, MSG_CLT_GOODBYE) != 0)
+ if (strcmp(buf_srv, MSG_TCP_CLT_GOODBYE) != 0)
ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",
- buf_srv, MSG_CLT_GOODBYE);
+ buf_srv, MSG_TCP_CLT_GOODBYE);
ex(SRV, sa_shutdown(sa_cld, "r"));
/* destroy client connection */
@@ -234,15 +234,15 @@
/* communicate with server */
ex(CLT, sa_readln(sa_clt, buf_clt, sizeof(buf_clt), &l));
- if (strcmp(buf_clt, MSG_SRV_WELCOME) != 0)
+ if (strcmp(buf_clt, MSG_TCP_SRV_WELCOME) != 0)
ts_test_fail(TS_CTX, "client: got \"%s\", expected \"%s\"",
- buf_clt, MSG_SRV_WELCOME);
- ex(CLT, sa_writef(sa_clt, "%s", MSG_CLT_WELCOME));
+ buf_clt, MSG_TCP_SRV_WELCOME);
+ ex(CLT, sa_writef(sa_clt, "%s", MSG_TCP_CLT_WELCOME));
ex(CLT, sa_readln(sa_clt, buf_clt, sizeof(buf_clt), &l));
- if (strcmp(buf_clt, MSG_SRV_GOODBYE) != 0)
+ if (strcmp(buf_clt, MSG_TCP_SRV_GOODBYE) != 0)
ts_test_fail(TS_CTX, "client: got \"%s\", expected \"%s\"",
- buf_clt, MSG_SRV_GOODBYE);
- ex(CLT, sa_writef(sa_clt, "%s", MSG_CLT_GOODBYE));
+ buf_clt, MSG_TCP_SRV_GOODBYE);
+ ex(CLT, sa_writef(sa_clt, "%s", MSG_TCP_CLT_GOODBYE));
ex(CLT, sa_shutdown(sa_clt, "rw"));
/* destroy server connection and wait for server to exit */
@@ -254,7 +254,76 @@
/* test: client/server datagram communication */
TS_TEST(test_sa_datagram)
{
- /* FIXME */
+ sa_t *sa_clt;
+ sa_t *sa_srv;
+ sa_addr_t *saa_srv;
+ sa_addr_t *saa_clt;
+ pid_t pid_srv;
+ char buf_srv[1024];
+ char buf_clt[1024];
+ size_t l;
+
+#define MSG_UDP_CLT_REQUEST "Who are you?\n"
+#define MSG_UDP_SRV_RESPONSE "I'm god\n"
+
+ if ((pid_srv = fork()) == 0) {
+ /*
+ ** SERVER
+ */
+
+ /* create server socket */
+ ex(SRV, sa_create(&sa_srv));
+ ex(SRV, sa_type(sa_srv, SA_TYPE_DATAGRAM));
+ ex(SRV, sa_option(sa_srv, SA_OPTION_REUSEADDR, 1));
+ ex(SRV, sa_option(sa_srv, SA_OPTION_REUSEPORT, 1));
+ ex(SRV, sa_timeout(sa_srv, SA_TIMEOUT_ALL, 10, 0));
+
+ /* bind socket to local port */
+ ex(SRV, sa_addr_create(&saa_srv));
+ ex(SRV, sa_addr_u2a(saa_srv, "inet://127.0.0.1:12345#udp"));
+ ex(SRV, sa_addr_destroy(saa_srv));
+ ex(SRV, sa_bind(sa_srv, saa_srv));
+
+ /* communicate with client */
+ ex(SRV, sa_recv(sa_srv, buf_srv, sizeof(buf_srv), &l, &saa_clt));
+ if (strncmp(buf_srv, MSG_UDP_CLT_REQUEST, l) != 0)
+ ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",
+ buf_srv, MSG_UDP_CLT_REQUEST);
+ ex(SRV, sa_send(sa_srv, MSG_UDP_SRV_RESPONSE,
+ strlen(MSG_UDP_SRV_RESPONSE), &l, saa_clt));
+ ex(SRV, sa_addr_destroy(saa_clt));
+
+ /* destroy server socket and die */
+ ex(SRV, sa_destroy(sa_srv));
+ exit(0);
+ }
+ else {
+ /*
+ ** CLIENT
+ **/
+ sleep(2);
+
+ /* create client socket */
+ ex(CLT, sa_create(&sa_clt));
+ ex(CLT, sa_type(sa_clt, SA_TYPE_DATAGRAM));
+ ex(CLT, sa_timeout(sa_clt, SA_TIMEOUT_ALL, 10, 0));
+
+ /* communicate with server */
+ ex(CLT, sa_addr_create(&saa_srv));
+ ex(CLT, sa_addr_u2a(saa_srv, "inet://127.0.0.1:12345#udp"));
+ ex(CLT, sa_send(sa_clt, MSG_UDP_CLT_REQUEST,
+ strlen(MSG_UDP_CLT_REQUEST), &l, saa_srv));
+ ex(CLT, sa_addr_destroy(saa_srv));
+ ex(CLT, sa_recv(sa_clt, buf_clt, sizeof(buf_clt), &l, &saa_srv));
+ if (strncmp(buf_clt, MSG_UDP_SRV_RESPONSE, l) != 0)
+ ts_test_fail(TS_CTX, "client: got \"%s\", expected \"%s\"",
+ buf_clt, MSG_UDP_SRV_RESPONSE);
+ ex(CLT, sa_addr_destroy(saa_srv));
+
+ /* destroy server connection and wait for server to exit */
+ ex(CLT, sa_destroy(sa_clt));
+ waitpid(pid_srv, NULL, 0);
+ }
}
/* test: exception handling */
|