2018-08-03 12:19:40 -04:00
|
|
|
require_relative '../spec_helper'
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2018-08-03 12:19:40 -04:00
|
|
|
describe "UDPSocket#send" do
|
2017-05-07 08:04:49 -04:00
|
|
|
before :each do
|
2017-06-15 08:48:52 -04:00
|
|
|
@port = nil
|
2017-05-07 08:04:49 -04:00
|
|
|
@server_thread = Thread.new do
|
|
|
|
@server = UDPSocket.open
|
|
|
|
begin
|
2017-06-15 08:48:52 -04:00
|
|
|
@server.bind(nil, 0)
|
|
|
|
@port = @server.addr[1]
|
2017-05-27 17:55:02 -04:00
|
|
|
begin
|
|
|
|
@msg = @server.recvfrom_nonblock(64)
|
|
|
|
rescue IO::WaitReadable
|
|
|
|
IO.select([@server])
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@server.close if !@server.closed?
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-15 08:48:52 -04:00
|
|
|
Thread.pass while @server_thread.status and !@port
|
|
|
|
end
|
|
|
|
|
|
|
|
after :each do
|
|
|
|
@server_thread.join
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sends data in ad hoc mode" do
|
|
|
|
@socket = UDPSocket.open
|
2017-06-15 08:48:52 -04:00
|
|
|
@socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.close
|
|
|
|
@server_thread.join
|
|
|
|
|
|
|
|
@msg[0].should == "ad hoc"
|
|
|
|
@msg[1][0].should == "AF_INET"
|
2018-09-25 06:41:16 -04:00
|
|
|
@msg[1][1].should be_kind_of(Integer)
|
2017-05-07 08:04:49 -04:00
|
|
|
@msg[1][3].should == "127.0.0.1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends data in ad hoc mode (with port given as a String)" do
|
|
|
|
@socket = UDPSocket.open
|
2017-06-15 08:48:52 -04:00
|
|
|
@socket.send("ad hoc", 0, SocketSpecs.hostname, @port.to_s)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.close
|
|
|
|
@server_thread.join
|
|
|
|
|
|
|
|
@msg[0].should == "ad hoc"
|
|
|
|
@msg[1][0].should == "AF_INET"
|
2018-09-25 06:41:16 -04:00
|
|
|
@msg[1][1].should be_kind_of(Integer)
|
2017-05-07 08:04:49 -04:00
|
|
|
@msg[1][3].should == "127.0.0.1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends data in connection mode" do
|
|
|
|
@socket = UDPSocket.open
|
2017-06-15 08:48:52 -04:00
|
|
|
@socket.connect(SocketSpecs.hostname, @port)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.send("connection-based", 0)
|
|
|
|
@socket.close
|
|
|
|
@server_thread.join
|
|
|
|
|
|
|
|
@msg[0].should == "connection-based"
|
|
|
|
@msg[1][0].should == "AF_INET"
|
2018-09-25 06:41:16 -04:00
|
|
|
@msg[1][1].should be_kind_of(Integer)
|
2017-05-07 08:04:49 -04:00
|
|
|
@msg[1][3].should == "127.0.0.1"
|
|
|
|
end
|
2017-05-27 17:55:02 -04:00
|
|
|
|
|
|
|
it "raises EMSGSIZE if data is too too big" do
|
|
|
|
@socket = UDPSocket.open
|
|
|
|
begin
|
|
|
|
lambda do
|
2017-06-15 08:48:52 -04:00
|
|
|
@socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s)
|
2017-05-27 17:55:02 -04:00
|
|
|
end.should raise_error(Errno::EMSGSIZE)
|
|
|
|
ensure
|
2017-06-15 08:48:52 -04:00
|
|
|
@socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket.close
|
|
|
|
@server_thread.join
|
|
|
|
end
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2018-08-03 12:19:40 -04:00
|
|
|
|
|
|
|
describe 'UDPSocket#send' do
|
|
|
|
SocketSpecs.each_ip_protocol do |family, ip_address|
|
|
|
|
before do
|
|
|
|
@server = UDPSocket.new(family)
|
|
|
|
@client = UDPSocket.new(family)
|
|
|
|
|
|
|
|
@server.bind(ip_address, 0)
|
|
|
|
|
|
|
|
@addr = @server.connect_address
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@server.close
|
|
|
|
@client.close
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'using a disconnected socket' do
|
|
|
|
describe 'without a destination address' do
|
|
|
|
it "raises #{SocketSpecs.dest_addr_req_error}" do
|
|
|
|
lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a destination address as separate arguments' do
|
|
|
|
it 'returns the amount of sent bytes' do
|
|
|
|
@client.send('hello', 0, @addr.ip_address, @addr.ip_port).should == 5
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not persist the connection after sending data' do
|
|
|
|
@client.send('hello', 0, @addr.ip_address, @addr.ip_port)
|
|
|
|
|
|
|
|
lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a destination address as a single String argument' do
|
|
|
|
it 'returns the amount of sent bytes' do
|
|
|
|
@client.send('hello', 0, @server.getsockname).should == 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'using a connected socket' do
|
|
|
|
describe 'without an explicit destination address' do
|
|
|
|
before do
|
|
|
|
@client.connect(@addr.ip_address, @addr.ip_port)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the amount of bytes written' do
|
|
|
|
@client.send('hello', 0).should == 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with an explicit destination address' do
|
|
|
|
before do
|
|
|
|
@alt_server = UDPSocket.new(family)
|
|
|
|
|
|
|
|
@alt_server.bind(ip_address, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@alt_server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends the data to the given address instead' do
|
|
|
|
@client.send('hello', 0, @alt_server.getsockname).should == 5
|
|
|
|
|
|
|
|
lambda { @server.recv(5) }.should block_caller
|
|
|
|
|
|
|
|
@alt_server.recv(5).should == 'hello'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|