first commit
CI / Build (clang, clang++, ubuntu-22.04, linux, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Build (clang, clang++, windows-2022, windows, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Release (push) Canceled after 0s

This commit is contained in:
2026-09-13 22:18:11 +01:00
commit 08c03ac533
6701 changed files with 1577314 additions and 0 deletions
@@ -0,0 +1,91 @@
assert('Addrinfo') do
assert_equal(Class, Addrinfo.class)
end
assert('super class of Addrinfo') do
assert_equal(Object, Addrinfo.superclass)
end
assert('Addrinfo.getaddrinfo') do
ary = Addrinfo.getaddrinfo("localhost", 53, Socket::AF_INET, Socket::SOCK_STREAM)
assert_true(ary.size >= 1)
ai = ary[0]
assert_equal(ai.afamily, Socket::AF_INET)
assert_equal(ai.pfamily, Socket::PF_INET)
assert_equal(ai.socktype, Socket::SOCK_STREAM)
assert_equal(ai.ip_address, '127.0.0.1')
assert_equal(ai.ip_port, 53)
end
assert('Addrinfo.foreach') do
# assume Addrinfo.getaddrinfo works well
a = Addrinfo.getaddrinfo("localhost", 80)
b = []
Addrinfo.foreach("localhost", 80) { |ai| b << ai }
assert_equal(a.size, b.size)
end
assert('Addrinfo.ip') do
ai = Addrinfo.ip('127.0.0.1')
assert_equal('127.0.0.1', ai.ip_address)
assert_equal(Socket::AF_INET, ai.afamily)
assert_equal(0, ai.ip_port)
assert_equal(0, ai.socktype)
assert_equal(0, ai.protocol)
end
assert('Addrinfo.tcp') do
ai = Addrinfo.tcp('127.0.0.1', 25)
assert_equal('127.0.0.1', ai.ip_address)
assert_equal(Socket::AF_INET, ai.afamily)
assert_equal(25, ai.ip_port)
assert_equal(Socket::SOCK_STREAM, ai.socktype)
assert_equal(Socket::IPPROTO_TCP, ai.protocol)
end
assert('Addrinfo.udp') do
ai = Addrinfo.udp('127.0.0.1', 53)
assert_equal('127.0.0.1', ai.ip_address)
assert_equal(Socket::AF_INET, ai.afamily)
assert_equal(53, ai.ip_port)
assert_equal(Socket::SOCK_DGRAM, ai.socktype)
assert_equal(Socket::IPPROTO_UDP, ai.protocol)
end
assert('Addrinfo.unix') do
skip "unix is not supported on Windows" if SocketTest.win?
a1 = Addrinfo.unix('/tmp/sock')
assert_true(a1.unix?)
assert_equal('/tmp/sock', a1.unix_path)
assert_equal(Socket::SOCK_STREAM, a1.socktype)
a2 = Addrinfo.unix('/tmp/sock', Socket::SOCK_DGRAM)
assert_equal(Socket::SOCK_DGRAM, a2.socktype)
end
assert('Addrinfo#afamily') do
skip "afamily is not supported on Windows" if SocketTest.win?
ai4 = Addrinfo.new(Socket.sockaddr_in(1, '127.0.0.1'))
ai6 = Addrinfo.new(Socket.sockaddr_in(1, '::1'))
aiu = Addrinfo.new(Socket.sockaddr_un('/tmp/sock'))
assert_equal(Socket::AF_INET, ai4.afamily)
assert_equal(Socket::AF_INET6, ai6.afamily)
assert_equal(Socket::AF_UNIX, aiu.afamily)
end
# assert('Addrinfo#canonname') do
# #getnameinfo
# assert('Addrinfo#inspect') do
# assert('Addrinfo#inspect_socket') do
# assert('Addrinfo#ip?') do
# assert('Addrinfo#ip_address') do
# assert('Addrinfo#ip_port') do
# assert('Addrinfo#ip_unpack') do
# assert('Addrinfo#ipv4?') do
# assert('Addrinfo#ipv6?') do
# assert('Addrinfo#pfamily') do
# assert('Addrinfo#protocol') do
# assert('Addrinfo#socktype') do
# assert('Addrinfo#to_sockaddr') do
# assert('Addrinfo#unix?') do
# #unix_path
@@ -0,0 +1,17 @@
assert('BasicSocket') do
assert_equal(Class, BasicSocket.class)
end
assert('super class of BasicSocket') do
assert_equal(IO, BasicSocket.superclass)
end
assert('BasicSocket.do_not_reverse_lookup') do
assert_equal(BasicSocket.do_not_reverse_lookup, true)
end
assert('BasicSocket.do_not_reverse_lookup=') do
BasicSocket.do_not_reverse_lookup = false
assert_equal(BasicSocket.do_not_reverse_lookup, false)
BasicSocket.do_not_reverse_lookup = true
end
@@ -0,0 +1,44 @@
unless SocketTest.win?
# Note: most of tests below will fail if UDPSocket is broken.
assert('IPSocket.getaddress') do
l = IPSocket.getaddress("localhost")
assert_true (l == "127.0.0.1" or l == "::1")
end
assert('IPSocket.addr') do
localhost = "127.0.0.1"
s = UDPSocket.new
s.bind(localhost, 0)
port = Addrinfo.new(s.getsockname).ip_port
a = s.addr
assert_equal "AF_INET", a[0]
assert_equal port, a[1]
assert_equal localhost, a[2]
assert_equal localhost, a[3]
s.close
true
end
assert('IPSocket.peeraddr') do
localhost = "127.0.0.1"
server = UDPSocket.new
server.bind(localhost, 0)
port = server.local_address.ip_port
client = UDPSocket.new
client.connect(localhost, port)
a = client.peeraddr
assert_equal "AF_INET", a[0]
assert_equal port, a[1]
assert_equal localhost, a[2]
assert_equal localhost, a[3]
client.close
server.close
true
end
end # win?
@@ -0,0 +1,38 @@
unless SocketTest.win?
assert('Socket.gethostname') do
assert_true(Socket.gethostname.is_a? String)
end
assert('Socket::getaddrinfo') do
ret = Socket.getaddrinfo("localhost", 53, Socket::AF_INET, Socket::SOCK_DGRAM)
assert_true ret.size >= 1
a = ret[0]
assert_equal "AF_INET", a[0]
assert_equal 53, a[1]
# documents says it's a hostname but CRuby returns an address
#assert_equal "127.0.0.1", a[2]
assert_equal "127.0.0.1", a[3]
assert_equal Socket::AF_INET, a[4]
assert_equal Socket::SOCK_DGRAM, a[5]
assert_equal Socket::IPPROTO_UDP, a[6] unless SocketTest.cygwin?
end
assert('Socket#recvfrom') do
begin
sstr = "abcdefg"
s = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
c = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
s.bind(Socket.sockaddr_in(0, "127.0.0.1"))
c.send sstr, 0, s.getsockname
rstr, ai = s.recvfrom sstr.size
assert_equal sstr, rstr
assert_equal "127.0.0.1", ai.ip_address
ensure
s.close rescue nil
c.close rescue nil
end
end
end # win?
@@ -0,0 +1,84 @@
#include <stdlib.h>
#include "mruby.h"
#include "mruby/error.h"
#if defined(_WIN32) || defined(_WIN64)
#include <io.h>
#if defined(_MSC_VER) || \
(defined(MRB_MINGW32_VERSION) && MRB_MINGW32_VERSION < 3021) || \
(defined(MRB_MINGW64_VERSION) && MRB_MINGW64_VERSION < 4000)
#include <fcntl.h>
#include <sys/stat.h>
#define open _open
#define close _close
#define unlink _unlink
static int
mkstemp(char *p)
{
int fd;
char* fname = _mktemp(p);
if (fname == NULL)
return -1;
fd = open(fname, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);
if (fd >= 0)
return fd;
return -1;
}
#endif
#else
#include <unistd.h>
#endif
mrb_value
mrb_sockettest_tmppath(mrb_state *mrb, mrb_value klass)
{
char name[] = "mruby-socket.XXXXXXXX";
int fd = mkstemp(name);
if (fd == -1) {
mrb_sys_fail(mrb, 0);
}
if (close(fd) == -1) {
mrb_sys_fail(mrb, 0);
}
if (unlink(name) == -1) {
mrb_sys_fail(mrb, 0);
}
return mrb_str_new_cstr(mrb, name);
}
mrb_value
mrb_sockettest_win_p(mrb_state *mrb, mrb_value klass)
{
#ifdef _WIN32
return mrb_true_value();
#else
return mrb_false_value();
#endif
}
mrb_value
mrb_sockettest_cygwin_p(mrb_state *mrb, mrb_value klass)
{
#if defined(__CYGWIN__) || defined(__CYGWIN32__)
return mrb_true_value();
#else
return mrb_false_value();
#endif
}
void
mrb_mruby_socket_gem_test(mrb_state* mrb)
{
struct RClass *c = mrb_define_module(mrb, "SocketTest");
mrb_define_class_method(mrb, c, "tmppath", mrb_sockettest_tmppath, MRB_ARGS_NONE());
mrb_define_class_method(mrb, c, "win?", mrb_sockettest_win_p, MRB_ARGS_NONE());
mrb_define_class_method(mrb, c, "cygwin?", mrb_sockettest_cygwin_p, MRB_ARGS_NONE());
}
@@ -0,0 +1,4 @@
#assert('TCPSocket.gethostbyname') do
#assert('TCPSocket.new') do
#assert('TCPSocket#close') do
#assert('TCPSocket#write') do
@@ -0,0 +1,16 @@
assert('UDPSocket.new') do
s = UDPSocket.new
assert_true(s.is_a? UDPSocket)
s.close
s = UDPSocket.new(Socket::AF_INET6)
assert_true(s.is_a? UDPSocket)
s.close
true
end
#assert('UDPSocket#connect') do
#assert('UDPSocket#send') do
#assert('UDPSocket#recv') do
#assert('UDPSocket#bind') do
#assert('UDPSocket#recvfrom_nonblock') do
@@ -0,0 +1,130 @@
unless SocketTest.win? || SocketTest.cygwin?
def unixserver_test_block
path = SocketTest.tmppath
File.unlink path rescue nil
begin
result = yield path
ensure
File.unlink path rescue nil
end
result
end
def with_unix_server
unixserver_test_block do |path|
UNIXServer.open(path) { |server|
yield path, server
}
end
end
def with_unix_client
with_unix_server do |path, server|
UNIXSocket.open(path) do |csock|
ssock = server.accept
begin
yield path, server, ssock, csock
ensure
ssock.close unless ssock.closed? rescue nil
end
end
end
end
assert('UNIXServer.new') do
unixserver_test_block do |path|
server = UNIXServer.new(path)
assert_true server.is_a? UNIXServer
server.close
File.unlink path
s2 = nil
result = UNIXServer.open(path) { |s1|
assert_true s1.is_a? UNIXServer
s2 = s1
1234
}
assert_equal 1234, result
assert_true s2.is_a? UNIXServer
assert_true s2.closed?
end
end
# assert('UNIXServer#accept_nonblock') - would block if fails
assert('UNIXServer#addr') do
with_unix_server do |path, server|
assert_equal [ "AF_UNIX", path], server.addr
end
end
assert('UNIXServer#path') do
with_unix_server do |path, server|
assert_equal path, server.path
end
end
# assert('UNIXServer#peeraddr') - will raise a runtime exception
assert('UNIXServer#listen') do
with_unix_server do |path, server|
assert_equal 0, server.listen(1)
end
end
assert('UNIXServer#sysaccept') do
with_unix_server do |path, server|
UNIXSocket.open(path) do |csock|
begin
fd = server.sysaccept
assert_true fd.kind_of? Integer
ensure
IO._sysclose(fd) rescue nil
end
end
end
end
assert('UNIXSocket.new') do
with_unix_server do |path, server|
c = UNIXSocket.new(path)
assert_true c.is_a? UNIXSocket
c.close
true
end
end
assert('UNIXSocket#addr') do
with_unix_client do |path, server, ssock, csock|
assert_equal [ "AF_UNIX", path ], ssock.addr
assert_equal [ "AF_UNIX", "" ], csock.addr
end
end
assert('UNIXSocket#path') do
with_unix_client do |path, server, ssock, csock|
assert_equal path, ssock.path
assert_equal "", csock.path
end
end
assert('UNIXSocket#peeraddr') do
with_unix_client do |path, server, ssock, csock|
assert_equal [ "AF_UNIX", "" ], ssock.peeraddr
assert_equal [ "AF_UNIX", path ], csock.peeraddr
end
end
assert('UNIXSocket#recvfrom') do
with_unix_client do |path, server, ssock, csock|
str = "0123456789"
ssock.send str, 0
a = csock.recvfrom(8)
assert_equal str[0, 8], a[0]
assert_equal "AF_UNIX", a[1][0]
# a[1][1] would be "" or something
end
end
end # SocketTest.win?