diff options
author | Anthony Pesch <inolen@gmail.com> | 2013-08-09 21:01:08 -0700 |
---|---|---|
committer | Anthony Pesch <inolen@gmail.com> | 2013-08-09 21:47:05 -0700 |
commit | 36d62242f8172eefc01b30fdf248dc30100dc141 (patch) | |
tree | 787b0062a3dbdfd7c0d8306f531674c97a23d4c3 /tests/sockets | |
parent | 4ae44dc8d0e8370d92ff4601ccb6cd59e7f0217f (diff) |
- converted all socket tests to use C for the server-side
- added new echo client / server test
- updated websock harness and added new CompiledServerHarness to help run tests on multiple backends
Diffstat (limited to 'tests/sockets')
-rw-r--r-- | tests/sockets/test_sockets_echo_client.c | 148 | ||||
-rw-r--r-- | tests/sockets/test_sockets_echo_server.c | 152 | ||||
-rw-r--r-- | tests/sockets/test_sockets_msg.h | 78 | ||||
-rw-r--r-- | tests/sockets/test_sockets_partial_client.c (renamed from tests/sockets/test_sockets_partial.c) | 2 | ||||
-rw-r--r-- | tests/sockets/test_sockets_partial_server.c | 130 | ||||
-rw-r--r-- | tests/sockets/test_sockets_select_server_closes_connection_client_rw.c (renamed from tests/sockets/test_sockets_select_server_closes_connection_rw.c) | 42 | ||||
-rw-r--r-- | tests/sockets/test_sockets_select_server_no_accept_client.c (renamed from tests/sockets/test_sockets_select.c) | 0 | ||||
-rw-r--r-- | tests/sockets/test_sockets_select_server_no_accept_server.c | 86 |
8 files changed, 620 insertions, 18 deletions
diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c new file mode 100644 index 00000000..6b3ccef3 --- /dev/null +++ b/tests/sockets/test_sockets_echo_client.c @@ -0,0 +1,148 @@ +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <assert.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +#include "test_sockets_msg.h" + +// message to send to the server +#ifndef MESSAGE +#define MESSAGE "pingtothepong" +#endif + +typedef enum { + MSG_READ, + MSG_WRITE +} msg_state_t; + +typedef struct { + int fd; + msg_t msg; + msg_state_t state; +} server_t; + +server_t server; +msg_t echo_msg; +int echo_read; +int echo_wrote; + +void finish(int result) { + close(server.fd); +#if EMSCRIPTEN + REPORT_RESULT(); +#endif + exit(result); +} + +void main_loop(void *arg) { + static char out[1024*2]; + static int pos = 0; + fd_set fdr; + fd_set fdw; + int res; + + // make sure that server.fd is ready to read / write + FD_ZERO(&fdr); + FD_ZERO(&fdw); + FD_SET(server.fd, &fdr); + FD_SET(server.fd, &fdw); + res = select(64, &fdr, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + finish(EXIT_FAILURE); + } + + if (server.state == MSG_READ) { + if (!FD_ISSET(server.fd, &fdr)) { + return; + } + + // 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); + + res = do_msg_read(server.fd, &server.msg, echo_read, 0, NULL, NULL); + if (res != -1) echo_read += res; + + // once we've read the entire message, validate it + if (echo_read >= server.msg.length) { + assert(!strcmp(server.msg.buffer, MESSAGE)); + finish(EXIT_SUCCESS); + } + } else { + if (!FD_ISSET(server.fd, &fdw)) { + return; + } + + res = do_msg_write(server.fd, &echo_msg, echo_wrote, 0, NULL, 0); + if (res != -1) echo_wrote += res; + + // once we're done writing the message, read it back + if (echo_wrote >= echo_msg.length) { + server.state = MSG_READ; + } + } +} + +int main() { + struct sockaddr_in addr; + int res; + + memset(&server, 0, sizeof(server_t)); + server.state = MSG_WRITE; + + // setup the message we're going to echo + memset(&echo_msg, 0, sizeof(msg_t)); + echo_msg.length = strlen(MESSAGE) + 1; + echo_msg.buffer = malloc(echo_msg.length); + strncpy(echo_msg.buffer, MESSAGE, echo_msg.length); + + // create the socket and set to non-blocking +#if !USE_UDP + server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#else + server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); +#endif + if (server.fd == -1) { + perror("cannot create socket"); + finish(EXIT_FAILURE); + } + fcntl(server.fd, F_SETFL, O_NONBLOCK); + + // connect the socket + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(SOCKK); + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { + perror("inet_pton failed"); + finish(EXIT_FAILURE); + } + + res = connect(server.fd, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1 && errno != EINPROGRESS) { + perror("connect failed"); + finish(EXIT_FAILURE); + } + +#if EMSCRIPTEN + emscripten_set_main_loop(main_loop, 0, 0); +#else + while (1) main_loop(NULL); +#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 new file mode 100644 index 00000000..8a48b878 --- /dev/null +++ b/tests/sockets/test_sockets_echo_server.c @@ -0,0 +1,152 @@ +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/socket.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +#include "test_sockets_msg.h" + +typedef enum { + MSG_READ, + MSG_WRITE +} msg_state_t; + +typedef struct { + int fd; +} server_t; + +typedef struct { + int fd; + struct sockaddr_in addr; + msg_t msg; + msg_state_t state; + int read; + int wrote; +} client_t; + +server_t server; +client_t client; + +void main_loop(void *arg) { + int res; + fd_set fdr; + fd_set fdw; + + // see if there are any connections to accept or read / write from + FD_ZERO(&fdr); + FD_ZERO(&fdw); + FD_SET(server.fd, &fdr); + FD_SET(server.fd, &fdw); +#if !USE_UDP + if (client.fd) FD_SET(client.fd, &fdr); + if (client.fd) FD_SET(client.fd, &fdw); +#endif + res = select(64, &fdr, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + exit(EXIT_SUCCESS); + } + +#if !USE_UDP + // for TCP sockets, we may need to accept a connection + if (FD_ISSET(server.fd, &fdr)) { + client.fd = accept(server.fd, NULL, NULL); + assert(client.fd != -1); + } +#endif + +#if !USE_UDP + int fd = client.fd; +#else + int fd = server.fd; +#endif + if (client.state == MSG_READ) { + socklen_t addrlen; + + if (!FD_ISSET(fd, &fdr)) { + return; + } + + res = do_msg_read(fd, &client.msg, client.read, 0, (struct sockaddr *)&client.addr, &addrlen); + if (res != -1) client.read += res; + + // once we've read the entire message, echo it back + if (client.read >= client.msg.length) { + client.read = 0; + client.state = MSG_WRITE; + } + } else { + if (!FD_ISSET(fd, &fdw)) { + return; + } + + res = do_msg_write(fd, &client.msg, client.wrote, 0, (struct sockaddr *)&client.addr, sizeof(client.addr)); + if (res != -1) client.wrote += res; + + // close the client once we've echo'd back the entire message + if (client.wrote >= client.msg.length) { + close(client.fd); + memset(&client, 0, sizeof(client_t)); + } + } +} + +int main() { + struct sockaddr_in addr; + int res; + + memset(&server, 0, sizeof(server_t)); + memset(&client, 0, sizeof(client_t)); + + // create the socket and set to non-blocking +#if !USE_UDP + server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); +#else + server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); +#endif + if (server.fd == -1) { + perror("cannot create socket"); + exit(EXIT_FAILURE); + } + fcntl(server.fd, F_SETFL, O_NONBLOCK); + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(SOCKK); + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { + perror("inet_pton failed"); + exit(EXIT_FAILURE); + } + + res = bind(server.fd, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + +#if !USE_UDP + res = listen(server.fd, 50); + if (res == -1) { + perror("listen failed"); + exit(EXIT_FAILURE); + } +#endif + +#if EMSCRIPTEN + emscripten_set_main_loop(main_loop, 60, 0); +#else + while (1) main_loop(NULL); +#endif + + return EXIT_SUCCESS; +} diff --git a/tests/sockets/test_sockets_msg.h b/tests/sockets/test_sockets_msg.h new file mode 100644 index 00000000..30094d65 --- /dev/null +++ b/tests/sockets/test_sockets_msg.h @@ -0,0 +1,78 @@ +#ifndef __TEST_SOCKETS_MSG_H__ +#define __TEST_SOCKETS_MSG_H__ + +typedef struct { + char *buffer; + int length; +} msg_t; + +int do_msg_read(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t *addrlen) { + int res; + + if (!msg->length) { + // read the message length + res = recvfrom(sockfd, &msg->length, sizeof(int), 0, (struct sockaddr *)addr, addrlen); + if (res == -1) { + assert(errno == EAGAIN); + return res; + } + assert(res != 0); + msg->buffer = malloc(msg->length); + + printf("do_msg_read: allocating %d bytes for message\n", msg->length); + } + + // read the actual message + int max = msg->length - offset; + if (length && max > length) { + max = length; + } + res = recvfrom(sockfd, msg->buffer + offset, max, 0, (struct sockaddr *)addr, addrlen); + if (res == -1) { + assert(errno == EAGAIN); + return res; + } + + printf("do_msg_read: read %d bytes\n", res); + + return res; +} + +int do_msg_write(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t addrlen) { + int res; + + // send the message length first + if (!offset) { + if (addr) { + res = sendto(sockfd, &msg->length, sizeof(int), 0, addr, addrlen); + } else { + res = send(sockfd, &msg->length, sizeof(int), 0); + } + if (res == -1) { + assert(errno == EAGAIN); + return res; + } + assert(res == sizeof(int)); + } + + // then the actual message + int max = msg->length - offset; + if (length && max > length) { + max = length; + } + if (addr) { + res = sendto(sockfd, msg->buffer + offset, max, 0, addr, addrlen); + } else { + res = send(sockfd, msg->buffer + offset, max, 0); + } + if (res == -1) { + assert(errno == EAGAIN); + return res; + } + + printf("do_msg_write: wrote %d bytes %d\n", res, msg->length); + + return res; +} + +#endif
\ No newline at end of file diff --git a/tests/sockets/test_sockets_partial.c b/tests/sockets/test_sockets_partial_client.c index 5fe34721..dcf90f19 100644 --- a/tests/sockets/test_sockets_partial.c +++ b/tests/sockets/test_sockets_partial_client.c @@ -111,7 +111,7 @@ int main() { #if EMSCRIPTEN emscripten_set_main_loop(iter, 0, 0); #else - while (!done) iter(NULL); + while (1) iter(NULL); #endif return EXIT_SUCCESS; diff --git a/tests/sockets/test_sockets_partial_server.c b/tests/sockets/test_sockets_partial_server.c new file mode 100644 index 00000000..57fae84b --- /dev/null +++ b/tests/sockets/test_sockets_partial_server.c @@ -0,0 +1,130 @@ +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/socket.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int serverfd = -1; +int clientfd = -1; + +// csock.send("\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09") +// csock.send("\x08\x01\x02\x03\x04\x05\x06\x07\x08") +// csock.send("\x07\x01\x02\x03\x04\x05\x06\x07") +// csock.send("\x06\x01\x02\x03\x04\x05\x06") +// csock.send("\x05\x01\x02\x03\x04\x05") +// csock.send("\x04\x01\x02\x03\x04") +// csock.send("\x03\x01\x02\x03") +// csock.send("\x02\x01\x02") +// csock.send("\x01\x01") + +void do_send(int sockfd) { + static char* buffers[] = { + "\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\0", + "\x08\x01\x02\x03\x04\x05\x06\x07\x08\0", + "\x07\x01\x02\x03\x04\x05\x06\x07\0", + "\x06\x01\x02\x03\x04\x05\x06\0", + "\x05\x01\x02\x03\x04\x05\0", + "\x04\x01\x02\x03\x04\0", + "\x03\x01\x02\x03\0", + "\x02\x01\x02\0", + "\x01\x01\0" + }; + + int i; + int res; + char *buffer; + struct sockaddr_in addr; + socklen_t addrlen; + + for (i = 0; i < sizeof(buffers) / sizeof(char*); i++) { + buffer = buffers[i]; + + res = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1) { + perror("send failed"); + exit(EXIT_FAILURE); + } + printf("sent \"%s\" (%d bytes)\n", buffer, res); + } + + exit(EXIT_SUCCESS); +} + +void iter(void *arg) { + int res; + fd_set fdr; + fd_set fdw; + + // see if there are any connections to accept / write to + FD_ZERO(&fdr); + FD_ZERO(&fdw); + FD_SET(serverfd, &fdr); + if (clientfd != -1) FD_SET(clientfd, &fdw); + res = select(64, &fdr, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + exit(EXIT_SUCCESS); + } + + if (FD_ISSET(serverfd, &fdr)) { + printf("accepted someone\n"); + clientfd = accept(serverfd, NULL, NULL); + assert(clientfd != -1); + } + + if (FD_ISSET(clientfd, &fdw)) { + do_send(clientfd); + } +} + +int main() { + struct sockaddr_in addr; + int res; + + // create the socket + serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (serverfd == -1) { + perror("cannot create socket"); + exit(EXIT_FAILURE); + } + fcntl(serverfd, F_SETFL, O_NONBLOCK); + + // bind and listen to the supplied port + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(SOCKK); + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { + perror("inet_pton failed"); + exit(EXIT_FAILURE); + } + + res = bind(serverfd, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + res = listen(serverfd, 50); + if (res == -1) { + perror("listen failed"); + exit(EXIT_FAILURE); + } + +#if EMSCRIPTEN + emscripten_set_main_loop(iter, 60, 0); +#else + while (1) iter(NULL); +#endif + + return EXIT_SUCCESS; +} diff --git a/tests/sockets/test_sockets_select_server_closes_connection_rw.c b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c index f7e19aca..198ad232 100644 --- a/tests/sockets/test_sockets_select_server_closes_connection_rw.c +++ b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c @@ -14,9 +14,13 @@ #include <emscripten.h> #endif -#define EXPECTED_BYTES 5 +#include "test_sockets_msg.h" -int sockfd = -1; +#define MESSAGE "0123456789" + +int sockfd; +msg_t readmsg; +msg_t writemsg; void finish(int result) { close(sockfd); @@ -26,12 +30,10 @@ void finish(int result) { exit(result); } -void iter(void *arg) { +void main_loop(void *arg) { static int state = 0; - static char writebuf[] = "01234567890123456789"; - static int writePos = 0; - static char readbuf[1024]; static int readPos = 0; + static int writePos = 0; int selectRes; ssize_t transferAmount; fd_set sett; @@ -63,11 +65,11 @@ void iter(void *arg) { } // send a single byte - transferAmount = send(sockfd, writebuf+writePos, 1, 0); - writePos += transferAmount; + transferAmount = do_msg_write(sockfd, &writemsg, writePos, 1, NULL, 0); + if (transferAmount != -1) writePos += transferAmount; // after 10 bytes switch to next state - if (writePos >= 10) { + if (writePos >= writemsg.length) { state = 1; } break; @@ -86,8 +88,8 @@ void iter(void *arg) { } // read a single byte - transferAmount = recv(sockfd, readbuf+readPos, 1, 0); - readPos += transferAmount; + transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL); + if (transferAmount != -1) readPos += transferAmount; // if successfully reading 1 byte, switch to next state if (readPos >= 1) { @@ -119,11 +121,11 @@ void iter(void *arg) { } // read a single byte - transferAmount = recv(sockfd, readbuf+readPos, 1, 0); - readPos += transferAmount; + transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL); + if (transferAmount != -1) readPos += transferAmount; // with 10 bytes read the inQueue is empty => switch state - if (readPos >= 10) { + if (readPos >= readmsg.length) { state = 3; } break; @@ -141,7 +143,7 @@ void iter(void *arg) { // but recv should return 0 signaling the remote // end has closed the connection. - transferAmount = recv(sockfd, readbuf, 1, 0); + transferAmount = do_msg_read(sockfd, &readmsg, readPos, 0, NULL, NULL); if (transferAmount) { printf("case 3: read != 0\n"); finish(EXIT_FAILURE); @@ -176,6 +178,12 @@ int main() { struct sockaddr_in addr; int res; + memset(&readmsg, 0, sizeof(msg_t)); + memset(&writemsg, 0, sizeof(msg_t)); + writemsg.length = strlen(MESSAGE) + 1; + writemsg.buffer = malloc(writemsg.length); + strncpy(writemsg.buffer, MESSAGE, writemsg.length); + sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd == -1) { perror("cannot create socket"); @@ -199,9 +207,9 @@ int main() { } #if EMSCRIPTEN - emscripten_set_main_loop(iter, 0, 0); + emscripten_set_main_loop(main_loop, 0, 0); #else - while (1) iter(NULL); + while (1) main_loop(NULL); #endif return EXIT_SUCCESS; diff --git a/tests/sockets/test_sockets_select.c b/tests/sockets/test_sockets_select_server_no_accept_client.c index e05bd4c8..e05bd4c8 100644 --- a/tests/sockets/test_sockets_select.c +++ b/tests/sockets/test_sockets_select_server_no_accept_client.c diff --git a/tests/sockets/test_sockets_select_server_no_accept_server.c b/tests/sockets/test_sockets_select_server_no_accept_server.c new file mode 100644 index 00000000..4a399ed1 --- /dev/null +++ b/tests/sockets/test_sockets_select_server_no_accept_server.c @@ -0,0 +1,86 @@ +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/socket.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int serverfd = -1; + +void iter(void *arg) { + int res; + fd_set fdr; + fd_set fdw; + + // see if there are any connections to accept / write to + FD_ZERO(&fdr); + FD_ZERO(&fdw); + FD_SET(serverfd, &fdr); + if (clientfd != -1) FD_SET(clientfd, &fdw); + res = select(64, &fdr, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + exit(EXIT_SUCCESS); + } + + if (FD_ISSET(serverfd, &fdr)) { + printf("accepted someone\n"); + clientfd = accept(serverfd, NULL, NULL); + assert(clientfd != -1); + } + + if (FD_ISSET(clientfd, &fdw)) { + do_send(clientfd); + } +} + +int main() { + struct sockaddr_in addr; + int res; + + // create the socket + serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (serverfd == -1) { + perror("cannot create socket"); + exit(EXIT_FAILURE); + } + fcntl(serverfd, F_SETFL, O_NONBLOCK); + + // bind and listen to the supplied port + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(SOCKK); + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { + perror("inet_pton failed"); + exit(EXIT_FAILURE); + } + + res = bind(serverfd, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + res = listen(serverfd, 50); + if (res == -1) { + perror("listen failed"); + exit(EXIT_FAILURE); + } + +#if EMSCRIPTEN + emscripten_set_main_loop(iter, 60, 0); +#else + while (1) iter(NULL); +#endif + + return EXIT_SUCCESS; +} |