diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/runner.py | 4 | ||||
-rw-r--r-- | tests/sockets/test_getaddrinfo.c | 197 | ||||
-rw-r--r-- | tests/sockets/test_gethostbyname.c (renamed from tests/sockets/test_sockets_gethostbyname.c) | 14 | ||||
-rw-r--r-- | tests/sockets/test_getnameinfo.c | 101 | ||||
-rw-r--r-- | tests/sockets/test_sockets_echo_client.c | 19 | ||||
-rw-r--r-- | tests/sockets/test_sockets_echo_server.c | 35 | ||||
-rw-r--r-- | tests/sockets/test_sockets_msg.h | 4 | ||||
-rw-r--r-- | tests/sockets/test_sockets_partial_server.c | 13 | ||||
-rw-r--r-- | tests/sockets/test_sockets_select_server_closes_connection_client_rw.c | 14 | ||||
-rw-r--r-- | tests/sockets/test_sockets_select_server_down_client.c | 2 | ||||
-rw-r--r-- | tests/test_core.py | 164 | ||||
-rw-r--r-- | tests/test_other.py | 3 | ||||
-rw-r--r-- | tests/test_sockets.py | 263 |
13 files changed, 580 insertions, 253 deletions
diff --git a/tests/runner.py b/tests/runner.py index bbbc23e5..f0e61c4e 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -63,6 +63,10 @@ class RunnerCore(unittest.TestCase): self.working_dir = dirname os.chdir(dirname) + # Use emscripten root for node module lookup + scriptdir = os.path.dirname(os.path.abspath(__file__)) + os.environ['NODE_PATH'] = os.path.join(scriptdir, '..', 'node_modules') + if not self.save_dir: self.has_prev_ll = False for temp_file in os.listdir(TEMP_DIR): diff --git a/tests/sockets/test_getaddrinfo.c b/tests/sockets/test_getaddrinfo.c new file mode 100644 index 00000000..717a9ae7 --- /dev/null +++ b/tests/sockets/test_getaddrinfo.c @@ -0,0 +1,197 @@ +#include <arpa/inet.h> +#include <sys/socket.h> +#include <assert.h> +#include <errno.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int main() { + struct addrinfo hints; + struct addrinfo *servinfo; + struct sockaddr_in *sa4; + struct sockaddr_in6 *sa6; + int err; + + // no name or service + err = getaddrinfo(NULL, NULL, NULL, &servinfo); + assert(err == EAI_NONAME); + + // invalid socket type + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = 9999; + err = getaddrinfo("www.mozilla.org", "80", &hints, &servinfo); +#ifdef __APPLE__ + assert(err == EAI_BADHINTS); +#else + assert(err == EAI_SOCKTYPE); +#endif + + // invalid family + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNIX; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "80", &hints, &servinfo); + assert(err == EAI_FAMILY); + + // invalid service + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "foobar", &hints, &servinfo); +#ifdef __APPLE__ + assert(err == EAI_NONAME); +#else + assert(err == EAI_SERVICE); +#endif + + // test loopback resolution (ipv4) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo(NULL, "80", &hints, &servinfo); + assert(!err); + sa4 = ((struct sockaddr_in*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET); + assert(servinfo->ai_socktype == SOCK_STREAM); + assert(*(uint32_t*)&(sa4->sin_addr) == ntohl(INADDR_LOOPBACK)); + assert(sa4->sin_port == ntohs(80)); + freeaddrinfo(servinfo); + + // test loopback resolution (ipv6) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo(NULL, "81", &hints, &servinfo); + assert(!err); + sa6 = ((struct sockaddr_in6*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET6); + assert(servinfo->ai_socktype == SOCK_STREAM); + memcmp(&sa6->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)); + assert(sa6->sin6_port == ntohs(81)); + freeaddrinfo(servinfo); + + // test bind preparation + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo(NULL, "82", &hints, &servinfo); + assert(!err); + sa4 = ((struct sockaddr_in*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET); + assert(servinfo->ai_socktype == SOCK_STREAM); + assert(*(uint32_t*)&(sa4->sin_addr) == 0); + assert(sa4->sin_port == ntohs(82)); + freeaddrinfo(servinfo); + + // test numeric address (ipv4) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + err = getaddrinfo("1.2.3.4", "83", &hints, &servinfo); + assert(!err); + sa4 = ((struct sockaddr_in*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET); + assert(servinfo->ai_socktype == SOCK_DGRAM); + assert(*(uint32_t*)&(sa4->sin_addr) == 67305985); + assert(sa4->sin_port == ntohs(83)); + freeaddrinfo(servinfo); + + // test numeric address (ipv4 address specified as ipv6 with AI_V4MAPPED) + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_V4MAPPED; + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("1.2.3.4", "84", &hints, &servinfo); + assert(!err); + sa6 = ((struct sockaddr_in6*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET6); + assert(servinfo->ai_socktype == SOCK_STREAM); + assert(*((uint32_t*)&(sa6->sin6_addr)+2) == htonl(0xffff)); + assert(*((uint32_t*)&(sa6->sin6_addr)+3) == 67305985); + assert(sa6->sin6_port == ntohs(84)); + freeaddrinfo(servinfo); + + // test numeric address (ipv4 address specified as ipv6 without AI_V4MAPPED) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("1.2.3.4", "85", &hints, &servinfo); +#ifdef __linux__ + assert(err == -9 /* EAI_ADDRFAMILY */); +#else + assert(err == EAI_NONAME); +#endif + + // test numeric address (ipv6) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; + err = getaddrinfo("2001:0db8:85a3:0042:1000:8a2e:0370:7334", "86", &hints, &servinfo); + assert(!err); + sa6 = ((struct sockaddr_in6*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET6); + assert(servinfo->ai_socktype == SOCK_DGRAM); + assert(*((uint32_t*)&(sa6->sin6_addr)+0) == -1207107296); + assert(*((uint32_t*)&(sa6->sin6_addr)+1) == 1107338117); + assert(*((uint32_t*)&(sa6->sin6_addr)+2) == 780795920); + assert(*((uint32_t*)&(sa6->sin6_addr)+3) == 879980547); + assert(sa6->sin6_port == ntohs(86)); + freeaddrinfo(servinfo); + + // test numeric address (ipv6 address specified as ipv4) + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("2001:0db8:85a3:0042:1000:8a2e:0370:7334", "87", &hints, &servinfo); +#ifdef __linux__ + assert(err == -9 /* EAI_ADDRFAMILY */); +#else + assert(err == EAI_NONAME); +#endif + + // test non-numeric host with AI_NUMERICHOST + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_NUMERICHOST; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "88", &hints, &servinfo); + assert(err == EAI_NONAME); + + // test non-numeric host with AF_INET + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "89", &hints, &servinfo); + assert(!err); + sa4 = ((struct sockaddr_in*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET); + assert(servinfo->ai_socktype == SOCK_STREAM); + assert(sa4->sin_port == ntohs(89)); + + // test non-numeric host with AF_INET6 + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "90", &hints, &servinfo); + assert(!err); + sa6 = ((struct sockaddr_in6*)servinfo->ai_addr); + assert(servinfo->ai_family == AF_INET6); + assert(servinfo->ai_socktype == SOCK_STREAM); + assert(*((uint32_t*)&(sa6->sin6_addr)+0) != 0 || + *((uint32_t*)&(sa6->sin6_addr)+1) != 0 || + *((uint32_t*)&(sa6->sin6_addr)+2) != 0 || + *((uint32_t*)&(sa6->sin6_addr)+3) != 0); + assert(sa6->sin6_port == ntohs(90)); + + puts("success"); + + return EXIT_SUCCESS; +} + diff --git a/tests/sockets/test_sockets_gethostbyname.c b/tests/sockets/test_gethostbyname.c index 12fc6d9d..de7da706 100644 --- a/tests/sockets/test_sockets_gethostbyname.c +++ b/tests/sockets/test_gethostbyname.c @@ -11,16 +11,6 @@ #include <emscripten.h> #endif -int sockfd; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - int main() { char str[INET_ADDRSTRLEN]; struct in_addr addr; @@ -29,6 +19,8 @@ int main() { // resolve the hostname ot an actual address struct hostent *host = gethostbyname("slashdot.org"); + assert(host->h_addrtype == AF_INET); + assert(host->h_length == sizeof(uint32_t)); // convert the raw address to a string char **raw_addr_list = host->h_addr_list; @@ -44,6 +36,8 @@ int main() { struct hostent *host1 = gethostbyaddr(&addr, sizeof(addr), host->h_addrtype); assert(strstr(host1->h_name, "slashdot.org")); + puts("success"); + return EXIT_SUCCESS; } diff --git a/tests/sockets/test_getnameinfo.c b/tests/sockets/test_getnameinfo.c new file mode 100644 index 00000000..c3fec6b4 --- /dev/null +++ b/tests/sockets/test_getnameinfo.c @@ -0,0 +1,101 @@ +#include <arpa/inet.h> +#include <sys/socket.h> +#include <assert.h> +#include <errno.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int main() { + struct addrinfo hints; + struct addrinfo *servinfo; + struct sockaddr_in sa4; + struct sockaddr_in6 sa6; + char node[256]; + char serv[256]; + int flags; + int err; + +#ifndef __APPLE__ + // incorrect sockaddr size + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4)-1, NULL, 0, NULL, 0, 0); + assert(err == EAI_FAMILY); + + memset(&sa6, 0, sizeof(sa6)); + sa6.sin6_family = AF_INET6; + err = getnameinfo((struct sockaddr*)&sa6, sizeof(sa6)-1, NULL, 0, NULL, 0, 0); + assert(err == EAI_FAMILY); + + // invalid family + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = 9999; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), NULL, 0, NULL, 0, 0); + assert(err == EAI_FAMILY); +#endif + + // NI_NUMERICHOST and NI_NAMEREQD conflict + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + flags = NI_NUMERICHOST | NI_NAMEREQD; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, sizeof(node), serv, sizeof(serv), flags); + assert(err == EAI_NONAME); + + // too small of buffer + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + *(uint32_t*)&sa4.sin_addr = 67305985; + sa4.sin_port = htons(54321); + flags = NI_NUMERICHOST; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, 1, serv, sizeof(serv), flags); + assert(err == EAI_OVERFLOW); + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, sizeof(node), serv, 1, flags); + assert(err == EAI_OVERFLOW); + + // NI_NAMEREQD and lookup failed + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + *(uint32_t*)&sa4.sin_addr = 67305985; + flags = NI_NAMEREQD; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, sizeof(node), serv, sizeof(serv), flags); + assert(err == EAI_NONAME); + + // lookup failed + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + *(uint32_t*)&sa4.sin_addr = 67305985; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, sizeof(node), serv, sizeof(serv), flags); + assert(err == EAI_NONAME); + + // no lookup + memset(&sa4, 0, sizeof(sa4)); + sa4.sin_family = AF_INET; + *(uint32_t*)&sa4.sin_addr = 67305985; + sa4.sin_port = htons(54321); + flags = NI_NUMERICHOST; + err = getnameinfo((struct sockaddr*)&sa4, sizeof(sa4), node, sizeof(node), serv, sizeof(serv), flags); + assert(!err); + assert(!strcmp(node, "1.2.3.4")); + assert(!strcmp(serv, "54321")); + + // lookup + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + err = getaddrinfo("www.mozilla.org", "54321", &hints, &servinfo); + assert(!err); + flags = NI_NAMEREQD; + err = getnameinfo(servinfo->ai_addr, servinfo->ai_addrlen, node, sizeof(node), serv, sizeof(serv), flags); + assert(!err); + assert(strstr(node, "mozilla")); + assert(!strcmp(serv, "54321")); + + puts("success"); + + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c index 6b3ccef3..f6ea85cf 100644 --- a/tests/sockets/test_sockets_echo_client.c +++ b/tests/sockets/test_sockets_echo_client.c @@ -38,7 +38,10 @@ int echo_read; int echo_wrote; void finish(int result) { - close(server.fd); + if (server.fd) { + close(server.fd); + server.fd = 0; + } #if EMSCRIPTEN REPORT_RESULT(); #endif @@ -76,7 +79,12 @@ void main_loop(void *arg) { assert(available); res = do_msg_read(server.fd, &server.msg, echo_read, 0, NULL, NULL); - if (res != -1) echo_read += res; + if (res == 0) { + perror("server closed"); + finish(EXIT_FAILURE); + } else if (res != -1) { + echo_read += res; + } // once we've read the entire message, validate it if (echo_read >= server.msg.length) { @@ -89,7 +97,12 @@ void main_loop(void *arg) { } res = do_msg_write(server.fd, &echo_msg, echo_wrote, 0, NULL, 0); - if (res != -1) echo_wrote += res; + if (res == 0) { + perror("server closed"); + finish(EXIT_FAILURE); + } else if (res != -1) { + echo_wrote += res; + } // once we're done writing the message, read it back if (echo_wrote >= echo_msg.length) { diff --git a/tests/sockets/test_sockets_echo_server.c b/tests/sockets/test_sockets_echo_server.c index f01004c3..dbc912a6 100644 --- a/tests/sockets/test_sockets_echo_server.c +++ b/tests/sockets/test_sockets_echo_server.c @@ -1,6 +1,7 @@ #include <assert.h> #include <errno.h> #include <fcntl.h> +#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -38,8 +39,14 @@ server_t server; client_t client; void cleanup() { - if (server.fd) close(server.fd); - if (client.fd) close(client.fd); + if (client.fd) { + close(client.fd); + client.fd = 0; + } + if (server.fd) { + close(server.fd); + server.fd = 0; + } } void main_loop(void *arg) { @@ -83,7 +90,13 @@ void main_loop(void *arg) { } res = do_msg_read(fd, &client.msg, client.read, 0, (struct sockaddr *)&client.addr, &addrlen); - if (res != -1) client.read += res; + if (res == 0) { + // client disconnected + memset(&client, 0, sizeof(client_t)); + return; + } else if (res != -1) { + client.read += res; + } // once we've read the entire message, echo it back if (client.read >= client.msg.length) { @@ -96,12 +109,22 @@ void main_loop(void *arg) { } res = do_msg_write(fd, &client.msg, client.wrote, 0, (struct sockaddr *)&client.addr, sizeof(client.addr)); - if (res != -1) client.wrote += res; + if (res == 0) { + // client disconnected + memset(&client, 0, sizeof(client_t)); + return; + } else if (res != -1) { + client.wrote += res; + } - // close the client once we've echo'd back the entire message if (client.wrote >= client.msg.length) { + client.wrote = 0; + client.state = MSG_READ; + +#if CLOSE_CLIENT_AFTER_ECHO close(client.fd); memset(&client, 0, sizeof(client_t)); +#endif } } } @@ -111,7 +134,7 @@ int main() { int res; atexit(cleanup); - //signal(SIGTERM, cleanup); + signal(SIGTERM, cleanup); memset(&server, 0, sizeof(server_t)); memset(&client, 0, sizeof(client_t)); diff --git a/tests/sockets/test_sockets_msg.h b/tests/sockets/test_sockets_msg.h index 30094d65..0e68803c 100644 --- a/tests/sockets/test_sockets_msg.h +++ b/tests/sockets/test_sockets_msg.h @@ -17,9 +17,10 @@ int do_msg_read(int sockfd, msg_t *msg, int offset, int length, struct sockaddr return res; } assert(res != 0); - msg->buffer = malloc(msg->length); printf("do_msg_read: allocating %d bytes for message\n", msg->length); + + msg->buffer = malloc(msg->length); } // read the actual message @@ -52,6 +53,7 @@ int do_msg_write(int sockfd, msg_t *msg, int offset, int length, struct sockaddr assert(errno == EAGAIN); return res; } + printf("do_msg_write: sending message header for %d bytes\n", msg->length); assert(res == sizeof(int)); } diff --git a/tests/sockets/test_sockets_partial_server.c b/tests/sockets/test_sockets_partial_server.c index 44ad40a3..19f7f2af 100644 --- a/tests/sockets/test_sockets_partial_server.c +++ b/tests/sockets/test_sockets_partial_server.c @@ -1,6 +1,7 @@ #include <assert.h> #include <errno.h> #include <fcntl.h> +#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -18,8 +19,14 @@ int serverfd = 0; int clientfd = 0; void cleanup() { - if (serverfd) close(serverfd); - if (clientfd) close(clientfd); + if (serverfd) { + close(serverfd); + serverfd = 0; + } + if (clientfd) { + close(clientfd); + clientfd = 0; + } } void do_send(int sockfd) { @@ -86,7 +93,7 @@ int main() { int res; atexit(cleanup); - //signal(SIGTERM, cleanup); + signal(SIGTERM, cleanup); // create the socket serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); diff --git a/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c index 198ad232..25dcdd05 100644 --- a/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c +++ b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c @@ -89,7 +89,12 @@ void main_loop(void *arg) { // read a single byte transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL); - if (transferAmount != -1) readPos += transferAmount; + if (transferAmount == 0) { + perror("server closed"); + finish(EXIT_FAILURE); + } else if (transferAmount != -1) { + readPos += transferAmount; + } // if successfully reading 1 byte, switch to next state if (readPos >= 1) { @@ -122,7 +127,12 @@ void main_loop(void *arg) { // read a single byte transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL); - if (transferAmount != -1) readPos += transferAmount; + if (transferAmount == 0) { + perror("server closed"); + finish(EXIT_FAILURE); + } else if (transferAmount != -1) { + readPos += transferAmount; + } // with 10 bytes read the inQueue is empty => switch state if (readPos >= readmsg.length) { diff --git a/tests/sockets/test_sockets_select_server_down_client.c b/tests/sockets/test_sockets_select_server_down_client.c index e05bd4c8..27e200e0 100644 --- a/tests/sockets/test_sockets_select_server_down_client.c +++ b/tests/sockets/test_sockets_select_server_down_client.c @@ -48,7 +48,7 @@ void iter(void *arg) { char buffer[1024]; int n = recv(sockfd, buffer, sizeof(buffer), 0); if (n == -1 && retries++ > 10) { - perror("revv failed"); + perror("recv failed"); finish(EXIT_FAILURE); } else if (!n) { perror("Connection to websocket server failed as expected."); diff --git a/tests/test_core.py b/tests/test_core.py index 491db66c..16bf9a00 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -3735,6 +3735,8 @@ def process(filename): self.do_run(src, 'hello world!\n*100*\n*fivesix*\nmann\n', post_build=check) def test_emscripten_get_now(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') + if self.run_name == 'o2': self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage self.do_run(open(path_from_root('tests', 'emscripten_get_now.cpp')).read(), 'Timer resolution is good.') @@ -7437,168 +7439,6 @@ def process(filename): ''' self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected)) - def test_inet(self): - src = r''' - #include <stdio.h> - #include <arpa/inet.h> - - int main() { - printf("*%x,%x,%x,%x,%x,%x*\n", htonl(0xa1b2c3d4), htonl(0xfe3572e0), htonl(0x07abcdf0), htons(0xabcd), ntohl(0x43211234), ntohs(0xbeaf)); - in_addr_t i = inet_addr("190.180.10.78"); - printf("%x\n", i); - return 0; - } - ''' - self.do_run(src, '*d4c3b2a1,e07235fe,f0cdab07,cdab,34122143,afbe*\n4e0ab4be\n') - - def test_inet2(self): - src = r''' - #include <stdio.h> - #include <arpa/inet.h> - - int main() { - struct in_addr x, x2; - int *y = (int*)&x; - *y = 0x12345678; - printf("%s\n", inet_ntoa(x)); - int r = inet_aton(inet_ntoa(x), &x2); - printf("%s\n", inet_ntoa(x2)); - return 0; - } - ''' - self.do_run(src, '120.86.52.18\n120.86.52.18\n') - - def test_inet3(self): - src = r''' - #include <stdio.h> - #include <arpa/inet.h> - #include <sys/socket.h> - int main() { - char dst[64]; - struct in_addr x, x2; - int *y = (int*)&x; - *y = 0x12345678; - printf("%s\n", inet_ntop(AF_INET,&x,dst,sizeof dst)); - int r = inet_aton(inet_ntoa(x), &x2); - printf("%s\n", inet_ntop(AF_INET,&x2,dst,sizeof dst)); - return 0; - } - ''' - self.do_run(src, '120.86.52.18\n120.86.52.18\n') - - def test_inet4(self): - if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') - - src = r''' - #include <stdio.h> - #include <arpa/inet.h> - #include <sys/socket.h> - - void test(char *test_addr){ - char str[40]; - struct in6_addr addr; - unsigned char *p = (unsigned char*)&addr; - int ret; - ret = inet_pton(AF_INET6,test_addr,&addr); - if(ret == -1) return; - if(ret == 0) return; - if(inet_ntop(AF_INET6,&addr,str,sizeof(str)) == NULL ) return; - printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x - %s\n", - p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15],str); - } - int main(){ - test("::"); - test("::1"); - test("::1.2.3.4"); - test("::17.18.19.20"); - test("::ffff:1.2.3.4"); - test("1::ffff"); - test("::255.255.255.255"); - test("0:ff00:1::"); - test("0:ff::"); - test("abcd::"); - test("ffff::a"); - test("ffff::a:b"); - test("ffff::a:b:c"); - test("ffff::a:b:c:d"); - test("ffff::a:b:c:d:e"); - test("::1:2:0:0:0"); - test("0:0:1:2:3::"); - test("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - test("1::255.255.255.255"); - - //below should fail and not produce results.. - test("1.2.3.4"); - test(""); - test("-"); - } - ''' - self.do_run(src, - "0000:0000:0000:0000:0000:0000:0000:0000 - ::\n" - "0000:0000:0000:0000:0000:0000:0000:0001 - ::1\n" - "0000:0000:0000:0000:0000:0000:0102:0304 - ::1.2.3.4\n" - "0000:0000:0000:0000:0000:0000:1112:1314 - ::17.18.19.20\n" - "0000:0000:0000:0000:0000:ffff:0102:0304 - ::ffff:1.2.3.4\n" - "0001:0000:0000:0000:0000:0000:0000:ffff - 1::ffff\n" - "0000:0000:0000:0000:0000:0000:ffff:ffff - ::255.255.255.255\n" - "0000:ff00:0001:0000:0000:0000:0000:0000 - 0:ff00:1::\n" - "0000:00ff:0000:0000:0000:0000:0000:0000 - 0:ff::\n" - "abcd:0000:0000:0000:0000:0000:0000:0000 - abcd::\n" - "ffff:0000:0000:0000:0000:0000:0000:000a - ffff::a\n" - "ffff:0000:0000:0000:0000:0000:000a:000b - ffff::a:b\n" - "ffff:0000:0000:0000:0000:000a:000b:000c - ffff::a:b:c\n" - "ffff:0000:0000:0000:000a:000b:000c:000d - ffff::a:b:c:d\n" - "ffff:0000:0000:000a:000b:000c:000d:000e - ffff::a:b:c:d:e\n" - "0000:0000:0000:0001:0002:0000:0000:0000 - ::1:2:0:0:0\n" - "0000:0000:0001:0002:0003:0000:0000:0000 - 0:0:1:2:3::\n" - "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n" - "0001:0000:0000:0000:0000:0000:ffff:ffff - 1::ffff:ffff\n" - ) - - def test_gethostbyname(self): - if Settings.USE_TYPED_ARRAYS != 2: return self.skip("assume t2 in gethostbyname") - - src = r''' - #include <netdb.h> - #include <stdio.h> - - void test(char *hostname) { - hostent *host = gethostbyname(hostname); - if (!host) { - printf("no such thing\n"); - return; - } - printf("%s : %d : %d\n", host->h_name, host->h_addrtype, host->h_length); - char **name = host->h_aliases; - while (*name) { - printf("- %s\n", *name); - name++; - } - name = host->h_addr_list; - while (name && *name) { - printf("* "); - for (int i = 0; i < host->h_length; i++) - printf("%d.", (*name)[i]); - printf("\n"); - name++; - } - } - - int main() { - test("www.cheezburger.com"); - test("fail.on.this.never.work"); // we will "work" on this - because we are just making aliases of names to ips - test("localhost"); - return 0; - } - ''' - self.do_run(src, '''www.cheezburger.com : 2 : 4 -* -84.29.1.0. -fail.on.this.never.work : 2 : 4 -* -84.29.2.0. -localhost : 2 : 4 -* -84.29.3.0. -''') - def test_799(self): src = open(path_from_root('tests', '799.cpp'), 'r').read() self.do_run(src, '''Set PORT family: 0, port: 3979 diff --git a/tests/test_other.py b/tests/test_other.py index eaa5b0d8..fd1a6245 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -933,6 +933,9 @@ f.close() open('in.txt', 'w').write('abcdef\nghijkl') for engine in JS_ENGINES: + print >> sys.stderr, engine + if engine == NODE_JS: continue # FIXME + if engine == V8_ENGINE: continue # no stdin support in v8 shell self.assertContained('abcdef\nghijkl\neof', run_js(os.path.join(self.get_dir(), 'a.out.js'), engine=engine, stdin=open('in.txt'))) def test_ungetc_fscanf(self): diff --git a/tests/test_sockets.py b/tests/test_sockets.py index 82ddc6fe..d2bc46a2 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -34,11 +34,11 @@ def make_relay_server(port1, port2): return proc class WebsockifyServerHarness: - def __init__(self, filename, args, listen_port, target_port): + def __init__(self, filename, args, listen_port): self.pids = [] self.filename = filename - self.target_port = target_port self.listen_port = listen_port + self.target_port = listen_port-1 self.args = args or [] def __enter__(self): @@ -48,7 +48,7 @@ class WebsockifyServerHarness: # NOTE empty filename support is a hack to support # the current test_enet if self.filename: - Popen([CLANG_CC, path_from_root('tests', self.filename), '-o', 'server'] + self.args).communicate() + Popen([CLANG_CC, path_from_root('tests', self.filename), '-o', 'server', '-DSOCKK=%d' % self.target_port] + self.args).communicate() process = Popen([os.path.abspath('server')]) self.pids.append(process.pid) @@ -71,17 +71,22 @@ class WebsockifyServerHarness: class CompiledServerHarness: - def __init__(self, filename, args): + def __init__(self, filename, args, listen_port): self.pids = [] self.filename = filename + self.listen_port = listen_port self.args = args or [] def __enter__(self): - import socket, websockify + # assuming this is only used for WebSocket tests at the moment, validate that + # the ws module is installed + child = Popen(listify(NODE_JS) + ['-e', 'require("ws");']) + child.communicate() + assert child.returncode == 0, 'ws module for Node.js not installed. Please run \'npm install\' from %s' % EMSCRIPTEN_ROOT # compile the server - Popen([PYTHON, EMCC, path_from_root('tests', self.filename), '-o', 'server.js'] + self.args).communicate() - process = Popen([NODE_JS, 'server.js']) + Popen([PYTHON, EMCC, path_from_root('tests', self.filename), '-o', 'server.js', '-DSOCKK=%d' % self.listen_port] + self.args).communicate() + process = Popen(listify(NODE_JS) + ['server.js']) self.pids.append(process.pid) def __exit__(self, *args, **kwargs): @@ -96,72 +101,196 @@ class CompiledServerHarness: # proper listen server support. class sockets(BrowserCore): + def test_inet(self): + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + + int main() { + printf("*%x,%x,%x,%x,%x,%x*\n", htonl(0xa1b2c3d4), htonl(0xfe3572e0), htonl(0x07abcdf0), htons(0xabcd), ntohl(0x43211234), ntohs(0xbeaf)); + in_addr_t i = inet_addr("190.180.10.78"); + printf("%x\n", i); + return 0; + } + ''' + self.do_run(src, '*d4c3b2a1,e07235fe,f0cdab07,cdab,34122143,afbe*\n4e0ab4be\n') + + def test_inet2(self): + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + + int main() { + struct in_addr x, x2; + int *y = (int*)&x; + *y = 0x12345678; + printf("%s\n", inet_ntoa(x)); + int r = inet_aton(inet_ntoa(x), &x2); + printf("%s\n", inet_ntoa(x2)); + return 0; + } + ''' + self.do_run(src, '120.86.52.18\n120.86.52.18\n') + + def test_inet3(self): + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + #include <sys/socket.h> + int main() { + char dst[64]; + struct in_addr x, x2; + int *y = (int*)&x; + *y = 0x12345678; + printf("%s\n", inet_ntop(AF_INET,&x,dst,sizeof dst)); + int r = inet_aton(inet_ntoa(x), &x2); + printf("%s\n", inet_ntop(AF_INET,&x2,dst,sizeof dst)); + return 0; + } + ''' + self.do_run(src, '120.86.52.18\n120.86.52.18\n') + + def test_inet4(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') + + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + #include <sys/socket.h> + + void test(char *test_addr){ + char str[40]; + struct in6_addr addr; + unsigned char *p = (unsigned char*)&addr; + int ret; + ret = inet_pton(AF_INET6,test_addr,&addr); + if(ret == -1) return; + if(ret == 0) return; + if(inet_ntop(AF_INET6,&addr,str,sizeof(str)) == NULL ) return; + printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x - %s\n", + p[0],p[1],p[2],p[3], |