ipaddress 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +73 -0
- data/README.rdoc +120 -86
- data/Rakefile +7 -15
- data/VERSION +1 -1
- data/lib/ipaddress.rb +131 -15
- data/lib/ipaddress/extensions/extensions.rb +8 -2
- data/lib/ipaddress/ipv4.rb +132 -49
- data/lib/ipaddress/ipv6.rb +64 -18
- data/lib/ipaddress/prefix.rb +38 -1
- data/test/ipaddress/ipv4_test.rb +92 -38
- data/test/ipaddress/ipv6_test.rb +34 -10
- data/test/ipaddress/prefix_test.rb +15 -0
- data/test/ipaddress_test.rb +17 -0
- data/test/test_helper.rb +0 -5
- metadata +11 -25
- data/lib/ipaddress/ipbase.rb +0 -83
- data/test/ipaddress/ipbase_test.rb +0 -28
    
        data/lib/ipaddress/ipv6.rb
    CHANGED
    
    | @@ -1,4 +1,3 @@ | |
| 1 | 
            -
            require 'ipaddress/ipbase'
         | 
| 2 1 | 
             
            require 'ipaddress/prefix'
         | 
| 3 2 |  | 
| 4 3 | 
             
            module IPAddress; 
         | 
| @@ -59,7 +58,7 @@ module IPAddress; | |
| 59 58 | 
             
              # portion.
         | 
| 60 59 | 
             
              #
         | 
| 61 60 | 
             
              #
         | 
| 62 | 
            -
              class IPv6  | 
| 61 | 
            +
              class IPv6 
         | 
| 63 62 |  | 
| 64 63 | 
             
                include IPAddress
         | 
| 65 64 | 
             
                include Enumerable  
         | 
| @@ -148,28 +147,29 @@ module IPAddress; | |
| 148 147 | 
             
                # if the object was created using the default prefix
         | 
| 149 148 | 
             
                # of 128 bits.
         | 
| 150 149 | 
             
                #
         | 
| 151 | 
            -
                #    | 
| 152 | 
            -
                #   puts ip
         | 
| 153 | 
            -
                #     #=> 2001:db8::8:800:200c:417a/128
         | 
| 150 | 
            +
                #   ip6 = IPAddress("2001:db8::8:800:200c:417a")
         | 
| 154 151 | 
             
                #
         | 
| 155 | 
            -
                #    | 
| 156 | 
            -
                # | 
| 157 | 
            -
                # | 
| 152 | 
            +
                #   puts ip6.to_string
         | 
| 153 | 
            +
                #     #=> "2001:db8::8:800:200c:417a/128"
         | 
| 154 | 
            +
                #
         | 
| 155 | 
            +
                #   ip6.prefix = 64
         | 
| 156 | 
            +
                #   puts ip6.to_string
         | 
| 157 | 
            +
                #     #=> "2001:db8::8:800:200c:417a/64"
         | 
| 158 158 | 
             
                #
         | 
| 159 159 | 
             
                def prefix=(num)
         | 
| 160 160 | 
             
                  @prefix = Prefix128.new(num)
         | 
| 161 161 | 
             
                end
         | 
| 162 162 |  | 
| 163 163 | 
             
                # 
         | 
| 164 | 
            -
                # Unlike its counterpart IPv6# | 
| 164 | 
            +
                # Unlike its counterpart IPv6#to_string method, IPv6#to_string_uncompressed 
         | 
| 165 165 | 
             
                # returns the whole IPv6 address and prefix in an uncompressed form
         | 
| 166 166 | 
             
                #
         | 
| 167 167 | 
             
                #   ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
         | 
| 168 168 | 
             
                #
         | 
| 169 | 
            -
                #   ip6. | 
| 169 | 
            +
                #   ip6.to_string_uncompressed
         | 
| 170 170 | 
             
                #     #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"
         | 
| 171 171 | 
             
                #
         | 
| 172 | 
            -
                def  | 
| 172 | 
            +
                def to_string_uncompressed
         | 
| 173 173 | 
             
                  "#@address/#@prefix"
         | 
| 174 174 | 
             
                end
         | 
| 175 175 |  | 
| @@ -177,13 +177,26 @@ module IPAddress; | |
| 177 177 | 
             
                # Returns the IPv6 address in a human readable form,
         | 
| 178 178 | 
             
                # using the compressed address.
         | 
| 179 179 | 
             
                #
         | 
| 180 | 
            -
                #   ip6 = IPAddress "2001: | 
| 180 | 
            +
                #   ip6 = IPAddress "2001:0db8:0000:0000:0008:0800:200c:417a/64"
         | 
