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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base_switches.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "net/android/network_change_notifier_delegate_android.h"
#include "net/android/network_library.h"
#include "net/base/features.h"
#include "net/base/network_change_notifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/quic/quic_chromium_client_session.h"
#include "net/test/cert_test_util.h"
#include "net/test/quic_simple_test_server.h"
#include "net/test/test_data_directory.h"
#include "net/third_party/quiche/src/quiche/common/http/http_header_block.h"
namespace content {
namespace {
class ScopedNetworkChangeWaiter
: public net::NetworkChangeNotifier::NetworkChangeObserver {
public:
ScopedNetworkChangeWaiter() {
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
if (!observed_) {
run_loop_.Run();
}
}
~ScopedNetworkChangeWaiter() override {
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
}
private:
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override {
observed_ = true;
run_loop_.Quit();
}
bool observed_ = false;
base::RunLoop run_loop_;
};
void WaitForNetworkChange() {
ScopedNetworkChangeWaiter waiter;
}
} // namespace
class QuicConnectionMigrationTest : public ContentBrowserTest {
public:
QuicConnectionMigrationTest() {
feature_list_.InitWithFeatures(
// Enabled features
{net::features::kQuicMigrationIgnoreDisconnectSignalDuringProbing},
// Disabled features
{});
}
~QuicConnectionMigrationTest() override = default;
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
// Configure mock cert verifier.
auto test_cert =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "quic-chain.pem");
net::CertVerifyResult verify_result;
verify_result.verified_cert = test_cert;
mock_cert_verifier_.mock_cert_verifier()->AddResultForCert(
test_cert, verify_result, net::OK);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
net::NetworkChangeNotifierDelegateAndroid::
EnableNetworkChangeNotifierAutoDetectForTest();
WaitForNetworkChange();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(switches::kOriginToForceQuicOn, "*");
command_line->AppendSwitchASCII(switches::kForceFieldTrials,
"QUIC/Enabled");
mock_cert_verifier_.SetUpCommandLine(command_line);
ASSERT_TRUE(net::QuicSimpleTestServer::Start());
// Set up a test page that fetches a resource.
quiche::HttpHeaderBlock headers;
headers[":status"] = "200";
headers["content-type"] = "text/html";
const std::string body = R"(
<script>
const response = fetch('/simple.txt').then(res => res.text());
</script>
)";
net::QuicSimpleTestServer::AddResponse("/test.html", std::move(headers),
body);
// Configure the resource to be delayed so that we can trigger connection
// migrations while fetching the resource.
net::QuicSimpleTestServer::SetResponseDelay("/simple.txt",
base::Seconds(2));
}
void TearDown() override {
base::ScopedAllowBaseSyncPrimitivesForTesting allow_wait;
net::QuicSimpleTestServer::Shutdown();
ContentBrowserTest::TearDown();
}
protected:
void SetUpInProcessBrowserTestFixture() override {
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void TearDownInProcessBrowserTestFixture() override {
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}
bool EnsureWifiEnabled() {
if (net::NetworkChangeNotifier::IsOffline()) {
return false;
}
std::string wifi_ssid = net::android::GetWifiSSID();
if (!wifi_ssid.empty()) {
return true;
}
net::android::SetWifiEnabledForTesting(true);
WaitForNetworkChange();
wifi_ssid = net::android::GetWifiSSID();
return !wifi_ssid.empty();
}
GURL GetTestPageUrl() {
return net::QuicSimpleTestServer::GetFileURL("/test.html");
}
EvalJsResult ResolveDelayedResponse() { return EvalJs(shell(), "response"); }
private:
base::test::ScopedFeatureList feature_list_;
ContentMockCertVerifier mock_cert_verifier_;
};
// Currently trybots skip this test because trybots disabled real networks.
// TODO(crbug.com/40282869): Run this test once the infra
// supports enabling networks.
IN_PROC_BROWSER_TEST_F(QuicConnectionMigrationTest, Basic) {
if (!EnsureWifiEnabled()) {
GTEST_SKIP() << "This test requires wifi network.";
}
base::HistogramTester histograms;
ASSERT_TRUE(NavigateToURL(shell(), GetTestPageUrl()));
net::android::SetWifiEnabledForTesting(false);
WaitForNetworkChange();
EvalJsResult result = ResolveDelayedResponse();
ASSERT_EQ(result, "Simple Hello from QUIC Server");
FetchHistogramsFromChildProcesses();
ASSERT_GE(histograms.GetBucketCount(
"Net.QuicSession.ConnectionMigration",
net::QuicConnectionMigrationStatus::MIGRATION_STATUS_SUCCESS),
1);
}
// Currently trybots skip this test because trybots disabled real networks.
// TODO(crbug.com/40282869): Run this test once the infra
// supports enabling networks.
IN_PROC_BROWSER_TEST_F(QuicConnectionMigrationTest,
ConnectionCloseDuringMigration) {
if (!EnsureWifiEnabled()) {
GTEST_SKIP() << "This test requires wifi network.";
}
base::HistogramTester histograms;
// This will close all connections in the middle of a connection migration.
net::QuicChromiumClientSession::SetMidMigrationCallbackForTesting(
base::BindLambdaForTesting(
[]() { net::QuicSimpleTestServer::ShutdownDispatcherForTesting(); }));
ASSERT_TRUE(NavigateToURL(shell(), GetTestPageUrl()));
net::android::SetWifiEnabledForTesting(false);
WaitForNetworkChange();
// The subresource fetch should fail because the server closed the connection
// and retry is disabled.
EvalJsResult result = ResolveDelayedResponse();
EXPECT_FALSE(result.error.empty());
EXPECT_EQ(histograms.GetTotalSum("Net.QuicProtocolError.RetryStatus"), 0);
FetchHistogramsFromChildProcesses();
ASSERT_EQ(histograms.GetBucketCount(
"Net.QuicSession.ConnectionMigration",
net::QuicConnectionMigrationStatus::MIGRATION_STATUS_SUCCESS),
0);
}
} // namespace content
|