chromium/content/browser/network/shared_dictionary_browsertest.cc

// 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 <string_view>
#include <tuple>

#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/metrics/statistics_recorder.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/clear_site_data_utils.h"
#include "content/public/browser/client_certificate_delegate.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.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_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "net/base/hash_value.h"
#include "net/base/schemeful_site.h"
#include "net/base/url_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/extras/shared_dictionary/shared_dictionary_usage_info.h"
#include "net/shared_dictionary/shared_dictionary_constants.h"
#include "net/shared_dictionary/shared_dictionary_isolation_key.h"
#include "net/ssl/client_cert_identity.h"
#include "net/ssl/client_cert_identity_test_util.h"
#include "net/ssl/client_cert_store.h"
#include "net/ssl/ssl_private_key.h"
#include "net/ssl/ssl_server_config.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/test_data_directory.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/shared_dictionary_access_observer.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/features.h"

UnorderedElementsAreArray;

namespace content {

namespace {

// The Structured Field sf-binary hash of sha256 of dictionary.
// (content/test/data/shared_dictionary/test.dict and test_dict.html).
constexpr std::string_view kExpectedDictionaryHashBase64 =;
constexpr net::SHA256HashValue kExpectedDictionaryHashValue =;

constexpr std::string_view kUncompressedDataString =;
constexpr std::string_view kErrorInvalidHashString =;
constexpr std::string_view kErrorNoSharedDictionaryAcceptEncodingString =;

constexpr std::string_view kCompressedDataOriginalString =;

// kBrotliCompressedData is generated using the following commands:
// $ echo "This is a test dictionary." > /tmp/dict
// $ echo -n "test(\"This is compressed test data using a test dictionary\");" \
//     > /tmp/data
// $ echo -en '\xffDCB' > /tmp/out.dcb
// $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcb
// $ brotli --stdout -D /tmp/dict /tmp/data >> /tmp/out.dcb
// $ xxd -i /tmp/out.dcb
constexpr uint8_t kBrotliCompressedData[] =;
const std::string kBrotliCompressedDataString =;

// kZstdCompressedData is generated using the following commands:
// $ echo "This is a test dictionary." > /tmp/dict
// $ echo -n "test(\"This is compressed test data using a test dictionary\");" \
//     > /tmp/data
// $ echo -en '\x5e\x2a\x4d\x18\x20\x00\x00\x00' > /tmp/out.dcz
// $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcz
// $ zstd -D /tmp/dict -f -o /tmp/tmp.zstd /tmp/data
// $ cat /tmp/tmp.zstd >> /tmp/out.dcz
// $ xxd -i /tmp/out.dcz
constexpr uint8_t kZstdCompressedData[] =;
const std::string kZstdCompressedDataString =;

constexpr std::string_view kUncompressedDataResultString =;
constexpr std::string_view kCompressedDataResultString =;

constexpr std::string_view kHttpAuthPath =;
const std::string kTestPath =;

class SharedDictionaryAccessObserver : public WebContentsObserver {};

bool WaitForHistogram(const std::string& histogram_name,
                      std::optional<base::TimeDelta> timeout = std::nullopt) {}

enum class FeatureState {};

enum class BrowserType {};
std::string ToString(BrowserType browser_type) {}

enum class FetchType {};

std::string LinkRelCompressionDictionaryScript(const GURL& dictionary_url) {}

std::string LinkRelCompressionDictionaryDocumentHeaderScript(
    const GURL& dictionary_url) {}

std::string LinkRelCompressionDictionarySubresourceHeaderScript(
    const GURL& dictionary_url) {}

std::string FetchDictionaryScript(const GURL& dictionary_url) {}

std::string FetchDictionaryWithSameOriginModeScript(
    const GURL& dictionary_url) {}

std::string FetchDictionaryWithNoCorsModeScript(const GURL& dictionary_url) {}

std::string StartTestDedicatedWorkerScript(const GURL& dictionary_url) {}

std::string StartTestSharedWorkerScript(const GURL& dictionary_url) {}

std::string RegisterTestServiceWorkerScript(const GURL& dictionary_url) {}

std::string FetchTargetDataScript(const GURL& dictionary_url) {}

std::string IframeLoadScript(const GURL& url) {}
std::optional<std::string> GetAvailableDictionary(
    const net::test_server::HttpRequest::HeaderMap& headers) {}

bool HasSharedDictionaryAcceptEncoding(
    const net::test_server::HttpRequest::HeaderMap& headers) {}

// A dummy ContentBrowserClient for testing HTTP Auth.
class DummyAuthContentBrowserClient
    : public ContentBrowserTestContentBrowserClient {};

// A dummy ContentBrowserClient for allowing all certificate errors.
class CertificateErrorAllowingContentBrowserClient
    : public ContentBrowserTestContentBrowserClient {};

// A dummy ContentBrowserClient for setting client certificate.
class DummyClientCertStoreContentBrowserClient
    : public ContentBrowserTestContentBrowserClient {};

class SharedDictionaryBrowserTestBase : public ContentBrowserTest {};

// Tests end to end functionality of "compression dictionary transport" feature
// with fully enabled features.
class SharedDictionaryBrowserTest
    : public SharedDictionaryBrowserTestBase,
      public ::testing::WithParamInterface<BrowserType> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       LinkRelCompressionDictionarySecureContext) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionarySecureContext) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       LinkRelCompressionDictionaryDocumentHeader) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       LinkRelCompressionDictionarySubresourceHeader) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryWithSameOriginMode) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryWithNoCorsMode) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       LinkRelCompressionDictionaryInsecureContext) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryInsecureContext) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryFromDedicatedWorker) {}

