#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/json/values_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/os_crypt/async/browser/key_provider.h"
#include "components/os_crypt/async/browser/os_crypt_async.h"
#include "components/os_crypt/async/browser/test_utils.h"
#include "components/os_crypt/async/common/encryptor.h"
#include "components/os_crypt/async/common/encryptor_features.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_utils.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/simple_url_loader_test_helper.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "net/base/features.h"
#include "net/cookies/cookie_util.h"
#include "net/disk_cache/disk_cache.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_response_headers.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/gtest_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "sandbox/policy/features.h"
#include "services/cert_verifier/public/mojom/cert_verifier_service_factory.mojom.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/cookie_encryption_provider.mojom.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
#include "services/network/test/udp_socket_test_util.h"
#include "sql/database.h"
#include "sql/sql_features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/application_status_listener.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <dbghelp.h>
#include <algorithm>
#include "base/files/memory_mapped_file.h"
#include "base/files/scoped_temp_file.h"
#include "base/rand_util.h"
#include "content/browser/network/network_service_process_tracker_win.h"
#include "sandbox/policy/features.h"
#endif
namespace content {
namespace {
class WebUITestWebUIControllerFactory : public WebUIControllerFactory { … };
class TestWebUIDataSource : public URLDataSource { … };
class NetworkServiceBrowserTest : public ContentBrowserTest { … };
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_WebUIBindingsNoHttp …
#else
#define MAYBE_WebUIBindingsNoHttp …
#endif
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest, MAYBE_WebUIBindingsNoHttp) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest, NoWebUIBindingsHttp) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest,
FileSystemBindingsCorrectOrigin) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest,
SimpleUrlLoader_NoAuthWhenNoWebContents) { … }
#if BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest,
HttpCacheWrittenToDiskOnApplicationStateChange) {
base::ScopedAllowBlockingForTesting allow_blocking;
mojo::Remote<network::mojom::NetworkContext> network_context;
network::mojom::NetworkContextParamsPtr context_params =
network::mojom::NetworkContextParams::New();
context_params->cert_verifier_params = GetCertVerifierParams(
cert_verifier::mojom::CertVerifierCreationParams::New());
context_params->file_paths = network::mojom::NetworkContextFilePaths::New();
context_params->file_paths->http_cache_directory = GetCacheDirectory();
CreateNetworkContextInNetworkService(
network_context.BindNewPipeAndPassReceiver(), std::move(context_params));
network::mojom::URLLoaderFactoryParamsPtr params =
network::mojom::URLLoaderFactoryParams::New();
params->process_id = network::mojom::kBrowserProcessId;
params->automatically_assign_isolation_info = true;
params->is_orb_enabled = false;
params->is_trusted = true;
mojo::Remote<network::mojom::URLLoaderFactory> loader_factory;
network_context->CreateURLLoaderFactory(
loader_factory.BindNewPipeAndPassReceiver(), std::move(params));
LoadURL(embedded_test_server()->GetURL("/cachetime"), loader_factory.get());
int64_t directory_size = base::ComputeDirectorySize(GetCacheIndexDirectory());
LoadURL(embedded_test_server()->GetURL("/cachetime?foo"),
loader_factory.get());
EXPECT_EQ(directory_size,
base::ComputeDirectorySize(GetCacheIndexDirectory()));
base::android::ApplicationStatusListener::NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
base::RunLoop().RunUntilIdle();
FlushNetworkServiceInstanceForTesting();
disk_cache::FlushCacheThreadForTesting();
EXPECT_GT(base::ComputeDirectorySize(GetCacheIndexDirectory()),
directory_size);
}
#endif
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX)
class NetworkConnectionObserver
: public network::NetworkConnectionTracker::NetworkConnectionObserver { … };
class NetworkServiceConnectionTypeSyncedBrowserTest
: public NetworkServiceBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceConnectionTypeSyncedBrowserTest,
ConnectionTypeChangeSyncedToNetworkProcess) { … }
#endif
class NetworkServiceOutOfProcessBrowserTest : public NetworkServiceBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceOutOfProcessBrowserTest,
MemoryPressureSentToNetworkProcess) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceOutOfProcessBrowserTest, SyncXHROnCrash) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceOutOfProcessBrowserTest,
SyncCookieGetOnCrash) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserTest, FactoryOverride) { … }
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
class NetworkServiceBrowserCacheResetTest : public NetworkServiceBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserCacheResetTest,
PRE_PRE_CacheResetTest) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserCacheResetTest,
PRE_CacheResetTest) { … }
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserCacheResetTest, CacheResetTest) { … }
#if BUILDFLAG(IS_POSIX)
IN_PROC_BROWSER_TEST_F(NetworkServiceBrowserCacheResetTest, CacheResetFailure) { … }
#endif
#endif
#if !BUILDFLAG(IS_FUCHSIA)
const base::FilePath::CharType kCheckpointFileName[] = …);
constexpr char kCookieName[] = …;
constexpr char kCookieValue[] = …;
net::CookieList GetCookies(
const mojo::Remote<network::mojom::CookieManager>& cookie_manager) { … }
void SetCookie(
const mojo::Remote<network::mojom::CookieManager>& cookie_manager) { … }
void FlushCookies(
const mojo::Remote<network::mojom::CookieManager>& cookie_manager) { … }
mojo::PendingRemote<network::mojom::NetworkContext>
CreateNetworkContextForPaths(network::mojom::NetworkContextFilePathsPtr paths,
const base::FilePath& cache_path) { … }
enum class FailureType { … };
static const FailureType kFailureTypes[] = …;
static const base::FilePath::CharType kCookieDatabaseName[] = …);
static const base::FilePath::CharType kNetworkSubpath[] = …);
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_NetworkServiceDataMigrationBrowserTest …
#define MAYBE_NetworkServiceDataMigrationBrowserTestWithFailures …
#else
#define MAYBE_NetworkServiceDataMigrationBrowserTest …
#define MAYBE_NetworkServiceDataMigrationBrowserTestWithFailures …
#endif
class MAYBE_NetworkServiceDataMigrationBrowserTest : public ContentBrowserTest { … };
class MAYBE_NetworkServiceDataMigrationBrowserTestWithFailures
: public MAYBE_NetworkServiceDataMigrationBrowserTest,
public ::testing::WithParamInterface<std::tuple<bool, FailureType>> { … };
void MigrationTestInternal(const base::FilePath& tempdir_one,
const base::FilePath& tempdir_two_parent,
FailureType failure_type) { … }
IN_PROC_BROWSER_TEST_P(MAYBE_NetworkServiceDataMigrationBrowserTestWithFailures,
MigrateDataTest) { … }
IN_PROC_BROWSER_TEST_F(MAYBE_NetworkServiceDataMigrationBrowserTest,
MigrateThenNoMigrate) { … }
IN_PROC_BROWSER_TEST_F(MAYBE_NetworkServiceDataMigrationBrowserTest,
NewDataDirWithMigrationTest) { … }
IN_PROC_BROWSER_TEST_F(MAYBE_NetworkServiceDataMigrationBrowserTest,
NewDataDirWithNoMigrationTest) { … }
IN_PROC_BROWSER_TEST_F(MAYBE_NetworkServiceDataMigrationBrowserTest,
LegacyDataDir) { … }
IN_PROC_BROWSER_TEST_F(MAYBE_NetworkServiceDataMigrationBrowserTest,
MigratedPreviouslyAndMigrateAgain) { … }
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_InProcess …
#define MAYBE_OutOfProcess …
#else
#define MAYBE_InProcess …
#define MAYBE_OutOfProcess …
#endif
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
#endif
class NetworkServiceInProcessBrowserTest : public ContentBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceInProcessBrowserTest, Basic) { … }
class NetworkServiceInvalidLogBrowserTest : public ContentBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceInvalidLogBrowserTest, Basic) { … }
class NetworkServiceWithUDPSocketLimit : public NetworkServiceBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceWithUDPSocketLimit,
UDPSocketBoundEnforced) { … }
class NetworkServiceNetLogBrowserTest : public ContentBrowserTest { … };
IN_PROC_BROWSER_TEST_F(NetworkServiceNetLogBrowserTest, LogCreated) { … }
class NetworkServiceBoundedNetLogBrowserTest
: public NetworkServiceNetLogBrowserTest { … };
#if BUILDFLAG(IS_APPLE)
#define MAYBE_LogCreated …
#else
#define MAYBE_LogCreated …
#endif
IN_PROC_BROWSER_TEST_F(NetworkServiceBoundedNetLogBrowserTest,
MAYBE_LogCreated) { … }
class TestCookieEncryptionProvider
: public network::mojom::CookieEncryptionProvider { … };
class NetworkServiceCookieEncryptionBrowserTest
: public ContentBrowserTest,
public testing::WithParamInterface< bool> { … };
IN_PROC_BROWSER_TEST_P(NetworkServiceCookieEncryptionBrowserTest,
CookieEncryptionProvider) { … }
INSTANTIATE_TEST_SUITE_P(…);
#if BUILDFLAG(IS_WIN)
class NetworkServiceCodeIntegrityTest : public NetworkServiceBrowserTest {
public:
NetworkServiceCodeIntegrityTest() {
scoped_feature_list_.InitWithFeatures(
{sandbox::policy::features::kNetworkServiceCodeIntegrity,
sandbox::policy::features::kNetworkServiceSandbox},
{});
ForceOutOfProcessNetworkService();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(NetworkServiceCodeIntegrityTest, Enabled) {
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/empty.html")));
}
#endif
}
}