| 181 181 | 
             
                #
         | 
| 182 182 | 
             
                #   ip6.to_string
         | 
| 183 183 | 
             
                #     #=> "2001:db8::8:800:200c:417a/64"
         | 
| 184 184 | 
             
                #
         | 
| 185 | 
            +
                def to_string
         | 
| 186 | 
            +
                  "#@compressed/#@prefix"
         | 
| 187 | 
            +
                end
         | 
| 188 | 
            +
             | 
| 189 | 
            +
                #
         | 
| 190 | 
            +
                # Returns the IPv6 address in a human readable form,
         | 
| 191 | 
            +
                # using the compressed address.
         | 
| 192 | 
            +
                #
         | 
| 193 | 
            +
                #   ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
         | 
| 194 | 
            +
                #
         | 
| 195 | 
            +
                #   ip6.to_s
         | 
| 196 | 
            +
                #     #=> "2001:db8::8:800:200c:417a"
         | 
| 197 | 
            +
                #
         | 
| 185 198 | 
             
                def to_s
         | 
| 186 | 
            -
                   | 
| 199 | 
            +
                  @compressed
         | 
| 187 200 | 
             
                end
         | 
| 188 201 |  | 
| 189 202 | 
             
                #
         | 
| @@ -204,10 +217,12 @@ module IPAddress; | |
| 204 217 | 
             
                # True if the IPv6 address is a network
         | 
| 205 218 | 
             
                #
         | 
| 206 219 | 
             
                #   ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
         | 
| 220 | 
            +
                #
         | 
| 207 221 | 
             
                #   ip6.network?
         | 
| 208 222 | 
             
                #     #=> false
         | 
| 209 223 | 
             
                #
         | 
| 210 224 | 
             
                #   ip6 = IPAddress "2001:db8:8:800::/64"
         | 
| 225 | 
            +
                #
         | 
| 211 226 | 
             
                #   ip6.network?
         | 
| 212 227 | 
             
                #     #=> true
         | 
| 213 228 | 
             
                #
         | 
| @@ -219,6 +234,7 @@ module IPAddress; | |
| 219 234 | 
             
                # Returns the 16-bits value specified by index
         | 
| 220 235 | 
             
                #
         | 
| 221 236 | 
             
                #   ip = IPAddress("2001:db8::8:800:200c:417a/64")
         | 
| 237 | 
            +
                #
         | 
| 222 238 | 
             
                #   ip[0]
         | 
| 223 239 | 
             
                #     #=> 8193
         | 
| 224 240 | 
             
                #   ip[1]
         | 
| @@ -250,6 +266,7 @@ module IPAddress; | |
| 250 266 | 
             
                # in a network byte order format.
         | 
| 251 267 | 
             
                #
         | 
| 252 268 | 
             
                #   ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
         | 
| 269 | 
            +
                #
         | 
| 253 270 | 
             
                #   ip6.data
         | 
| 254 271 | 
             
                #     #=> " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
         | 
| 255 272 | 
             
                #
         | 
| @@ -282,6 +299,21 @@ module IPAddress; | |
| 282 299 | 
             
                  @address.split(":")
         | 
| 283 300 | 
             
                end
         | 
| 284 301 |  | 
| 302 | 
            +
                #
         | 
| 303 | 
            +
                # Returns the IPv6 address in a DNS reverse lookup
         | 
| 304 | 
            +
                # string, as per RFC3172 and RFC2874.
         | 
| 305 | 
            +
                #   
         | 
| 306 | 
            +
                #   ip6 = IPAddress "3ffe:505:2::f")
         | 
| 307 | 
            +
                #   
         | 
| 308 | 
            +
                #   ip6.reverse
         | 
| 309 | 
            +
                #     #=> "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa"
         | 
| 310 | 
            +
                #
         | 
| 311 | 
            +
                def reverse
         | 
| 312 | 
            +
                  to_hex.reverse.gsub(/./){|c| c+"."} + "ip6.arpa"
         | 
| 313 | 
            +
                end
         | 
| 314 | 
            +
                alias_method :arpa, :reverse
         | 
| 315 | 
            +
                
         | 
| 316 | 
            +
             | 
| 285 317 | 
             
                #
         | 
| 286 318 | 
             
                # Compressed form of the IPv6 address
         | 
| 287 319 | 
             
                #
         | 
| @@ -325,10 +357,10 @@ module IPAddress; | |
| 325 357 | 
             
                # Returns the address portion of an IP in binary format,
         | 
| 326 358 | 
             
                # as a string containing a sequence of 0 and 1
         | 
| 327 359 | 
             
                #
         | 
| 328 | 
            -
                #    | 
