From: Paul Hänsch Date: Mon, 16 Mar 2026 22:29:28 +0000 (+0100) Subject: refined "mustard" test X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=4d1a6c07f6499c17feeb162a1b8655fa84829449;p=quickjs_net refined "mustard" test --- diff --git a/socket-tests.js b/socket-tests.js index bb640d8..0a65e39 100644 --- a/socket-tests.js +++ b/socket-tests.js @@ -30,13 +30,15 @@ function server() { var t4, t6, u4, u6, ct4, ct6, ux, cux, sock assert(true, "running server"); - assert(t4 = net.tcpListen("localhost", 1024), "listening on ip4/tcp"); + // set up listeners + assert(t4 = net.tcpListen("localhost", 1024), "listening on ip4/tcp"); assert(t6 = net.tcp6Listen("localhost", 1024), "listening on ip6/tcp"); - assert(u4 = net.udpListen("localhost", 1024), "listening on ip4/udp"); + assert(u4 = net.udpListen("localhost", 1024), "listening on ip4/udp"); assert(u6 = net.udp6Listen("localhost", 1024), "listening on ip6/udp"); assert(ux = net.unixListen(xsock), "listening on unix/" + xsock); std.printf("\n") + // check socket props assert (t4.family == net.AF_INET, "ipv4/tcp is AF_INET"); assert (t4.type == net.SOCK_STREAM, "ipv4/tcp is SOCK_STREAM"); assert (t6.family == net.AF_INET6, "ipv6/tcp is AF_INET6"); @@ -48,6 +50,7 @@ function server() { assert (ux.family == net.AF_UNIX, "unix is AF_UNIX"); assert (ux.type == net.SOCK_STREAM, "unix is SOCK_STREAM"); + // test accept timeouts for tcp, tcp6, unix for (sock of [t4, t6, ux]) { std.printf("\n"); assert(true, "testing accept timeouts"); @@ -67,6 +70,7 @@ function server() { } std.printf("\n") + // test local*/peer* properties for (sock of [t4, t6, u4, u6]) { assert(sock.localName && sock.localPort && !sock.peerName && !sock.peerPort, "socket has local Name/Port and no peer (" + sock.localName + "/" + sock.localPort + ")"); @@ -75,18 +79,22 @@ function server() { assert(sock.localName && !sock.localPort && !sock.peerName && !sock.peerPort, "unix socket has local Name but no port or peer (" + sock.localName + ")"); + // launch client new os.Worker(scriptArgs[0]); + // accept connections assert(ct4 = t4.accept(), "received connection on ip4/tcp (by "+ct4.peerName+"/"+ct4.peerPort+")"); assert(ct6 = t6.accept(), "received connection on ip6/tcp (by "+ct6.peerName+"/"+ct6.peerPort+")"); assert(cux = ux.accept(), "received connection on unix"); + // test connection state properties assert(ct4.connected, "socket connected: ip4/tcp"); assert(ct6.connected, "socket connected: ip6/tcp"); assert(cux.connected, "socket connected: unix"); assert(!u4.connected, "socket not connected: ip4/udp"); assert(!u6.connected, "socket not connected: ip6/udp"); + // receive "ketchup", send "mayo" for ( sock of [ct4, ct6, cux, u4, u6] ) { sock.timeout = -1; assert(sock.recvString() === "ketchup", @@ -96,6 +104,7 @@ function server() { } std.printf("\n") + // close connections (causing short read in worker) assert(!t4.close(), "closed ip4/tcp listener"); assert(!t6.close(), "closed ip6/tcp listener"); assert(!ux.close(), "closed unix listener"); @@ -103,6 +112,7 @@ function server() { assert(!ct6.close(), "closed ip6/tcp connection"); assert(!cux.close(), "closed unix connection"); + // receive "mustard" on reused tcp listener assert(u4.recvString() === "mustard" && u4.ketchupPort != u4.peerPort, "received string \"mustard\" from "+u4.peerName+"/"+u4.peerPort+" ("+family[u4.family]+"/"+type[u4.type]+")") assert(u6.recvString() === "mustard" && u6.ketchupPort != u6.peerPort, @@ -116,9 +126,12 @@ function client() { assert(true, "running client"); var t4, t6, u4, u6, ux, sock; + // test global timeout setting assert(! net.getTimeout(), "default connect timeout unset"); net.setTimeout(1); assert(net.getTimeout() == 1, "set default connect timeout"); + + // test failing connections try { t4 = net.tcpConnect( "localhost", 1); } catch(e) {} assert(!t4, "conrefused for ip4/tcp"); try { t6 = net.tcp6Connect("localhost", 1); } catch(e) {} @@ -126,12 +139,14 @@ function client() { try { ux = net.unixConnect("/nonexist"); } catch(e) {} assert(!ux, "conrefused for unix"); + // test connection state property assert(t4 = net.tcpConnect("localhost", 1024), "connected to ip4/tcp"); assert(t6 = net.tcp6Connect("localhost", 1024), "connected to ip6/tcp"); assert(u4 = net.udpConnect("localhost", 1024), "connected to ip4/udp"); assert(u6 = net.udp6Connect("localhost", 1024), "connected to ip6/udp"); assert(ux = net.unixConnect(xsock), "connected to unix"); + // send "ketchup", receive "mayo" for ( sock of [t4, t6, ux, u4, u6] ) { assert(sock.connected, "socket connected: "+family[sock.family]+"/"+type[sock.type]+""); sock.timeout = 0; @@ -141,6 +156,7 @@ function client() { assert(String.fromCharCode(... new Uint8Array(sock.recv())) === "mayo", "received ArrayBuffer \"mayo\" on "+family[sock.family]+"/"+type[sock.type]+""); } + // test short reads assert(t4.recvString() === "", "short read from ip4/tcp"); assert(!t4.connected, "socket not connected: ip4/tcp"); assert(t6.recvString() === "", "short read from ip6/tcp"); @@ -148,12 +164,13 @@ function client() { assert(ux.recvString() === "", "short read from unix"); assert(!ux.connected, "socket not connected: unix"); + // set up new udp connections, test destination option in udp send, send "mustard" assert(!u4.close(), "closed ip4/udp client"); assert(!u6.close(), "closed ip6/udp client"); - assert(u4 = net.udpConnect("localhost", 1024), "connected to ip4/udp"); - assert(u6 = net.udp6Connect("localhost", 1024), "connected to ip6/udp"); - assert(u4.send("mustard"), "send string from different ip4/udp connection") - assert(u6.send("mustard"), "send string from different ip6/udp connection") + assert(u4 = net.udpConnect("localhost", 1025), "connected to ip4/udp"); + assert(u6 = net.udp6Connect("localhost", 1025), "connected to ip6/udp"); + assert(u4.send("mustard", "localhost", 1024), "send string from different ip4/udp connection") + assert(u6.send("mustard", "localhost", 1024), "send string from different ip6/udp connection") assert(!t4.close(), "closed ip4/tcp client"); assert(!t6.close(), "closed ip6/tcp client"); @@ -163,11 +180,24 @@ function client() { } if (os.Worker.parent) { + // test global timeout setting + // test failing connections + // test connection state property + // send "ketchup", receive "mayo" + // test short reads + // set up new udp connections, test destination option in udp send, send "mustard" client(); } else { - // var w = new os.Worker(scriptArgs[0]); - // new os.Worker(scriptArgs[0]); - //os.sleep(2); + // set up listeners + // check socket props + // test accept timeouts for tcp, tcp6, unix + // test local*/peer* properties + // launch client + // accept connections + // test connection state properties + // receive "ketchup", send "mayo" + // close connections (causing short read in worker) + // receive "mustard" on reused tcp listener server(); os.remove(xsock); }