PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/MacRuby/MacRuby
Ruby | 89 lines | 70 code | 12 blank | 7 comment | 19 complexity | 390b212ce750e8a98ac20d26db16de26 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. 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', SocketSpecs.port)
  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, SocketSpecs.port)
  19. addr = @server.addr
  20. if addr[0] == 'AF_INET'
  21. addr[1].should == SocketSpecs.port
  22. addr[2].should =~ /^#{SocketSpecs.hostname}\b/
  23. addr[3].should == '127.0.0.1'
  24. else
  25. addr[1].should == SocketSpecs.port
  26. addr[2].should =~ /^#{SocketSpecs.hostnamev6}\b/
  27. addr[3].should == '::1'
  28. end
  29. end
  30. it "binds to INADDR_ANY if the hostname is empty" do
  31. @server = TCPServer.new('', SocketSpecs.port)
  32. addr = @server.addr
  33. addr[0].should == 'AF_INET'
  34. addr[1].should == SocketSpecs.port
  35. expected = ['', '0.0.0.0']
  36. expected.should include(addr[2])
  37. addr[3].should == '0.0.0.0'
  38. end
  39. it "binds to INADDR_ANY if the hostname is empty and the port is a string" do
  40. @server = TCPServer.new('', SocketSpecs.port.to_s)
  41. addr = @server.addr
  42. addr[0].should == 'AF_INET'
  43. addr[1].should == SocketSpecs.port
  44. expected = ['', '0.0.0.0']
  45. expected.should include(addr[2])
  46. addr[3].should == '0.0.0.0'
  47. end
  48. it "coerces port to string, then determines port from that number or service name" do
  49. t = Object.new
  50. lambda { TCPServer.new(SocketSpecs.hostname, t) }.should raise_error(TypeError)
  51. def t.to_str; SocketSpecs.port.to_s; end
  52. @server = TCPServer.new(SocketSpecs.hostname, t)
  53. addr = @server.addr
  54. addr[1].should == SocketSpecs.port
  55. # TODO: This should also accept strings like 'https', but I don't know how to
  56. # pick such a service port that will be able to reliably bind...
  57. end
  58. it "raises Errno::EADDRNOTAVAIL when the adress is unknown" do
  59. lambda { TCPServer.new("1.2.3.4", 4000) }.should raise_error(Errno::EADDRNOTAVAIL)
  60. end
  61. # There is no way to make this fail-proof on all machines, because
  62. # DNS servers like opendns return A records for ANY host, including
  63. # traditionally invalidly named ones.
  64. quarantine! do
  65. it "raises a SocketError when the host is unknown" do
  66. lambda {
  67. TCPServer.new("--notavalidname", 4000)
  68. }.should raise_error(SocketError)
  69. end
  70. end
  71. it "raises Errno::EADDRINUSE when address is already in use" do
  72. lambda {
  73. @server = TCPServer.new('127.0.0.1', SocketSpecs.port)
  74. @server = TCPServer.new('127.0.0.1', SocketSpecs.port)
  75. }.should raise_error(Errno::EADDRINUSE)
  76. end
  77. end