chromium/chrome/browser/net/chrome_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 <optional>
#include <string>
#include <string_view>
#include <utility>

#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/browsing_data/counters/site_data_counting_helper.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/browsing_data/core/counters/browsing_data_counter.h"
#include "components/content_settings/core/browser/cookie_settings.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/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_devtools_protocol_client.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/test/embedded_test_server/controllable_http_response.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/shared_dictionary_access_observer.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"
#include "url/url_constants.h"

namespace {

constexpr std::string_view kTestDictionaryString =;

constexpr std::string_view kCompressedDataOriginalString =;

// kBrotliCompressedData is generated using the following commands:
// $ echo -n "A dictionary" > /tmp/dict
// $ echo -n "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[] =;

// NOLINTNEXTLINE(runtime/string)
const std::string kBrotliCompressedDataString =;

// kZstdCompressedData is generated using the following commands:
// $ echo -n "A dictionary" > /tmp/dict
// $ echo -n "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[] =;

// NOLINTNEXTLINE(runtime/string)
const std::string kZstdCompressedDataString =;

class SharedDictionaryAccessObserver : public content::WebContentsObserver {};

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

void CheckSharedDictionaryUseCounter(
    base::HistogramTester& histograms,
    int expected_used_count_with_sbr,
    int expected_used_count_with_zstd_d,
    int expected_used_for_navigation_count,
    int expected_used_for_main_frame_navigation_count,
    int expected_used_for_sub_frame_navigation_count,
    int expected_used_for_subresource_count) {}

std::string FetchUrlScript(const GURL& url) {}

std::string FetchUrlWithNoCorsModeScript(const GURL& url) {}

}  // namespace

// `ChromeSharedDictionaryBrowserTest` is required to test Chrome
// specific code such as Site Settings.
// See `SharedDictionaryBrowserTest` for content's version of tests.
class ChromeSharedDictionaryBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest, BlockWriting) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       BlockWritingCrossOrigin) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest, BlockReading) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       BlockReadingCrossOrigin) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       BlockReadingWhileNavigation) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       BlockReadingWhileIframeNavigation) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       UseCounterMainFrameNavigation) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       UseCounterSubFrameNavigation) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       UseCounterSubresource) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       UseCounterZstdMainFrameNavigation) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest,
                       UseCounterZstdSubresource) {}

IN_PROC_BROWSER_TEST_F(ChromeSharedDictionaryBrowserTest, SiteDataCount) {}

class SharedDictionaryDevToolsBrowserTest
    : public InProcessBrowserTest,
      public content::TestDevToolsProtocolClient {};

class DevToolsSharedDictionaryFeatureDisabledBrowserTest
    : public SharedDictionaryDevToolsBrowserTest {};

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       UseErrorCrossOriginNoCorsRequest) {}

// Can't cause the dictionary load failure by deletaing the disk cache directory
// on Windows.
#if !BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       UseErrorDictionaryLoadFailure) {}
#endif  // !BUILDFLAG(IS_WIN)

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       UseErrorMatchingDictionaryNotUsed) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       UseErrorUnexpectedContentDictionaryHeader) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorCossOriginNoCorsRequest) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorDisallowedBySettings) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorExpiredResponse) {}

IN_PROC_BROWSER_TEST_F(DevToolsSharedDictionaryFeatureDisabledBrowserTest,
                       WriteErrorFeatureDisabled) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorInvalidMatchField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorInvalidStructuredHeader) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNavigationRequest) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNoMatchField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonListMatchDestField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonSecureContext) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonStringIdField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonStringInMatchDestList) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonStringMatchField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorNonTokenTypeField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorRequestAborted) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorTooLongIdField) {}

IN_PROC_BROWSER_TEST_F(SharedDictionaryDevToolsBrowserTest,
                       WriteErrorUnsupportedType) {}