PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/ruby/library/ipaddr/new_spec.rb

http://github.com/jruby/jruby
Ruby | 93 lines | 82 code | 11 blank | 0 comment | 33 complexity | 6ae7aa74cda9695d380a4100e74c65d1 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. require File.expand_path('../../../spec_helper', __FILE__)
  2. require 'ipaddr'
  3. describe "IPAddr#new" do
  4. it "initializes IPAddr" do
  5. lambda{ IPAddr.new("3FFE:505:ffff::/48") }.should_not raise_error
  6. lambda{ IPAddr.new("0:0:0:1::") }.should_not raise_error
  7. lambda{ IPAddr.new("2001:200:300::/48") }.should_not raise_error
  8. end
  9. it "initializes IPAddr ipv6 address with short notation" do
  10. a = IPAddr.new
  11. a.to_s.should == "::"
  12. a.to_string.should == "0000:0000:0000:0000:0000:0000:0000:0000"
  13. a.family.should == Socket::AF_INET6
  14. end
  15. it "initializes IPAddr ipv6 address with long notation" do
  16. a = IPAddr.new("0123:4567:89ab:cdef:0ABC:DEF0:1234:5678")
  17. a.to_s.should == "123:4567:89ab:cdef:abc:def0:1234:5678"
  18. a.to_string.should == "0123:4567:89ab:cdef:0abc:def0:1234:5678"
  19. a.family.should == Socket::AF_INET6
  20. end
  21. it "initializes IPAddr ipv6 address with / subnet notation" do
  22. a = IPAddr.new("3ffe:505:2::/48")
  23. a.to_s.should == "3ffe:505:2::"
  24. a.to_string.should == "3ffe:0505:0002:0000:0000:0000:0000:0000"
  25. a.family.should == Socket::AF_INET6
  26. a.ipv4?.should == false
  27. a.ipv6?.should == true
  28. a.inspect.should == "#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>"
  29. end
  30. it "initializes IPAddr ipv6 address with mask subnet notation" do
  31. a = IPAddr.new("3ffe:505:2::/ffff:ffff:ffff::")
  32. a.to_s.should == "3ffe:505:2::"
  33. a.to_string.should == "3ffe:0505:0002:0000:0000:0000:0000:0000"
  34. a.family.should == Socket::AF_INET6
  35. end
  36. it "initializes IPAddr ipv4 address with all zeroes" do
  37. a = IPAddr.new("0.0.0.0")
  38. a.to_s.should == "0.0.0.0"
  39. a.to_string.should == "0.0.0.0"
  40. a.family.should == Socket::AF_INET
  41. end
  42. it "initializes IPAddr ipv4 address" do
  43. a = IPAddr.new("192.168.1.2")
  44. a.to_s.should == "192.168.1.2"
  45. a.to_string.should == "192.168.1.2"
  46. a.family.should == Socket::AF_INET
  47. a.ipv4?.should == true
  48. a.ipv6?.should == false
  49. end
  50. it "initializes IPAddr ipv4 address with / subnet notation" do
  51. a = IPAddr.new("192.168.1.2/24")
  52. a.to_s.should == "192.168.1.0"
  53. a.to_string.should == "192.168.1.0"
  54. a.family.should == Socket::AF_INET
  55. a.inspect.should == "#<IPAddr: IPv4:192.168.1.0/255.255.255.0>"
  56. end
  57. it "initializes IPAddr ipv4 address with subnet mask" do
  58. a = IPAddr.new("192.168.1.2/255.255.255.0")
  59. a.to_s.should == "192.168.1.0"
  60. a.to_string.should == "192.168.1.0"
  61. a.family.should == Socket::AF_INET
  62. end
  63. it "initializes IPAddr ipv4 mapped address with subnet mask" do
  64. a = IPAddr.new("::1:192.168.1.2/120")
  65. a.to_s.should == "::1:c0a8:100"
  66. a.to_string.should == "0000:0000:0000:0000:0000:0001:c0a8:0100"
  67. a.family.should == Socket::AF_INET6
  68. end
  69. it "raises on incorrect IPAddr strings" do
  70. [
  71. ["fe80::1%fxp0"],
  72. ["::1/255.255.255.0"],
  73. [IPAddr.new("::1").to_i],
  74. ["::ffff:192.168.1.2/120", Socket::AF_INET],
  75. ["[192.168.1.2]/120"],
  76. ].each { |args|
  77. lambda{
  78. IPAddr.new(*args)
  79. }.should raise_error(ArgumentError)
  80. }
  81. end
  82. end