diff options
Diffstat (limited to 'spec/ruby/library/socket/shared')
| -rw-r--r-- | spec/ruby/library/socket/shared/address.rb | 92 | ||||
| -rw-r--r-- | spec/ruby/library/socket/shared/pack_sockaddr.rb | 99 | ||||
| -rw-r--r-- | spec/ruby/library/socket/shared/partially_closable_sockets.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/library/socket/shared/socketpair.rb | 138 |
4 files changed, 52 insertions, 279 deletions
diff --git a/spec/ruby/library/socket/shared/address.rb b/spec/ruby/library/socket/shared/address.rb index f3be9cfb99..c64602df2f 100644 --- a/spec/ruby/library/socket/shared/address.rb +++ b/spec/ruby/library/socket/shared/address.rb @@ -46,7 +46,7 @@ describe :socket_local_remote_address, shared: true do end it 'returns an Addrinfo' do - @addr.should be_an_instance_of(Addrinfo) + @addr.should.instance_of?(Addrinfo) end it 'uses 0 as the protocol' do @@ -110,7 +110,7 @@ describe :socket_local_remote_address, shared: true do end it 'returns an Addrinfo' do - @addr.should be_an_instance_of(Addrinfo) + @addr.should.instance_of?(Addrinfo) end it 'uses 0 as the protocol' do @@ -129,41 +129,51 @@ describe :socket_local_remote_address, shared: true do end end - with_feature :unix_socket do - describe 'using UNIXSocket' do - before :each do - @path = SocketSpecs.socket_path - @s = UNIXServer.new(@path) - @a = UNIXSocket.new(@path) - @b = @s.accept - @addr = @object.call(@a) - end + describe 'using UNIXSocket' do + before :each do + @path = SocketSpecs.socket_path + @s = UNIXServer.new(@path) + @a = UNIXSocket.new(@path) + @b = @s.accept + @addr = @object.call(@a) + end - after :each do - [@b, @a, @s].each(&:close) - rm_r(@path) - end + after :each do + [@b, @a, @s].each(&:close) + rm_r(@path) + end - it 'uses AF_UNIX as the address family' do - @addr.afamily.should == Socket::AF_UNIX - end + it 'uses AF_UNIX as the address family' do + @addr.afamily.should == Socket::AF_UNIX + end - it 'uses PF_UNIX as the protocol family' do - @addr.pfamily.should == Socket::PF_UNIX - end + it 'uses PF_UNIX as the protocol family' do + @addr.pfamily.should == Socket::PF_UNIX + end - it 'uses SOCK_STREAM as the socket type' do - @addr.socktype.should == Socket::SOCK_STREAM + it 'uses SOCK_STREAM as the socket type' do + @addr.socktype.should == Socket::SOCK_STREAM + end + + it 'uses the correct socket path' do + if @method == :local_address + @addr.unix_path.should == "" + else + @addr.unix_path.should == @path end + end - it 'uses the correct socket path' do + platform_is_not :windows do + it 'equals address of peer socket' do if @method == :local_address - @addr.unix_path.should == "" + @addr.to_s.should == @b.remote_address.to_s else - @addr.unix_path.should == @path + @addr.to_s.should == @b.local_address.to_s end end + end + guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.2" } do it 'equals address of peer socket' do if @method == :local_address @addr.to_s.should == @b.remote_address.to_s @@ -171,23 +181,23 @@ describe :socket_local_remote_address, shared: true do @addr.to_s.should == @b.local_address.to_s end end + end - it 'returns an Addrinfo' do - @addr.should be_an_instance_of(Addrinfo) - end + it 'returns an Addrinfo' do + @addr.should.instance_of?(Addrinfo) + end - it 'uses 0 as the protocol' do - @addr.protocol.should == 0 - end + it 'uses 0 as the protocol' do + @addr.protocol.should == 0 + end - it 'can be used to connect to the server' do - skip if @method == :local_address - b = @addr.connect - begin - b.remote_address.to_s.should == @addr.to_s - ensure - b.close - end + it 'can be used to connect to the server' do + skip if @method == :local_address + b = @addr.connect + begin + b.remote_address.to_s.should == @addr.to_s + ensure + b.close end end end @@ -230,7 +240,7 @@ describe :socket_local_remote_address, shared: true do end it 'returns an Addrinfo' do - @addr.should be_an_instance_of(Addrinfo) + @addr.should.instance_of?(Addrinfo) end it 'uses 0 as the protocol' do diff --git a/spec/ruby/library/socket/shared/pack_sockaddr.rb b/spec/ruby/library/socket/shared/pack_sockaddr.rb deleted file mode 100644 index 2df09027c9..0000000000 --- a/spec/ruby/library/socket/shared/pack_sockaddr.rb +++ /dev/null @@ -1,99 +0,0 @@ -# coding: utf-8 -describe :socket_pack_sockaddr_in, shared: true do - it "packs and unpacks" do - sockaddr_in = Socket.public_send(@method, 0, nil) - port, addr = Socket.unpack_sockaddr_in(sockaddr_in) - ["127.0.0.1", "::1"].include?(addr).should == true - port.should == 0 - - sockaddr_in = Socket.public_send(@method, 0, '') - Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '0.0.0.0'] - - sockaddr_in = Socket.public_send(@method, 80, '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] - - sockaddr_in = Socket.public_send(@method, '80', '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] - - sockaddr_in = Socket.public_send(@method, nil, '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1'] - end - - describe 'using an IPv4 address' do - it 'returns a String of 16 bytes' do - str = Socket.public_send(@method, 80, '127.0.0.1') - - str.should be_an_instance_of(String) - str.bytesize.should == 16 - end - end - - platform_is_not :solaris do - describe 'using an IPv6 address' do - it 'returns a String of 28 bytes' do - str = Socket.public_send(@method, 80, '::1') - - str.should be_an_instance_of(String) - str.bytesize.should == 28 - end - end - end - - platform_is :solaris do - describe 'using an IPv6 address' do - it 'returns a String of 32 bytes' do - str = Socket.public_send(@method, 80, '::1') - - str.should be_an_instance_of(String) - str.bytesize.should == 32 - end - end - end -end - -describe :socket_pack_sockaddr_un, shared: true do - with_feature :unix_socket do - it 'should be idempotent' do - bytes = Socket.public_send(@method, '/tmp/foo').bytes - bytes[2..9].should == [47, 116, 109, 112, 47, 102, 111, 111] - bytes[10..-1].all?(&:zero?).should == true - end - - it "packs and unpacks" do - sockaddr_un = Socket.public_send(@method, '/tmp/s') - Socket.unpack_sockaddr_un(sockaddr_un).should == '/tmp/s' - end - - it "handles correctly paths with multibyte chars" do - sockaddr_un = Socket.public_send(@method, '/home/вася/sock') - path = Socket.unpack_sockaddr_un(sockaddr_un).encode('UTF-8', 'UTF-8') - path.should == '/home/вася/sock' - end - end - - platform_is :linux do - it 'returns a String of 110 bytes' do - str = Socket.public_send(@method, '/tmp/test.sock') - - str.should be_an_instance_of(String) - str.bytesize.should == 110 - end - end - - platform_is :bsd do - it 'returns a String of 106 bytes' do - str = Socket.public_send(@method, '/tmp/test.sock') - - str.should be_an_instance_of(String) - str.bytesize.should == 106 - end - end - - platform_is_not :windows, :aix do - it "raises ArgumentError for paths that are too long" do - # AIX doesn't raise error - long_path = 'a' * 110 - -> { Socket.public_send(@method, long_path) }.should raise_error(ArgumentError) - end - end -end diff --git a/spec/ruby/library/socket/shared/partially_closable_sockets.rb b/spec/ruby/library/socket/shared/partially_closable_sockets.rb index 1bdff08bf6..b1c2ebabe1 100644 --- a/spec/ruby/library/socket/shared/partially_closable_sockets.rb +++ b/spec/ruby/library/socket/shared/partially_closable_sockets.rb @@ -1,4 +1,4 @@ -describe "partially closable sockets", shared: true do +describe :partially_closable_sockets, shared: true do it "if the write end is closed then the other side can read past EOF without blocking" do @s1.write("foo") @s1.close_write diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb deleted file mode 100644 index 25146cfff6..0000000000 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ /dev/null @@ -1,138 +0,0 @@ -describe :socket_socketpair, shared: true do - platform_is_not :windows do - it "ensures the returned sockets are connected" do - s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, 1, 0) - s1.puts("test") - s2.gets.should == "test\n" - s1.close - s2.close - end - - it "responses with array of two sockets" do - begin - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - ensure - s1.close - s2.close - end - end - - describe 'using an Integer as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, Socket::SOCK_STREAM) - - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - s1.close - s2.close - end - end - - describe 'using a Symbol as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - s1.close - s2.close - end - - it 'raises SocketError for an unknown address family' do - -> { Socket.public_send(@method, :CATS, :STREAM) }.should raise_error(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - -> { Socket.public_send(@method, :UNIX, :CATS) }.should raise_error(SocketError) - end - end - - describe 'using a String as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, 'UNIX', 'STREAM') - - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - s1.close - s2.close - end - - it 'raises SocketError for an unknown address family' do - -> { Socket.public_send(@method, 'CATS', 'STREAM') }.should raise_error(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - -> { Socket.public_send(@method, 'UNIX', 'CATS') }.should raise_error(SocketError) - end - end - - describe 'using an object that responds to #to_str as the 1st and 2nd argument' do - it 'returns two Socket objects' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('UNIX') - type.stub!(:to_str).and_return('STREAM') - - s1, s2 = Socket.public_send(@method, family, type) - - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - s1.close - s2.close - end - - it 'raises TypeError when #to_str does not return a String' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return(Socket::AF_UNIX) - type.stub!(:to_str).and_return(Socket::SOCK_STREAM) - - -> { Socket.public_send(@method, family, type) }.should raise_error(TypeError) - end - - it 'raises SocketError for an unknown address family' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('CATS') - type.stub!(:to_str).and_return('STREAM') - - -> { Socket.public_send(@method, family, type) }.should raise_error(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('UNIX') - type.stub!(:to_str).and_return('CATS') - - -> { Socket.public_send(@method, family, type) }.should raise_error(SocketError) - end - end - - it 'accepts a custom protocol as an Integer as the 3rd argument' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM, Socket::IPPROTO_IP) - s1.should be_an_instance_of(Socket) - s2.should be_an_instance_of(Socket) - s1.close - s2.close - end - - it 'connects the returned Socket objects' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - begin - s1.write('hello') - s2.recv(5).should == 'hello' - ensure - s1.close - s2.close - end - end - end -end |