| 360 | 
            +
                #   ip6 = IPAddress("2001:db8::8:800:200c:417a")
         | 
| 329 361 | 
             
                #
         | 
| 330 | 
            -
                #    | 
| 331 | 
            -
                #     #=> " | 
| 362 | 
            +
                #   ip6.bits 
         | 
| 363 | 
            +
                #     #=> "0010000000000001000011011011100000 [...] "
         | 
| 332 364 | 
             
                #
         | 
| 333 365 | 
             
                def bits
         | 
| 334 366 | 
             
                  data.unpack("B*").first
         | 
| @@ -392,7 +424,7 @@ module IPAddress; | |
| 392 424 | 
             
                #
         | 
| 393 425 | 
             
                # With that data you can create a new IPv6 object:
         | 
| 394 426 | 
             
                #
         | 
| 395 | 
            -
                #   ip6 = IPAddress:: | 
| 427 | 
            +
                #   ip6 = IPAddress::IPv6::parse_data " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
         | 
| 396 428 | 
             
                #   ip6.prefix = 64
         | 
| 397 429 | 
             
                #
         | 
| 398 430 | 
             
                #   ip6.to_s
         | 
| @@ -660,9 +692,23 @@ module IPAddress; | |
| 660 692 | 
             
                #   ip6 = IPAddress "::ffff:172.16.10.1/128"
         | 
| 661 693 | 
             
                #
         | 
| 662 694 | 
             
                #   ip6.to_s
         | 
| 663 | 
            -
                #     #=> "::FFFF:172.16.10.1 | 
| 695 | 
            +
                #     #=> "::FFFF:172.16.10.1"
         | 
| 664 696 | 
             
                #
         | 
| 665 697 | 
             
                def to_s
         | 
| 698 | 
            +
                  "::ffff:#{@ipv4.address}"
         | 
| 699 | 
            +
                end
         | 
| 700 | 
            +
             | 
| 701 | 
            +
                # 
         | 
| 702 | 
            +
                # Similar to IPv6#to_string, but prints out the IPv4 address 
         | 
| 703 | 
            +
                # in dotted decimal format
         | 
| 704 | 
            +
                #
         | 
| 705 | 
            +
                #
         | 
| 706 | 
            +
                #   ip6 = IPAddress "::ffff:172.16.10.1/128"
         | 
| 707 | 
            +
                #
         | 
| 708 | 
            +
                #   ip6.to_s
         | 
| 709 | 
            +
                #     #=> "::FFFF:172.16.10.1/128"
         | 
| 710 | 
            +
                #
         | 
| 711 | 
            +
                def to_string
         | 
| 666 712 | 
             
                  "::ffff:#{@ipv4.address}/#@prefix"
         | 
| 667 713 | 
             
                end
         | 
| 668 714 |  | 
    
        data/lib/ipaddress/prefix.rb
    CHANGED
    
    | @@ -11,7 +11,7 @@ module IPAddress | |
| 11 11 | 
             
              #
         | 
| 12 12 | 
             
              # =DESCRIPTION
         | 
| 13 13 | 
             
              #
         | 
| 14 | 
            -
              #  | 
| 14 | 
            +
              # IPAddress::Prefix is the parent class for IPAddress::Prefix32 
         | 
| 15 15 | 
             
              # and IPAddress::Prefix128, defining some modules in common for
         | 
| 16 16 | 
             
              # both the subclasses.
         | 
| 17 17 | 
             
              #
         | 
| @@ -24,23 +24,60 @@ module IPAddress | |
| 24 24 |  | 
| 25 25 | 
             
                attr_reader :prefix
         | 
| 26 26 |  | 
| 27 | 
            +
                #
         | 
| 28 | 
            +
                # Creates a new general prefix
         | 
| 29 | 
            +
                #
         | 
| 27 30 | 
             
                def initialize(num)
         | 
| 28 31 | 
             
                  @prefix = num.to_i
         | 
| 29 32 | 
             
                end
         | 
| 30 33 |  | 
| 34 | 
            +
                #
         | 
| 35 | 
            +
                # Returns a string with the prefix 
         | 
| 36 | 
            +
                #
         | 
| 31 37 | 
             
                def to_s
         | 
| 32 38 | 
             
                  "#@prefix"
         | 
| 33 39 | 
             
                end
         | 
| 34 40 | 
             
                alias_method :inspect, :to_s
         | 
| 35 41 |  | 
| 42 | 
            +
                # 
         | 
| 43 | 
            +
                # Returns the prefix
         | 
| 44 | 
            +
                #
         | 
| 36 45 | 
             
                def to_i
         | 
| 37 46 | 
             
                  @prefix
         | 
