PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/frozen/library/socket/socket/getaddrinfo_spec.rb

http://github.com/MacRuby/MacRuby
Ruby | 121 lines | 92 code | 19 blank | 10 comment | 7 complexity | d8cf341b42db0055637e4f8205c959ee MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. require File.expand_path('../../../../spec_helper', __FILE__)
  2. require File.expand_path('../../fixtures/classes', __FILE__)
  3. require 'socket'
  4. describe "Socket#getaddrinfo" do
  5. before :all do
  6. @do_not_reverse_lookup = BasicSocket.do_not_reverse_lookup
  7. end
  8. before :each do
  9. BasicSocket.do_not_reverse_lookup = true
  10. end
  11. after :each do
  12. BasicSocket.do_not_reverse_lookup = false
  13. end
  14. after :all do
  15. BasicSocket.do_not_reverse_lookup = @do_not_reverse_lookup
  16. end
  17. it "gets the address information" do
  18. expected = []
  19. # The check for AP_INET6's class is needed because ipaddr.rb adds
  20. # fake AP_INET6 even in case when IPv6 is not really supported.
  21. # Without such check, this test might fail when ipaddr was required
  22. # by some other specs.
  23. if (Socket.constants.include? 'AF_INET6') &&
  24. (Socket::AF_INET6.class != Object) then
  25. expected.concat [
  26. ['AF_INET6', 80, SocketSpecs.hostname, '::1', Socket::AF_INET6,
  27. Socket::SOCK_DGRAM, Socket::IPPROTO_UDP],
  28. ['AF_INET6', 80, SocketSpecs.hostname, '::1', Socket::AF_INET6,
  29. Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  30. ['AF_INET6', 80, SocketSpecs.hostname, 'fe80::1%lo0', Socket::AF_INET6,
  31. Socket::SOCK_DGRAM, Socket::IPPROTO_UDP],
  32. ['AF_INET6', 80, SocketSpecs.hostname, 'fe80::1%lo0', Socket::AF_INET6,
  33. Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  34. ]
  35. end
  36. expected.concat [
  37. ['AF_INET', 80, SocketSpecs.hostname, '127.0.0.1', Socket::AF_INET,
  38. Socket::SOCK_DGRAM, Socket::IPPROTO_UDP],
  39. ['AF_INET', 80, SocketSpecs.hostname, '127.0.0.1', Socket::AF_INET,
  40. Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  41. ]
  42. addrinfo = Socket.getaddrinfo SocketSpecs.hostname, 'http'
  43. addrinfo.each do |a|
  44. case a.last
  45. when Socket::IPPROTO_UDP, Socket::IPPROTO_TCP
  46. expected.should include(a)
  47. else
  48. # don't check this. It's some weird protocol we don't know about
  49. # so we can't spec it.
  50. end
  51. end
  52. end
  53. # #getaddrinfo will return a INADDR_ANY address (0.0.0.0
  54. # or "::") if it's a passive socket. In the case of non-passive
  55. # sockets (AI_PASSIVE not set) it should return the loopback
  56. # address (127.0.0.1 or "::1".
  57. it "accepts empty addresses for IPv4 passive sockets" do
  58. res = Socket::getaddrinfo(nil, "http",
  59. Socket::AF_INET,
  60. Socket::SOCK_STREAM,
  61. Socket::IPPROTO_TCP,
  62. Socket::AI_PASSIVE)
  63. expected = [["AF_INET", 80, "0.0.0.0", "0.0.0.0", Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  64. ["AF_INET", 80, "", "0.0.0.0", Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]]
  65. res.each do |a|
  66. expected.should include(a)
  67. end
  68. end
  69. it "accepts empty addresses for IPv4 non-passive sockets" do
  70. res = Socket::getaddrinfo(nil, "http",
  71. Socket::AF_INET,
  72. Socket::SOCK_STREAM,
  73. Socket::IPPROTO_TCP,
  74. 0)
  75. expected = [["AF_INET", 80, "127.0.0.1", "127.0.0.1", Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]]
  76. res.should == expected
  77. end
  78. it "accepts empty addresses for IPv6 passive sockets" do
  79. res = Socket::getaddrinfo(nil, "http",
  80. Socket::AF_INET6,
  81. Socket::SOCK_STREAM,
  82. Socket::IPPROTO_TCP,
  83. Socket::AI_PASSIVE)
  84. expected = [
  85. ["AF_INET6", 80, "::", "::", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  86. ["AF_INET6", 80, "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]
  87. ]
  88. res.each { |a| expected.should include (a) }
  89. end
  90. it "accepts empty addresses for IPv6 non-passive sockets" do
  91. res = Socket::getaddrinfo(nil, "http",
  92. Socket::AF_INET6,
  93. Socket::SOCK_STREAM,
  94. Socket::IPPROTO_TCP,
  95. 0)
  96. expected = [
  97. ["AF_INET6", 80, "::1", "::1", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP],
  98. ["AF_INET6", 80, "0:0:0:0:0:0:0:1", "0:0:0:0:0:0:0:1", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]
  99. ]
  100. res.each { |a| expected.should include (a) }
  101. end
  102. end