72 lines
2.4 KiB
Elixir
72 lines
2.4 KiB
Elixir
defmodule ToweropsWeb.DeviceLive.FormTest do
|
|
use ToweropsWeb.ConnCase, async: true
|
|
|
|
describe "non_routable_ip?/1" do
|
|
# Access private function via Module.get_attribute or test via public interface
|
|
# Since these are private functions, we test through the validation behavior
|
|
|
|
test "RFC1918 10.0.0.0/8 range" do
|
|
assert non_routable?("10.0.0.1")
|
|
assert non_routable?("10.255.255.255")
|
|
assert non_routable?("10.100.50.25")
|
|
end
|
|
|
|
test "RFC1918 172.16.0.0/12 range" do
|
|
assert non_routable?("172.16.0.1")
|
|
assert non_routable?("172.31.255.255")
|
|
assert non_routable?("172.20.10.5")
|
|
refute non_routable?("172.15.0.1")
|
|
refute non_routable?("172.32.0.1")
|
|
end
|
|
|
|
test "RFC1918 192.168.0.0/16 range" do
|
|
assert non_routable?("192.168.0.1")
|
|
assert non_routable?("192.168.255.255")
|
|
assert non_routable?("192.168.1.100")
|
|
refute non_routable?("192.167.1.1")
|
|
refute non_routable?("192.169.1.1")
|
|
end
|
|
|
|
test "RFC6598 CGNAT 100.64.0.0/10 range" do
|
|
assert non_routable?("100.64.0.1")
|
|
assert non_routable?("100.127.255.255")
|
|
assert non_routable?("100.100.50.25")
|
|
refute non_routable?("100.63.255.255")
|
|
refute non_routable?("100.128.0.1")
|
|
end
|
|
|
|
test "public IPs are not flagged" do
|
|
refute non_routable?("8.8.8.8")
|
|
refute non_routable?("1.1.1.1")
|
|
refute non_routable?("203.0.113.1")
|
|
refute non_routable?("198.51.100.1")
|
|
end
|
|
|
|
test "invalid IPs are not flagged" do
|
|
refute non_routable?("invalid")
|
|
refute non_routable?("999.999.999.999")
|
|
refute non_routable?("")
|
|
end
|
|
|
|
test "IPv6 addresses are not flagged" do
|
|
refute non_routable?("::1")
|
|
refute non_routable?("fe80::1")
|
|
refute non_routable?("2001:db8::1")
|
|
end
|
|
end
|
|
|
|
# Helper to test the private function logic
|
|
# Duplicated here for testing since the actual function is private
|
|
defp non_routable?(ip_string) do
|
|
case ip_string |> String.to_charlist() |> :inet.parse_address() do
|
|
{:ok, {a, b, _c, _d}} -> non_routable_ipv4_range?(a, b)
|
|
_ -> false
|
|
end
|
|
end
|
|
|
|
defp non_routable_ipv4_range?(10, _b), do: true
|
|
defp non_routable_ipv4_range?(172, b) when b >= 16 and b <= 31, do: true
|
|
defp non_routable_ipv4_range?(192, 168), do: true
|
|
defp non_routable_ipv4_range?(100, b) when b >= 64 and b <= 127, do: true
|
|
defp non_routable_ipv4_range?(_a, _b), do: false
|
|
end
|