chromium/components/web_package/web_bundle_parser_unittest.cc

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/web_package/web_bundle_parser.h"

#include <optional>
#include <string_view>

#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/ranges/algorithm.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "components/cbor/writer.h"
#include "components/web_package/mojom/web_bundle_parser.mojom-forward.h"
#include "components/web_package/mojom/web_bundle_parser.mojom.h"
#include "components/web_package/signed_web_bundles/ed25519_public_key.h"
#include "components/web_package/signed_web_bundles/integrity_block_attributes.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "components/web_package/test_support/signed_web_bundles/ed25519_key_pair.h"
#include "components/web_package/test_support/signed_web_bundles/web_bundle_signer.h"
#include "components/web_package/web_bundle_builder.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace web_package {

namespace {

constexpr char kPrimaryUrl[] =;

std::string GetTestFileContents(const base::FilePath& path) {}

class TestDataSource : public mojom::BundleDataSource {};

template <typename... T>
auto to_pair(std::tuple<T...>&& t)
    -> decltype(std::make_pair(std::get<0>(t), std::get<1>(t))) {}

ParseSignedBundleIntegrityBlockResult;

ParseSignedBundleIntegrityBlockResult ParseSignedBundleIntegrityBlock(
    TestDataSource* data_source,
    const GURL& base_url = GURL()) {}

ParseUnsignedBundleResult;

ParseUnsignedBundleResult ParseUnsignedBundle(
    TestDataSource* data_source,
    const GURL& base_url = GURL(),
    std::optional<uint64_t> offset = std::nullopt) {}

void ExpectFormatError(ParseUnsignedBundleResult result) {}

// Finds the response for |url|.
mojom::BundleResponseLocationPtr FindResponse(
    const mojom::BundleMetadataPtr& metadata,
    const GURL& url) {}

mojom::BundleResponsePtr ParseResponse(
    TestDataSource* data_source,
    const mojom::BundleResponseLocationPtr& location,
    const GURL& base_url = GURL()) {}

std::vector<uint8_t> CreateSmallBundle() {}

struct SignedWebBundleAndKeys {};

SignedWebBundleAndKeys SignBundle(
    const std::vector<uint8_t>& unsigned_bundle,
    std::optional<test::WebBundleSigner::IntegrityBlockAttributes>
        ib_attributes,
    test::WebBundleSigner::ErrorsForTesting errors_for_testing = {}

SignedWebBundleAndKeys SignBundle(
    const std::vector<uint8_t>& unsigned_bundle,
    test::WebBundleSigner::ErrorsForTesting errors_for_testing = {}

void CheckIfSignatureStackEntryIsValid(
    const mojom::BundleIntegrityBlockSignatureStackEntryPtr& entry,
    const Ed25519PublicKey& public_key) {}

}  // namespace

ErrorIs;
AllOf;
Eq;
Field;
HasSubstr;
Optional;
Pointee;
Property;

class WebBundleParserTest : public testing::Test {};

TEST_F(WebBundleParserTest, WrongMagic) {}

TEST_F(WebBundleParserTest, UnknownVersion) {}

TEST_F(WebBundleParserTest, SectionLengthsTooLarge) {}

TEST_F(WebBundleParserTest, DuplicateSectionName) {}

TEST_F(WebBundleParserTest, InvalidRequestURL) {}

TEST_F(WebBundleParserTest, RequestURLIsNotUTF8) {}

// TODO(crbug.com/40629011): Revisit this once
// https://github.com/WICG/webpackage/issues/468 is resolved.
TEST_F(WebBundleParserTest, RequestURLHasNonStandardScheme) {}

TEST_F(WebBundleParserTest, RequestURLHasIsolatedAppScheme) {}

TEST_F(WebBundleParserTest, RequestURLHasCredentials) {}

TEST_F(WebBundleParserTest, RequestURLHasFragment) {}

TEST_F(WebBundleParserTest, RequestURLIsValidUuidInPackage) {}

TEST_F(WebBundleParserTest, NoStatusInResponseHeaders) {}

TEST_F(WebBundleParserTest, InvalidResponseStatus) {}

TEST_F(WebBundleParserTest, ExtraPseudoInResponseHeaders) {}

TEST_F(WebBundleParserTest, UpperCaseCharacterInHeaderName) {}

TEST_F(WebBundleParserTest, InvalidHeaderValue) {}

TEST_F(WebBundleParserTest, NoContentTypeWithNonEmptyContent) {}

TEST_F(WebBundleParserTest, NoContentTypeWithEmptyContent) {}

TEST_F(WebBundleParserTest, AllKnownSectionInCritical) {}

TEST_F(WebBundleParserTest, UnknownSectionInCritical) {}

TEST_F(WebBundleParserTest, ParseGoldenFile) {}

TEST_F(WebBundleParserTest, SingleEntry) {}

TEST_F(WebBundleParserTest, NoPrimaryUrlSingleEntry) {}

TEST_F(WebBundleParserTest, RelativeURL) {}

TEST_F(WebBundleParserTest, RandomAccessContextWithAutomaticOffset) {}

TEST_F(WebBundleParserTest, RandomAccessContextWithFixedCorrectOffset) {}

TEST_F(WebBundleParserTest, RandomAccessContextWithFixedIncorrectOffset) {}

TEST_F(WebBundleParserTest,
       RandomAccessContextPrependedDataWithAutomaticOffset) {}

TEST_F(WebBundleParserTest,
       RandomAccessContextPrependedDataWithFixedCorrectOffset) {}

TEST_F(WebBundleParserTest,
       RandomAccessContextPrependedDataWithFixedIncorrectOffset) {}

TEST_F(WebBundleParserTest, RandomAccessContextLengthSmallerThanWebBundle) {}

TEST_F(WebBundleParserTest, RandomAccessContextFileSmallerThanLengthField) {}

TEST_F(WebBundleParserTest, RandomAccessContextLengthBiggerThanFile) {}

// TODO(crbug.com/40630324): Add a test case that loads a wbn file with
// variants, once gen-bundle supports variants.

// This test verifies that even if a bundle is signed, it is still readable as
// an unsigned bundle in random-access contexts, since the `length` field of the
// web bundle can be used to find the start of the unsigned bundle.
TEST_F(WebBundleParserTest, SignedBundleMetadataOnlyInRandomAccessContexts) {}

// This test verifies that when a bundle is signed, it can not be read as an
// unsigned bundle in non-random-access contexts, since the `length` field of
// the web bundle can't be used then.
TEST_F(WebBundleParserTest, SignedBundleMetadataOnlyInNonRandomAccessContexts) {}

TEST_F(WebBundleParserTest, SignedBundleIntegrityBlockIsParsedCorrectly) {}

TEST_F(WebBundleParserTest,
       SignedBundleSignatureStackWithMultipleEntries_AllValid) {}

TEST_F(WebBundleParserTest,
       SignedBundleSignatureStackWithMultipleEntries_SomeInvalid) {}

TEST_F(WebBundleParserTest,
       SignedBundleSignatureStackWithMultipleEntries_FirstInvalid) {}

TEST_F(WebBundleParserTest, SignedBundleWrongMagic) {}

TEST_F(WebBundleParserTest, SignedBundleUnknownVersion) {}

TEST_F(WebBundleParserTest, SignedBundleEmptySignatureStack) {}

TEST_F(WebBundleParserTest, SignedBundleNoBundleId) {}

TEST_F(WebBundleParserTest, SignedBundleNoAttributes) {}

TEST_F(WebBundleParserTest, SignedBundleWrongSignatureLength) {}

TEST_F(WebBundleParserTest, SignedBundleWrongSignatureStackEntryLength) {}

TEST_F(WebBundleParserTest, SignedBundleWithMultipleAttributes) {}

TEST_F(WebBundleParserTest, SignedBundleV2) {}

TEST_F(WebBundleParserTest, SignedBundleWithMultiplePublicKeyAttributes) {}

TEST_F(WebBundleParserTest, SignedBundleUnsupportedSignatureAttributeMap) {}

TEST_F(WebBundleParserTest, SignedBundleUnsupportedSignatureAttributeArray) {}

TEST_F(WebBundleParserTest, SignedBundleNoPublicKeyAttribute) {}

TEST_F(WebBundleParserTest, SignedBundleWrongPublicKeyAttributeName) {}

TEST_F(WebBundleParserTest, SignedBundleWrongPublicKeyAttributeLength) {}

TEST_F(WebBundleParserTest, SignedBundleWrongPublicKeyLength) {}

TEST_F(WebBundleParserTest, DisconnectWhileParsingMetadata) {}

TEST_F(WebBundleParserTest, DisconnectWhileParsingResponse) {}

// This data source implementation never run result callback
// making the calls to it permanently pending.
class BlockingDataSource : public mojom::BundleDataSource {};

TEST_F(WebBundleParserTest, DestructorWhileParsing) {}

TEST_F(WebBundleParserTest, Close) {}

}  // namespace web_package