metasploit-framework/lib/rex/socket/tcp.rb.ut.rb

48 lines
1.4 KiB
Ruby

#!/usr/bin/ruby
$:.unshift(File.join('..', '..', File.dirname(__FILE__)))
require 'test/unit'
require 'Rex/Socket/Tcp'
class Rex::Socket::Tcp::UnitTest < Test::Unit::TestCase
def test_tcp
serv_port = 65432
serv = TCPServer.new('127.0.0.1', serv_port)
begin
# Connect to the temp server
t = nil
assert_nothing_raised {
t = Rex::Socket.create_tcp(
'PeerHost' => '127.0.0.1',
'PeerPort' => serv_port)
}
assert_kind_of(Rex::Socket::Tcp, t, "valid tcp socket")
# Accept the client connection
serv_con = serv.accept
assert_kind_of(TCPSocket, serv_con, "valid server socket connection")
assert_equal(5, t.write("test\n"), "cli: write test")
assert_equal("test\n", serv_con.recv(5), "srv: read test")
assert_equal(10, serv_con.send("A" * 10, 10), "srv: write A*10")
assert_equal("A" * 10, t.get, "cli: gobble A*10")
assert_equal(5, t << "test\n", "cli: << test")
assert_equal("test\n", serv_con.recv(5), "srv: read test (2)")
assert_equal(5, serv_con.send("testa", 6), "srv: write testa (3)")
assert_kind_of(Array, t.poll_read(1), "cli: poll read")
assert_equal("testa", t.get, "cli: gobble testa")
assert_equal(true, t.shutdown(::Socket::SHUT_RD), "cli: shutdown read")
assert_equal(true, t.shutdown(::Socket::SHUT_WR), "cli: shutdown read")
assert_nothing_raised {
t.close
}
ensure
serv.close
end
end
end