Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv4: private and reserved #784

Merged
merged 1 commit into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions lib/faker/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,47 @@ def private_ip_v4_address
end

def public_ip_v4_address
is_private = private_net_checker
is_reserved = reserved_net_checker
addr = nil
begin
addr = ip_v4_address
end while is_private[addr]
end while is_reserved[addr]
addr
end

def private_nets_regex
[
/^10\./,
/^127\./,
/^169\.254\./,
/^172\.(16|17|18|19|2\d|30|31)\./,
/^192\.168\./
/^10\./, # 10.0.0.0 – 10.255.255.255
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255
/^127\./, # 127.0.0.0 – 127.255.255.255
/^169\.254\./, # 169.254.0.0 – 169.254.255.255
/^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255
/^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255
/^192\.168\./, # 192.168.0.0 – 192.168.255.255
/^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255
]
end

def private_net_checker
lambda { |addr| private_nets_regex.any? { |net| net =~ addr } }
end

def reserved_nets_regex
[
/^0\./, # 0.0.0.0 – 0.255.255.255
/^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255
/^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255
/^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255
/^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255
/^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255
/^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255
]
end

def reserved_net_checker
lambda { |addr| (private_nets_regex + reserved_nets_regex).any? { |net| net =~ addr } }
end

def ip_v4_cidr
"#{ip_v4_address}/#{1 + rand(31)}"
end
Expand Down
52 changes: 34 additions & 18 deletions test/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,50 @@ def test_ip_v4_address
end

def test_private_ip_v4_address
ten_dot = /^10\./
one_two_seven = /^127\./
one_six_nine = /^169\.254/
one_nine_two = /^192\.168\./
one_seven_two = /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
regexps = [
/^10\./, # 10.0.0.0 – 10.255.255.255
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255
/^127\./, # 127.0.0.0 – 127.255.255.255
/^169\.254\./, # 169.254.0.0 – 169.254.255.255
/^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255
/^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255
/^192\.168\./, # 192.168.0.0 – 192.168.255.255
/^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255
]
expected = Regexp.new regexps.collect{|reg| "(#{reg})"}.join('|')

1000.times do
address = @tester.private_ip_v4_address
assert_match Regexp.new(
"(#{ten_dot})|(#{one_two_seven})|(#{one_six_nine})|(#{one_nine_two})|(#{one_seven_two})"
), address
assert_match expected, address
end
end

def test_public_ip_v4_address
ten_dot = /^10\./
one_two_seven = /^127\./
one_six_nine = /^169\.254/
one_nine_two = /^192\.168\./
one_seven_two = /^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./
private = [
/^10\./, # 10.0.0.0 – 10.255.255.255
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255
/^127\./, # 127.0.0.0 – 127.255.255.255
/^169\.254\./, # 169.254.0.0 – 169.254.255.255
/^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255
/^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255
/^192\.168\./, # 192.168.0.0 – 192.168.255.255
/^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255
]

reserved = [
/^0\./, # 0.0.0.0 – 0.255.255.255
/^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255
/^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255
/^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255
/^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255
/^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255
/^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255
]

1000.times do
address = @tester.public_ip_v4_address
assert_not_match ten_dot, address
assert_not_match one_two_seven, address
assert_not_match one_six_nine, address
assert_not_match one_nine_two, address
assert_not_match one_seven_two, address
private.each { |reg| assert_not_match reg, address }
reserved.each { |reg| assert_not_match reg, address }
end
end

Expand Down