2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-06-03 05:21:49 +00:00
|
|
|
|
2005-06-09 06:18:27 +00:00
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', '..'))
|
2005-06-03 05:21:49 +00:00
|
|
|
|
|
|
|
require 'test/unit'
|
2006-01-30 17:12:51 +00:00
|
|
|
require 'rex'
|
2005-06-03 05:21:49 +00:00
|
|
|
|
|
|
|
class Rex::Socket::Tcp::UnitTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_tcp
|
2006-01-30 17:12:51 +00:00
|
|
|
port = 65434
|
|
|
|
listener = Rex::Socket.create_tcp_server( 'LocalPort' => port )
|
|
|
|
client = nil
|
2005-06-03 05:21:49 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
# Connect to the temp server
|
|
|
|
assert_nothing_raised {
|
2006-01-30 17:12:51 +00:00
|
|
|
client = Rex::Socket.create_tcp(
|
2005-06-03 05:21:49 +00:00
|
|
|
'PeerHost' => '127.0.0.1',
|
2006-01-30 17:12:51 +00:00
|
|
|
'PeerPort' => port)
|
2005-06-03 05:21:49 +00:00
|
|
|
}
|
2006-01-30 17:12:51 +00:00
|
|
|
|
|
|
|
assert_kind_of(Rex::Socket::Tcp, client, 'kindof?')
|
|
|
|
assert_equal('127.0.0.1', client.peerhost, 'peerhost')
|
|
|
|
assert_equal(port, client.peerport, 'peerport')
|
2005-06-03 05:21:49 +00:00
|
|
|
|
|
|
|
# Accept the client connection
|
2006-01-30 17:12:51 +00:00
|
|
|
server = listener.accept
|
|
|
|
assert_kind_of(Socket, server, "valid server socket connection")
|
|
|
|
|
|
|
|
# do all of the tests, once for each side
|
|
|
|
{ 'c/s' => [client, server], 's/c' => [server, client] }.each_pair { |mode, sockets|
|
|
|
|
a = sockets[0]
|
|
|
|
b = sockets[1]
|
|
|
|
|
|
|
|
string = "test\n"
|
|
|
|
assert_equal(false, a.has_read_data?(1), "#{mode} : has_read_data?, no data")
|
|
|
|
assert_equal(string.length, b.write(string), "#{mode} : write")
|
|
|
|
assert_equal(true, a.has_read_data?(1), "#{mode} : has_read_data?, with data")
|
|
|
|
assert_equal(string, a.recv(string.length), "#{mode} : recv")
|
|
|
|
|
|
|
|
string = "string\rtest\nwith\x00null"
|
|
|
|
assert_equal(string.length, a << string, "#{mode} : append")
|
|
|
|
tmp = ''; tmp = b.>>
|
|
|
|
assert_equal(string, tmp, "#{mode} : append (reverse)")
|
|
|
|
|
|
|
|
string = "\x00foobar\x00"
|
|
|
|
assert_equal(string.length, a.send(string, 0), "#{mode} : send")
|
|
|
|
assert_equal(string, b.get(), "#{mode} : get")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(true, client.shutdown(::Socket::SHUT_RD), 'client: shutdown read handle')
|
|
|
|
assert_equal(true, client.shutdown(::Socket::SHUT_WR), 'client: shutdown write handle')
|
2005-06-03 05:21:49 +00:00
|
|
|
assert_nothing_raised {
|
2006-01-30 17:12:51 +00:00
|
|
|
client.close
|
|
|
|
client = nil
|
2005-06-03 05:21:49 +00:00
|
|
|
}
|
|
|
|
ensure
|
2006-01-30 17:12:51 +00:00
|
|
|
client.close if (client)
|
|
|
|
listener.close
|
2005-06-03 05:21:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-19 21:03:39 +00:00
|
|
|
end
|