diff options
Diffstat (limited to 'spec/ruby/library/socket/shared')
-rw-r--r-- | spec/ruby/library/socket/shared/pack_sockaddr.rb | 42 | ||||
-rw-r--r-- | spec/ruby/library/socket/shared/recv_nonblock.rb | 52 | ||||
-rw-r--r-- | spec/ruby/library/socket/shared/socketpair.rb | 115 |
3 files changed, 154 insertions, 55 deletions
diff --git a/spec/ruby/library/socket/shared/pack_sockaddr.rb b/spec/ruby/library/socket/shared/pack_sockaddr.rb index 4ffa02a8d8..714becc488 100644 --- a/spec/ruby/library/socket/shared/pack_sockaddr.rb +++ b/spec/ruby/library/socket/shared/pack_sockaddr.rb @@ -18,10 +18,28 @@ describe :socket_pack_sockaddr_in, shared: true do 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 + + 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 describe :socket_pack_sockaddr_un, shared: true do - platform_is_not :windows 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] @@ -40,10 +58,28 @@ describe :socket_pack_sockaddr_un, shared: true do 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 if path length exceeds max size" do + it "raises ArgumentError for paths that are too long" do # AIX doesn't raise error - long_path = Array.new(512, 0).join + long_path = 'a' * 110 lambda { Socket.public_send(@method, long_path) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/library/socket/shared/recv_nonblock.rb b/spec/ruby/library/socket/shared/recv_nonblock.rb deleted file mode 100644 index 2b584d52a2..0000000000 --- a/spec/ruby/library/socket/shared/recv_nonblock.rb +++ /dev/null @@ -1,52 +0,0 @@ -describe :socket_recv_nonblock, shared: true do - before :each do - @s1 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) - @s2 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) - end - - after :each do - @s1.close unless @s1.closed? - @s2.close unless @s2.closed? - end - - it "raises an exception extending IO::WaitReadable if there's no data available" do - @s1.bind(Socket.pack_sockaddr_in(0, "127.0.0.1")) - lambda { - @s1.recv_nonblock(5) - }.should raise_error(IO::WaitReadable) { |e| - platform_is_not :windows do - e.should be_kind_of(Errno::EAGAIN) - end - platform_is :windows do - e.should be_kind_of(Errno::EWOULDBLOCK) - end - } - end - - it "receives data after it's ready" do - @s1.bind(Socket.pack_sockaddr_in(0, "127.0.0.1")) - @s2.send("aaa", 0, @s1.getsockname) - IO.select([@s1], nil, nil, 2) - @s1.recv_nonblock(5).should == "aaa" - end - - it "allows an output buffer as third argument" do - @s1.bind(Socket.pack_sockaddr_in(0, "127.0.0.1")) - @s2.send("data", 0, @s1.getsockname) - IO.select([@s1], nil, nil, 2) - - buf = "foo" - @s1.recv_nonblock(5, 0, buf) - buf.should == "data" - end - - it "does not block if there's no data available" do - @s1.bind(Socket.pack_sockaddr_in(0, "127.0.0.1")) - @s2.send("a", 0, @s1.getsockname) - IO.select([@s1], nil, nil, 2) - @s1.recv_nonblock(1).should == "a" - lambda { - @s1.recv_nonblock(5) - }.should raise_error(IO::WaitReadable) - end -end diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb index 03ee0e1a52..1e08deccc2 100644 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ b/spec/ruby/library/socket/shared/socketpair.rb @@ -19,5 +19,120 @@ describe :socket_socketpair, shared: true do s2.close end end + + describe 'using a Fixnum 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 a Fixnum 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 |