PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/nobu/ruby
Ruby | 110 lines | 98 code | 12 blank | 0 comment | 29 complexity | 5453b1158648af9ece13e9a2b4f9efe0 MD5 | raw file
  1. require_relative '../../spec_helper'
  2. require 'ipaddr'
  3. describe "IPAddr#new" do
  4. it "initializes IPAddr" do
  5. ->{ IPAddr.new("3FFE:505:ffff::/48") }.should_not raise_error
  6. ->{ IPAddr.new("0:0:0:1::") }.should_not raise_error
  7. ->{ 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.should_not.ipv4?
  27. a.should.ipv6?
  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.should.ipv4?
  48. a.should_not.ipv6?
  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. ruby_version_is ""..."3.1" do
  70. it "raises on incorrect IPAddr strings" do
  71. [
  72. ["fe80::1%fxp0"],
  73. ["::1/255.255.255.0"],
  74. [IPAddr.new("::1").to_i],
  75. ["::ffff:192.168.1.2/120", Socket::AF_INET],
  76. ["[192.168.1.2]/120"],
  77. ].each { |args|
  78. ->{
  79. IPAddr.new(*args)
  80. }.should raise_error(ArgumentError)
  81. }
  82. end
  83. end
  84. ruby_version_is "3.1" do
  85. it "raises on incorrect IPAddr strings" do
  86. [
  87. ["::1/255.255.255.0"],
  88. [IPAddr.new("::1").to_i],
  89. ["::ffff:192.168.1.2/120", Socket::AF_INET],
  90. ["[192.168.1.2]/120"],
  91. ].each { |args|
  92. ->{
  93. IPAddr.new(*args)
  94. }.should raise_error(ArgumentError)
  95. }
  96. end
  97. end
  98. end