1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_REPORTING_MOCK_PERSISTENT_REPORTING_STORE_H_ 6 #define NET_REPORTING_MOCK_PERSISTENT_REPORTING_STORE_H_ 7 8 #include <vector> 9 10 #include "base/functional/callback.h" 11 #include "net/base/network_anonymization_key.h" 12 #include "net/reporting/reporting_cache.h" 13 #include "net/reporting/reporting_endpoint.h" 14 #include "url/origin.h" 15 16 namespace net { 17 18 // A ReportingCache::PersistentReportingStore implementation that stashes the 19 // received commands in order in a vector, to be checked by tests. Simulates 20 // loading pre-existing stored endpoints and endpoint groups, which can be 21 // provided using SetPrestoredClients(). 22 // 23 // TODO(sburnett): Replace this with a fake store to reduce awkwardness. 24 class MockPersistentReportingStore 25 : public ReportingCache::PersistentReportingStore { 26 public: 27 struct Command { 28 enum class Type { 29 LOAD_REPORTING_CLIENTS, 30 ADD_REPORTING_ENDPOINT, 31 ADD_REPORTING_ENDPOINT_GROUP, 32 UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, 33 UPDATE_REPORTING_ENDPOINT_DETAILS, 34 UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS, 35 DELETE_REPORTING_ENDPOINT, 36 DELETE_REPORTING_ENDPOINT_GROUP, 37 FLUSH 38 }; 39 40 // Constructor for LOAD_REPORTING_CLIENTS commands. 41 Command(Type type, ReportingClientsLoadedCallback loaded_callback); 42 // Constructors for endpoint commands. |type| must be one of 43 // ADD_REPORTING_ENDPOINT, UPDATE_REPORTING_ENDPOINT_DETAILS, or 44 // DELETE_REPORTING_ENDPOINT 45 Command(Type type, const ReportingEndpoint& endpoint); 46 Command(Type type, 47 const ReportingEndpointGroupKey& group_key, 48 const GURL& endpoint_url); 49 // Constructors for endpoint group commands. |type| must be one of 50 // ADD_REPORTING_ENDPOINT_GROUP, 51 // UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, 52 // UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS, or 53 // DELETE_REPORTING_ENDPOINT_GROUP 54 Command(Type type, const CachedReportingEndpointGroup& group); 55 Command(Type type, const ReportingEndpointGroupKey& group_key); 56 // |type| must be LOAD_REPORTING_CLIENTS or FLUSH. 57 explicit Command(Type type); 58 59 Command(const Command& other); 60 Command(Command&& other); 61 62 ~Command(); 63 64 // Type of command. 65 Type type; 66 67 // Identifies the group to which the command pertains. (Applies to endpoint 68 // and endpoint group commands.) 69 ReportingEndpointGroupKey group_key = ReportingEndpointGroupKey(); 70 71 // Identifies the endpoint to which the command pertains. (Applies to 72 // endpoint commands only.) 73 GURL url; 74 75 // The supplied callback to be run when loading is complete. (Only applies 76 // for load commands.) 77 ReportingClientsLoadedCallback loaded_callback; 78 }; 79 80 using CommandList = std::vector<Command>; 81 82 MockPersistentReportingStore(); 83 84 MockPersistentReportingStore(const MockPersistentReportingStore&) = delete; 85 MockPersistentReportingStore& operator=(const MockPersistentReportingStore&) = 86 delete; 87 88 ~MockPersistentReportingStore() override; 89 90 // PersistentReportingStore implementation: 91 void LoadReportingClients( 92 ReportingClientsLoadedCallback loaded_callback) override; 93 void AddReportingEndpoint(const ReportingEndpoint& endpoint) override; 94 void AddReportingEndpointGroup( 95 const CachedReportingEndpointGroup& group) override; 96 void UpdateReportingEndpointGroupAccessTime( 97 const CachedReportingEndpointGroup& group) override; 98 void UpdateReportingEndpointDetails( 99 const ReportingEndpoint& endpoint) override; 100 void UpdateReportingEndpointGroupDetails( 101 const CachedReportingEndpointGroup& group) override; 102 void DeleteReportingEndpoint(const ReportingEndpoint& endpoint) override; 103 void DeleteReportingEndpointGroup( 104 const CachedReportingEndpointGroup& group) override; 105 void Flush() override; 106 107 // Simulates pre-existing clients that were stored previously. Should only be 108 // called once, at the beginning of the test before any other method calls. 109 void SetPrestoredClients(std::vector<ReportingEndpoint> endpoints, 110 std::vector<CachedReportingEndpointGroup> groups); 111 112 // Simulate finishing loading clients by executing the loaded_callback of the 113 // first LOAD_REPORTING_CLIENTS command (which should also be the only 114 // LOAD_REPORTING_CLIENTS command). If |load_success| is false, the vectors of 115 // endpoints and groups passed to the callback will be empty. If 116 // |load_success| is true, the prestored clients will be passed to the 117 // callback. 118 void FinishLoading(bool load_success); 119 120 // Verify that |command_list_| matches |expected_commands|. 121 // TODO(sburnett): Replace this with a set of gmock matchers. 122 bool VerifyCommands(const CommandList& expected_commands) const; 123 124 // Count the number of commands with type |t|. 125 int CountCommands(Command::Type t); 126 127 void ClearCommands(); 128 129 CommandList GetAllCommands() const; 130 131 // Gets the number of stored endpoints/groups, simulating the actual number 132 // that would be written on disk if this were a real store. StoredEndpointsCount()133 int StoredEndpointsCount() const { return endpoint_count_; } StoredEndpointGroupsCount()134 int StoredEndpointGroupsCount() const { return endpoint_group_count_; } 135 136 private: 137 // List of commands that we have received so far. 138 CommandList command_list_; 139 140 // Simulated pre-existing stored clients. 141 std::vector<ReportingEndpoint> prestored_endpoints_; 142 std::vector<CachedReportingEndpointGroup> prestored_endpoint_groups_; 143 144 // Set when LoadReportingClients() is called. 145 bool load_started_ = false; 146 147 // Simulates the total number of endpoints/groups that would be stored in the 148 // store. Updated when pre-stored policies are added, and when Flush() is 149 // called. 150 int endpoint_count_ = 0; 151 int endpoint_group_count_ = 0; 152 153 // Simulates the delta to be added to to the counts the next time Flush() is 154 // called. Reset to 0 when Flush() is called. 155 int queued_endpoint_count_delta_ = 0; 156 int queued_endpoint_group_count_delta_ = 0; 157 }; 158 159 bool operator==(const MockPersistentReportingStore::Command& lhs, 160 const MockPersistentReportingStore::Command& rhs); 161 bool operator!=(const MockPersistentReportingStore::Command& lhs, 162 const MockPersistentReportingStore::Command& rhs); 163 std::ostream& operator<<(std::ostream& out, 164 const MockPersistentReportingStore::Command& cmd); 165 166 } // namespace net 167 168 #endif // NET_REPORTING_MOCK_PERSISTENT_REPORTING_STORE_H_ 169