blob: 0523bd9cc4089e09a14fa5d174cd898367af486f [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
eroman9ab64842015-07-21 05:07:522// 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 Sonzogni46716872024-12-12 12:00:047#include <array>
eroman9ab64842015-07-21 05:07:528#include <string>
9
Tom Van Goethemd39f48c2025-05-02 12:03:3710#include "base/test/metrics/histogram_tester.h"
Tom Van Goethem8f760752025-04-24 01:28:5311#include "base/test/scoped_feature_list.h"
12#include "net/base/features.h"
Tom Van Goethem59535072025-05-09 11:07:2913#include "net/base/ip_address.h"
eroman9ab64842015-07-21 05:07:5214#include "testing/gtest/include/gtest/gtest.h"
15
16namespace net {
17
18TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) {
Arthur Sonzogni46716872024-12-12 12:00:0419 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 });
eroman9ab64842015-07-21 05:07:5226
Daniel Cheng5feb16f2022-02-28 06:52:0727 for (size_t i = 0; i < std::size(valid); ++i) {
eroman9ab64842015-07-21 05:07:5228 SetExplicitlyAllowedPorts(valid[i]);
29 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts());
30 }
31}
32
Tom Van Goethem8f760752025-04-24 01:28:5333TEST(NetUtilTest, RestrictedAbusePortsTest) {
Tom Van Goethemd39f48c2025-05-02 12:03:3734 base::HistogramTester histogram_tester;
Tom Van Goethem8f760752025-04-24 01:28:5335 base::test::ScopedFeatureList feature_list;
36 feature_list.InitAndEnableFeatureWithParameters(
37 features::kRestrictAbusePorts,
Tom Van Goethemd39f48c2025-05-02 12:03:3738 {{"restrict_ports", "12345,23456,34567"}, {"monitor_ports", "45678"}});
Tom Van Goethem8f760752025-04-24 01:28:5339 EXPECT_TRUE(IsPortAllowedForScheme(443, "https"));
40 for (int port : {12345, 23456, 34567}) {
41 EXPECT_FALSE(IsPortAllowedForScheme(port, "https"));
42 }
Tom Van Goethemd39f48c2025-05-02 12:03:3743 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 Goethem8f760752025-04-24 01:28:5349}
50
Tom Van Goethem59535072025-05-09 11:07:2951TEST(NetUtilTest, RestrictedAbusePortsLocalhostTest) {
Tom Van Goethemeeb1f9d82025-05-29 10:25:1752 base::HistogramTester histogram_tester;
Tom Van Goethem59535072025-05-09 11:07:2953 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 Goethemeeb1f9d82025-05-29 10:25:1765 histogram_tester.ExpectTotalCount("Net.RestrictedLocalhostPorts", 0);
Tom Van Goethem59535072025-05-09 11:07:2966 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 Goethemeeb1f9d82025-05-29 10:25:1772 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 Goethem59535072025-05-09 11:07:2976}
77
Tom Van Goethemd9774012025-05-30 23:10:3978TEST(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
eroman9ab64842015-07-21 05:07:52100} // namespace net