Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #2014 from frodsan/fix_socket_constants
Browse files Browse the repository at this point in the history
Fix Socket#socketpair on X19.
  • Loading branch information
dbussink committed Nov 17, 2012
2 parents 01ec9b2 + 477e3ec commit bcb15bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/18/socket.rb
Expand Up @@ -682,6 +682,8 @@ def self.socketpair(domain, type, protocol, klass=self)
else
raise SocketError, "unknown socket type #{type}"
end
elsif !type.kind_of? Integer
raise Errno::EPROTONOSUPPORT, type.inspect
end

FFI::MemoryPointer.new :int, 2 do |mp|
Expand Down
8 changes: 8 additions & 0 deletions lib/19/socket.rb
Expand Up @@ -684,6 +684,14 @@ def self.socketpair(domain, type, protocol, klass=self)
end
end

if type.kind_of? Symbol
begin
type = Socket::Constants.const_get("SOCK_#{type}")
rescue NameError
raise SocketError, "unknown socket type #{type}"
end
end

FFI::MemoryPointer.new :int, 2 do |mp|
Socket::Foreign.socketpair(domain, type, protocol, mp)
fd0, fd1 = mp.read_array_of_int(2)
Expand Down
18 changes: 18 additions & 0 deletions spec/ruby/library/socket/shared/socketpair.rb
Expand Up @@ -8,4 +8,22 @@
s2.close
end
end

ruby_version_is "1.9" do
it "raises SocketError if given symbol is not a Socket constants reference" do
lambda { Socket.socketpair(Socket::AF_UNIX, :NO_EXIST, 0) }.should raise_error(SocketError)
end

it "not raises SocketError if given symbol references a Socket constant" do
[ :DGRAM, :RAW, :RDM, :SEQPACKET, :STREAM ].each do |socket_type|
lambda { Socket.socketpair(Socket::AF_UNIX, socket_type, 0) }.should_not raise_error(SocketError)
end
end
end

ruby_version_is ""..."1.9" do
it "raises Errno::EPROTONOSUPPORT if socket type is not a String or Integer" do
lambda { Socket.socketpair(Socket::AF_UNIX, :DGRAM, 0) }.should raise_error(Errno::EPROTONOSUPPORT)
end
end
end

0 comments on commit bcb15bd

Please sign in to comment.