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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// 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.
#include "net/reporting/mock_persistent_reporting_store.h"
#include <vector>
#include "base/location.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "net/base/network_anonymization_key.h"
#include "net/reporting/reporting_endpoint.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
using CommandType = MockPersistentReportingStore::Command::Type;
struct ReportingData {
ReportingEndpoint endpoint;
CachedReportingEndpointGroup group;
};
ReportingData GetReportingData() {
const url::Origin kOrigin =
url::Origin::Create(GURL("https://example.test/"));
const char kGroupName[] = "groupname";
const ReportingEndpointGroupKey kGroupKey(NetworkAnonymizationKey(), kOrigin,
kGroupName);
const ReportingEndpoint kEndpoint(kGroupKey,
{GURL("https://endpoint.test/reports")});
const CachedReportingEndpointGroup kGroup(
kGroupKey, OriginSubdomains::DEFAULT, base::Time::Now() + base::Days(1),
base::Time::Now());
return {kEndpoint, kGroup};
}
void RunClosureOnClientsLoaded(
base::OnceClosure closure,
std::vector<ReportingEndpoint>* endpoints_out,
std::vector<CachedReportingEndpointGroup>* groups_out,
std::vector<ReportingEndpoint> loaded_endpoints,
std::vector<CachedReportingEndpointGroup> loaded_groups) {
std::move(closure).Run();
loaded_endpoints.swap(*endpoints_out);
loaded_groups.swap(*groups_out);
}
// Makes a ReportingClientsLoadedCallback that will fail if it's never run
// before destruction.
MockPersistentReportingStore::ReportingClientsLoadedCallback
MakeExpectedRunReportingClientsLoadedCallback(
std::vector<ReportingEndpoint>* endpoints_out,
std::vector<CachedReportingEndpointGroup>* groups_out) {
base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
return base::BindOnce(&RunClosureOnClientsLoaded, std::move(closure),
endpoints_out, groups_out);
}
// Test that FinishLoading() runs the callback.
TEST(MockPersistentReportingStoreTest, FinishLoading) {
MockPersistentReportingStore store;
MockPersistentReportingStore::CommandList expected_commands;
std::vector<ReportingEndpoint> loaded_endpoints;
std::vector<CachedReportingEndpointGroup> loaded_groups;
store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
&loaded_endpoints, &loaded_groups));
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
store.FinishLoading(true /* load_success */);
EXPECT_EQ(0u, loaded_endpoints.size());
EXPECT_EQ(0u, loaded_groups.size());
EXPECT_TRUE(store.VerifyCommands(expected_commands));
// Test should not crash because the callback has been run.
}
TEST(MockPersistentReportingStoreTest, PreStoredClients) {
MockPersistentReportingStore store;
MockPersistentReportingStore::CommandList expected_commands;
std::vector<ReportingEndpoint> loaded_endpoints;
std::vector<CachedReportingEndpointGroup> loaded_groups;
const auto reporting_data = GetReportingData();
store.SetPrestoredClients({reporting_data.endpoint}, {reporting_data.group});
EXPECT_EQ(1, store.StoredEndpointsCount());
EXPECT_EQ(1, store.StoredEndpointGroupsCount());
store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
&loaded_endpoints, &loaded_groups));
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
store.FinishLoading(true /* load_success */);
EXPECT_EQ(1u, loaded_endpoints.size());
EXPECT_EQ(1u, loaded_groups.size());
EXPECT_TRUE(store.VerifyCommands(expected_commands));
}
// Failed load should yield empty vectors of endpoints and endpoint groups.
TEST(MockPersistentReportingStoreTest, FailedLoad) {
MockPersistentReportingStore store;
MockPersistentReportingStore::CommandList expected_commands;
std::vector<ReportingEndpoint> loaded_endpoints;
std::vector<CachedReportingEndpointGroup> loaded_groups;
const auto reporting_data = GetReportingData();
store.SetPrestoredClients({reporting_data.endpoint}, {reporting_data.group});
EXPECT_EQ(1, store.StoredEndpointsCount());
EXPECT_EQ(1, store.StoredEndpointGroupsCount());
store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
&loaded_endpoints, &loaded_groups));
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
store.FinishLoading(false /* load_success */);
EXPECT_EQ(0u, loaded_endpoints.size());
EXPECT_EQ(0u, loaded_groups.size());
EXPECT_TRUE(store.VerifyCommands(expected_commands));
}
TEST(MockPersistentReportingStoreTest, AddFlushDeleteFlush) {
MockPersistentReportingStore store;
MockPersistentReportingStore::CommandList expected_commands;
std::vector<ReportingEndpoint> loaded_endpoints;
std::vector<CachedReportingEndpointGroup> loaded_groups;
store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
&loaded_endpoints, &loaded_groups));
expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS);
EXPECT_EQ(1u, store.GetAllCommands().size());
store.FinishLoading(true /* load_success */);
EXPECT_EQ(0u, loaded_endpoints.size());
EXPECT_EQ(0u, loaded_groups.size());
EXPECT_EQ(0, store.StoredEndpointsCount());
EXPECT_EQ(0, store.StoredEndpointGroupsCount());
const auto reporting_data = GetReportingData();
store.AddReportingEndpoint(reporting_data.endpoint);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
reporting_data.endpoint);
EXPECT_EQ(2u, store.GetAllCommands().size());
store.AddReportingEndpointGroup(reporting_data.group);
expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
reporting_data.group);
EXPECT_EQ(3u, store.GetAllCommands().size());
store.Flush();
expected_commands.emplace_back(CommandType::FLUSH);
EXPECT_EQ(4u, store.GetAllCommands().size());
EXPECT_EQ(1, store.StoredEndpointsCount());
EXPECT_EQ(1, store.StoredEndpointGroupsCount());
store.DeleteReportingEndpoint(reporting_data.endpoint);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
reporting_data.endpoint);
EXPECT_EQ(5u, store.GetAllCommands().size());
store.DeleteReportingEndpointGroup(reporting_data.group);
expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
reporting_data.group);
EXPECT_EQ(6u, store.GetAllCommands().size());
store.Flush();
expected_commands.emplace_back(CommandType::FLUSH);
EXPECT_EQ(7u, store.GetAllCommands().size());
EXPECT_EQ(0, store.StoredEndpointsCount());
EXPECT_EQ(0, store.StoredEndpointGroupsCount());
EXPECT_TRUE(store.VerifyCommands(expected_commands));
EXPECT_EQ(1, store.CountCommands(CommandType::LOAD_REPORTING_CLIENTS));
EXPECT_EQ(
0, store.CountCommands(CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS));
}
TEST(MockPersistentReportingStoreTest, CountCommands) {
MockPersistentReportingStore store;
std::vector<ReportingEndpoint> loaded_endpoints;
std::vector<CachedReportingEndpointGroup> loaded_groups;
store.LoadReportingClients(MakeExpectedRunReportingClientsLoadedCallback(
&loaded_endpoints, &loaded_groups));
store.FinishLoading(true /* load_success */);
const auto reporting_data = GetReportingData();
store.AddReportingEndpoint(reporting_data.endpoint);
store.AddReportingEndpointGroup(reporting_data.group);
store.Flush();
store.DeleteReportingEndpoint(reporting_data.endpoint);
store.DeleteReportingEndpointGroup(reporting_data.group);
store.Flush();
EXPECT_EQ(1, store.CountCommands(CommandType::LOAD_REPORTING_CLIENTS));
EXPECT_EQ(1, store.CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
EXPECT_EQ(1, store.CountCommands(CommandType::ADD_REPORTING_ENDPOINT_GROUP));
EXPECT_EQ(0, store.CountCommands(
CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME));
EXPECT_EQ(
0, store.CountCommands(CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS));
EXPECT_EQ(0, store.CountCommands(
CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS));
EXPECT_EQ(1, store.CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
EXPECT_EQ(1,
store.CountCommands(CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
EXPECT_EQ(2, store.CountCommands(CommandType::FLUSH));
}
} // namespace
} // namespace net
|