| 38 47 | 
             
                end
         | 
| 39 48 |  | 
| 49 | 
            +
                # 
         | 
| 50 | 
            +
                # Compare the prefix
         | 
| 51 | 
            +
                #
         | 
| 40 52 | 
             
                def <=>(oth)
         | 
| 41 53 | 
             
                  @prefix <=> oth.to_i
         | 
| 42 54 | 
             
                end
         | 
| 43 55 |  | 
| 56 | 
            +
                #
         | 
| 57 | 
            +
                # Sums two prefixes or a prefix to a 
         | 
| 58 | 
            +
                # number, returns a Fixnum
         | 
| 59 | 
            +
                #
         | 
| 60 | 
            +
                def +(oth)
         | 
| 61 | 
            +
                  if oth.is_a? Fixnum
         | 
| 62 | 
            +
                    self.prefix + oth
         | 
| 63 | 
            +
                  else
         | 
| 64 | 
            +
                    self.prefix + oth.prefix
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                #
         | 
| 69 | 
            +
                # Returns the difference between two
         | 
| 70 | 
            +
                # prefixes, or a prefix and a number,
         | 
| 71 | 
            +
                # as a Fixnum
         | 
| 72 | 
            +
                #
         | 
| 73 | 
            +
                def -(oth)
         | 
| 74 | 
            +
                  if oth.is_a? Fixnum
         | 
| 75 | 
            +
                    self.prefix - oth
         | 
| 76 | 
            +
                  else
         | 
| 77 | 
            +
                    (self.prefix - oth.prefix).abs
         | 
| 78 | 
            +
                  end
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
                
         | 
| 44 81 | 
             
               end # class Prefix
         | 
| 45 82 |  | 
| 46 83 |  | 
    
        data/test/ipaddress/ipv4_test.rb
    CHANGED
    
    | @@ -74,6 +74,14 @@ class IPv4Test < Test::Unit::TestCase | |
| 74 74 | 
             
                assert_raise (ArgumentError) {@klass.new("10.0.0.0/asd")}
         | 
| 75 75 | 
             
              end
         | 
| 76 76 |  | 
| 77 | 
            +
              def test_initialize_without_prefix
         | 
| 78 | 
            +
                assert_nothing_raised do
         | 
| 79 | 
            +
                  @klass.new("10.10.0.0")
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
                ip = @klass.new("10.10.0.0")
         | 
| 82 | 
            +
                assert_instance_of IPAddress::Prefix32, ip.prefix
         | 
| 83 | 
            +
              end
         | 
| 84 | 
            +
             | 
| 77 85 | 
             
              def test_attributes
         | 
| 78 86 | 
             
                @valid_ipv4.each do |arg,attr|
         | 
| 79 87 | 
             
                  ip = @klass.new(arg)
         | 
| @@ -95,13 +103,20 @@ class IPv4Test < Test::Unit::TestCase | |
| 95 103 | 
             
                assert_equal "\254\020\n\001", @ip.data
         | 
| 96 104 | 
             
              end
         | 
| 97 105 |  | 
| 106 | 
            +
              def test_method_to_string
         | 
| 107 | 
            +
                @valid_ipv4.each do |arg,attr|
         | 
| 108 | 
            +
                  ip = @klass.new(arg)
         | 
| 109 | 
            +
                  assert_equal attr.join("/"), ip.to_string
         | 
| 110 | 
            +
                end
         | 
| 111 | 
            +
              end
         | 
| 112 | 
            +
             | 
| 98 113 | 
             
              def test_method_to_s
         | 
| 99 114 | 
             
                @valid_ipv4.each do |arg,attr|
         | 
| 100 115 | 
             
                  ip = @klass.new(arg)
         | 
| 101 | 
            -
                  assert_equal attr. | 
| 116 | 
            +
                  assert_equal attr.first, ip.to_s
         | 
| 102 117 | 
             
                end
         | 
| 103 118 | 
             
              end
         | 
| 104 | 
            -
             | 
| 119 | 
            +
             | 
| 105 120 | 
             
              def test_netmask
         | 
| 106 121 | 
             
                @netmask_values.each do |addr,mask|
         | 
| 107 122 | 
             
                  ip = @klass.new(addr)
         | 
| @@ -125,7 +140,7 @@ class IPv4Test < Test::Unit::TestCase | |
| 125 140 | 
             
                @broadcast.each do |addr,bcast|
         | 
| 126 141 | 
             
                  ip = @klass.new(addr)
         | 
| 127 142 | 
             
                  assert_instance_of @klass, ip.broadcast
         | 
