blob: 04f1d8979659d2a27f4fe71503a61790b15d660e [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "net/base/network_isolation_key.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_endpoint.h"
#include "url/origin.h"
namespace net {
// A ReportingCache::PersistentReportingStore implementation that stashes the
// received commands in order in a vector, to be checked by tests. Simulates
// loading pre-existing stored endpoints and endpoint groups, which can be
// provided using SetPrestoredClients().
// TODO(sburnett): Replace this with a fake store to reduce awkwardness.
class MockPersistentReportingStore
: public ReportingCache::PersistentReportingStore {
struct Command {
enum class Type {
// Constructor for LOAD_REPORTING_CLIENTS commands.
Command(Type type, ReportingClientsLoadedCallback loaded_callback);
// Constructors for endpoint commands. |type| must be one of
Command(Type type, const ReportingEndpoint& endpoint);
Command(Type type,
const ReportingEndpointGroupKey& group_key,
const GURL& endpoint_url);
// Constructors for endpoint group commands. |type| must be one of
Command(Type type, const CachedReportingEndpointGroup& group);
Command(Type type, const ReportingEndpointGroupKey& group_key);
// |type| must be LOAD_REPORTING_CLIENTS or FLUSH.
explicit Command(Type type);
Command(const Command& other);
Command(Command&& other);
// Type of command.
Type type;
// Identifies the group to which the command pertains. (Applies to endpoint
// and endpoint group commands.)
ReportingEndpointGroupKey group_key = ReportingEndpointGroupKey();
// Identifies the endpoint to which the command pertains. (Applies to
// endpoint commands only.)
GURL url;
// The supplied callback to be run when loading is complete. (Only applies
// for load commands.)
ReportingClientsLoadedCallback loaded_callback;
using CommandList = std::vector<Command>;
~MockPersistentReportingStore() override;
// PersistentReportingStore implementation:
void LoadReportingClients(
ReportingClientsLoadedCallback loaded_callback) override;
void AddReportingEndpoint(const ReportingEndpoint& endpoint) override;
void AddReportingEndpointGroup(
const CachedReportingEndpointGroup& group) override;
void UpdateReportingEndpointGroupAccessTime(
const CachedReportingEndpointGroup& group) override;
void UpdateReportingEndpointDetails(
const ReportingEndpoint& endpoint) override;
void UpdateReportingEndpointGroupDetails(
const CachedReportingEndpointGroup& group) override;
void DeleteReportingEndpoint(const ReportingEndpoint& endpoint) override;
void DeleteReportingEndpointGroup(
const CachedReportingEndpointGroup& group) override;
void Flush() override;
// Simulates pre-existing clients that were stored previously. Should only be
// called once, at the beginning of the test before any other method calls.
void SetPrestoredClients(std::vector<ReportingEndpoint> endpoints,
std::vector<CachedReportingEndpointGroup> groups);
// Simulate finishing loading clients by executing the loaded_callback of the
// first LOAD_REPORTING_CLIENTS command (which should also be the only
// LOAD_REPORTING_CLIENTS command). If |load_success| is false, the vectors of
// endpoints and groups passed to the callback will be empty. If
// |load_success| is true, the prestored clients will be passed to the
// callback.
void FinishLoading(bool load_success);
// Verify that |command_list_| matches |expected_commands|.
// TODO(sburnett): Replace this with a set of gmock matchers.
bool VerifyCommands(const CommandList& expected_commands) const;
// Count the number of commands with type |t|.
int CountCommands(Command::Type t);
void ClearCommands();
CommandList GetAllCommands() const;
// Gets the number of stored endpoints/groups, simulating the actual number
// that would be written on disk if this were a real store.
int StoredEndpointsCount() const { return endpoint_count_; }
int StoredEndpointGroupsCount() const { return endpoint_group_count_; }
// List of commands that we have received so far.
CommandList command_list_;
// Simulated pre-existing stored clients.
std::vector<ReportingEndpoint> prestored_endpoints_;
std::vector<CachedReportingEndpointGroup> prestored_endpoint_groups_;
// Set when LoadReportingClients() is called.
bool load_started_;
// Simulates the total number of endpoints/groups that would be stored in the
// store. Updated when pre-stored policies are added, and when Flush() is
// called.
int endpoint_count_;
int endpoint_group_count_;
// Simulates the delta to be added to to the counts the next time Flush() is
// called. Reset to 0 when Flush() is called.
int queued_endpoint_count_delta_;
int queued_endpoint_group_count_delta_;
bool operator==(const MockPersistentReportingStore::Command& lhs,
const MockPersistentReportingStore::Command& rhs);
bool operator!=(const MockPersistentReportingStore::Command& lhs,
const MockPersistentReportingStore::Command& rhs);
std::ostream& operator<<(std::ostream& out,
const MockPersistentReportingStore::Command& cmd);
} // namespace net