1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// 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.
#ifndef NET_NETWORK_ERROR_LOGGING_MOCK_PERSISTENT_NEL_STORE_H_
#define NET_NETWORK_ERROR_LOGGING_MOCK_PERSISTENT_NEL_STORE_H_
#include <vector>
#include "base/functional/callback.h"
#include "net/network_error_logging/network_error_logging_service.h"
#include "url/origin.h"
namespace net {
// A NetworkErrorLoggingService::PersistentNelStore implementation that stashes
// the received commands in order in a vector, to be checked by tests.
// Simulates loading pre-existing stored policies, which can be provided
// using SetLoadExpectation().
class MockPersistentNelStore
: public NetworkErrorLoggingService::PersistentNelStore {
public:
// Represents a command that has been passed to the MockPersistentNelStore.
struct Command {
enum class Type {
LOAD_NEL_POLICIES,
ADD_NEL_POLICY,
UPDATE_NEL_POLICY,
DELETE_NEL_POLICY,
FLUSH
};
// Constructor for LOAD_NEL_POLICIES commands.
Command(Type type, NelPoliciesLoadedCallback loaded_callback);
// Constructor for ADD_NEL_POLICY, UPDATE_NEL_POLICY, and DELETE_NEL_POLICY
// commands.
Command(Type type, const NetworkErrorLoggingService::NelPolicy& policy);
// Constructor for FLUSH commands.
explicit Command(Type type);
Command(const Command& other);
Command(Command&& other);
~Command();
// Type of command.
Type type;
// The key of the policy that the command pertains to. (Only applies for
// add, update, and delete)
NetworkErrorLoggingService::NelPolicyKey key;
// The supplied callback to be run when loading is complete. (Only applies
// for load commands).
NelPoliciesLoadedCallback loaded_callback;
};
using CommandList = std::vector<Command>;
MockPersistentNelStore();
MockPersistentNelStore(const MockPersistentNelStore&) = delete;
MockPersistentNelStore& operator=(const MockPersistentNelStore&) = delete;
~MockPersistentNelStore() override;
// PersistentNelStore implementation:
void LoadNelPolicies(NelPoliciesLoadedCallback loaded_callback) override;
void AddNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) override;
void UpdateNelPolicyAccessTime(
const NetworkErrorLoggingService::NelPolicy& policy) override;
void DeleteNelPolicy(
const NetworkErrorLoggingService::NelPolicy& policy) override;
void Flush() override;
// Simulates pre-existing policies that were stored previously. Should only be
// called once, at the beginning of the test before any other method calls.
void SetPrestoredPolicies(
std::vector<NetworkErrorLoggingService::NelPolicy> policies);
// Simulate finishing loading policies by executing the loaded_callback of the
// first LOAD_NEL_POLICIES command (which should also be the only
// LOAD_NEL_POLICIES command). If |load_success| is false, the vector of
// policies passed to the callback will be empty. If |load_success| is true,
// the vector of policies passed to the callback will be
// |prestored_policies_|.
void FinishLoading(bool load_success);
// Verify that |command_list_| matches |expected_commands|.
bool VerifyCommands(const CommandList& expected_commands) const;
CommandList GetAllCommands() const;
// Returns the total number of policies that would be stored in the store, if
// this were a real store.
int StoredPoliciesCount() const { return policy_count_; }
private:
// List of commands that we have received so far.
CommandList command_list_;
// Simulated pre-existing stored policies.
std::vector<NetworkErrorLoggingService::NelPolicy> prestored_policies_;
// Set when LoadNelPolicies() is called.
bool load_started_ = false;
// Simulates the total number of policies that would be stored in the store.
// Updated when pre-stored policies are added, and when Flush() is called.
int policy_count_ = 0;
// Simulates the delta to be added to |policy_count_| the next time Flush() is
// called. Reset to 0 when Flush() is called.
int queued_policy_count_delta_ = 0;
};
bool operator==(const MockPersistentNelStore::Command& lhs,
const MockPersistentNelStore::Command& rhs);
} // namespace net
#endif // NET_NETWORK_ERROR_LOGGING_MOCK_PERSISTENT_NEL_STORE_H_
|