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