2017-05-07 08:04:49 -04:00
|
|
|
# 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
|
2018-08-03 12:19:40 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-08-18 15:37:12 -04:00
|
|
|
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')
|
2018-08-03 12:19:40 -04:00
|
|
|
|
2018-08-18 15:37:12 -04:00
|
|
|
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
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe :socket_pack_sockaddr_un, shared: true do
|
2018-08-03 12:19:40 -04:00
|
|
|
with_feature :unix_socket do
|
2017-05-07 08:04:49 -04:00
|
|
|
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
|
|
|
|
|
2018-08-03 12:19:40 -04:00
|
|
|
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
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
platform_is_not :windows, :aix do
|
2018-08-03 12:19:40 -04:00
|
|
|
it "raises ArgumentError for paths that are too long" do
|
2017-05-07 08:04:49 -04:00
|
|
|
# AIX doesn't raise error
|
2018-08-03 12:19:40 -04:00
|
|
|
long_path = 'a' * 110
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { Socket.public_send(@method, long_path) }.should raise_error(ArgumentError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|