PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/jruby/test_addrinfo.rb

http://github.com/jruby/jruby
Ruby | 216 lines | 154 code | 24 blank | 38 comment | 21 complexity | 24482cc793404330eef718b662fdb8b6 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. require 'test/unit'
  2. require 'socket'
  3. require 'thread'
  4. require 'test/jruby/test_helper'
  5. require 'ipaddr'
  6. WINDOWS = RbConfig::CONFIG['host_os'] =~ /Windows|mswin/
  7. class AddrinfoTest < Test::Unit::TestCase
  8. include TestHelper
  9. # these assertions emulate Addrinfo behavior in Ruby MRI 2.2.0
  10. # get system generated addresses to test with from interfaces
  11. # TODO: (gf) mock Addrinfo creation for platform specific behaviors ?
  12. def getaddrs
  13. begin
  14. @addrs ||= Socket.getifaddrs.collect { |i| i.addr }
  15. rescue NotImplementedError
  16. @addrs = []
  17. end
  18. end
  19. def test_link_afamily_pfamily
  20. # at least one address (loopback link interface) is AF/PF_UNSPEC
  21. link_addrs = getaddrs.select { |addr| addr.afamily == Socket::AF_UNSPEC && addr.pfamily == Socket::PF_UNSPEC }
  22. assert( link_addrs.count > 0 ) if getaddrs.count > 0
  23. end
  24. def test_afamily
  25. getaddrs.each do |addr|
  26. assert( [ Socket::AF_INET, Socket::AF_INET6, Socket::AF_UNSPEC ].include? addr.afamily )
  27. end
  28. end
  29. def test_ip?
  30. getaddrs.each do |addr|
  31. case addr.afamily
  32. when Socket::AF_INET then assert_equal(true, addr.ip?)
  33. when Socket::AF_INET6 then assert_equal(true, addr.ip?)
  34. when Socket::AF_UNSPEC then assert_equal(false, addr.ip?)
  35. else assert_equal(false, addr.ip?)
  36. end
  37. end
  38. end
  39. def test_ip_address
  40. getaddrs.each do |addr|
  41. if addr.ip?
  42. assert_instance_of(String, addr.ip_address)
  43. else
  44. assert_raise(SocketError) { addr.ip_address }
  45. end
  46. end
  47. end
  48. def test_ip_port
  49. getaddrs.each do |addr|
  50. if addr.ip?
  51. assert_equal(0, addr.ip_port)
  52. else
  53. assert_raise(SocketError) { addr.ip_port }
  54. end
  55. end
  56. end
  57. def test_ip_unpack
  58. getaddrs.each do |addr|
  59. if addr.ip?
  60. unpacked = addr.ip_unpack
  61. assert_instance_of(Array, unpacked)
  62. assert_equal(2, unpacked.count)
  63. assert_equal(addr.ip_address, unpacked[0])
  64. assert_equal(0, unpacked[1])
  65. else
  66. assert_raise(SocketError) { addr.ip_port }
  67. end
  68. end
  69. end
  70. def test_ipv4?
  71. getaddrs.each do |addr|
  72. if addr.afamily == Socket::AF_INET
  73. assert_equal(true, addr.ipv4?)
  74. else
  75. assert_equal(false, addr.ipv4?)
  76. end
  77. end
  78. end
  79. def test_ipv4_loopback?
  80. loopbacks = getaddrs.select do |addr|
  81. if addr.afamily == Socket::AF_INET
  82. addr.ipv4_loopback?
  83. else
  84. assert_equal(false, addr.ipv4_loopback?)
  85. nil
  86. end
  87. end
  88. assert(loopbacks.count > 0) # at least one ipv4 loopback
  89. end
  90. def test_ipv6?
  91. getaddrs.each do |addr|
  92. if addr.afamily == Socket::AF_INET6
  93. assert_equal(true, addr.ipv6?)
  94. else
  95. assert_equal(false, addr.ipv6?)
  96. end
  97. end
  98. end
  99. def test_ipv6_loopback?
  100. loopbacks = getaddrs.select do |addr|
  101. if addr.afamily == Socket::AF_INET6
  102. addr.ipv6_loopback?
  103. else
  104. assert_equal(false, addr.ipv6_loopback?)
  105. nil
  106. end
  107. end
  108. assert_equal(1, loopbacks.count) # only one ipv6 loopback
  109. end
  110. def test_pfamily
  111. getaddrs.each do |addr|
  112. assert( [ Socket::PF_INET, Socket::PF_INET6, Socket::PF_UNSPEC ].include? addr.pfamily )
  113. end
  114. end
  115. def test_protocol
  116. getaddrs.each do |addr|
  117. assert_equal(Socket::IPPROTO_IP, addr.protocol) # all interfaces address are IP protocol ?
  118. end
  119. end
  120. def test_socktype
  121. getaddrs.each do |addr|
  122. assert_equal(0, addr.socktype) # all interfaces address are socktype 0 ?
  123. end
  124. end
  125. def test_to_sockaddr
  126. getaddrs.each do |addr|
  127. sockaddr = addr.to_sockaddr
  128. assert_instance_of(String, sockaddr)
  129. assert_equal(sockaddr, addr.to_s)
  130. if addr.afamily == Socket::AF_UNSPEC
  131. # better tests here would be platform and interface hardware specific
  132. assert_equal(17, sockaddr.bytes[0])
  133. assert_equal( 0, sockaddr.bytes[1])
  134. else
  135. # TODO: (gf) test ipv6 addresses when Socket.unpack_sockaddr_in works for ipv6 and ipv6
  136. if addr.ipv4?
  137. assert_equal([addr.ip_port,addr.ip_address], Socket.unpack_sockaddr_in(sockaddr))
  138. end
  139. end
  140. end
  141. end
  142. def test_inspect
  143. getaddrs.each do |addr|
  144. if addr.ip?
  145. match = addr.inspect.match(/^#<Addrinfo: (.+)>$/)
  146. assert_equal(addr.ip_address, match[1])
  147. else
  148. match = addr.inspect.match(/^#<Addrinfo: PACKET\[protocol=0 (.+) hatype=(.+) HOST hwaddr=(.+)\]/)
  149. assert_instance_of(MatchData, match)
  150. end
  151. end
  152. end
  153. def test_inspect_sockaddr
  154. getaddrs.each do |addr|
  155. if addr.ip?
  156. assert_equal(addr.ip_address, addr.inspect_sockaddr)
  157. end
  158. end
  159. end
  160. # def test_create_with_interface ; end
  161. # def test_create_with_interface_ipaddress ; end
  162. # def test_create_with_ipaddress ; end
  163. # def test_create_with_ipaddress_port ; end
  164. # def test_create_with_ipaddress_port_socktype ; end
  165. # def test_bind ; end
  166. # def test_canonname ; end
  167. # def test_connect ; end
  168. # def test_connect_from ; end
  169. # def test_connect_to ; end
  170. # def test_family_addrinfo ; end
  171. # def test_getnameinfo ; end
  172. # def test_ipv4_multicast? ; end
  173. # def test_ipv4_private? ; end
  174. # def test_ipv6_linklocal? ; end
  175. # def test_ipv6_mc_global? ; end
  176. # def test_ipv6_mc_linklocal? ; end
  177. # def test_ipv6_mc_nodelocal? ; end
  178. # def test_ipv6_mc_orglocal? ; end
  179. # def test_ipv6_mc_sitelocal? ; end
  180. # def test_ipv6_multicast? ; end
  181. # def test_ipv6_sitelocal? ; end
  182. # def test_ipv6_to_ipv4 ; end
  183. # def test_ipv6_unspecified? ; end
  184. # def test_ipv6_v4compat? ; end
  185. # def test_ipv6_v4mapped? ; end
  186. # def test_listen ; end
  187. # def test_marshal_dump ; end
  188. # def test_marshal_load ; end
  189. # def test_to_str ; end
  190. # def test_unix? ; end
  191. # def test_unix_path ; end
  192. end