2004-12-06 11:15:56 +03:00
|
|
|
begin
|
|
|
|
require "socket"
|
|
|
|
require "test/unit"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class TestTCPSocket < Test::Unit::TestCase
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_recvfrom
|
2007-09-01 11:27:26 +04:00
|
|
|
assert false, "TODO: doesn't work on mswin32/64" if /mswin/ =~ RUBY_PLATFORM
|
2004-12-06 11:15:56 +03:00
|
|
|
svr = TCPServer.new("localhost", 0)
|
2004-12-09 04:19:30 +03:00
|
|
|
th = Thread.new {
|
2004-12-07 21:26:12 +03:00
|
|
|
c = svr.accept
|
2008-07-23 18:25:32 +04:00
|
|
|
c.write "foo"
|
|
|
|
c.close
|
2004-12-06 11:15:56 +03:00
|
|
|
}
|
2004-12-08 02:35:18 +03:00
|
|
|
addr = svr.addr
|
|
|
|
sock = TCPSocket.open(addr[2], addr[1])
|
2008-07-23 18:25:32 +04:00
|
|
|
assert_equal(["foo", nil], sock.recvfrom(0x10000))
|
2004-12-09 04:19:30 +03:00
|
|
|
ensure
|
2008-12-02 09:02:23 +03:00
|
|
|
th.kill if th
|
|
|
|
th.join if th
|
2004-12-06 11:15:56 +03:00
|
|
|
end
|
2008-10-17 06:50:43 +04:00
|
|
|
|
|
|
|
def test_encoding
|
|
|
|
svr = TCPServer.new("localhost", 0)
|
|
|
|
th = Thread.new {
|
|
|
|
c = svr.accept
|
|
|
|
c.write "foo\r\n"
|
|
|
|
c.close
|
|
|
|
}
|
|
|
|
addr = svr.addr
|
|
|
|
sock = TCPSocket.open(addr[2], addr[1])
|
|
|
|
assert_equal(true, sock.binmode?)
|
|
|
|
s = sock.gets
|
|
|
|
assert_equal("foo\r\n", s)
|
|
|
|
assert_equal(Encoding.find("ASCII-8BIT"), s.encoding)
|
|
|
|
ensure
|
2008-12-02 09:02:23 +03:00
|
|
|
th.kill if th
|
|
|
|
th.join if th
|
2008-10-17 06:50:43 +04:00
|
|
|
sock.close if sock
|
|
|
|
end
|
2004-12-06 11:15:56 +03:00
|
|
|
end if defined?(TCPSocket)
|