Project

General

Profile

Feature #11666 ยป patch.diff

Glass_saga (Masaki Matsushita), 11/08/2015 07:19 AM

View differences:

lib/ipaddr.rb
195 195
  end
196 196
  alias === include?
197 197

  
198
  def private?
199
    # RFC1918: Private Address Space
200
    @@__private_network_class_a ||= self.class.new("10.0.0.0/8")
201
    @@__private_network_class_b ||= self.class.new("172.16.0.0/12")
202
    @@__private_network_class_c ||= self.class.new("192.168.0.0/16")
203

  
204
    return true if @@__private_network_class_a.include?(self) || \
205
                   @@__private_network_class_b.include?(self) || \
206
                   @@__private_network_class_c.include?(self)
207

  
208
    return false
209
  end
210

  
198 211
  # Returns the integer representation of the ipaddr.
199 212
  def to_i
200 213
    return @addr
test/test_ipaddr.rb
243 243

  
244 244
  end
245 245

  
246
  def test_private?
247
    assert_equal(true, IPAddr.new("10.0.0.0").private?)
248
    assert_equal(true, IPAddr.new("10.255.255.255").private?)
249

  
250
    assert_equal(true, IPAddr.new("172.16.0.0").private?)
251
    assert_equal(true, IPAddr.new("172.31.255.255").private?)
252

  
253
    assert_equal(true, IPAddr.new("192.168.0.0").private?)
254
    assert_equal(true, IPAddr.new("192.168.255.255").private?)
255

  
256
    assert_equal(false, IPAddr.new("192.1.0.0").private?)
257
    assert_equal(false, IPAddr.new("172.32.0.0").private?)
258
    assert_equal(false, IPAddr.new("11.0.0.0").private?)
259
  end
260

  
246 261
  def test_hash
247 262
    a1 = IPAddr.new('192.168.2.0')
248 263
    a2 = IPAddr.new('192.168.2.0')