| 128 | 
            -
                  assert_equal bcast, ip.broadcast. | 
| 143 | 
            +
                  assert_equal bcast, ip.broadcast.to_string
         | 
| 129 144 | 
             
                end
         | 
| 130 145 | 
             
              end
         | 
| 131 146 |  | 
| @@ -133,7 +148,7 @@ class IPv4Test < Test::Unit::TestCase | |
| 133 148 | 
             
                @networks.each do |addr,net|
         | 
| 134 149 | 
             
                  ip = @klass.new addr
         | 
| 135 150 | 
             
                  assert_instance_of @klass, ip.network
         | 
| 136 | 
            -
                  assert_equal net, ip.network. | 
| 151 | 
            +
                  assert_equal net, ip.network.to_string
         | 
| 137 152 | 
             
                end
         | 
| 138 153 | 
             
              end
         | 
| 139 154 |  | 
| @@ -145,27 +160,27 @@ class IPv4Test < Test::Unit::TestCase | |
| 145 160 | 
             
              def test_method_first
         | 
| 146 161 | 
             
                ip = @klass.new("192.168.100.0/24")
         | 
| 147 162 | 
             
                assert_instance_of @klass, ip.first
         | 
| 148 | 
            -
                assert_equal "192.168.100.1 | 
| 163 | 
            +
                assert_equal "192.168.100.1", ip.first.to_s
         | 
| 149 164 | 
             
                ip = @klass.new("192.168.100.50/24")
         | 
| 150 165 | 
             
                assert_instance_of @klass, ip.first
         | 
| 151 | 
            -
                assert_equal "192.168.100.1 | 
| 166 | 
            +
                assert_equal "192.168.100.1", ip.first.to_s
         | 
| 152 167 | 
             
              end
         | 
| 153 168 |  | 
| 154 169 | 
             
              def test_method_last
         | 
| 155 170 | 
             
                ip = @klass.new("192.168.100.0/24")
         | 
| 156 171 | 
             
                assert_instance_of @klass, ip.last
         | 
| 157 | 
            -
                assert_equal  "192.168.100.254 | 
| 172 | 
            +
                assert_equal  "192.168.100.254", ip.last.to_s
         | 
| 158 173 | 
             
                ip = @klass.new("192.168.100.50/24")
         | 
| 159 174 | 
             
                assert_instance_of @klass, ip.last
         | 
| 160 | 
            -
                assert_equal  "192.168.100.254 | 
| 175 | 
            +
                assert_equal  "192.168.100.254", ip.last.to_s
         | 
| 161 176 | 
             
              end
         | 
| 162 177 |  | 
| 163 178 | 
             
              def test_method_each_host
         | 
| 164 179 | 
             
                ip = @klass.new("10.0.0.1/29")
         | 
| 165 180 | 
             
                arr = []
         | 
| 166 181 | 
             
                ip.each_host {|i| arr << i.to_s}
         | 
