PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/mspec/rubyspec/library/socket/socket/getaddrinfo_spec.rb

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