Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

JRUBY-4868: Socket.getservbyname doesn't accept string port argument

Regression test added as well.
  • Loading branch information...
commit 7ceaee3430b9642c20d0b846b983f3e6357e8893 1 parent 8186bc4
@vvs vvs authored
Showing with 23 additions and 5 deletions.
  1. +16 −4 src/org/jruby/ext/socket/RubySocket.java
  2. +7 −1 test/test_socket.rb
View
20 src/org/jruby/ext/socket/RubySocket.java
@@ -316,22 +316,34 @@ public static IRubyObject gethostbyaddr(ThreadContext context, IRubyObject recv,
ret[3] = args[0];
return runtime.newArrayNoCopy(ret);
}
+
@Deprecated
public static IRubyObject getservbyname(IRubyObject recv, IRubyObject[] args) {
return getservbyname(recv.getRuntime().getCurrentContext(), recv, args);
}
+
@JRubyMethod(required = 1, optional = 1, meta = true)
public static IRubyObject getservbyname(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
int argc = Arity.checkArgumentCount(runtime, args, 1, 2);
String name = args[0].convertToString().toString();
String proto = argc == 1 ? "tcp" : args[1].convertToString().toString();
-
+
jnr.netdb.Service service = jnr.netdb.Service.getServiceByName(name, proto);
- if(service == null) {
- throw sockerr(runtime, "no such service " + name + "/" + proto);
+
+ int port;
+ if (service != null) {
+ port = service.getPort();
+ } else {
+ // MRI behavior: try to convert the name string to port directly
+ try {
+ port = Integer.parseInt(name.trim());
+ } catch (NumberFormatException nfe) {
+ throw sockerr(runtime, "no such service " + name + "/" + proto);
+ }
}
- return runtime.newFixnum(service.getPort());
+
+ return runtime.newFixnum(port);
}
@Deprecated
View
8 test/test_socket.rb
@@ -105,6 +105,13 @@ def test_udp_socket_reuse_addr
ensure
socket.close
end
+
+ # JRUBY-4868
+ def test_getservbyname
+ assert_equal(21, Socket.getservbyname('ftp'))
+ assert_equal(21, Socket.getservbyname('21'))
+ assert_equal(21, Socket.getservbyname(' 21'))
+ end
end
class UNIXSocketTests < Test::Unit::TestCase
@@ -370,7 +377,6 @@ def test_can_read_and_get_minus_one
sock1.recv(1)
end
end
-
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.