PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test_ipv6.rb

http://github.com/eventmachine/eventmachine
Ruby | 107 lines | 80 code | 18 blank | 9 comment | 3 complexity | 25b3c541ebca0d3c658397400857d63b MD5 | raw file
  1. require_relative 'em_test_helper'
  2. class TestIPv6 < Test::Unit::TestCase
  3. if Test::Unit::TestCase.public_ipv6?
  4. # Runs a TCP server in the local IPv6 address, connects to it and sends a specific data.
  5. # Timeout in 2 seconds.
  6. def test_ipv6_tcp_local_server
  7. @@received_data = nil
  8. @local_port = next_port
  9. setup_timeout(2)
  10. EM.run do
  11. EM.start_server(@@public_ipv6, @local_port) do |s|
  12. def s.receive_data data
  13. @@received_data = data
  14. EM.stop
  15. end
  16. end
  17. EM::connect(@@public_ipv6, @local_port) do |c|
  18. def c.unbind(reason)
  19. warn "unbind: #{reason.inspect}" if reason # XXX at least find out why it failed
  20. end
  21. c.send_data "ipv6/tcp"
  22. end
  23. end
  24. assert_equal "ipv6/tcp", @@received_data
  25. end
  26. # Runs a UDP server in the local IPv6 address, connects to it and sends a specific data.
  27. # Timeout in 2 seconds.
  28. def test_ipv6_udp_local_server
  29. @@received_data = nil
  30. @local_port = next_port
  31. @@remote_ip = nil
  32. setup_timeout(2)
  33. EM.run do
  34. EM.open_datagram_socket(@@public_ipv6, @local_port) do |s|
  35. def s.receive_data data
  36. _port, @@remote_ip = Socket.unpack_sockaddr_in(get_peername)
  37. @@received_data = data
  38. EM.stop
  39. end
  40. end
  41. EM.open_datagram_socket(@@public_ipv6, next_port) do |c|
  42. c.send_datagram "ipv6/udp", @@public_ipv6, @local_port
  43. end
  44. end
  45. assert_equal @@remote_ip, @@public_ipv6
  46. assert_equal "ipv6/udp", @@received_data
  47. end
  48. # Try to connect via TCP to an invalid IPv6. EM.connect should raise
  49. # EM::ConnectionError.
  50. def test_tcp_connect_to_invalid_ipv6
  51. invalid_ipv6 = "1:A"
  52. EM.run do
  53. begin
  54. error = nil
  55. EM.connect(invalid_ipv6, 1234)
  56. rescue => e
  57. error = e
  58. ensure
  59. EM.stop
  60. assert_equal EM::ConnectionError, (error && error.class)
  61. end
  62. end
  63. end
  64. # Try to send a UDP datagram to an invalid IPv6. EM.send_datagram should raise
  65. # EM::ConnectionError.
  66. def test_udp_send_datagram_to_invalid_ipv6
  67. invalid_ipv6 = "1:A"
  68. EM.run do
  69. begin
  70. error = nil
  71. EM.open_datagram_socket(@@public_ipv6, next_port) do |c|
  72. c.send_datagram "hello", invalid_ipv6, 1234
  73. end
  74. rescue => e
  75. error = e
  76. ensure
  77. EM.stop
  78. assert_equal EM::ConnectionError, (error && error.class)
  79. end
  80. end
  81. end
  82. else
  83. warn "no IPv6 in this host, skipping tests in #{__FILE__}"
  84. # Because some rubies will complain if a TestCase class has no tests.
  85. def test_ipv6_unavailable
  86. assert true
  87. end
  88. end
  89. end