Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/port_util.h" |
| 6 | |
Arthur Sonzogni | 4671687 | 2024-12-12 12:00:04 | [diff] [blame] | 7 | #include <array> |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Tom Van Goethem | d39f48c | 2025-05-02 12:03:37 | [diff] [blame] | 10 | #include "base/test/metrics/histogram_tester.h" |
Tom Van Goethem | 8f76075 | 2025-04-24 01:28:53 | [diff] [blame] | 11 | #include "base/test/scoped_feature_list.h" |
| 12 | #include "net/base/features.h" |
Tom Van Goethem | 5953507 | 2025-05-09 11:07:29 | [diff] [blame] | 13 | #include "net/base/ip_address.h" |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { |
Arthur Sonzogni | 4671687 | 2024-12-12 12:00:04 | [diff] [blame] | 19 | const auto valid = std::to_array<std::vector<uint16_t>>({ |
| 20 | {}, |
| 21 | {1}, |
| 22 | {1, 2}, |
| 23 | {1, 2, 3}, |
| 24 | {10, 11, 12, 13}, |
| 25 | }); |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 26 | |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 27 | for (size_t i = 0; i < std::size(valid); ++i) { |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 28 | SetExplicitlyAllowedPorts(valid[i]); |
| 29 | EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); |
| 30 | } |
| 31 | } |
| 32 | |
Tom Van Goethem | 8f76075 | 2025-04-24 01:28:53 | [diff] [blame] | 33 | TEST(NetUtilTest, RestrictedAbusePortsTest) { |
Tom Van Goethem | d39f48c | 2025-05-02 12:03:37 | [diff] [blame] | 34 | base::HistogramTester histogram_tester; |
Tom Van Goethem | 8f76075 | 2025-04-24 01:28:53 | [diff] [blame] | 35 | base::test::ScopedFeatureList feature_list; |
| 36 | feature_list.InitAndEnableFeatureWithParameters( |
| 37 | features::kRestrictAbusePorts, |
Tom Van Goethem | d39f48c | 2025-05-02 12:03:37 | [diff] [blame] | 38 | {{"restrict_ports", "12345,23456,34567"}, {"monitor_ports", "45678"}}); |
Tom Van Goethem | 8f76075 | 2025-04-24 01:28:53 | [diff] [blame] | 39 | EXPECT_TRUE(IsPortAllowedForScheme(443, "https")); |
| 40 | for (int port : {12345, 23456, 34567}) { |
| 41 | EXPECT_FALSE(IsPortAllowedForScheme(port, "https")); |
| 42 | } |
Tom Van Goethem | d39f48c | 2025-05-02 12:03:37 | [diff] [blame] | 43 | EXPECT_TRUE(IsPortAllowedForScheme(45678, "https")); |
| 44 | histogram_tester.ExpectTotalCount("Net.RestrictedPorts", 4); |
| 45 | histogram_tester.ExpectBucketCount("Net.RestrictedPorts", 12345, 1); |
| 46 | histogram_tester.ExpectBucketCount("Net.RestrictedPorts", 23456, 1); |
| 47 | histogram_tester.ExpectBucketCount("Net.RestrictedPorts", 34567, 1); |
| 48 | histogram_tester.ExpectBucketCount("Net.RestrictedPorts", 45678, 1); |
Tom Van Goethem | 8f76075 | 2025-04-24 01:28:53 | [diff] [blame] | 49 | } |
| 50 | |
Tom Van Goethem | 5953507 | 2025-05-09 11:07:29 | [diff] [blame] | 51 | TEST(NetUtilTest, RestrictedAbusePortsLocalhostTest) { |
Tom Van Goethem | eeb1f9d8 | 2025-05-29 10:25:17 | [diff] [blame] | 52 | base::HistogramTester histogram_tester; |
Tom Van Goethem | 5953507 | 2025-05-09 11:07:29 | [diff] [blame] | 53 | base::test::ScopedFeatureList feature_list; |
| 54 | feature_list.InitAndEnableFeatureWithParameters( |
| 55 | features::kRestrictAbusePortsOnLocalhost, |
| 56 | {{"localhost_restrict_ports", "12345,23456,34567"}}); |
| 57 | ReloadLocalhostRestrictedPortsForTesting(); |
| 58 | IPAddress public_address(8, 8, 8, 8); |
| 59 | EXPECT_TRUE(IsPortAllowedForIpEndpoint(IPEndPoint(public_address, 12345))); |
| 60 | EXPECT_TRUE(IsPortAllowedForIpEndpoint(IPEndPoint(public_address, 443))); |
| 61 | EXPECT_TRUE( |
| 62 | IsPortAllowedForIpEndpoint(IPEndPoint(IPAddress::IPv4Localhost(), 443))); |
| 63 | EXPECT_TRUE( |
| 64 | IsPortAllowedForIpEndpoint(IPEndPoint(IPAddress::IPv6Localhost(), 443))); |
Tom Van Goethem | eeb1f9d8 | 2025-05-29 10:25:17 | [diff] [blame] | 65 | histogram_tester.ExpectTotalCount("Net.RestrictedLocalhostPorts", 0); |
Tom Van Goethem | 5953507 | 2025-05-09 11:07:29 | [diff] [blame] | 66 | for (int port : {12345, 23456, 34567}) { |
| 67 | EXPECT_FALSE(IsPortAllowedForIpEndpoint( |
| 68 | IPEndPoint(IPAddress::IPv4Localhost(), port))); |
| 69 | EXPECT_FALSE(IsPortAllowedForIpEndpoint( |
| 70 | IPEndPoint(IPAddress::IPv6Localhost(), port))); |
| 71 | } |
Tom Van Goethem | eeb1f9d8 | 2025-05-29 10:25:17 | [diff] [blame] | 72 | histogram_tester.ExpectTotalCount("Net.RestrictedLocalhostPorts", 6); |
| 73 | histogram_tester.ExpectBucketCount("Net.RestrictedLocalhostPorts", 12345, 2); |
| 74 | histogram_tester.ExpectBucketCount("Net.RestrictedLocalhostPorts", 23456, 2); |
| 75 | histogram_tester.ExpectBucketCount("Net.RestrictedLocalhostPorts", 34567, 2); |
Tom Van Goethem | 5953507 | 2025-05-09 11:07:29 | [diff] [blame] | 76 | } |
| 77 | |
Tom Van Goethem | d977401 | 2025-05-30 23:10:39 | [diff] [blame] | 78 | TEST(NetUtilTest, RestrictedAbusePortsLocalhostTestNoParamSet) { |
| 79 | base::HistogramTester histogram_tester; |
| 80 | base::test::ScopedFeatureList feature_list; |
| 81 | feature_list.InitAndEnableFeature(features::kRestrictAbusePortsOnLocalhost); |
| 82 | ReloadLocalhostRestrictedPortsForTesting(); |
| 83 | IPAddress public_address(8, 8, 8, 8); |
| 84 | EXPECT_TRUE(IsPortAllowedForIpEndpoint(IPEndPoint(public_address, 12345))); |
| 85 | EXPECT_TRUE(IsPortAllowedForIpEndpoint(IPEndPoint(public_address, 443))); |
| 86 | EXPECT_TRUE( |
| 87 | IsPortAllowedForIpEndpoint(IPEndPoint(IPAddress::IPv4Localhost(), 443))); |
| 88 | EXPECT_TRUE( |
| 89 | IsPortAllowedForIpEndpoint(IPEndPoint(IPAddress::IPv6Localhost(), 443))); |
| 90 | histogram_tester.ExpectTotalCount("Net.RestrictedLocalhostPorts", 0); |
| 91 | for (int port : {12345, 23456, 34567}) { |
| 92 | EXPECT_TRUE(IsPortAllowedForIpEndpoint( |
| 93 | IPEndPoint(IPAddress::IPv4Localhost(), port))); |
| 94 | EXPECT_TRUE(IsPortAllowedForIpEndpoint( |
| 95 | IPEndPoint(IPAddress::IPv6Localhost(), port))); |
| 96 | } |
| 97 | histogram_tester.ExpectTotalCount("Net.RestrictedLocalhostPorts", 0); |
| 98 | } |
| 99 | |
eroman | 9ab6484 | 2015-07-21 05:07:52 | [diff] [blame] | 100 | } // namespace net |