1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <ostream>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "net/base/completion_once_callback.h"
#include "net/base/elements_upload_data_stream.h"
#include "net/base/ip_address.h"
#include "net/base/test_completion_callback.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/cert/mock_cert_verifier.h"
#include "net/cert/multi_log_ct_verifier.h"
#include "net/dns/mapped_host_resolver.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_network_session.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_server_properties.h"
#include "net/http/http_transaction_test_util.h"
#include "net/http/transport_security_state.h"
#include "net/log/net_log_with_source.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "net/quic/crypto_test_utils_chromium.h"
#include "net/quic/quic_context.h"
#include "net/socket/client_socket_factory.h"
#include "net/ssl/ssl_config_service.h"
#include "net/ssl/test_ssl_config_service.h"
#include "net/test/cert_test_util.h"
#include "net/test/gtest_util.h"
#include "net/test/test_data_directory.h"
#include "net/test/test_with_task_environment.h"
#include "net/third_party/quiche/src/quiche/quic/test_tools/crypto_test_utils.h"
#include "net/third_party/quiche/src/quiche/quic/test_tools/quic_test_utils.h"
#include "net/third_party/quiche/src/quiche/quic/tools/quic_memory_cache_backend.h"
#include "net/tools/quic/quic_simple_server.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/static_http_user_agent_settings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace net {
using test::IsOk;
namespace test {
namespace {
const char kResponseBody[] = "some arbitrary response body";
// Factory for creating HttpTransactions, used by TestTransactionConsumer.
class TestTransactionFactory : public HttpTransactionFactory {
public:
explicit TestTransactionFactory(
const HttpNetworkSessionParams& session_params,
const HttpNetworkSessionContext& session_context)
: session_(std::make_unique<HttpNetworkSession>(session_params,
session_context)) {}
~TestTransactionFactory() override = default;
// HttpTransactionFactory methods
std::unique_ptr<HttpTransaction> CreateTransaction(
RequestPriority priority) override {
return std::make_unique<HttpNetworkTransaction>(priority, session_.get());
}
HttpCache* GetCache() override { return nullptr; }
HttpNetworkSession* GetSession() override { return session_.get(); }
private:
std::unique_ptr<HttpNetworkSession> session_;
};
} // namespace
class QuicEndToEndTest : public ::testing::Test, public WithTaskEnvironment {
protected:
QuicEndToEndTest()
: host_resolver_(CreateResolverImpl()),
ssl_config_service_(
std::make_unique<TestSSLConfigService>(SSLContextConfig())),
proxy_resolution_service_(
ConfiguredProxyResolutionService::CreateDirect()),
auth_handler_factory_(HttpAuthHandlerFactory::CreateDefault()) {
request_.method = "GET";
request_.url = GURL("https://test.example.com/");
request_.load_flags = 0;
request_.traffic_annotation =
net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
session_params_.enable_quic = true;
session_context_.client_socket_factory =
ClientSocketFactory::GetDefaultFactory();
session_context_.quic_context = &quic_context_;
session_context_.host_resolver = &host_resolver_;
session_context_.cert_verifier = &cert_verifier_;
session_context_.transport_security_state = &transport_security_state_;
session_context_.proxy_resolution_service = proxy_resolution_service_.get();
session_context_.ssl_config_service = ssl_config_service_.get();
session_context_.http_user_agent_settings = &http_user_agent_settings_;
session_context_.http_auth_handler_factory = auth_handler_factory_.get();
session_context_.http_server_properties = &http_server_properties_;
CertVerifyResult verify_result;
verify_result.verified_cert =
ImportCertFromFile(GetTestCertsDirectory(), "quic-chain.pem");
cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(),
"test.example.com", verify_result,
OK);
}
// Creates a mock host resolver in which test.example.com
// resolves to localhost.
static std::unique_ptr<MockHostResolver> CreateResolverImpl() {
auto resolver = std::make_unique<MockHostResolver>();
resolver->rules()->AddRule("test.example.com", "127.0.0.1");
return resolver;
}
void SetUp() override {
StartServer();
// Use a mapped host resolver so that request for test.example.com (port 80)
// reach the server running on localhost.
std::string map_rule =
"MAP test.example.com test.example.com:" +
base::NumberToString(server_->server_address().port());
EXPECT_TRUE(host_resolver_.AddRuleFromString(map_rule));
// To simplify the test, and avoid the race with the HTTP request, we force
// QUIC for these requests.
quic_context_.params()->origins_to_force_quic_on.insert(
HostPortPair::FromString("test.example.com:443"));
transaction_factory_ = std::make_unique<TestTransactionFactory>(
session_params_, session_context_);
}
void TearDown() override {}
// Starts the QUIC server listening on a random port.
void StartServer() {
server_address_ = IPEndPoint(IPAddress(127, 0, 0, 1), 0);
server_config_.SetInitialStreamFlowControlWindowToSend(
quic::test::kInitialStreamFlowControlWindowForTest);
server_config_.SetInitialSessionFlowControlWindowToSend(
quic::test::kInitialSessionFlowControlWindowForTest);
server_ = std::make_unique<QuicSimpleServer>(
net::test::ProofSourceForTestingChromium(), server_config_,
server_config_options_, AllSupportedQuicVersions(),
&memory_cache_backend_);
server_->Listen(server_address_);
server_address_ = server_->server_address();
server_->StartReading();
server_started_ = true;
}
// Adds an entry to the cache used by the QUIC server to serve
// responses.
void AddToCache(std::string_view path,
int response_code,
std::string_view response_detail,
std::string_view body) {
memory_cache_backend_.AddSimpleResponse("test.example.com", path,
response_code, body);
}
// Populates |request_body_| with |length_| ASCII bytes.
void GenerateBody(size_t length) {
request_body_.clear();
request_body_.reserve(length);
for (size_t i = 0; i < length; ++i) {
request_body_.append(1, static_cast<char>(32 + i % (126 - 32)));
}
}
// Initializes |request_| for a post of |length| bytes.
void InitializePostRequest(size_t length) {
GenerateBody(length);
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
element_readers.push_back(std::make_unique<UploadBytesElementReader>(
base::as_byte_span(request_body_)));
upload_data_stream_ = std::make_unique<ElementsUploadDataStream>(
std::move(element_readers), 0);
request_.method = "POST";
request_.url = GURL("https://test.example.com/");
request_.upload_data_stream = upload_data_stream_.get();
ASSERT_THAT(request_.upload_data_stream->Init(CompletionOnceCallback(),
NetLogWithSource()),
IsOk());
}
// Checks that |consumer| completed and received |status_line| and |body|.
void CheckResponse(const TestTransactionConsumer& consumer,
const std::string& status_line,
const std::string& body) {
ASSERT_TRUE(consumer.is_done());
ASSERT_THAT(consumer.error(), IsOk());
EXPECT_EQ(status_line, consumer.response_info()->headers->GetStatusLine());
EXPECT_EQ(body, consumer.content());
}
QuicContext quic_context_;
MappedHostResolver host_resolver_;
MockCertVerifier cert_verifier_;
TransportSecurityState transport_security_state_;
std::unique_ptr<TestSSLConfigService> ssl_config_service_;
std::unique_ptr<ProxyResolutionService> proxy_resolution_service_;
std::unique_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
StaticHttpUserAgentSettings http_user_agent_settings_ = {"*", "test-ua"};
HttpServerProperties http_server_properties_;
HttpNetworkSessionParams session_params_;
HttpNetworkSessionContext session_context_;
std::unique_ptr<TestTransactionFactory> transaction_factory_;
std::string request_body_;
std::unique_ptr<UploadDataStream> upload_data_stream_;
HttpRequestInfo request_;
quic::QuicMemoryCacheBackend memory_cache_backend_;
std::unique_ptr<QuicSimpleServer> server_;
IPEndPoint server_address_;
std::string server_hostname_;
quic::QuicConfig server_config_;
quic::QuicCryptoServerConfig::ConfigOptions server_config_options_;
bool server_started_;
bool strike_register_no_startup_period_ = false;
};
TEST_F(QuicEndToEndTest, LargeGetWithNoPacketLoss) {
std::string response(10 * 1024, 'x');
AddToCache(request_.url.PathForRequest(), 200, "OK", response);
TestTransactionConsumer consumer(DEFAULT_PRIORITY,
transaction_factory_.get());
consumer.Start(&request_, NetLogWithSource());
CheckResponse(consumer, "HTTP/1.1 200", response);
}
// crbug.com/559173
#if defined(THREAD_SANITIZER)
TEST_F(QuicEndToEndTest, DISABLED_LargePostWithNoPacketLoss) {
#else
TEST_F(QuicEndToEndTest, LargePostWithNoPacketLoss) {
#endif
InitializePostRequest(1024 * 1024);
AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
TestTransactionConsumer consumer(DEFAULT_PRIORITY,
transaction_factory_.get());
consumer.Start(&request_, NetLogWithSource());
CheckResponse(consumer, "HTTP/1.1 200", kResponseBody);
}
// crbug.com/559173
#if defined(THREAD_SANITIZER)
TEST_F(QuicEndToEndTest, DISABLED_LargePostWithPacketLoss) {
#else
TEST_F(QuicEndToEndTest, LargePostWithPacketLoss) {
#endif
// FLAGS_fake_packet_loss_percentage = 30;
InitializePostRequest(1024 * 1024);
AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
TestTransactionConsumer consumer(DEFAULT_PRIORITY,
transaction_factory_.get());
consumer.Start(&request_, NetLogWithSource());
CheckResponse(consumer, "HTTP/1.1 200", kResponseBody);
}
// crbug.com/536845
#if defined(THREAD_SANITIZER)
TEST_F(QuicEndToEndTest, DISABLED_UberTest) {
#else
TEST_F(QuicEndToEndTest, UberTest) {
#endif
// FLAGS_fake_packet_loss_percentage = 30;
AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
std::vector<std::unique_ptr<TestTransactionConsumer>> consumers;
for (size_t i = 0; i < 100; ++i) {
TestTransactionConsumer* consumer = new TestTransactionConsumer(
DEFAULT_PRIORITY, transaction_factory_.get());
consumers.push_back(base::WrapUnique(consumer));
consumer->Start(&request_, NetLogWithSource());
}
for (const auto& consumer : consumers)
CheckResponse(*consumer.get(), "HTTP/1.1 200", kResponseBody);
}
TEST_F(QuicEndToEndTest, EnableMLKEM) {
// Configure the server to only support ML-KEM.
server_->crypto_config()->set_preferred_groups({SSL_GROUP_X25519_MLKEM768});
AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
TestTransactionConsumer consumer(DEFAULT_PRIORITY,
transaction_factory_.get());
consumer.Start(&request_, NetLogWithSource());
CheckResponse(consumer, "HTTP/1.1 200", kResponseBody);
EXPECT_EQ(consumer.response_info()->ssl_info.key_exchange_group,
SSL_GROUP_X25519_MLKEM768);
}
TEST_F(QuicEndToEndTest, MLKEMDisabled) {
// Disable ML-KEM on the client.
SSLContextConfig config;
config.post_quantum_key_agreement_enabled = false;
ssl_config_service_->UpdateSSLConfigAndNotify(config);
// Configure the server to only support ML-KEM.
server_->crypto_config()->set_preferred_groups({SSL_GROUP_X25519_MLKEM768});
AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
TestTransactionConsumer consumer(DEFAULT_PRIORITY,
transaction_factory_.get());
consumer.Start(&request_, NetLogWithSource());
// Connection should fail because there's no supported group in common between
// client and server.
EXPECT_EQ(consumer.error(), net::ERR_QUIC_PROTOCOL_ERROR);
}
} // namespace test
} // namespace net
|