diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/socket/udpsocket | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/socket/udpsocket')
6 files changed, 142 insertions, 0 deletions
diff --git a/spec/rubyspec/library/socket/udpsocket/bind_spec.rb b/spec/rubyspec/library/socket/udpsocket/bind_spec.rb new file mode 100644 index 0000000000..067baa2472 --- /dev/null +++ b/spec/rubyspec/library/socket/udpsocket/bind_spec.rb @@ -0,0 +1,35 @@ +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, SocketSpecs.port) + + lambda { @socket.bind(SocketSpecs.hostname, SocketSpecs.port) }.should raise_error + end + + it "receives a hostname and a port" do + @socket.bind(SocketSpecs.hostname, SocketSpecs.port) + + port, host = Socket.unpack_sockaddr_in(@socket.getsockname) + + host.should == "127.0.0.1" + port.should == SocketSpecs.port + end + + it "binds to INADDR_ANY if the hostname is empty" do + @socket.bind("", SocketSpecs.port) + port, host = Socket.unpack_sockaddr_in(@socket.getsockname) + host.should == "0.0.0.0" + port.should == SocketSpecs.port + end +end diff --git a/spec/rubyspec/library/socket/udpsocket/connect_spec.rb b/spec/rubyspec/library/socket/udpsocket/connect_spec.rb new file mode 100644 index 0000000000..fcd29e1257 --- /dev/null +++ b/spec/rubyspec/library/socket/udpsocket/connect_spec.rb @@ -0,0 +1,2 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) diff --git a/spec/rubyspec/library/socket/udpsocket/new_spec.rb b/spec/rubyspec/library/socket/udpsocket/new_spec.rb new file mode 100644 index 0000000000..5a2e4e1f31 --- /dev/null +++ b/spec/rubyspec/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 && !@socket.closed? + 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/rubyspec/library/socket/udpsocket/open_spec.rb b/spec/rubyspec/library/socket/udpsocket/open_spec.rb new file mode 100644 index 0000000000..188f879ed1 --- /dev/null +++ b/spec/rubyspec/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 && !@socket.closed? + end + + it "allows calls to open without arguments" do + @socket = UDPSocket.open + @socket.should be_kind_of(UDPSocket) + end +end diff --git a/spec/rubyspec/library/socket/udpsocket/recvfrom_nonblock_spec.rb b/spec/rubyspec/library/socket/udpsocket/recvfrom_nonblock_spec.rb new file mode 100644 index 0000000000..fcd29e1257 --- /dev/null +++ b/spec/rubyspec/library/socket/udpsocket/recvfrom_nonblock_spec.rb @@ -0,0 +1,2 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) diff --git a/spec/rubyspec/library/socket/udpsocket/send_spec.rb b/spec/rubyspec/library/socket/udpsocket/send_spec.rb new file mode 100644 index 0000000000..ad0e6a7f2f --- /dev/null +++ b/spec/rubyspec/library/socket/udpsocket/send_spec.rb @@ -0,0 +1,58 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UDPSocket.send" do + before :each do + @ready = false + @server_thread = Thread.new do + @server = UDPSocket.open + @server.bind(nil, SocketSpecs.port) + @ready = true + begin + @msg = @server.recvfrom_nonblock(64) + rescue IO::WaitReadable + IO.select([@server]) + retry + end + @server.close + end + Thread.pass while @server_thread.status and !@ready + end + + it "sends data in ad hoc mode" do + @socket = UDPSocket.open + @socket.send("ad hoc", 0, SocketSpecs.hostname,SocketSpecs.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,SocketSpecs.str_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 connection mode" do + @socket = UDPSocket.open + @socket.connect(SocketSpecs.hostname,SocketSpecs.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 +end |