mock_persistent_reporting_store_unittest.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/reporting/mock_persistent_reporting_store.h"
  5. #include <vector>
  6. #include "base/location.h"
  7. #include "base/test/bind.h"
  8. #include "base/time/time.h"
  9. #include "net/base/network_isolation_key.h"
  10. #include "net/reporting/reporting_endpoint.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "url/gurl.h"
  13. #include "url/origin.h"
  14. namespace net {
  15. namespace {
  16. using CommandType = MockPersistentReportingStore::Command::Type;
  17. struct ReportingData {
  18. ReportingEndpoint endpoint;
  19. CachedReportingEndpointGroup group;
  20. };
  21. ReportingData GetReportingData() {
  22. const url::Origin kOrigin =
  23. url::Origin::Create(GURL("https://example.test/"));
  24. const char kGroupName[] = "groupname";
  25. const ReportingEndpointGroupKey kGroupKey(NetworkIsolationKey(), kOrigin,
  26. kGroupName);
  27. const ReportingEndpoint kEndpoint(kGroupKey,
  28. {GURL("https://endpoint.test/reports")});
  29. const CachedReportingEndpointGroup kGroup(
  30. kGroupKey, OriginSubdomains::DEFAULT, base::Time::Now() + base::Days(1),
  31. base::Time::Now());
  32. return {kEndpoint, kGroup};
  33. }
  34. void RunClosureOnClientsLoaded(
  35. base::OnceClosure closure,
  36. std::vector<ReportingEndpoint>* endpoints_out,
  37. std::vector<CachedReportingEndpointGroup>* groups_out,
  38. std::vector<ReportingEndpoint> loaded_endpoints,
  39. std::vector<CachedReportingEndpointGroup> loaded_groups) {
  40. std::move(closure).Run();
  41. loaded_endpoints.swap(*endpoints_out);
  42. loaded_groups.swap(*groups_out);
  43. }
  44. // Makes a ReportingClientsLoadedCallback that will fail if it's never run
  45. // before destruction.
  46. MockPersistentReportingStore::ReportingClientsLoadedCallback
  47. MakeExpectedRunReportingClientsLoadedCallback(
  48. std::vector<ReportingEndpoint>* endpoints_out,
  49. std::vector<CachedReportingEndpointGroup>* groups_out) {
  50. base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
  51. return base::BindOnce(&RunClosureOnClientsLoaded, std::move(closure),
  52. endpoints_out, groups_out);
  53. }
  54. // Test that FinishLoading() runs the callback.
  55. TEST(MockPersistentReportingStoreTest, FinishLoading) {
  56. MockPersistentReportingStore store;
  57. MockPersistentReportingStore::CommandList expected_commands;
  58. std::vector<ReportingEndpoint> loaded_endpoints;
  59. std::vector<CachedReportingEndpointGroup> loaded_groups;
  60. store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
  61. &loaded_endpoints, &loaded_groups));
  62. expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
  63. store.FinishLoading(true /* load_success */);
  64. EXPECT_EQ(0u, loaded_endpoints.size());
  65. EXPECT_EQ(0u, loaded_groups.size());
  66. EXPECT_TRUE(store.VerifyCommands(expected_commands));
  67. // Test should not crash because the callback has been run.
  68. }
  69. TEST(MockPersistentReportingStoreTest, PreStoredClients) {
  70. MockPersistentReportingStore store;
  71. MockPersistentReportingStore::CommandList expected_commands;
  72. std::vector<ReportingEndpoint> loaded_endpoints;
  73. std::vector<CachedReportingEndpointGroup> loaded_groups;
  74. const auto reporting_data = GetReportingData();
  75. store.SetPrestoredClients({reporting_data.endpoint}, {reporting_data.group});
  76. EXPECT_EQ(1, store.StoredEndpointsCount());
  77. EXPECT_EQ(1, store.StoredEndpointGroupsCount());
  78. store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
  79. &loaded_endpoints, &loaded_groups));
  80. expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
  81. store.FinishLoading(true /* load_success */);
  82. EXPECT_EQ(1u, loaded_endpoints.size());
  83. EXPECT_EQ(1u, loaded_groups.size());
  84. EXPECT_TRUE(store.VerifyCommands(expected_commands));
  85. }
  86. // Failed load should yield empty vectors of endpoints and endpoint groups.
  87. TEST(MockPersistentReportingStoreTest, FailedLoad) {
  88. MockPersistentReportingStore store;
  89. MockPersistentReportingStore::CommandList expected_commands;
  90. std::vector<ReportingEndpoint> loaded_endpoints;
  91. std::vector<CachedReportingEndpointGroup> loaded_groups;
  92. const auto reporting_data = GetReportingData();
  93. store.SetPrestoredClients({reporting_data.endpoint}, {reporting_data.group});
  94. EXPECT_EQ(1, store.StoredEndpointsCount());
  95. EXPECT_EQ(1, store.StoredEndpointGroupsCount());
  96. store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
  97. &loaded_endpoints, &loaded_groups));
  98. expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
  99. store.FinishLoading(false /* load_success */);
  100. EXPECT_EQ(0u, loaded_endpoints.size());
  101. EXPECT_EQ(0u, loaded_groups.size());
  102. EXPECT_TRUE(store.VerifyCommands(expected_commands));
  103. }
  104. TEST(MockPersistentReportingStoreTest, AddFlushDeleteFlush) {
  105. MockPersistentReportingStore store;
  106. MockPersistentReportingStore::CommandList expected_commands;
  107. std::vector<ReportingEndpoint> loaded_endpoints;
  108. std::vector<CachedReportingEndpointGroup> loaded_groups;
  109. store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
  110. &loaded_endpoints, &loaded_groups));
  111. expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
  112. EXPECT_EQ(1u, store.GetAllCommands().size());
  113. store.FinishLoading(true /* load_success */);
  114. EXPECT_EQ(0u, loaded_endpoints.size());
  115. EXPECT_EQ(0u, loaded_groups.size());
  116. EXPECT_EQ(0, store.StoredEndpointsCount());
  117. EXPECT_EQ(0, store.StoredEndpointGroupsCount());
  118. const auto reporting_data = GetReportingData();
  119. store.AddReportingEndpoint(reporting_data.endpoint);
  120. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  121. reporting_data.endpoint);
  122. EXPECT_EQ(2u, store.GetAllCommands().size());
  123. store.AddReportingEndpointGroup(reporting_data.group);
  124. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  125. reporting_data.group);
  126. EXPECT_EQ(3u, store.GetAllCommands().size());
  127. store.Flush();
  128. expected_commands.emplace_back(CommandType::FLUSH);
  129. EXPECT_EQ(4u, store.GetAllCommands().size());
  130. EXPECT_EQ(1, store.StoredEndpointsCount());
  131. EXPECT_EQ(1, store.StoredEndpointGroupsCount());
  132. store.DeleteReportingEndpoint(reporting_data.endpoint);
  133. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  134. reporting_data.endpoint);
  135. EXPECT_EQ(5u, store.GetAllCommands().size());
  136. store.DeleteReportingEndpointGroup(reporting_data.group);
  137. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  138. reporting_data.group);
  139. EXPECT_EQ(6u, store.GetAllCommands().size());
  140. store.Flush();
  141. expected_commands.emplace_back(CommandType::FLUSH);
  142. EXPECT_EQ(7u, store.GetAllCommands().size());
  143. EXPECT_EQ(0, store.StoredEndpointsCount());
  144. EXPECT_EQ(0, store.StoredEndpointGroupsCount());
  145. EXPECT_TRUE(store.VerifyCommands(expected_commands));
  146. EXPECT_EQ(1, store.CountCommands(CommandType::LOAD_REPORTING_CLIENTS));
  147. EXPECT_EQ(
  148. 0, store.CountCommands(CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS));
  149. }
  150. TEST(MockPersistentReportingStoreTest, CountCommands) {
  151. MockPersistentReportingStore store;
  152. std::vector<ReportingEndpoint> loaded_endpoints;
  153. std::vector<CachedReportingEndpointGroup> loaded_groups;
  154. store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
  155. &loaded_endpoints, &loaded_groups));
  156. store.FinishLoading(true /* load_success */);
  157. const auto reporting_data = GetReportingData();
  158. store.AddReportingEndpoint(reporting_data.endpoint);
  159. store.AddReportingEndpointGroup(reporting_data.group);
  160. store.Flush();
  161. store.DeleteReportingEndpoint(reporting_data.endpoint);
  162. store.DeleteReportingEndpointGroup(reporting_data.group);
  163. store.Flush();
  164. EXPECT_EQ(1, store.CountCommands(CommandType::LOAD_REPORTING_CLIENTS));
  165. EXPECT_EQ(1, store.CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  166. EXPECT_EQ(1, store.CountCommands(CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  167. EXPECT_EQ(0, store.CountCommands(
  168. CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME));
  169. EXPECT_EQ(
  170. 0, store.CountCommands(CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS));
  171. EXPECT_EQ(0, store.CountCommands(
  172. CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS));
  173. EXPECT_EQ(1, store.CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  174. EXPECT_EQ(1,
  175. store.CountCommands(CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  176. EXPECT_EQ(2, store.CountCommands(CommandType::FLUSH));
  177. }
  178. } // namespace
  179. } // namespace net