mock_persistent_nel_store.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/network_error_logging/mock_persistent_nel_store.h"
  5. #include <sstream>
  6. namespace net {
  7. MockPersistentNelStore::Command::Command(
  8. Type type,
  9. NelPoliciesLoadedCallback loaded_callback)
  10. : type(type), loaded_callback(std::move(loaded_callback)) {}
  11. MockPersistentNelStore::Command::Command(
  12. Type type,
  13. const NetworkErrorLoggingService::NelPolicy& policy)
  14. : type(type), key(policy.key) {}
  15. MockPersistentNelStore::Command::Command(Type type) : type(type) {}
  16. MockPersistentNelStore::Command::Command(const Command& other)
  17. : type(other.type), key(other.key) {}
  18. MockPersistentNelStore::Command::Command(Command&& other) = default;
  19. MockPersistentNelStore::Command::~Command() = default;
  20. bool operator==(const MockPersistentNelStore::Command& lhs,
  21. const MockPersistentNelStore::Command& rhs) {
  22. if (lhs.type != rhs.type)
  23. return false;
  24. switch (lhs.type) {
  25. // For LOAD_NEL_POLICIES and FLUSH, just check the type.
  26. case MockPersistentNelStore::Command::Type::LOAD_NEL_POLICIES:
  27. case MockPersistentNelStore::Command::Type::FLUSH:
  28. return true;
  29. // For ADD_NEL_POLICY, UPDATE_NEL_POLICY, and DELETE_NEL_POLICY,
  30. // additionally check the policy's key.
  31. case MockPersistentNelStore::Command::Type::ADD_NEL_POLICY:
  32. case MockPersistentNelStore::Command::Type::UPDATE_NEL_POLICY:
  33. case MockPersistentNelStore::Command::Type::DELETE_NEL_POLICY:
  34. return (lhs.key == rhs.key);
  35. }
  36. }
  37. bool operator!=(const MockPersistentNelStore::Command& lhs,
  38. const MockPersistentNelStore::Command& rhs) {
  39. return !(lhs == rhs);
  40. }
  41. MockPersistentNelStore::MockPersistentNelStore() = default;
  42. MockPersistentNelStore::~MockPersistentNelStore() = default;
  43. void MockPersistentNelStore::LoadNelPolicies(
  44. NelPoliciesLoadedCallback loaded_callback) {
  45. DCHECK(!load_started_);
  46. command_list_.emplace_back(Command::Type::LOAD_NEL_POLICIES,
  47. std::move(loaded_callback));
  48. load_started_ = true;
  49. }
  50. void MockPersistentNelStore::AddNelPolicy(
  51. const NetworkErrorLoggingService::NelPolicy& policy) {
  52. DCHECK(load_started_);
  53. command_list_.emplace_back(Command::Type::ADD_NEL_POLICY, policy);
  54. ++queued_policy_count_delta_;
  55. }
  56. void MockPersistentNelStore::UpdateNelPolicyAccessTime(
  57. const NetworkErrorLoggingService::NelPolicy& policy) {
  58. DCHECK(load_started_);
  59. command_list_.emplace_back(Command::Type::UPDATE_NEL_POLICY, policy);
  60. }
  61. void MockPersistentNelStore::DeleteNelPolicy(
  62. const NetworkErrorLoggingService::NelPolicy& policy) {
  63. DCHECK(load_started_);
  64. command_list_.emplace_back(Command::Type::DELETE_NEL_POLICY, policy);
  65. --queued_policy_count_delta_;
  66. }
  67. void MockPersistentNelStore::Flush() {
  68. // Can be called before |load_started_| is true, if the
  69. // NetworkErrorLoggingService is destroyed before getting a chance to load.
  70. command_list_.emplace_back(Command::Type::FLUSH);
  71. policy_count_ += queued_policy_count_delta_;
  72. queued_policy_count_delta_ = 0;
  73. }
  74. void MockPersistentNelStore::SetPrestoredPolicies(
  75. std::vector<NetworkErrorLoggingService::NelPolicy> policies) {
  76. DCHECK(!load_started_);
  77. DCHECK_EQ(0, policy_count_);
  78. policy_count_ += policies.size();
  79. prestored_policies_.swap(policies);
  80. }
  81. void MockPersistentNelStore::FinishLoading(bool load_success) {
  82. DCHECK(load_started_);
  83. for (size_t i = 0; i < command_list_.size(); ++i) {
  84. Command& command = command_list_[i];
  85. if (command.type == Command::Type::LOAD_NEL_POLICIES) {
  86. // If LOAD_NEL_POLICIES has been initiated, it should be the first
  87. // operation.
  88. DCHECK_EQ(0u, i);
  89. DCHECK(!command.loaded_callback.is_null());
  90. if (load_success) {
  91. std::move(command.loaded_callback).Run(std::move(prestored_policies_));
  92. } else {
  93. std::move(command.loaded_callback)
  94. .Run(std::vector<NetworkErrorLoggingService::NelPolicy>());
  95. }
  96. }
  97. if (i > 0) {
  98. // LOAD_NEL_POLICIES should not have been called twice.
  99. DCHECK(command.type != Command::Type::LOAD_NEL_POLICIES);
  100. }
  101. }
  102. }
  103. bool MockPersistentNelStore::VerifyCommands(
  104. const CommandList& expected_commands) const {
  105. return command_list_ == expected_commands;
  106. }
  107. MockPersistentNelStore::CommandList MockPersistentNelStore::GetAllCommands()
  108. const {
  109. return command_list_;
  110. }
  111. } // namespace net