Commit 7ae5d573 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Add browsertest verifying that UDP socket limit is enforced by Network Service.

This also moves udp_socket_test_util.cc into the test/ subdirectory, in order to satisfy existing DEPS rules.

Bug: 1083278
Change-Id: I45fd4ceb8b46e0464ff0cd2a41a897efa294f64b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2360125Reviewed-by: default avatarDavid Schinazi <dschinazi@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Auto-Submit: Eric Roman <eroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799396}
parent 55ffc0a7
......@@ -29,6 +29,7 @@
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/features.h"
#include "net/disk_cache/disk_cache.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_response_headers.h"
......@@ -40,6 +41,7 @@
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
#include "services/network/test/udp_socket_test_util.h"
#if defined(OS_ANDROID)
#include "base/android/application_status_listener.h"
......@@ -598,6 +600,91 @@ IN_PROC_BROWSER_TEST_F(NetworkServiceWithCorsBrowserTest, FactoryOverride) {
EXPECT_TRUE(test_loader_factory->has_received_request());
}
// Test fixture for using a NetworkService that has a non-default limit on the
// number of allowed open UDP sockets.
class NetworkServiceWithUDPSocketLimit : public NetworkServiceBrowserTest {
public:
NetworkServiceWithUDPSocketLimit() {
base::FieldTrialParams params;
params[net::features::kLimitOpenUDPSocketsMax.name] =
base::NumberToString(kMaxUDPSockets);
scoped_feature_list_.InitAndEnableFeatureWithParameters(
net::features::kLimitOpenUDPSockets, params);
}
protected:
static constexpr int kMaxUDPSockets = 4;
// Creates and synchronously connects a UDPSocket using |network_context|.
// Returns the network error for Connect().
int ConnectUDPSocketSync(
mojo::Remote<network::mojom::NetworkContext>* network_context,
mojo::Remote<network::mojom::UDPSocket>* socket) {
network_context->get()->CreateUDPSocket(
socket->BindNewPipeAndPassReceiver(), mojo::NullRemote());
// The address of this endpoint doesn't matter, since Connect() will not
// actually send any datagrams, and is only being called to verify the
// socket limit enforcement.
net::IPEndPoint remote_addr(net::IPAddress(127, 0, 0, 1), 8080);
network::mojom::UDPSocketOptionsPtr options =
network::mojom::UDPSocketOptions::New();
net::IPEndPoint local_addr;
network::test::UDPSocketTestHelper helper(socket);
return helper.ConnectSync(remote_addr, std::move(options), &local_addr);
}
// Creates a NetworkContext using default parameters.
mojo::Remote<network::mojom::NetworkContext> CreateNetworkContext() {
mojo::Remote<network::mojom::NetworkContext> network_context;
network::mojom::NetworkContextParamsPtr context_params =
network::mojom::NetworkContextParams::New();
GetNetworkService()->CreateNetworkContext(
network_context.BindNewPipeAndPassReceiver(),
std::move(context_params));
return network_context;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests calling Connect() on |kMaxUDPSockets + 4| sockets. The first
// kMaxUDPSockets should succeed, whereas the last 4 should fail with
// ERR_INSUFFICIENT_RESOURCES due to having exceeding the global bound.
IN_PROC_BROWSER_TEST_F(NetworkServiceWithUDPSocketLimit,
UDPSocketBoundEnforced) {
constexpr size_t kNumContexts = 2;
mojo::Remote<network::mojom::NetworkContext> network_contexts[kNumContexts] =
{CreateNetworkContext(), CreateNetworkContext()};
mojo::Remote<network::mojom::UDPSocket> sockets[kMaxUDPSockets];
// Try to connect the maximum number of UDP sockets (|kMaxUDPSockets|),
// spread evenly between 2 NetworkContexts. These should succeed as the
// global limit has not been reached yet. This assumes there are no
// other consumers of UDP sockets in the browser yet.
for (size_t i = 0; i < kMaxUDPSockets; ++i) {
auto* network_context = &network_contexts[i % kNumContexts];
EXPECT_EQ(net::OK, ConnectUDPSocketSync(network_context, &sockets[i]));
}
// Try to connect an additional 4 sockets, alternating between each of the
// NetworkContexts. These should all fail with ERR_INSUFFICIENT_RESOURCES as
// the limit has already been reached. Spreading across NetworkContext
// is done to ensure the socket limit is global and not per
// NetworkContext.
for (size_t i = 0; i < 4; ++i) {
auto* network_context = &network_contexts[i % kNumContexts];
mojo::Remote<network::mojom::UDPSocket> socket;
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
ConnectUDPSocketSync(network_context, &socket));
}
}
} // namespace
} // namespace content
......@@ -440,8 +440,8 @@ source_set("test_support") {
"test/test_url_loader_factory.h",
"test/test_utils.cc",
"test/test_utils.h",
"udp_socket_test_util.cc",
"udp_socket_test_util.h",
"test/udp_socket_test_util.cc",
"test/udp_socket_test_util.h",
]
if (is_chromeos) {
......
......@@ -123,11 +123,11 @@
#include "services/network/public/mojom/url_loader.mojom-shared.h"
#include "services/network/test/fake_test_cert_verifier_params_factory.h"
#include "services/network/test/test_url_loader_client.h"
#include "services/network/test/udp_socket_test_util.h"
#include "services/network/test_mojo_proxy_resolver_factory.h"
#include "services/network/trust_tokens/pending_trust_token_store.h"
#include "services/network/trust_tokens/trust_token_parameterization.h"
#include "services/network/trust_tokens/trust_token_store.h"
#include "services/network/udp_socket_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/udp_socket_test_util.h"
#include "services/network/test/udp_socket_test_util.h"
#include <utility>
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
#define SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
#ifndef SERVICES_NETWORK_TEST_UDP_SOCKET_TEST_UTIL_H_
#define SERVICES_NETWORK_TEST_UDP_SOCKET_TEST_UTIL_H_
#include <stdint.h>
......@@ -85,4 +85,4 @@ class UDPSocketListenerImpl : public mojom::UDPSocketListener {
} // namespace network
#endif // SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
#endif // SERVICES_NETWORK_TEST_UDP_SOCKET_TEST_UTIL_H_
......@@ -26,7 +26,7 @@
#include "net/url_request/url_request_test_util.h"
#include "services/network/public/mojom/udp_socket.mojom.h"
#include "services/network/socket_factory.h"
#include "services/network/udp_socket_test_util.h"
#include "services/network/test/udp_socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment