aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-01-29 17:08:13 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-01-29 17:08:13 -0800
commita01edb0254417321bc1b254f431bf39a82e8c49c (patch)
tree8e9435700741cd91a3cefd3023b4867934f34abc
parentee756a273a609153ac2f89ee3e5b1c89cdf65458 (diff)
parent9cc5eeca41394bf190ea3465e66abaa0372b2c04 (diff)
Merge pull request #2077 from inolen/enable_udp
enable UDP tests in socket tests
-rw-r--r--tests/sockets/test_sockets_echo_client.c25
-rw-r--r--tests/sockets/test_sockets_echo_server.c26
2 files changed, 32 insertions, 19 deletions
diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c
index f6ea85cf..18cff97e 100644
--- a/tests/sockets/test_sockets_echo_client.c
+++ b/tests/sockets/test_sockets_echo_client.c
@@ -71,21 +71,25 @@ void main_loop(void *arg) {
return;
}
+#if !TEST_DGRAM
// as a test, confirm with ioctl that we have data available
// after selecting
int available;
res = ioctl(server.fd, FIONREAD, &available);
assert(res != -1);
assert(available);
+#endif
res = do_msg_read(server.fd, &server.msg, echo_read, 0, NULL, NULL);
- if (res == 0) {
+ if (res == -1) {
+ return;
+ } else if (res == 0) {
perror("server closed");
finish(EXIT_FAILURE);
- } else if (res != -1) {
- echo_read += res;
}
+ echo_read += res;
+
// once we've read the entire message, validate it
if (echo_read >= server.msg.length) {
assert(!strcmp(server.msg.buffer, MESSAGE));
@@ -97,13 +101,15 @@ void main_loop(void *arg) {
}
res = do_msg_write(server.fd, &echo_msg, echo_wrote, 0, NULL, 0);
- if (res == 0) {
+ if (res == -1) {
+ return;
+ } else if (res == 0) {
perror("server closed");
finish(EXIT_FAILURE);
- } else if (res != -1) {
- echo_wrote += res;
}
+ echo_wrote += res;
+
// once we're done writing the message, read it back
if (echo_wrote >= echo_msg.length) {
server.state = MSG_READ;
@@ -124,8 +130,11 @@ int main() {
echo_msg.buffer = malloc(echo_msg.length);
strncpy(echo_msg.buffer, MESSAGE, echo_msg.length);
+ echo_read = 0;
+ echo_wrote = 0;
+
// create the socket and set to non-blocking
-#if !USE_UDP
+#if !TEST_DGRAM
server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#else
server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -158,4 +167,4 @@ int main() {
#endif
return EXIT_SUCCESS;
-} \ No newline at end of file
+}
diff --git a/tests/sockets/test_sockets_echo_server.c b/tests/sockets/test_sockets_echo_server.c
index 55ace660..850b7e89 100644
--- a/tests/sockets/test_sockets_echo_server.c
+++ b/tests/sockets/test_sockets_echo_server.c
@@ -59,7 +59,7 @@ void main_loop(void *arg) {
FD_ZERO(&fdw);
FD_SET(server.fd, &fdr);
FD_SET(server.fd, &fdw);
-#if !USE_UDP
+#if !TEST_DGRAM
if (client.fd) FD_SET(client.fd, &fdr);
if (client.fd) FD_SET(client.fd, &fdw);
#endif
@@ -69,7 +69,7 @@ void main_loop(void *arg) {
exit(EXIT_SUCCESS);
}
-#if !USE_UDP
+#if !TEST_DGRAM
// for TCP sockets, we may need to accept a connection
if (FD_ISSET(server.fd, &fdr)) {
#if TEST_ACCEPT_ADDR
@@ -86,7 +86,7 @@ void main_loop(void *arg) {
}
#endif
-#if !USE_UDP
+#if !TEST_DGRAM
int fd = client.fd;
#else
int fd = server.fd;
@@ -99,14 +99,16 @@ void main_loop(void *arg) {
}
res = do_msg_read(fd, &client.msg, client.read, 0, (struct sockaddr *)&client.addr, &addrlen);
- if (res == 0) {
+ if (res == -1) {
+ return;
+ } else if (res == 0) {
// client disconnected
memset(&client, 0, sizeof(client_t));
return;
- } else if (res != -1) {
- client.read += res;
}
+ client.read += res;
+
// once we've read the entire message, echo it back
if (client.read >= client.msg.length) {
client.read = 0;
@@ -118,14 +120,16 @@ void main_loop(void *arg) {
}
res = do_msg_write(fd, &client.msg, client.wrote, 0, (struct sockaddr *)&client.addr, sizeof(client.addr));
- if (res == 0) {
+ if (res == -1) {
+ return;
+ } else if (res == 0) {
// client disconnected
memset(&client, 0, sizeof(client_t));
return;
- } else if (res != -1) {
- client.wrote += res;
}
+ client.wrote += res;
+
if (client.wrote >= client.msg.length) {
client.wrote = 0;
client.state = MSG_READ;
@@ -149,7 +153,7 @@ int main() {
memset(&client, 0, sizeof(client_t));
// create the socket and set to non-blocking
-#if !USE_UDP
+#if !TEST_DGRAM
server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#else
server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -174,7 +178,7 @@ int main() {
exit(EXIT_FAILURE);
}
-#if !USE_UDP
+#if !TEST_DGRAM
res = listen(server.fd, 50);
if (res == -1) {
perror("listen failed");