From 5cdb8299db6b18398954c21a83e6dbc83f66622b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sun, 15 Mar 2026 23:42:32 +0100 Subject: [PATCH] test functions for socket module --- socket-tests.js | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 socket-tests.js diff --git a/socket-tests.js b/socket-tests.js new file mode 100644 index 0000000..002d59e --- /dev/null +++ b/socket-tests.js @@ -0,0 +1,104 @@ +import * as os from "os"; +import * as std from "std"; +import * as net from "socket.so"; + +var xsock="/tmp/js_socket_test_" + os.getpid(); + +function gassert(cond, msgstr, errstr, name) { + if ( ! errstr ) errstr = msgstr; + if (cond){ + std.printf("%s: Assert Success: %s\n", name, msgstr); + } else { + std.printf("%s: Assert FAILED: %s\n", name, msgstr); + os.remove(xsock); + os.exit(1); + } +} + +function server() { + function assert(cond, m, e) { gassert(cond, m, e, "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"); + assert(t6 = net.tcp6Listen("localhost", 1024), "listening on ip6/tcp"); + 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") + 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"); + assert (t6.type == net.SOCK_STREAM, "ipv6/tcp is SOCK_STREAM"); + assert (u4.family == net.AF_INET, "ipv4/udp is AF_INET"); + assert (u4.type == net.SOCK_DGRAM, "ipv4/udp is SOCK_DGRAM"); + assert (u6.family == net.AF_INET6, "ipv6/udp is AF_INET6"); + assert (u6.type == net.SOCK_DGRAM, "ipv6/udp is SOCK_DGRAM"); + assert (ux.family == net.AF_UNIX, "unix is AF_UNIX"); + assert (ux.type == net.SOCK_STREAM, "unix is SOCK_STREAM"); + + for (sock of [t4, t6, ux]) { + std.printf("\n"); + assert(true, "testing accept timeouts"); + assert(true, "Socketfamily: " + sock.family + " name: " + sock.localName + " port: " + sock.localPort); + sock.timeout = .000001; + assert(sock.timeout == .000001, "set short timeout"); + assert (! sock.accept(), "timeout during accept"); + sock.timeout = 0; + assert(sock.timeout == 0, "set timeout 0/nonblocking"); + assert (! sock.accept(), "noblock/timeout during accept"); + sock.timeout = 2; + assert(sock.timeout == 2, "set \"long\" (2 sec.) timeout..."); + assert (! sock.accept(), "\"long\" timeout during accept"); + sock.timeout = "junk"; + assert(! sock.timeout, "set blocking mode (but not testing due to blocking)"); + assert(! sock.connected, "socket is yet unconnected"); + } + + std.printf("\n") + 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 + ")"); + } + sock = ux; + assert(sock.localName && !sock.localPort && !sock.peerName && !sock.peerPort, + "unix socket has local Name but no port or peer (" + sock.localName + ")"); + + //new os.Worker(scriptArgs[0]); + + std.printf("\n") + assert(!t4.close(), "closed ip4/tcp"); + assert(!t6.close(), "closed ip6/tcp"); + assert(!u4.close(), "closed ip4/udp"); + assert(!u6.close(), "closed ip6/udp"); +} + +function client() { + function assert(cond, m, e) { gassert(cond, m, e, "Client"); } + assert(true, "running client"); + + var t4, t6, u4, u6, ux, sock; + try { t4 = net.tcpConnect( "localhost", 1); } catch(e) {} + try { t6 = net.tcp6Connect("localhost", 1); } catch(e) {} + try { ux = net.unixConnect("/nonexist"); } catch(e) {} + assert(!t4, "conrefused for ip4/tcp"); + assert(!t6, "conrefused for ip6/tcp"); + assert(!ux, "conrefused for unix"); + + // assert(t4 = net.tcpConnect("localhost", 1024), "listening on ip4/tcp"); + // assert(t6 = net.tcp6Connect("localhost", 1024), "listening on ip6/tcp"); + // assert(u4 = net.udpConnect("localhost", 1024), "listening on ip4/udp"); + // assert(u6 = net.udp6Connect("localhost", 1024), "listening on ip6/udp"); +} + +if (os.Worker.parent) { + client(); +} else { + // var w = new os.Worker(scriptArgs[0]); + //new os.Worker(scriptArgs[0]); + os.sleep(1); + server(); + os.remove(xsock); +} -- 2.39.5