summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/socket/shared')
-rw-r--r--spec/ruby/library/socket/shared/pack_sockaddr.rb99
-rw-r--r--spec/ruby/library/socket/shared/partially_closable_sockets.rb13
-rw-r--r--spec/ruby/library/socket/shared/socketpair.rb138
3 files changed, 250 insertions, 0 deletions
diff --git a/spec/ruby/library/socket/shared/pack_sockaddr.rb b/spec/ruby/library/socket/shared/pack_sockaddr.rb
new file mode 100644
index 0000000000..40b0d8d59e
--- /dev/null
+++ b/spec/ruby/library/socket/shared/pack_sockaddr.rb
@@ -0,0 +1,99 @@
+# 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
+ lambda { 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
new file mode 100644
index 0000000000..1bdff08bf6
--- /dev/null
+++ b/spec/ruby/library/socket/shared/partially_closable_sockets.rb
@@ -0,0 +1,13 @@
+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
+ @s2.read("foo".size + 1).should == "foo"
+ end
+
+ it "closing the write end ensures that the other side can read until EOF" do
+ @s1.write("hello world")
+ @s1.close_write
+ @s2.read.should == "hello world"
+ end
+end
diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb
new file mode 100644
index 0000000000..08db2e59b8
--- /dev/null
+++ b/spec/ruby/library/socket/shared/socketpair.rb
@@ -0,0 +1,138 @@
+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
+ lambda { Socket.public_send(@method, :CATS, :STREAM) }.should raise_error(SocketError)
+ end
+
+ it 'raises SocketError for an unknown socket type' do
+ lambda { 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
+ lambda { Socket.public_send(@method, 'CATS', 'STREAM') }.should raise_error(SocketError)
+ end
+
+ it 'raises SocketError for an unknown socket type' do
+ lambda { 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)
+
+ lambda { 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')
+
+ lambda { 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')
+
+ lambda { 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