chromium/services/network/prefetch_url_loader_client_unittest.cc

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

#include "services/network/prefetch_url_loader_client.h"

#include <stdint.h>

#include <type_traits>
#include <utility>

#include "base/functional/bind.h"
#include "base/ranges/algorithm.h"
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/base/isolation_info.h"
#include "net/base/network_isolation_key.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_status_code.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/referrer_policy.h"
#include "services/network/prefetch_cache.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/test/mock_url_loader_client.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace network {

namespace {

_;
Eq;
InSequence;
Invoke;
IsTrue;
MockFunction;
NotNull;
Optional;
Pointee;
StrictMock;
WithArg;

constexpr uint64_t kTestCurrentPosition =;
constexpr uint64_t kTestTotalSize =;
constexpr int32_t kTransferSizeDiff =;
constexpr size_t kDataPipeCapacity =;
constexpr char kResponseBody[] =;
constexpr size_t kBigBufferSize =;

// Returns a URL for our tests to use. The actual value isn't important.
GURL TestURL() {}

// Returns an origin matching our test URL.
url::Origin TestOrigin() {}

// Returns an IsolationInfo object matching our test URL.
net::IsolationInfo TestIsolationInfo() {}

// Returns a NetworkIsolationKey object matching our test URL.
net::NetworkIsolationKey TestNIK() {}

// Returns a ResourceRequest matching our test URL.
ResourceRequest TestRequest() {}

// Returns a dummy value for EarlyHints.
mojom::EarlyHintsPtr TestEarlyHints() {}

// Returns a successfully URLResponseHead.
mojom::URLResponseHeadPtr TestURLResponseHead() {}

// A GMock matcher for the URLResponseHead returned by TestURLResponseHead().
MATCHER(URLResponseHeadIsOk, "URLResponseHead is ok") {}

// Returns a ScopedDataPipeConsumerHandle for use in tests. Reading from the
// pipe will give `kResponseBody`.
mojo::ScopedDataPipeConsumerHandle TestDataPipeConsumer() {}

// A function which will cause the test to fail if `consumer` is not a data pipe
// that yields kResponseBody. Blocks until the data pipe is completely read.
void CheckDataPipeContents(mojo::ScopedDataPipeConsumerHandle consumer) {}

// Creates a BigBuffer suitable for use in tests. The contents are sufficiently
// unique that there shouldn't be an accidental match.
mojo_base::BigBuffer TestBigBuffer() {}

// Verifies that a BigBuffer object matches the one created by TestBigBuffer().
MATCHER(BigBufferHasExpectedContents,
        "does the BigBuffer have the right contents") {}

// Returns a RedirectInfo object that is useful for use in tests. It is
// same-origin with the URL returned by TestURL().
net::RedirectInfo TestRedirectInfo() {}

// Returns true if two SiteForCookies objects match.
bool Equals(const net::SiteForCookies& lhs, const net::SiteForCookies& rhs) {}

// Returns true if a RedirectInfo object matches the one created by
// TestRedirectInfo().
MATCHER(EqualsTestRedirectInfo, "equals test RedirectInfo") {}

// Returns a successful URLLoaderCompletionStatus.
URLLoaderCompletionStatus TestURLLoaderCompletionStatus() {}

// A GMock matcher that Verifies that a URLLoaderCompletionStatus matches the
// one returned by TestURLLoaderCompletionStatus().
MATCHER(URLLoaderCompletionStatusIsOk, "URLLoaderCompletionStatus is ok") {}

// Calls all the mojo methods on `client` in order with verifiable parameters.
// This doesn't in any way correspond to the real behaviour of a URLLoader.
void CallAllMojoMethods(mojom::URLLoaderClient* client) {}

// This adds expectations that all the methods on `client` will be called with
// arguments matching those in `CallAllMojoMethods()`.
void ExpectCallMojoMethods(StrictMock<MockURLLoaderClient>& mock_client) {}

// A wrapper for a mojo::Receiver that calls Call() on `disconnect` when the
// pending remote is disconnected.
class DisconnectDetectingReceiver final {};

class PrefetchURLLoaderClientTest : public ::testing::Test {};

TEST_F(PrefetchURLLoaderClientTest, Construct) {}

TEST_F(PrefetchURLLoaderClientTest, RecordAndReplay) {}

// The difference from the previous test is that now SetClient() is called
// before any of the delegating methods, so there's no need to record them.
TEST_F(PrefetchURLLoaderClientTest, DelegateDirectly) {}

// This test just verifies that all the recorded callbacks can be destroyed
// without leaks.
TEST_F(PrefetchURLLoaderClientTest, RecordAndDiscard) {}

// Verifies that setting the client after the response comes but before it
// completes works.
TEST_F(PrefetchURLLoaderClientTest, ReplayAfterResponse) {}

TEST_F(PrefetchURLLoaderClientTest, GetURLLoaderPendingReceiver) {}

TEST_F(PrefetchURLLoaderClientTest, SetClientRemovesFromCache) {}

TEST_F(PrefetchURLLoaderClientTest, BadResponseCode) {}

TEST_F(PrefetchURLLoaderClientTest, BadHeader) {}

TEST_F(PrefetchURLLoaderClientTest, NoStore) {}

}  // namespace

}  // namespace network