diff options
author | Jean Boussier <[email protected]> | 2022-09-20 16:10:56 +0200 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2023-08-30 10:07:18 +0200 |
commit | bcc905100f1079e191632cfd02319c10af82dac0 (patch) | |
tree | e8dbe37eb4de741c51210f65ffaa0336ce579c8a /test/socket/test_unix.rb | |
parent | acedbcb1b4eb6b362f11e783bff53c237d05afc6 (diff) |
BasicSocket#recv* return `nil` rather than an empty packet
[Bug #19012]
man recvmsg(2) states:
> Return Value
> These calls return the number of bytes received, or -1 if an error occurred.
> The return value will be 0 when the peer has performed an orderly shutdown.
Not too sure how one is supposed to make the difference between a packet of
size 0 and a closed connection.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6407
Diffstat (limited to 'test/socket/test_unix.rb')
-rw-r--r-- | test/socket/test_unix.rb | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index e206339db0..9d9faa4387 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -420,9 +420,10 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase s1.recv_nonblock(10) fail rescue => e - assert(IO::EAGAINWaitReadable === e) - assert(IO::WaitReadable === e) + assert_kind_of(IO::EWOULDBLOCKWaitReadable, e) + assert_kind_of(IO::WaitReadable, e) end + s2.send("", 0) s2.send("haha", 0) s2.send("", 0) @@ -446,6 +447,69 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase s2.close if s2 end + def test_stream_pair + s1, s2 = UNIXSocket.pair(Socket::SOCK_STREAM) + begin + s1.recv_nonblock(10) + fail + rescue => e + assert_kind_of(IO::EWOULDBLOCKWaitReadable, e) + assert_kind_of(IO::WaitReadable, e) + end + + s2.send("", 0) + s2.send("haha", 0) + assert_equal("haha", s1.recv(10)) + assert_raise(IO::EWOULDBLOCKWaitReadable) { s1.recv_nonblock(10) } + + buf = "".dup + s2.send("BBBBBB", 0) + IO.select([s1]) + rv = s1.recv(100, 0, buf) + assert_equal buf.object_id, rv.object_id + assert_equal "BBBBBB", rv + + s2.close + assert_nil(s1.recv(10)) + rescue Errno::EPROTOTYPE => error + omit error.message + ensure + s1.close if s1 + s2.close if s2 + end + + def test_seqpacket_pair + s1, s2 = UNIXSocket.pair(Socket::SOCK_SEQPACKET) + begin + s1.recv_nonblock(10) + fail + rescue => e + assert_kind_of(IO::EWOULDBLOCKWaitReadable, e) + assert_kind_of(IO::WaitReadable, e) + end + + s2.send("", 0) + s2.send("haha", 0) + assert_equal(nil, s1.recv(10)) # no way to distinguish empty packet from EOF with SOCK_SEQPACKET + assert_equal("haha", s1.recv(10)) + assert_raise(IO::EWOULDBLOCKWaitReadable) { s1.recv_nonblock(10) } + + buf = "".dup + s2.send("BBBBBB", 0) + IO.select([s1]) + rv = s1.recv(100, 0, buf) + assert_equal buf.object_id, rv.object_id + assert_equal "BBBBBB", rv + + s2.close + assert_nil(s1.recv(10)) + rescue Errno::EPROTOTYPE, Errno::EPROTONOSUPPORT => error + omit error.message + ensure + s1.close if s1 + s2.close if s2 + end + def test_dgram_pair_sendrecvmsg_errno_set if /mswin|mingw/ =~ RUBY_PLATFORM omit("AF_UNIX + SOCK_DGRAM is not supported on windows") |