PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/ruby/library/socket/tcpserver/new_spec.rb

https://github.com/eregon/ruby
Ruby | 96 lines | 74 code | 13 blank | 9 comment | 18 complexity | e0f8b2269b58e0433c95f935e67de41b MD5 | raw file
  1. require File.expand_path('../../../../spec_helper', __FILE__)
  2. require File.expand_path('../../fixtures/classes', __FILE__)
  3. describe "TCPServer.new" do
  4. after :each do
  5. @server.close if @server && !@server.closed?
  6. end
  7. it "binds to a host and a port" do
  8. @server = TCPServer.new('127.0.0.1', 0)
  9. addr = @server.addr
  10. addr[0].should == 'AF_INET'
  11. addr[1].should be_kind_of(Fixnum)
  12. # on some platforms (Mac), MRI
  13. # returns comma at the end.
  14. addr[2].should =~ /^#{SocketSpecs.hostname}\b/
  15. addr[3].should == '127.0.0.1'
  16. end
  17. it "binds to localhost and a port with either IPv4 or IPv6" do
  18. @server = TCPServer.new(SocketSpecs.hostname, 0)
  19. addr = @server.addr
  20. addr[1].should be_kind_of(Fixnum)
  21. if addr[0] == 'AF_INET'
  22. addr[2].should =~ /^#{SocketSpecs.hostname}\b/
  23. addr[3].should == '127.0.0.1'
  24. else
  25. addr[2].should =~ /^#{SocketSpecs.hostnamev6}\b/
  26. addr[3].should == '::1'
  27. end
  28. end
  29. it "binds to INADDR_ANY if the hostname is empty" do
  30. @server = TCPServer.new('', 0)
  31. addr = @server.addr
  32. addr[0].should == 'AF_INET'
  33. addr[1].should be_kind_of(Fixnum)
  34. addr[2].should == '0.0.0.0'
  35. addr[3].should == '0.0.0.0'
  36. end
  37. it "binds to INADDR_ANY if the hostname is empty and the port is a string" do
  38. @server = TCPServer.new('', 0)
  39. addr = @server.addr
  40. addr[0].should == 'AF_INET'
  41. addr[1].should be_kind_of(Fixnum)
  42. addr[2].should == '0.0.0.0'
  43. addr[3].should == '0.0.0.0'
  44. end
  45. it "coerces port to string, then determines port from that number or service name" do
  46. lambda { TCPServer.new(SocketSpecs.hostname, Object.new) }.should raise_error(TypeError)
  47. port = Object.new
  48. port.should_receive(:to_str).and_return("0")
  49. @server = TCPServer.new(SocketSpecs.hostname, port)
  50. addr = @server.addr
  51. addr[1].should be_kind_of(Fixnum)
  52. # TODO: This should also accept strings like 'https', but I don't know how to
  53. # pick such a service port that will be able to reliably bind...
  54. end
  55. it "raises Errno::EADDRNOTAVAIL when the address is unknown" do
  56. lambda { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL)
  57. end
  58. # There is no way to make this fail-proof on all machines, because
  59. # DNS servers like opendns return A records for ANY host, including
  60. # traditionally invalidly named ones.
  61. quarantine! do
  62. it "raises a SocketError when the host is unknown" do
  63. lambda {
  64. TCPServer.new("--notavalidname", 0)
  65. }.should raise_error(SocketError)
  66. end
  67. end
  68. it "raises Errno::EADDRINUSE when address is already in use" do
  69. @server = TCPServer.new('127.0.0.1', 0)
  70. lambda {
  71. @server = TCPServer.new('127.0.0.1', @server.addr[1])
  72. }.should raise_error(Errno::EADDRINUSE)
  73. end
  74. platform_is_not :windows, :aix do
  75. # A known bug in AIX. getsockopt(2) does not properly set
  76. # the fifth argument for SO_REUSEADDR.
  77. it "sets SO_REUSEADDR on the resulting server" do
  78. @server = TCPServer.new('127.0.0.1', 0)
  79. @server.getsockopt(:SOCKET, :REUSEADDR).data.should_not == "\x00\x00\x00\x00"
  80. @server.getsockopt(:SOCKET, :REUSEADDR).int.should_not == 0
  81. end
  82. end
  83. end