spec/network/tcp/tcp_spec.rb in ronin-support-0.5.1 vs spec/network/tcp/tcp_spec.rb in ronin-support-0.5.2
- old
+ new
@@ -4,264 +4,307 @@
require 'resolv'
describe Network::TCP do
describe "helper methods", :network do
let(:host) { 'smtp.gmail.com' }
- let(:port) { 25 }
+ let(:port) { 587 }
- let(:server_host) { 'localhost' }
- let(:server_ip) { Resolv.getaddress(server_host) }
-
subject do
obj = Object.new
obj.extend described_class
obj
end
+ shared_examples "TCP Server" do
+ let(:server_host) { 'localhost' }
+ let(:server_port) { 1024 + rand(65535 - 1024) }
+ let(:server_ip) { Resolv.getaddress(server_host) }
+ let(:server) { TCPServer.new(server_host,server_port) }
+ let(:server_bind_ip) { server.addr[3] }
+ let(:server_bind_port) { server.addr[1] }
+
+ before(:each) { server.listen(1) }
+ after(:each) { server.close }
+ end
+
describe "#tcp_open?" do
- let(:host) { 'example.com' }
- let(:port) { 80 }
+ include_examples "TCP Server"
+ let(:host) { server_bind_ip }
+ let(:port) { server_bind_port }
+
it "should return true for open ports" do
- subject.tcp_open?(host,port).should == true
+ expect(subject.tcp_open?(host,port)).to be(true)
end
+ let(:closed_port) { port + 1 }
+
it "should return false for closed ports" do
- subject.tcp_open?('localhost',rand(1024) + 1).should == false
+ expect(subject.tcp_open?(host,closed_port)).to be(false)
end
- it "should have a timeout for firewalled ports" do
- timeout = 2
+ context "when given a timeout" do
+ it "should have a timeout for firewalled ports" do
+ timeout = 2
- t1 = Time.now
- subject.tcp_open?(host,1337,nil,nil,timeout)
- t2 = Time.now
+ t1 = Time.now
+ subject.tcp_open?(host,port+1,nil,nil,timeout)
+ t2 = Time.now
- (t2 - t1).to_i.should <= timeout
+ expect((t2 - t1).to_i).to be <= timeout
+ end
end
end
describe "#tcp_connect" do
- let(:local_port) { 1024 + rand(65535 - 1024) }
+ let(:host) { 'example.com' }
+ let(:port) { 80 }
it "should open a TCPSocket" do
socket = subject.tcp_connect(host,port)
- socket.should be_kind_of(TCPSocket)
- socket.should_not be_closed
+ expect(socket).to be_kind_of(TCPSocket)
+ expect(socket).not_to be_closed
socket.close
end
- it "should bind to a local host and port" do
- socket = subject.tcp_connect(host,port,nil,local_port)
- bound_port = socket.addr[1]
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- bound_port.should == local_port
+ it "should bind to a local host and port" do
+ socket = subject.tcp_connect(host,port,nil,local_port)
+ bound_port = socket.addr[1]
- socket.close
+ expect(bound_port).to eq(local_port)
+
+ socket.close
+ end
end
- it "should yield the new TCPSocket" do
- socket = nil
+ context "when given a block" do
+ it "should yield the new TCPSocket" do
+ socket = nil
- subject.tcp_connect(host,port) do |yielded_socket|
- socket = yielded_socket
- end
+ subject.tcp_connect(host,port) do |yielded_socket|
+ socket = yielded_socket
+ end
- socket.should_not be_closed
- socket.close
+ expect(socket).not_to be_closed
+ socket.close
+ end
end
end
describe "#tcp_connect_and_send" do
- let(:data) { "HELO ronin\n" }
- let(:local_port) { 1024 + rand(65535 - 1024) }
+ let(:data) { "HELO ronin-support\n" }
- let(:expected_response) { "250 mx.google.com at your service\r\n" }
+ let(:expected_response) { "250 #{host} at your service\r\n" }
it "should connect and then send data" do
socket = subject.tcp_connect_and_send(data,host,port)
banner = socket.readline
response = socket.readline
- response.should == expected_response
+ expect(response).to eq(expected_response)
socket.close
end
- it "should bind to a local host and port" do
- socket = subject.tcp_connect_and_send(data,host,port,nil,local_port)
- bound_port = socket.addr[1]
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- bound_port.should == local_port
+ it "should bind to a local host and port" do
+ socket = subject.tcp_connect_and_send(data,host,port,nil,local_port)
+ bound_port = socket.addr[1]
- socket.close
+ expect(bound_port).to eq(local_port)
+
+ socket.close
+ end
end
- it "should yield the TCPSocket" do
- response = nil
+ context "when given a block" do
+ it "should yield the TCPSocket" do
+ response = nil
- socket = subject.tcp_connect_and_send(data,host,port) do |socket|
- banner = socket.readline
- response = socket.readline
- end
+ socket = subject.tcp_connect_and_send(data,host,port) do |socket|
+ banner = socket.readline
+ response = socket.readline
+ end
- response.should == expected_response
+ expect(response).to eq(expected_response)
- socket.close
+ socket.close
+ end
end
end
describe "#tcp_session" do
- let(:local_port) { 1024 + rand(65535 - 1024) }
-
it "should open then close a TCPSocket" do
socket = nil
subject.tcp_session(host,port) do |yielded_socket|
socket = yielded_socket
end
- socket.should be_kind_of(TCPSocket)
- socket.should be_closed
+ expect(socket).to be_kind_of(TCPSocket)
+ expect(socket).to be_closed
end
- it "should bind to a local host and port" do
- bound_port = nil
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- subject.tcp_session(host,port,nil,local_port) do |socket|
- bound_port = socket.addr[1]
- end
+ it "should bind to a local host and port" do
+ bound_port = nil
- bound_port.should == local_port
+ subject.tcp_session(host,port,nil,local_port) do |socket|
+ bound_port = socket.addr[1]
+ end
+
+ expect(bound_port).to eq(local_port)
+ end
end
end
describe "#tcp_banner" do
let(:host) { 'smtp.gmail.com' }
- let(:port) { 25 }
- let(:local_port) { 1024 + rand(65535 - 1024) }
+ let(:port) { 587 }
- let(:expected_banner) { /^220 mx\.google\.com ESMTP/ }
+ let(:expected_banner) { /^220 #{Regexp.escape(host)} ESMTP [^\s]+ - gsmtp$/ }
it "should return the read service banner" do
banner = subject.tcp_banner(host,port)
- banner.should =~ expected_banner
+ expect(banner).to match(expected_banner)
end
- it "should bind to a local host and port" do
- banner = subject.tcp_banner(host,port,nil,local_port)
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- banner.should =~ expected_banner
- end
+ it "should bind to a local host and port" do
+ banner = subject.tcp_banner(host,port,nil,local_port)
- it "should yield the banner" do
- banner = nil
-
- subject.tcp_banner(host,port) do |yielded_banner|
- banner = yielded_banner
+ expect(banner).to match(expected_banner)
end
+ end
- banner.should =~ expected_banner
+ context "when given a block" do
+ it "should yield the banner" do
+ banner = nil
+
+ subject.tcp_banner(host,port) do |yielded_banner|
+ banner = yielded_banner
+ end
+
+ expect(banner).to match(expected_banner)
+ end
end
end
+ let(:local_host) { 'localhost' }
+ let(:local_ip) { Resolv.getaddress(local_host) }
+
describe "#tcp_send" do
- let(:server) { TCPServer.new(server_host,0) }
- let(:server_port) { server.addr[1] }
+ include_context "TCP Server"
- let(:data) { "hello\n" }
- let(:local_port) { 1024 + rand(65535 - 1024) }
+ let(:data) { "hello\n" }
- after(:all) { server.close }
-
it "should send data to a service" do
- subject.tcp_send(data,server_host,server_port)
+ subject.tcp_send(data,server_bind_ip,server_bind_port)
client = server.accept
sent = client.readline
client.close
- sent.should == data
+ expect(sent).to eq(data)
end
- it "should bind to a local host and port" do
- subject.tcp_send(data,server_host,server_port,nil,local_port)
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- client = server.accept
- client_port = client.peeraddr[1]
+ it "should bind to a local host and port" do
+ subject.tcp_send(data,server_bind_ip,server_bind_port,server_bind_ip,local_port)
- client_port.should == local_port
+ client = server.accept
+ client_port = client.peeraddr[1]
- client.close
+ expect(client_port).to eq(local_port)
+
+ client.close
+ end
end
end
describe "#tcp_server" do
- let(:server_port) { 1024 + rand(65535 - 1024) }
-
it "should create a new TCPServer" do
server = subject.tcp_server
- server.should be_kind_of(TCPServer)
- server.should_not be_closed
+ expect(server).to be_kind_of(TCPServer)
+ expect(server).not_to be_closed
server.close
end
- it "should bind to a specific port and host" do
- server = subject.tcp_server(server_port,server_host)
- bound_host = server.addr[3]
- bound_port = server.addr[1]
+ context "when given a local host and port" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- bound_host.should == server_ip
- bound_port.should == server_port
+ it "should bind to a specific port and host" do
+ server = subject.tcp_server(local_port,local_host)
+ bound_host = server.addr[3]
+ bound_port = server.addr[1]
- server.close
- end
+ expect(bound_host).to eq(local_ip)
+ expect(bound_port).to eq(local_port)
- it "should yield the new TCPServer" do
- server = nil
-
- subject.tcp_server do |yielded_server|
- server = yielded_server
+ server.close
end
+ end
- server.should be_kind_of(TCPServer)
- server.should_not be_closed
+ context "when given a block" do
+ it "should yield the new TCPServer" do
+ server = nil
- server.close
+ subject.tcp_server do |yielded_server|
+ server = yielded_server
+ end
+
+ expect(server).to be_kind_of(TCPServer)
+ expect(server).not_to be_closed
+
+ server.close
+ end
end
end
describe "#tcp_server_session" do
- let(:server_port) { 1024 + rand(65535 - 1024) }
-
it "should create a temporary TCPServer" do
server = nil
subject.tcp_server_session do |yielded_server|
server = yielded_server
end
- server.should be_kind_of(TCPServer)
- server.should be_closed
+ expect(server).to be_kind_of(TCPServer)
+ expect(server).to be_closed
end
- it "should bind to a specific port and host" do
- bound_host = nil
- bound_port = nil
-
- subject.tcp_server_session(server_port,server_host) do |yielded_server|
- bound_host = yielded_server.addr[3]
- bound_port = yielded_server.addr[1]
- end
+ context "when given a block" do
+ let(:local_port) { 1024 + rand(65535 - 1024) }
- bound_host.should == server_ip
- bound_port.should == server_port
+ it "should bind to a specific port and host" do
+ bound_host = nil
+ bound_port = nil
+
+ subject.tcp_server_session(local_port,local_host) do |server|
+ bound_host = server.addr[3]
+ bound_port = server.addr[1]
+ end
+
+ expect(bound_host).to eq(local_ip)
+ expect(bound_port).to eq(local_port)
+ end
end
end
describe "#tcp_accept" do
let(:server_port) { 1024 + rand(65535 - 1024) }