blob: bfebf559446f85a062876a6be2ca29756a04d947 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/profile_network_context_service.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/net/profile_network_context_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths_internal.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/simple_url_loader_test_helper.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/base/load_flags.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
enum class NetworkServiceState {
kDisabled,
kEnabled,
};
// Most tests for this class are in NetworkContextConfigurationBrowserTest.
class ProfileNetworkContextServiceBrowsertest
: public InProcessBrowserTest,
public testing::WithParamInterface<NetworkServiceState> {
public:
ProfileNetworkContextServiceBrowsertest() {
EXPECT_TRUE(embedded_test_server()->Start());
}
~ProfileNetworkContextServiceBrowsertest() override {}
void SetUpInProcessBrowserTestFixture() override {
if (GetParam() == NetworkServiceState::kEnabled)
feature_list_.InitAndEnableFeature(network::features::kNetworkService);
}
void SetUpOnMainThread() override {
loader_factory_ = content::BrowserContext::GetDefaultStoragePartition(
browser()->profile())
->GetURLLoaderFactoryForBrowserProcess()
.get();
}
network::mojom::URLLoaderFactory* loader_factory() const {
return loader_factory_;
}
private:
base::test::ScopedFeatureList feature_list_;
network::mojom::URLLoaderFactory* loader_factory_ = nullptr;
};
IN_PROC_BROWSER_TEST_P(ProfileNetworkContextServiceBrowsertest,
DiskCacheLocation) {
// Run a request that caches the response, to give the network service time to
// create a cache directory.
std::unique_ptr<network::ResourceRequest> request =
std::make_unique<network::ResourceRequest>();
request->url = embedded_test_server()->GetURL("/cachetime");
content::SimpleURLLoaderTestHelper simple_loader_helper;
std::unique_ptr<network::SimpleURLLoader> simple_loader =
network::SimpleURLLoader::Create(std::move(request),
TRAFFIC_ANNOTATION_FOR_TESTS);
simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
loader_factory(), simple_loader_helper.GetCallback());
simple_loader_helper.WaitForCallback();
ASSERT_TRUE(simple_loader_helper.response_body());
base::FilePath expected_cache_path;
chrome::GetUserCacheDirectory(browser()->profile()->GetPath(),
&expected_cache_path);
expected_cache_path = expected_cache_path.Append(chrome::kCacheDirname);
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathExists(expected_cache_path));
}
IN_PROC_BROWSER_TEST_P(ProfileNetworkContextServiceBrowsertest, BrotliEnabled) {
// Brotli is only used over encrypted connections.
net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
https_server.AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("content/test/data")));
ASSERT_TRUE(https_server.Start());
std::unique_ptr<network::ResourceRequest> request =
std::make_unique<network::ResourceRequest>();
request->url = https_server.GetURL("/echoheader?accept-encoding");
// On OSX, test certs aren't currently hooked up correctly when using the
// network service.
// TODO(mmenke): Remove this line once that's fixed.
#if defined(OS_MACOSX)
request->load_flags |= net::LOAD_IGNORE_ALL_CERT_ERRORS;
#endif // !defined(OS_MACOSX)
content::SimpleURLLoaderTestHelper simple_loader_helper;
std::unique_ptr<network::SimpleURLLoader> simple_loader =
network::SimpleURLLoader::Create(std::move(request),
TRAFFIC_ANNOTATION_FOR_TESTS);
simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
loader_factory(), simple_loader_helper.GetCallback());
simple_loader_helper.WaitForCallback();
ASSERT_TRUE(simple_loader_helper.response_body());
std::vector<std::string> encodings =
base::SplitString(*simple_loader_helper.response_body(), ",",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
EXPECT_TRUE(base::ContainsValue(encodings, "br"));
}
// Test subclass that adds switches::kDiskCacheDir to the command line, to make
// sure it's respected.
class ProfileNetworkContextServiceDiskCacheDirBrowsertest
: public ProfileNetworkContextServiceBrowsertest {
public:
ProfileNetworkContextServiceDiskCacheDirBrowsertest() {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
}
~ProfileNetworkContextServiceDiskCacheDirBrowsertest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchPath(switches::kDiskCacheDir,
temp_dir_.GetPath());
}
const base::FilePath& TempPath() { return temp_dir_.GetPath(); }
private:
base::ScopedTempDir temp_dir_;
};
// Makes sure switches::kDiskCacheDir is hooked up correctly.
IN_PROC_BROWSER_TEST_P(ProfileNetworkContextServiceDiskCacheDirBrowsertest,
DiskCacheLocation) {
// Make sure command line switch is hooked up to the pref.
ASSERT_EQ(TempPath(), browser()->profile()->GetPrefs()->GetFilePath(
prefs::kDiskCacheDir));
// Run a request that caches the response, to give the network service time to
// create a cache directory.
std::unique_ptr<network::ResourceRequest> request =
std::make_unique<network::ResourceRequest>();
request->url = embedded_test_server()->GetURL("/cachetime");
content::SimpleURLLoaderTestHelper simple_loader_helper;
std::unique_ptr<network::SimpleURLLoader> simple_loader =
network::SimpleURLLoader::Create(std::move(request),
TRAFFIC_ANNOTATION_FOR_TESTS);
simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
loader_factory(), simple_loader_helper.GetCallback());
simple_loader_helper.WaitForCallback();
ASSERT_TRUE(simple_loader_helper.response_body());
// Cache directory should now exist.
base::FilePath expected_cache_path =
TempPath()
.Append(browser()->profile()->GetPath().BaseName())
.Append(chrome::kCacheDirname);
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathExists(expected_cache_path));
}
INSTANTIATE_TEST_CASE_P(
/* No test prefix */,
ProfileNetworkContextServiceBrowsertest,
::testing::Values(NetworkServiceState::kDisabled,
NetworkServiceState::kEnabled));
INSTANTIATE_TEST_CASE_P(
/* No test prefix */,
ProfileNetworkContextServiceDiskCacheDirBrowsertest,
::testing::Values(NetworkServiceState::kDisabled,
NetworkServiceState::kEnabled));
} // namespace