Skip to content

Commit

Permalink
Replace SocketError with Socket::ResolutionError in rsock_raise_socke…
Browse files Browse the repository at this point in the history
…t_error

rsock_raise_socket_error is called only when getaddrinfo and getaddrname fail
  • Loading branch information
shioimm authored and mame committed Nov 30, 2023
1 parent e905027 commit 52f6de4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
8 changes: 6 additions & 2 deletions ext/socket/init.c
Expand Up @@ -50,10 +50,14 @@ rsock_raise_socket_error(const char *reason, int error)
VALUE msg = rb_sprintf("%s: ", reason);
if (!enc) enc = rb_default_internal_encoding();
rb_str_concat(msg, rb_w32_conv_from_wchar(gai_strerrorW(error), enc));
rb_exc_raise(rb_exc_new_str(rb_eSocket, msg));
#else
rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
VALUE msg = rb_sprintf("%s: %s", reason, gai_strerror(error));
#endif

StringValue(msg);
VALUE self = rb_class_new_instance(1, &msg, rb_eResolution);
rb_ivar_set(self, id_error_code, INT2NUM(error));
rb_exc_raise(self);
}

#if defined __APPLE__
Expand Down
2 changes: 1 addition & 1 deletion test/fiber/test_address_resolve.rb
Expand Up @@ -179,7 +179,7 @@ def test_addrinfo_getaddrinfo_non_existing_domain_blocking
Fiber.set_scheduler scheduler

Fiber.schedule do
assert_raise(SocketError) {
assert_raise(Socket::ResolutionError) {
Addrinfo.getaddrinfo("non-existing-domain.abc", nil)
}
end
Expand Down
4 changes: 2 additions & 2 deletions test/socket/test_addrinfo.rb
Expand Up @@ -103,7 +103,7 @@ def test_addrinfo_predicates
end

def test_error_message
e = assert_raise_with_message(SocketError, /getaddrinfo/) do
e = assert_raise_with_message(Socket::ResolutionError, /getaddrinfo/) do
Addrinfo.ip("...")
end
m = e.message
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_family_addrinfo
ai = Addrinfo.unix("/testdir/sock").family_addrinfo("/testdir/sock2")
assert_equal("/testdir/sock2", ai.unix_path)
assert_equal(Socket::SOCK_STREAM, ai.socktype)
assert_raise(SocketError) { Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("::1", 80) }
assert_raise(Socket::ResolutionError) { Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("::1", 80) }
end

def random_port
Expand Down
14 changes: 11 additions & 3 deletions test/socket/test_socket.rb
Expand Up @@ -91,20 +91,20 @@ def test_bind

def test_getaddrinfo
# This should not send a DNS query because AF_UNIX.
assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") }
assert_raise(Socket::ResolutionError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") }
end

def test_getaddrinfo_raises_no_errors_on_port_argument_of_0 # [ruby-core:29427]
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', 0, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', '0', Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', '00', Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_raise(SocketError, '[ruby-core:29427]'){ Socket.getaddrinfo(nil, nil, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_raise(Socket::ResolutionError, '[ruby-core:29427]'){ Socket.getaddrinfo(nil, nil, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ TCPServer.open('localhost', 0) {} }
end


def test_getnameinfo
assert_raise(SocketError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) }
assert_raise(Socket::ResolutionError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) }
assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http\0", "example.net"])}
assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http", "example.net\0"])}
end
Expand Down Expand Up @@ -770,4 +770,12 @@ def test_udp_recvmsg_truncation
s2.close
end

def test_resolurion_error_error_code
begin
Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX")
rescue => e
assert_equal(e.error_code, Socket::EAI_FAMILY)
end
end

end if defined?(Socket)

0 comments on commit 52f6de4

Please sign in to comment.