| 167 | 
            -
                expected = ["10.0.0.1 | 
| 168 | 
            -
                            "10.0.0.4 | 
| 182 | 
            +
                expected = ["10.0.0.1","10.0.0.2","10.0.0.3",
         | 
| 183 | 
            +
                            "10.0.0.4","10.0.0.5","10.0.0.6"]
         | 
| 169 184 | 
             
                assert_equal expected, arr
         | 
| 170 185 | 
             
              end
         | 
| 171 186 |  | 
| @@ -173,9 +188,9 @@ class IPv4Test < Test::Unit::TestCase | |
| 173 188 | 
             
                ip = @klass.new("10.0.0.1/29")
         | 
| 174 189 | 
             
                arr = []
         | 
| 175 190 | 
             
                ip.each {|i| arr << i.to_s}
         | 
| 176 | 
            -
                expected = ["10.0.0.0 | 
| 177 | 
            -
                            "10.0.0.3 | 
| 178 | 
            -
                            "10.0.0.6 | 
| 191 | 
            +
                expected = ["10.0.0.0","10.0.0.1","10.0.0.2",
         | 
| 192 | 
            +
                            "10.0.0.3","10.0.0.4","10.0.0.5",
         | 
| 193 | 
            +
                            "10.0.0.6","10.0.0.7"]
         | 
| 179 194 | 
             
                assert_equal expected, arr
         | 
| 180 195 | 
             
              end
         | 
| 181 196 |  | 
| @@ -186,8 +201,8 @@ class IPv4Test < Test::Unit::TestCase | |
| 186 201 |  | 
| 187 202 | 
             
              def test_method_hosts
         | 
| 188 203 | 
             
                ip = @klass.new("10.0.0.1/29")
         | 
| 189 | 
            -
                expected = ["10.0.0.1 | 
| 190 | 
            -
                            "10.0.0.4 | 
| 204 | 
            +
                expected = ["10.0.0.1","10.0.0.2","10.0.0.3",
         | 
| 205 | 
            +
                            "10.0.0.4","10.0.0.5","10.0.0.6"]
         | 
| 191 206 | 
             
                assert_equal expected, ip.hosts.map {|i| i.to_s}
         | 
| 192 207 | 
             
              end
         | 
| 193 208 |  | 
| @@ -272,7 +287,7 @@ class IPv4Test < Test::Unit::TestCase | |
| 272 287 | 
             
                assert_equal true, ip1 == ip4
         | 
| 273 288 | 
             
                # test sorting
         | 
| 274 289 | 
             
                arr = ["10.1.1.1/16","10.1.1.1/8","172.16.1.1/14"]
         | 
| 275 | 
            -
                assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s. | 
| 290 | 
            +
                assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string}
         | 
| 276 291 | 
             
              end
         | 
| 277 292 |  | 
| 278 293 | 
             
              def test_method_minus
         | 
| @@ -285,12 +300,30 @@ class IPv4Test < Test::Unit::TestCase | |
| 285 300 | 
             
              def test_method_plus
         | 
| 286 301 | 
             
                ip1 = @klass.new("172.16.10.1/24")
         | 
| 287 302 | 
             
                ip2 = @klass.new("172.16.11.2/24")
         | 
| 288 | 
            -
                assert_equal "172.16.10.0/23", (ip1 | 
| 303 | 
            +
                assert_equal ["172.16.10.0/23"], (ip1+ip2).map{|i| i.to_string}
         | 
| 304 | 
            +
             | 
| 289 305 | 
             
                ip2 = @klass.new("172.16.12.2/24")
         | 
| 290 | 
            -
                assert_equal [ip1.network. | 
| 306 | 
            +
                assert_equal [ip1.network.to_string, ip2.network.to_string], 
         | 
| 307 | 
            +
                (ip1 + ip2).map{|i| i.to_string}
         | 
| 308 | 
            +
             | 
| 309 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 310 | 
            +
                ip2 = @klass.new("10.0.2.0/24")
         | 
| 311 | 
            +
                assert_equal ["10.0.0.0/23","10.0.2.0/24"], (ip1+ip2).map{|i| i.to_string}
         | 
| 312 | 
            +
             | 
| 313 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 314 | 
            +
                ip2 = @klass.new("10.0.2.0/24")
         | 
| 315 | 
            +
                assert_equal ["10.0.0.0/23","10.0.2.0/24"], (ip2+ip1).map{|i| i.to_string}
         | 
| 316 | 
            +
             | 
| 317 | 
            +
                ip1 = @klass.new("10.0.0.0/16")
         | 
| 318 | 
            +
                ip2 = @klass.new("10.0.2.0/24")
         | 
| 319 | 
            +
                assert_equal ["10.0.0.0/16"], (ip1+ip2).map{|i| i.to_string}
         | 
| 320 | 
            +
             | 
| 321 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 322 | 
            +
                ip2 = @klass.new("10.1.0.0/24")
         | 
| 323 | 
            +
                assert_equal ["10.0.0.0/23","10.1.0.0/24"], (ip1+ip2).map{|i| i.to_string}
         | 
| 324 | 
            +
             | 
| 291 325 | 
             
              end
         | 
| 292 326 |  | 
| 293 | 
            -
              
         | 
| 294 327 | 
             
              def test_method_netmask_equal
         | 
| 295 328 | 
             
                ip = @klass.new("10.1.1.1/16")
         | 
| 296 329 | 
             
                assert_equal 16, ip.prefix.to_i
         | 
| @@ -305,78 +338,99 @@ class IPv4Test < Test::Unit::TestCase | |
| 305 338 | 
             
                 arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", 
         | 
| 306 339 | 
             
                        "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", 
         | 
| 307 340 | 
             
                        "172.16.10.192/27", "172.16.10.224/27"]
         | 
| 308 | 
            -
                 assert_equal arr, @network.subnet(8).map {|s| s. | 
| 341 | 
            +
                 assert_equal arr, @network.subnet(8).map {|s| s.to_string}
         | 
| 309 342 | 
             
                 arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", 
         | 
| 310 343 | 
             
                        "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", 
         | 
| 311 344 | 
             
                        "172.16.10.192/26"]
         | 
| 312 | 
            -
                 assert_equal arr, @network.subnet(7).map {|s| s. | 
| 345 | 
            +
                 assert_equal arr, @network.subnet(7).map {|s| s.to_string}
         | 
| 313 346 | 
             
                 arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", 
         | 
| 314 347 | 
             
                        "172.16.10.96/27", "172.16.10.128/26", "172.16.10.192/26"]
         | 
| 315 | 
            -
                 assert_equal arr, @network.subnet(6).map {|s| s. | 
| 348 | 
            +
                 assert_equal arr, @network.subnet(6).map {|s| s.to_string}
         | 
| 316 349 | 
             
                 arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", 
         | 
| 317 350 | 
             
                        "172.16.10.96/27", "172.16.10.128/25"]
         | 
| 318 | 
            -
                 assert_equal arr, @network.subnet(5).map {|s| s. | 
| 351 | 
            +
                 assert_equal arr, @network.subnet(5).map {|s| s.to_string}
         | 
| 319 352 | 
             
                 arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", 
         | 
| 320 353 | 
             
                        "172.16.10.192/26"]
         | 
| 321 | 
            -
                 assert_equal arr, @network.subnet(4).map {|s| s. | 
| 354 | 
            +
                 assert_equal arr, @network.subnet(4).map {|s| s.to_string}
         | 
| 322 355 | 
             
                 arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"]
         | 
| 323 | 
            -
                 assert_equal arr, @network.subnet(3).map {|s| s. | 
| 356 | 
            +
                 assert_equal arr, @network.subnet(3).map {|s| s.to_string}
         | 
| 324 357 | 
             
                 arr = ["172.16.10.0/25", "172.16.10.128/25"]
         | 
| 325 | 
            -
                 assert_equal arr, @network.subnet(2).map {|s| s. | 
| 358 | 
            +
                 assert_equal arr, @network.subnet(2).map {|s| s.to_string}
         | 
| 326 359 | 
             
                 arr = ["172.16.10.0/24"]
         | 
| 327 | 
            -
                 assert_equal arr, @network.subnet(1).map {|s| s. | 
| 360 | 
            +
                 assert_equal arr, @network.subnet(1).map {|s| s.to_string}
         | 
| 328 361 | 
             
               end
         | 
| 329 362 |  | 
| 330 363 | 
             
               def test_method_supernet
         | 
| 331 364 | 
             
                 assert_raise(ArgumentError) {@ip.supernet(0)}
         | 
| 332 365 | 
             
                 assert_raise(ArgumentError) {@ip.supernet(24)}     
         | 
| 333 | 
            -
                 assert_equal "172.16.10.0/23", @ip.supernet(23). | 
| 334 | 
            -
                 assert_equal "172.16.8.0/22", @ip.supernet(22). | 
| 366 | 
            +
                 assert_equal "172.16.10.0/23", @ip.supernet(23).to_string
         | 
| 367 | 
            +
                 assert_equal "172.16.8.0/22", @ip.supernet(22).to_string
         | 
| 335 368 | 
             
               end
         | 
| 336 369 |  | 
| 337 370 | 
             
              def test_classmethod_parse_u32
         | 
| 338 371 | 
             
                @decimal_values.each do  |addr,int|
         | 
| 339 372 | 
             
                  ip = @klass.parse_u32(int)
         | 
| 340 373 | 
             
                  ip.prefix = addr.split("/").last.to_i
         | 
| 341 | 
            -
                  assert_equal ip. | 
| 374 | 
            +
                  assert_equal ip.to_string, addr
         | 
| 342 375 | 
             
                end
         | 
| 343 376 | 
             
              end
         | 
| 344 377 |  | 
| 345 378 | 
             
              def test_classhmethod_extract
         | 
| 346 379 | 
             
                str = "foobar172.16.10.1barbaz"
         | 
| 347 | 
            -
                assert_equal "172.16.10.1 | 
| 380 | 
            +
                assert_equal "172.16.10.1", @klass.extract(str).to_s
         | 
| 348 381 | 
             
              end
         | 
| 349 382 |  | 
| 350 383 | 
             
              def test_classmethod_summarize
         | 
| 351 384 |  | 
| 352 385 | 
             
                # Should return self if only one network given
         | 
| 353 | 
            -
                assert_equal @ip, @klass.summarize(@ip)
         | 
| 386 | 
            +
                assert_equal [@ip.network], @klass.summarize(@ip)
         | 
| 354 387 |  | 
| 355 388 | 
             
                # Summarize homogeneous networks
         | 
| 356 389 | 
             
                ip1 = @klass.new("172.16.10.1/24")
         | 
| 357 390 | 
             
                ip2 = @klass.new("172.16.11.2/24")
         | 
| 358 | 
            -
                assert_equal "172.16.10.0/23", @klass.summarize(ip1,ip2). | 
| 391 | 
            +
                assert_equal ["172.16.10.0/23"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
         | 
| 359 392 |  | 
| 360 393 | 
             
                ip1 = @klass.new("10.0.0.1/24")
         | 
| 361 394 | 
             
                ip2 = @klass.new("10.0.1.1/24")
         | 
| 362 395 | 
             
                ip3 = @klass.new("10.0.2.1/24")
         | 
| 363 396 | 
             
                ip4 = @klass.new("10.0.3.1/24")
         | 
| 364 | 
            -
                assert_equal "10.0.0.0/22", @klass.summarize(ip1,ip2,ip3,ip4). | 
| 365 | 
            -
                
         | 
| 397 | 
            +
                assert_equal ["10.0.0.0/22"], @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
         | 
| 398 | 
            +
                assert_equal ["10.0.0.0/22"], @klass.summarize(ip4,ip3,ip2,ip1).map{|i| i.to_string}
         | 
| 399 | 
            +
             | 
| 366 400 | 
             
                # Summarize non homogeneous networks
         | 
| 401 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 402 | 
            +
                ip2 = @klass.new("10.0.2.0/24")
         | 
| 403 | 
            +
                assert_equal ["10.0.0.0/23","10.0.2.0/24"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
         | 
| 404 | 
            +
             | 
| 405 | 
            +
                ip1 = @klass.new("10.0.0.0/16")
         | 
| 406 | 
            +
                ip2 = @klass.new("10.0.2.0/24")
         | 
| 407 | 
            +
                assert_equal ["10.0.0.0/16"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
         | 
| 408 | 
            +
             | 
| 409 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 410 | 
            +
                ip2 = @klass.new("10.1.0.0/24")
         | 
| 411 | 
            +
                assert_equal ["10.0.0.0/23","10.1.0.0/24"], @klass.summarize(ip1,ip2).map{|i| i.to_string}
         | 
| 412 | 
            +
             | 
| 413 | 
            +
                ip1 = @klass.new("10.0.0.0/23")
         | 
| 414 | 
            +
                ip2 = @klass.new("10.0.2.0/23")
         | 
| 415 | 
            +
                ip3 = @klass.new("10.0.4.0/24")
         | 
| 416 | 
            +
                ip4 = @klass.new("10.0.6.0/24")
         | 
| 417 | 
            +
                assert_equal ["10.0.0.0/22","10.0.4.0/24","10.0.6.0/24"], 
         | 
| 418 | 
            +
                          @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
         | 
| 419 | 
            +
             | 
| 367 420 | 
             
                ip1 = @klass.new("10.0.1.1/24")
         | 
| 368 421 | 
             
                ip2 = @klass.new("10.0.2.1/24")
         | 
| 369 422 | 
             
                ip3 = @klass.new("10.0.3.1/24")
         | 
| 370 423 | 
             
                ip4 = @klass.new("10.0.4.1/24")
         | 
| 371 424 | 
             
                result = ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
         | 
| 372 | 
            -
                assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i. | 
| 425 | 
            +
                assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
         | 
| 426 | 
            +
                assert_equal result, @klass.summarize(ip4,ip3,ip2,ip1).map{|i| i.to_string}
         | 
| 373 427 |  | 
| 374 428 | 
             
                ip1 = @klass.new("10.0.1.1/24")
         | 
| 375 429 | 
             
                ip2 = @klass.new("10.10.2.1/24")
         | 
| 376 430 | 
             
                ip3 = @klass.new("172.16.0.1/24")
         | 
| 377 431 | 
             
                ip4 = @klass.new("172.16.1.1/24")
         | 
| 378 432 | 
             
                result = ["10.0.1.0/24","10.10.2.0/24","172.16.0.0/23"]
         | 
| 379 | 
            -
                assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i. | 
| 433 | 
            +
                assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
         | 
| 380 434 |  | 
| 381 435 | 
             
              end
         | 
| 382 436 |  | 
| @@ -384,7 +438,7 @@ class IPv4Test < Test::Unit::TestCase | |
| 384 438 | 
             
                ip = @klass.parse_data "\254\020\n\001"
         | 
| 385 439 | 
             
                assert_instance_of @klass, ip
         | 
| 386 440 | 
             
                assert_equal "172.16.10.1", ip.address
         | 
| 387 | 
            -
                assert_equal "172.16.10.1/16", ip. | 
| 441 | 
            +
                assert_equal "172.16.10.1/16", ip.to_string
         | 
| 388 442 | 
             
              end
         | 
| 389 443 |  | 
| 390 444 | 
             
            end # class IPv4Test
         |