Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / gtest / TestIsValidIp.cpp
blobdfee8c5c0face965e31216963c6c262d6ebd1c75
1 #include "gtest/MozGTestBench.h" // For MOZ_GTEST_BENCH
2 #include "gtest/gtest.h"
4 #include "nsURLHelper.h"
6 TEST(TestIsValidIp, IPV4Localhost)
8 constexpr auto ip = "127.0.0.1"_ns;
9 ASSERT_EQ(true, net_IsValidIPv4Addr(ip));
12 TEST(TestIsValidIp, IPV4Only0)
14 constexpr auto ip = "0.0.0.0"_ns;
15 ASSERT_EQ(true, net_IsValidIPv4Addr(ip));
18 TEST(TestIsValidIp, IPV4Max)
20 constexpr auto ip = "255.255.255.255"_ns;
21 ASSERT_EQ(true, net_IsValidIPv4Addr(ip));
24 TEST(TestIsValidIp, IPV4LeadingZero)
26 constexpr auto ip = "055.225.255.255"_ns;
27 ASSERT_EQ(false, net_IsValidIPv4Addr(ip));
29 constexpr auto ip2 = "255.055.255.255"_ns;
30 ASSERT_EQ(false, net_IsValidIPv4Addr(ip2));
32 constexpr auto ip3 = "255.255.055.255"_ns;
33 ASSERT_EQ(false, net_IsValidIPv4Addr(ip3));
35 constexpr auto ip4 = "255.255.255.055"_ns;
36 ASSERT_EQ(false, net_IsValidIPv4Addr(ip4));
39 TEST(TestIsValidIp, IPV4StartWithADot)
41 constexpr auto ip = ".192.168.120.197"_ns;
42 ASSERT_EQ(false, net_IsValidIPv4Addr(ip));
45 TEST(TestIsValidIp, IPV4StartWith4Digits)
47 constexpr auto ip = "1927.168.120.197"_ns;
48 ASSERT_EQ(false, net_IsValidIPv4Addr(ip));
51 TEST(TestIsValidIp, IPV4OutOfRange)
53 constexpr auto invalid1 = "421.168.120.124"_ns;
54 constexpr auto invalid2 = "192.997.120.124"_ns;
55 constexpr auto invalid3 = "192.168.300.124"_ns;
56 constexpr auto invalid4 = "192.168.120.256"_ns;
58 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid1));
59 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid2));
60 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid3));
61 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid4));
64 TEST(TestIsValidIp, IPV4EmptyDigits)
66 constexpr auto invalid1 = "..0.0.0"_ns;
67 constexpr auto invalid2 = "127..0.0"_ns;
68 constexpr auto invalid3 = "127.0..0"_ns;
69 constexpr auto invalid4 = "127.0.0."_ns;
71 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid1));
72 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid2));
73 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid3));
74 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid4));
77 TEST(TestIsValidIp, IPV4NonNumeric)
79 constexpr auto invalid1 = "127.0.0.f"_ns;
80 constexpr auto invalid2 = "127.0.0.!"_ns;
81 constexpr auto invalid3 = "127#0.0.1"_ns;
83 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid1));
84 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid2));
85 ASSERT_EQ(false, net_IsValidIPv4Addr(invalid3));
88 TEST(TestIsValidIp, IPV4TooManyDigits)
90 constexpr auto ip = "127.0.0.1.2"_ns;
91 ASSERT_EQ(false, net_IsValidIPv4Addr(ip));
94 TEST(TestIsValidIp, IPV4TooFewDigits)
96 constexpr auto ip = "127.0.1"_ns;
97 ASSERT_EQ(false, net_IsValidIPv4Addr(ip));
100 TEST(TestIsValidIp, IPV6WithIPV4Inside)
102 constexpr auto ipv6 = "0123:4567:89ab:cdef:0123:4567:127.0.0.1"_ns;
103 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
106 TEST(TestIsValidIp, IPv6FullForm)
108 constexpr auto ipv6 = "0123:4567:89ab:cdef:0123:4567:890a:bcde"_ns;
109 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
112 TEST(TestIsValidIp, IPv6TrimLeading0)
114 constexpr auto ipv6 = "123:4567:0:0:123:4567:890a:bcde"_ns;
115 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
118 TEST(TestIsValidIp, IPv6Collapsed)
120 constexpr auto ipv6 = "FF01::101"_ns;
121 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
124 TEST(TestIsValidIp, IPV6WithIPV4InsideCollapsed)
126 constexpr auto ipv6 = "::FFFF:129.144.52.38"_ns;
127 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
130 TEST(TestIsValidIp, IPV6Localhost)
132 constexpr auto ipv6 = "::1"_ns;
133 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
136 TEST(TestIsValidIp, IPV6LinkLocalPrefix)
138 constexpr auto ipv6 = "fe80::"_ns;
139 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
142 TEST(TestIsValidIp, IPV6GlobalUnicastPrefix)
144 constexpr auto ipv6 = "2001::"_ns;
145 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
148 TEST(TestIsValidIp, IPV6Unspecified)
150 constexpr auto ipv6 = "::"_ns;
151 ASSERT_EQ(true, net_IsValidIPv6Addr(ipv6));
154 TEST(TestIsValidIp, IPV6InvalidIPV4Inside)
156 constexpr auto ipv6 = "0123:4567:89ab:cdef:0123:4567:127.0."_ns;
157 ASSERT_EQ(false, net_IsValidIPv6Addr(ipv6));
160 TEST(TestIsValidIp, IPV6InvalidCharacters)
162 constexpr auto ipv6 = "012g:4567:89ab:cdef:0123:4567:127.0.0.1"_ns;
163 ASSERT_EQ(false, net_IsValidIPv6Addr(ipv6));
165 constexpr auto ipv6pound = "0123:456#:89ab:cdef:0123:4567:127.0.0.1"_ns;
166 ASSERT_EQ(false, net_IsValidIPv6Addr(ipv6pound));
169 TEST(TestIsValidIp, IPV6TooManyCharacters)
171 constexpr auto ipv6 = "0123:45671:89ab:cdef:0123:4567:127.0.0.1"_ns;
172 ASSERT_EQ(false, net_IsValidIPv6Addr(ipv6));
174 TEST(TestIsValidIp, IPV6DoubleDoubleDots)
176 constexpr auto ipv6 = "0123::4567:890a::bcde:0123:4567"_ns;
177 ASSERT_EQ(false, net_IsValidIPv6Addr(ipv6));