Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1414)

Unified Diff: net/base/ip_address_unittest.cc

Issue 1860823005: Move some net::IPAddressNumber implementations to net::IPAddress. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« net/base/ip_address.cc ('K') | « net/base/ip_address_number_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/ip_address_unittest.cc
diff --git a/net/base/ip_address_unittest.cc b/net/base/ip_address_unittest.cc
index 3b9f3dfc183b823d5d1041a1f6a1e812da9e793d..2ad8900f6f635d90f3dadf3f3389ed10a92f0c79 100644
--- a/net/base/ip_address_unittest.cc
+++ b/net/base/ip_address_unittest.cc
@@ -6,7 +6,9 @@
#include <vector>
+#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
+#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
@@ -75,6 +77,32 @@ TEST(IPAddressTest, IsValid) {
EXPECT_TRUE(ip_address4.empty());
}
+TEST(IPAddressTest, IsReserved) {
martijnc 2016/04/11 22:02:09 Added these tests, the IPv6 addresses are from [1]
eroman 2016/04/13 01:36:21 Agreed, thanks! I have some suggestions on the IP
+ struct {
+ const char* const address;
+ bool is_reserved;
+ } tests[] = {{"10.10.10.10", true},
+ {"9.9.255.255", false},
+ {"127.0.0.1", true},
+ {"128.0.0.1", false},
+ {"198.18.0.0", true},
+ {"198.18.255.255", true},
+ {"198.17.255.255", false},
+ {"198.19.255.255", true},
+ {"198.20.0.0", false},
+ {"0000::", true},
+ {"FFC0:ba98:7654:3210:FEDC:BA98:7654:3210", false},
+ {"2000:ba98:7654:2301:EFCD:BA98:7654:3210", false},
+ {"::192.9.5.5", true},
+ {"FEED::BEEF", true}};
+
+ IPAddress address;
+ for (const auto& test : tests) {
+ EXPECT_TRUE(address.AssignFromIPLiteral(test.address));
+ EXPECT_EQ(test.is_reserved, address.IsReserved());
+ }
+}
+
TEST(IPAddressTest, IsZero) {
uint8_t address1[4] = {};
IPAddress zero_ipv4_address(address1);
@@ -252,6 +280,47 @@ TEST(IPAddressTest, ConvertIPv4MappedIPv6ToIPv4) {
EXPECT_EQ(expected, result);
}
+TEST(IPAddressTest, IPAddressMatchesPrefix) {
martijnc 2016/04/11 22:02:09 Tests from net/base/ip_address_number_unittest.cc.
+ struct {
+ const char* const cidr_literal;
+ size_t prefix_length_in_bits;
+ const char* const ip_literal;
+ bool expected_to_match;
+ } tests[] = {
+ // IPv4 prefix with IPv4 inputs.
+ {"10.10.1.32", 27, "10.10.1.44", true},
+ {"10.10.1.32", 27, "10.10.1.90", false},
+ {"10.10.1.32", 27, "10.10.1.90", false},
+
+ // IPv6 prefix with IPv6 inputs.
+ {"2001:db8::", 32, "2001:DB8:3:4::5", true},
+ {"2001:db8::", 32, "2001:c8::", false},
+
+ // IPv6 prefix with IPv4 inputs.
+ {"2001:db8::", 33, "192.168.0.1", false},
+ {"::ffff:192.168.0.1", 112, "192.168.33.77", true},
+
+ // IPv4 prefix with IPv6 inputs.
+ {"10.11.33.44", 16, "::ffff:0a0b:89", true},
+ {"10.11.33.44", 16, "::ffff:10.12.33.44", false},
+ };
+ for (size_t i = 0; i < arraysize(tests); ++i) {
+ SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i,
+ tests[i].cidr_literal,
+ tests[i].ip_literal));
+
+ IPAddress ip_address;
+ EXPECT_TRUE(ip_address.AssignFromIPLiteral(tests[i].ip_literal));
+
+ IPAddress ip_prefix;
+ EXPECT_TRUE(ip_prefix.AssignFromIPLiteral(tests[i].cidr_literal));
+
+ EXPECT_EQ(tests[i].expected_to_match,
+ IPAddressMatchesPrefix(ip_address, ip_prefix,
+ tests[i].prefix_length_in_bits));
+ }
+}
+
// Test parsing invalid CIDR notation literals.
TEST(IPAddressTest, ParseCIDRBlock_Invalid) {
const char* const bad_literals[] = {"foobar",
« net/base/ip_address.cc ('K') | « net/base/ip_address_number_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698