diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/library/socket/udpsocket | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/socket/udpsocket')
-rw-r--r-- | spec/ruby/library/socket/udpsocket/bind_spec.rb | 42 | ||||
-rw-r--r-- | spec/ruby/library/socket/udpsocket/new_spec.rb | 32 | ||||
-rw-r--r-- | spec/ruby/library/socket/udpsocket/open_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/library/socket/udpsocket/send_spec.rb | 78 | ||||
-rw-r--r-- | spec/ruby/library/socket/udpsocket/write_spec.rb | 21 |
5 files changed, 186 insertions, 0 deletions
diff --git a/spec/ruby/library/socket/udpsocket/bind_spec.rb b/spec/ruby/library/socket/udpsocket/bind_spec.rb new file mode 100644 index 0000000000..bdc3c3d33f --- /dev/null +++ b/spec/ruby/library/socket/udpsocket/bind_spec.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UDPSocket.bind" do + + before :each do + @socket = UDPSocket.new + end + + after :each do + @socket.close unless @socket.closed? + end + + it "binds the socket to a port" do + @socket.bind(SocketSpecs.hostname, 0) + @socket.addr[1].should be_kind_of(Integer) + end + + it "raises Errno::EINVAL when already bound" do + @socket.bind(SocketSpecs.hostname, 0) + + lambda { + @socket.bind(SocketSpecs.hostname, @socket.addr[1]) + }.should raise_error(Errno::EINVAL) + end + + it "receives a hostname and a port" do + @socket.bind(SocketSpecs.hostname, 0) + + port, host = Socket.unpack_sockaddr_in(@socket.getsockname) + + host.should == "127.0.0.1" + port.should == @socket.addr[1] + end + + it "binds to INADDR_ANY if the hostname is empty" do + @socket.bind("", 0) + port, host = Socket.unpack_sockaddr_in(@socket.getsockname) + host.should == "0.0.0.0" + port.should == @socket.addr[1] + end +end diff --git a/spec/ruby/library/socket/udpsocket/new_spec.rb b/spec/ruby/library/socket/udpsocket/new_spec.rb new file mode 100644 index 0000000000..5a2e4e1f31 --- /dev/null +++ b/spec/ruby/library/socket/udpsocket/new_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe 'UDPSocket.new' do + after :each do + @socket.close if @socket && [email protected]? + end + + it 'without arguments' do + @socket = UDPSocket.new + @socket.should be_an_instance_of(UDPSocket) + end + + it 'using Fixnum argument' do + @socket = UDPSocket.new(Socket::AF_INET) + @socket.should be_an_instance_of(UDPSocket) + end + + it 'using Symbol argument' do + @socket = UDPSocket.new(:INET) + @socket.should be_an_instance_of(UDPSocket) + end + + it 'using String argument' do + @socket = UDPSocket.new('INET') + @socket.should be_an_instance_of(UDPSocket) + end + + it 'raises Errno::EAFNOSUPPORT if unsupported family passed' do + lambda { UDPSocket.new(-1) }.should raise_error(Errno::EAFNOSUPPORT) + end +end diff --git a/spec/ruby/library/socket/udpsocket/open_spec.rb b/spec/ruby/library/socket/udpsocket/open_spec.rb new file mode 100644 index 0000000000..188f879ed1 --- /dev/null +++ b/spec/ruby/library/socket/udpsocket/open_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UDPSocket.open" do + after :each do + @socket.close if @socket && [email protected]? + end + + it "allows calls to open without arguments" do + @socket = UDPSocket.open + @socket.should be_kind_of(UDPSocket) + end +end diff --git a/spec/ruby/library/socket/udpsocket/send_spec.rb b/spec/ruby/library/socket/udpsocket/send_spec.rb new file mode 100644 index 0000000000..1a6f44b26e --- /dev/null +++ b/spec/ruby/library/socket/udpsocket/send_spec.rb @@ -0,0 +1,78 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UDPSocket.send" do + before :each do + @port = nil + @server_thread = Thread.new do + @server = UDPSocket.open + begin + @server.bind(nil, 0) + @port = @server.addr[1] + begin + @msg = @server.recvfrom_nonblock(64) + rescue IO::WaitReadable + IO.select([@server]) + retry + end + ensure + @server.close if [email protected]? + end + end + Thread.pass while @server_thread.status and !@port + end + + after :each do + @server_thread.join + end + + it "sends data in ad hoc mode" do + @socket = UDPSocket.open + @socket.send("ad hoc", 0, SocketSpecs.hostname, @port) + @socket.close + @server_thread.join + + @msg[0].should == "ad hoc" + @msg[1][0].should == "AF_INET" + @msg[1][1].should be_kind_of(Fixnum) + @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 + @socket.send("ad hoc", 0, SocketSpecs.hostname, @port.to_s) + @socket.close + @server_thread.join + + @msg[0].should == "ad hoc" + @msg[1][0].should == "AF_INET" + @msg[1][1].should be_kind_of(Fixnum) + @msg[1][3].should == "127.0.0.1" + end + + it "sends data in connection mode" do + @socket = UDPSocket.open + @socket.connect(SocketSpecs.hostname, @port) + @socket.send("connection-based", 0) + @socket.close + @server_thread.join + + @msg[0].should == "connection-based" + @msg[1][0].should == "AF_INET" + @msg[1][1].should be_kind_of(Fixnum) + @msg[1][3].should == "127.0.0.1" + end + + it "raises EMSGSIZE if data is too too big" do + @socket = UDPSocket.open + begin + lambda do + @socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s) + end.should raise_error(Errno::EMSGSIZE) + ensure + @socket.send("ad hoc", 0, SocketSpecs.hostname, @port) + @socket.close + @server_thread.join + end + end +end diff --git a/spec/ruby/library/socket/udpsocket/write_spec.rb b/spec/ruby/library/socket/udpsocket/write_spec.rb new file mode 100644 index 0000000000..11e38bb470 --- /dev/null +++ b/spec/ruby/library/socket/udpsocket/write_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UDPSocket#write" do + it "raises EMSGSIZE if msg is too long" do + begin + host = SocketSpecs.hostname + s1 = UDPSocket.new + s1.bind(host, 0) + s2 = UDPSocket.new + s2.connect(host, s1.addr[1]) + + lambda do + s2.write('1' * 100_000) + end.should raise_error(Errno::EMSGSIZE) + ensure + s1.close if s1 && !s1.closed? + s2.close if s2 && !s2.closed? + end + end +end |