PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/mspec/rubyspec/library/ipaddr/new_spec.rb

http://github.com/IronLanguages/main
Ruby | 87 lines | 77 code | 10 blank | 0 comment | 30 complexity | 10eeb38f6bd8964ac917f54ba85b0d5f MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require File.dirname(__FILE__) + '/../../spec_helper'
  2. require 'ipaddr'
  3. describe "IPAddr#new" do
  4. it 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize 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 'it should initialize IPAddr ipv4 address wuth 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 'it should raise errors on incorrect IPAddr strings' do
  64. [
  65. ["fe80::1%fxp0"],
  66. ["::1/255.255.255.0"],
  67. ["::1:192.168.1.2/120"],
  68. [IPAddr.new("::1").to_i],
  69. ["::ffff:192.168.1.2/120", Socket::AF_INET],
  70. ["[192.168.1.2]/120"],
  71. ].each { |args|
  72. lambda{
  73. IPAddr.new(*args)
  74. }.should raise_error(ArgumentError)
  75. }
  76. end
  77. end