2015-11-14 08:15:33 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-02-10 07:09:57 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'socket'
|
|
|
|
|
2009-02-10 23:17:57 -05:00
|
|
|
class TestSocketOption < Test::Unit::TestCase
|
2009-02-10 23:31:19 -05:00
|
|
|
def test_new
|
|
|
|
data = [1].pack("i")
|
|
|
|
sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, data)
|
|
|
|
assert_equal(Socket::AF_INET, sockopt.family)
|
|
|
|
assert_equal(Socket::SOL_SOCKET, sockopt.level)
|
|
|
|
assert_equal(Socket::SO_KEEPALIVE, sockopt.optname)
|
|
|
|
assert_equal(Socket::SO_KEEPALIVE, sockopt.optname)
|
|
|
|
assert_equal(data, sockopt.data)
|
|
|
|
end
|
|
|
|
|
2009-02-10 07:09:57 -05:00
|
|
|
def test_bool
|
|
|
|
opt = Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
|
|
|
|
assert_equal(1, opt.int)
|
|
|
|
opt = Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
|
|
|
|
assert_equal(0, opt.int)
|
|
|
|
opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 0)
|
|
|
|
assert_equal(false, opt.bool)
|
|
|
|
opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
|
|
|
|
assert_equal(true, opt.bool)
|
|
|
|
opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 2)
|
|
|
|
assert_equal(true, opt.bool)
|
2016-01-17 01:31:36 -05:00
|
|
|
Socket.open(:INET, :STREAM) {|s|
|
|
|
|
s.setsockopt(Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true))
|
|
|
|
assert_equal(true, s.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool)
|
|
|
|
s.setsockopt(Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false))
|
|
|
|
assert_equal(false, s.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool)
|
|
|
|
}
|
2009-02-10 07:09:57 -05:00
|
|
|
end
|
2009-02-10 23:31:19 -05:00
|
|
|
|
2013-06-02 12:37:14 -04:00
|
|
|
def test_ipv4_multicast_loop
|
2013-06-02 11:59:18 -04:00
|
|
|
sockopt = Socket::Option.ipv4_multicast_loop(128)
|
2013-05-31 13:37:55 -04:00
|
|
|
assert_equal('#<Socket::Option: INET IP MULTICAST_LOOP 128>', sockopt.inspect)
|
|
|
|
assert_equal(Socket::AF_INET, sockopt.family)
|
|
|
|
assert_equal(Socket::IPPROTO_IP, sockopt.level)
|
|
|
|
assert_equal(Socket::IP_MULTICAST_LOOP, sockopt.optname)
|
2013-06-02 11:59:18 -04:00
|
|
|
assert_equal(128, sockopt.ipv4_multicast_loop)
|
2013-05-31 13:37:55 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 14:32:03 -05:00
|
|
|
def test_ipv4_multicast_loop_size
|
|
|
|
expected_size = Socket.open(:INET, :DGRAM) {|s|
|
|
|
|
s.getsockopt(:IP, :MULTICAST_LOOP).to_s.bytesize
|
|
|
|
}
|
|
|
|
assert_equal(expected_size, Socket::Option.ipv4_multicast_loop(0).to_s.bytesize)
|
|
|
|
end
|
|
|
|
|
2013-06-02 12:37:14 -04:00
|
|
|
def test_ipv4_multicast_ttl
|
2013-06-02 11:59:18 -04:00
|
|
|
sockopt = Socket::Option.ipv4_multicast_ttl(128)
|
2013-05-31 13:37:55 -04:00
|
|
|
assert_equal('#<Socket::Option: INET IP MULTICAST_TTL 128>', sockopt.inspect)
|
|
|
|
assert_equal(Socket::AF_INET, sockopt.family)
|
|
|
|
assert_equal(Socket::IPPROTO_IP, sockopt.level)
|
|
|
|
assert_equal(Socket::IP_MULTICAST_TTL, sockopt.optname)
|
2013-06-02 11:59:18 -04:00
|
|
|
assert_equal(128, sockopt.ipv4_multicast_ttl)
|
2013-05-31 13:37:55 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 10:51:33 -05:00
|
|
|
def test_ipv4_multicast_ttl_size
|
|
|
|
expected_size = Socket.open(:INET, :DGRAM) {|s|
|
|
|
|
s.getsockopt(:IP, :MULTICAST_TTL).to_s.bytesize
|
|
|
|
}
|
|
|
|
assert_equal(expected_size, Socket::Option.ipv4_multicast_ttl(0).to_s.bytesize)
|
|
|
|
end
|
|
|
|
|
2009-02-10 23:31:19 -05:00
|
|
|
def test_unpack
|
|
|
|
sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
|
|
|
|
assert_equal([1], sockopt.unpack("i"))
|
|
|
|
assert_equal([1], sockopt.data.unpack("i"))
|
|
|
|
end
|
2009-02-10 07:09:57 -05:00
|
|
|
end
|