#if !BUILDFLAG(IS_ANDROID)
// Shared workers are not supported on Android.
IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryFromSharedWorker) {}
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryFromServiceWorker) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       FetchDictionaryUingIframeNavigation) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginLinkRelCompressionDictionary) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginFetchDictionary) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginLinkRelCompressionDictionaryWithoutACAO) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginFetchDictionaryWithoutACAO) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginFetchDictionaryWithNoCorsMode) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CrossOriginFetchDictionaryUingIframeNavigation) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       SameOriginModeRequestSameOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       NoCorsModeRequestSameOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       NoCorsModeRequestCrossOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CorsModeRequestSameOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CorModeRequestCrossOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CorsModeRequestWithCredentialsSameOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       CorModeRequestWithCredentialsCrossOriginResource) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, Navigation) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       NavigationAfterSameOriginRedirect) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       NavigationAfterCrossOriginRedirect) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, IframeNavigation) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       IframeNavigationAfterSameOriginRedirect) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       IframeNavigationAfterCrossOriginRedirect) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, MatchDestEmptyString) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, MatchDestScript) {}

IN_PROC_BROWSER_TEST_P(
    SharedDictionaryBrowserTest,
    GetUsageInfoAndClearSharedDictionaryCacheForIsolationKey) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, GetTotalSizeAndOrigins) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, GetSharedDictionaryInfo) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, ClearSiteData) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataNavigationCacheDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataNavigationCookiesDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataNavigationStorageDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataFetchCacheDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataFetchCookiesDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataFetchStorageDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataCrossOriginFetchCacheDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataCrossOriginFetchCookiesDirective) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       ClearSiteDataCrossOriginFetchStorageDirective) {}

// TODO(crbug.com/40599527): When we support wildcard directive
// `Clear-Site-Data: "*"", add test for it.

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       DictionaryReadCountForNavigation) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest, RestartWithAuth) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       RestartedAfterCertErrorPageUseSbr) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       RestartWithCertificatePageUseSbr) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       EncodedBodySizeAndDecodedBodySize) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       PreloadSharedDictionaryInfo) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       DoNotPreloadDictionayUnderMemoryPressure) {}

IN_PROC_BROWSER_TEST_P(SharedDictionaryBrowserTest,
                       PreloadedDictionayDiscardedByMemoryPressure) {}

}  // namespace

}  // namespace content