diff --git a/test-http_simple.js b/http_simple.js similarity index 100% rename from test-http_simple.js rename to http_simple.js diff --git a/src/net.cc b/src/net.cc index 8148937c18..3b262b6283 100644 --- a/src/net.cc +++ b/src/net.cc @@ -42,14 +42,14 @@ using namespace node; #define CLOSED_SYMBOL String::NewSymbol("closed") static const struct addrinfo server_tcp_hints = -/* ai_flags */ { AI_PASSIVE | AI_ADDRCONFIG +/* ai_flags */ { AI_PASSIVE /* ai_family */ , AF_UNSPEC /* ai_socktype */ , SOCK_STREAM , 0 }; static const struct addrinfo client_tcp_hints = -/* ai_flags */ { AI_ADDRCONFIG +/* ai_flags */ { 0 /* ai_family */ , AF_UNSPEC /* ai_socktype */ , SOCK_STREAM , 0 @@ -591,21 +591,21 @@ static void SetRemoteAddress (Local connection_handle, struct sockaddr *addr) { HandleScope scope; - char ip4[INET_ADDRSTRLEN], ip6[INET6_ADDRSTRLEN]; + char ip[INET6_ADDRSTRLEN]; Local remote_address; + if (addr->sa_family == AF_INET) { struct sockaddr_in *sa = reinterpret_cast(addr); - inet_ntop(AF_INET, &(sa->sin_addr), ip4, INET_ADDRSTRLEN); - remote_address = String::New(ip4); + inet_ntop(AF_INET, &(sa->sin_addr), ip, INET6_ADDRSTRLEN); + remote_address = String::New(ip); } else if (addr->sa_family == AF_INET6) { struct sockaddr_in6 *sa6 = reinterpret_cast(addr); - inet_ntop(AF_INET6, &(sa6->sin6_addr), ip6, INET6_ADDRSTRLEN); - remote_address = String::New(ip6); + inet_ntop(AF_INET6, &(sa6->sin6_addr), ip, INET6_ADDRSTRLEN); + remote_address = String::New(ip); + + } else assert(0 && "received a bad sa_family"); - } else { - assert(0 && "received a bad sa_family"); - } connection_handle->Set(REMOTE_ADDRESS_SYMBOL, remote_address); } diff --git a/test/test-tcp-pingpong.js b/test/test-tcp-pingpong.js index aa7570d086..9e03fbda73 100644 --- a/test/test-tcp-pingpong.js +++ b/test/test-tcp-pingpong.js @@ -1,40 +1,44 @@ include("mjsunit.js"); -var port = 12123; -var N = 1000; -var count = 0; -var sent_final_ping = false; - -function Ponger (socket) { - socket.setEncoding("utf8"); - socket.timeout = 0; - - assertEquals("127.0.0.1", socket.remoteAddress); - - socket.onReceive = function (data) { - assertEquals("open", socket.readyState); - assertTrue(count <= N); - if (/PING/.exec(data)) { - socket.send("PONG"); - } - }; - - socket.onEOF = function () { - assertEquals("writeOnly", socket.readyState); - socket.close(); - }; - - socket.onDisconnect = function (had_error) { - assertFalse(had_error); - assertEquals("closed", socket.readyState); - socket.server.close(); - }; -} - -function onLoad () { - var server = new node.tcp.Server(Ponger); - server.listen(port); +var tests_run = 0; + +function pingPongTest (port, host, on_complete) { + var N = 1000; + var count = 0; + var sent_final_ping = false; + + var server = new node.tcp.Server(function (socket) { + assertTrue(socket.remoteAddress !== null); + assertTrue(socket.remoteAddress !== undefined); + if (host === "127.0.0.1") + assertEquals(socket.remoteAddress, "127.0.0.1"); + else if (host == null) + assertEquals(socket.remoteAddress, "127.0.0.1"); + + socket.setEncoding("utf8"); + socket.timeout = 0; + + socket.onReceive = function (data) { + assertEquals("open", socket.readyState); + assertTrue(count <= N); + if (/PING/.exec(data)) { + socket.send("PONG"); + } + }; + + socket.onEOF = function () { + assertEquals("writeOnly", socket.readyState); + socket.close(); + }; + + socket.onDisconnect = function (had_error) { + assertFalse(had_error); + assertEquals("closed", socket.readyState); + socket.server.close(); + }; + }); + server.listen(port, host); var client = new node.tcp.Connection(); assertEquals("closed", client.readyState); @@ -65,15 +69,25 @@ function onLoad () { client.close(); } }; - - client.onEOF = function () { + + client.onDisconnect = function () { + assertEquals(N+1, count); + assertTrue(sent_final_ping); + if (on_complete) on_complete(); + tests_run += 1; }; assertEquals("closed", client.readyState); - client.connect(port, "localhost"); + client.connect(port, host); +} + +function onLoad () { + /* All are run at once, so run on different ports */ + pingPongTest(20989, "localhost"); + pingPongTest(20988, null); + pingPongTest(20997, "::1"); } function onExit () { - assertEquals(N+1, count); - assertTrue(sent_final_ping); + assertEquals(3, tests_run); } diff --git a/test_client.js b/test_client.js deleted file mode 100644 index 02ba11956c..0000000000 --- a/test_client.js +++ /dev/null @@ -1,41 +0,0 @@ -var c = new node.http.Client(8000, "127.0.0.1"); - -c.onError = function () { - puts("http client connection error."); -}; - -var req = c.get("/bytes/123", [["Accept", "*/*"]]); -req.finish(function (res) { - puts("response 1: " + res.statusCode.toString()); - - res.onBody = function (chunk) { - chunk = chunk.encodeUtf8(); - puts("response 1 body <" + JSON.stringify(chunk) + ">"); - return true; - }; - - res.onBodyComplete = function () { - puts("response 1 complete!"); - return true; - }; -}); - -setTimeout(function () { - var req2 = c.get("/something/else"); - //node.debug("start req2"); - req2.finish(function (res) { - puts("response 2: " + res.statusCode.toString()); - - res.onBody = function (chunk) { - chunk = chunk.encodeUtf8(); - puts("response 2 body <" + JSON.stringify(chunk) + ">"); - return true; - }; - - res.onBodyComplete = function () { - puts("response 2 complete!"); - return true; - }; - }); -}, 2000); -