From 4ae44dc8d0e8370d92ff4601ccb6cd59e7f0217f Mon Sep 17 00:00:00 2001 From: Anthony Pesch Date: Fri, 9 Aug 2013 20:44:58 -0700 Subject: [PATCH] updated and temporarily disabled test_sockets_gethostbyname --- tests/runner.py | 7 +- tests/sockets/test_sockets_gethostbyname.c | 129 ++++----------------- 2 files changed, 23 insertions(+), 113 deletions(-) diff --git a/tests/runner.py b/tests/runner.py index 22cac2b02..5a82d8f8c 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -12450,7 +12450,6 @@ elif 'browser' in str(sys.argv): i = sys.argv.index('browser') sys.argv = sys.argv[:i] + sys.argv[i+1:] sys.argv += [ - 'browser.test_sockets_gethostbyname', 'browser.test_sockets_select_server_down', 'browser.test_sockets_select_server_closes_connection_rw', 'browser.test_enet' @@ -14104,9 +14103,9 @@ Press any key to continue.''' proc.communicate() return relay_server - def test_sockets_gethostbyname(self): - with self.WebsockHarness(7000): - self.btest('sockets/test_sockets_gethostbyname.c', expected='571', args=['-O2', '-DSOCKK=7001']) + # TODO add support for gethostbyaddr to re-enable this test + # def test_sockets_gethostbyname(self): + # self.btest('sockets/test_sockets_gethostbyname.c', expected='0', args=['-O2', '-DSOCKK=8997']) def test_sockets_select_server_down(self): def closedServer(q): diff --git a/tests/sockets/test_sockets_gethostbyname.c b/tests/sockets/test_sockets_gethostbyname.c index 59c55ba15..12fc6d9d9 100644 --- a/tests/sockets/test_sockets_gethostbyname.c +++ b/tests/sockets/test_sockets_gethostbyname.c @@ -1,20 +1,16 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include #include #include #include #include -#include -#include #if EMSCRIPTEN #include #endif -#define EXPECTED_BYTES 5 - int sockfd; void finish(int result) { @@ -25,113 +21,28 @@ void finish(int result) { exit(result); } -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - int bytes; - if (ioctl(sock, FIONREAD, &bytes)) return 0; - if (bytes == 0) return 0; - - char buffer[1024]; - int n; - unsigned int offset = 0; - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || - errno == EINTR) { - if (n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - perror("error in get_all_buf!"); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter() { - static char out[1024*2]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd has finished connecting and is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - int n = get_all_buf(sockfd, out+pos, 1024-pos); - if (n) printf("read! %d\n", n); - pos += n; - if (pos >= EXPECTED_BYTES) { - int i, sum = 0; - for (i=0; i < pos; i++) { - printf("%x\n", out[i]); - sum += out[i]; - } - - shutdown(sockfd, SHUT_RDWR); - - close(sockfd); - - printf("sum: %d\n", sum); - - finish(sum); - } -} - int main() { - struct sockaddr_in addr; - int res; + char str[INET_ADDRSTRLEN]; + struct in_addr addr; + const char *res; + int err; + + // resolve the hostname ot an actual address + struct hostent *host = gethostbyname("slashdot.org"); - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - struct hostent *host0 = gethostbyname("test.com"); // increment hostname counter to check for possible but at 0,0 not differentiating low/high - struct hostent *host = gethostbyname("localhost"); + // convert the raw address to a string char **raw_addr_list = host->h_addr_list; int *raw_addr = (int*)*raw_addr_list; - printf("raw addr: %d\n", *raw_addr); - char name[INET_ADDRSTRLEN]; - if (!inet_ntop(AF_INET, raw_addr, name, sizeof(name))) { - printf("could not figure out name\n"); - finish(EXIT_FAILURE); - } - printf("localhost has 'ip' of %s\n", name); + res = inet_ntop(host->h_addrtype, raw_addr, str, INET_ADDRSTRLEN); + assert(res); - if (inet_pton(AF_INET, name, &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } + // convert the string to an in_addr structure + err = inet_pton(AF_INET, str, &addr); + assert(err == 1); - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_set_main_loop(iter, 0, 0); -#else - while (1) iter(); -#endif + // do a reverse lookup on the ip address + struct hostent *host1 = gethostbyaddr(&addr, sizeof(addr), host->h_addrtype); + assert(strstr(host1->h_name, "slashdot.org")); return EXIT_SUCCESS; }