Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 | |
| 5 #include "net/reporting/reporting_cache.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "base/values.h" | |
| 12 #include "net/reporting/reporting_client.h" | |
| 13 #include "net/reporting/reporting_report.h" | |
| 14 #include "net/reporting/reporting_test_util.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "url/gurl.h" | |
| 17 #include "url/origin.h" | |
| 18 | |
| 19 namespace net { | |
| 20 namespace { | |
| 21 | |
| 22 const GURL kUrl1("https://origin1/path"); | |
| 23 const url::Origin kOrigin1(GURL("https://origin1/")); | |
| 24 const url::Origin kOrigin2(GURL("https://origin2/")); | |
| 25 const GURL kEndpoint1("https://endpoint1/"); | |
| 26 const GURL kEndpoint2("https://endpoint2/"); | |
| 27 const std::string kGroup1("group1"); | |
| 28 const std::string kGroup2("group2"); | |
| 29 const std::string kType("default"); | |
| 30 const base::TimeTicks kNow(base::TimeTicks::Now()); | |
| 31 const base::TimeTicks kExpires1(kNow + base::TimeDelta::FromDays(7)); | |
| 32 const base::TimeTicks kExpires2(kExpires1 + base::TimeDelta::FromDays(7)); | |
|
jkarlin
2017/03/17 15:07:03
Note that the above still need to be POD
Julia Tuttle
2017/03/21 18:41:02
*mutter, grumble, move into ReportingCacheTest*
(
| |
| 33 | |
| 34 class ReportingCacheTest : public ::testing::Test { | |
| 35 protected: | |
| 36 ReportingCache cache_; | |
| 37 }; | |
| 38 | |
| 39 TEST_F(ReportingCacheTest, Reports) { | |
| 40 std::vector<const ReportingReport*> reports; | |
| 41 cache_.GetReports(&reports); | |
| 42 EXPECT_TRUE(reports.empty()); | |
| 43 | |
| 44 cache_.AddReport(kUrl1, kGroup1, kType, | |
| 45 base::MakeUnique<base::DictionaryValue>(), kNow, 0); | |
| 46 | |
| 47 cache_.GetReports(&reports); | |
| 48 ASSERT_EQ(1u, reports.size()); | |
| 49 const ReportingReport* report = reports[0]; | |
| 50 ASSERT_TRUE(report); | |
| 51 EXPECT_EQ(kUrl1, report->url); | |
| 52 EXPECT_EQ(kGroup1, report->group); | |
| 53 EXPECT_EQ(kType, report->type); | |
| 54 // TODO(juliatuttle): Check body? | |
| 55 EXPECT_EQ(kNow, report->queued); | |
| 56 EXPECT_EQ(0, report->attempts); | |
| 57 EXPECT_FALSE(cache_.IsReportPendingForTesting(report)); | |
| 58 EXPECT_FALSE(cache_.IsReportDoomedForTesting(report)); | |
| 59 | |
| 60 cache_.IncrementReportsAttempts(reports); | |
| 61 | |
| 62 cache_.GetReports(&reports); | |
| 63 ASSERT_EQ(1u, reports.size()); | |
| 64 report = reports[0]; | |
| 65 ASSERT_TRUE(report); | |
| 66 EXPECT_EQ(1, report->attempts); | |
| 67 | |
| 68 cache_.RemoveReports(reports); | |
| 69 | |
| 70 cache_.GetReports(&reports); | |
| 71 EXPECT_TRUE(reports.empty()); | |
| 72 } | |
| 73 | |
| 74 TEST_F(ReportingCacheTest, PendingReports) { | |
| 75 cache_.AddReport(kUrl1, kGroup1, kType, | |
| 76 base::MakeUnique<base::DictionaryValue>(), kNow, 0); | |
| 77 | |
| 78 std::vector<const ReportingReport*> reports; | |
| 79 cache_.GetReports(&reports); | |
| 80 ASSERT_EQ(1u, reports.size()); | |
| 81 EXPECT_FALSE(cache_.IsReportPendingForTesting(reports[0])); | |
| 82 EXPECT_FALSE(cache_.IsReportDoomedForTesting(reports[0])); | |
| 83 | |
| 84 cache_.SetReportsPending(reports); | |
| 85 EXPECT_TRUE(cache_.IsReportPendingForTesting(reports[0])); | |
| 86 EXPECT_FALSE(cache_.IsReportDoomedForTesting(reports[0])); | |
| 87 | |
| 88 cache_.RemoveReports(reports); | |
| 89 EXPECT_TRUE(cache_.IsReportPendingForTesting(reports[0])); | |
| 90 EXPECT_TRUE(cache_.IsReportDoomedForTesting(reports[0])); | |
| 91 | |
| 92 // After removing report, future calls to GetReports should not return it. | |
| 93 std::vector<const ReportingReport*> visible_reports; | |
| 94 cache_.GetReports(&visible_reports); | |
| 95 EXPECT_TRUE(visible_reports.empty()); | |
| 96 EXPECT_EQ(1u, cache_.GetFullReportCountForTesting()); | |
| 97 | |
| 98 // After clearing pending flag, report should be deleted. | |
| 99 cache_.ClearReportsPending(reports); | |
| 100 EXPECT_EQ(0u, cache_.GetFullReportCountForTesting()); | |
| 101 } | |
| 102 | |
| 103 TEST_F(ReportingCacheTest, Endpoints) { | |
| 104 cache_.SetClient(kOrigin1, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 105 kGroup1, kExpires1); | |
| 106 | |
| 107 const ReportingClient* client = | |
| 108 FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
| 109 ASSERT_TRUE(client); | |
| 110 EXPECT_EQ(kOrigin1, client->origin); | |
| 111 EXPECT_EQ(kEndpoint1, client->endpoint); | |
| 112 EXPECT_FALSE(client->subdomains); | |
| 113 EXPECT_EQ(kGroup1, client->group); | |
| 114 EXPECT_EQ(kExpires1, client->expires); | |
| 115 | |
| 116 // Replaces original configuration with new Subdomains, group, and expires | |
| 117 // values. | |
| 118 cache_.SetClient(kOrigin1, kEndpoint1, ReportingClient::INCLUDE_SUBDOMAINS, | |
| 119 kGroup2, kExpires2); | |
| 120 | |
| 121 client = FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
| 122 ASSERT_TRUE(client); | |
| 123 EXPECT_EQ(kOrigin1, client->origin); | |
| 124 EXPECT_EQ(kEndpoint1, client->endpoint); | |
| 125 EXPECT_TRUE(client->subdomains); | |
| 126 EXPECT_EQ(kGroup2, client->group); | |
| 127 EXPECT_EQ(kExpires2, client->expires); | |
| 128 | |
| 129 cache_.RemoveClients(std::vector<const ReportingClient*>{client}); | |
| 130 | |
| 131 client = FindClientInCache(&cache_, kOrigin1, kEndpoint1); | |
| 132 EXPECT_FALSE(client); | |
| 133 } | |
| 134 | |
| 135 TEST_F(ReportingCacheTest, GetClientsForOriginAndGroup) { | |
| 136 cache_.SetClient(kOrigin1, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 137 kGroup1, kExpires1); | |
| 138 cache_.SetClient(kOrigin1, kEndpoint2, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 139 kGroup2, kExpires1); | |
| 140 cache_.SetClient(kOrigin2, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 141 kGroup1, kExpires1); | |
| 142 | |
| 143 std::vector<const ReportingClient*> clients; | |
| 144 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
| 145 ASSERT_EQ(1u, clients.size()); | |
| 146 const ReportingClient* client = clients[0]; | |
| 147 ASSERT_TRUE(client); | |
| 148 EXPECT_EQ(kOrigin1, client->origin); | |
| 149 EXPECT_EQ(kGroup1, client->group); | |
| 150 } | |
| 151 | |
| 152 TEST_F(ReportingCacheTest, RemoveClientForOriginAndEndpoint) { | |
| 153 cache_.SetClient(kOrigin1, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 154 kGroup1, kExpires1); | |
| 155 cache_.SetClient(kOrigin1, kEndpoint2, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 156 kGroup2, kExpires1); | |
| 157 cache_.SetClient(kOrigin2, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 158 kGroup1, kExpires1); | |
| 159 | |
| 160 cache_.RemoveClientForOriginAndEndpoint(kOrigin1, kEndpoint1); | |
| 161 | |
| 162 std::vector<const ReportingClient*> clients; | |
| 163 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
| 164 EXPECT_TRUE(clients.empty()); | |
| 165 | |
| 166 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup2, &clients); | |
| 167 EXPECT_EQ(1u, clients.size()); | |
| 168 | |
| 169 cache_.GetClientsForOriginAndGroup(kOrigin2, kGroup1, &clients); | |
| 170 EXPECT_EQ(1u, clients.size()); | |
| 171 } | |
| 172 | |
| 173 TEST_F(ReportingCacheTest, RemoveClientsForEndpoint) { | |
| 174 cache_.SetClient(kOrigin1, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 175 kGroup1, kExpires1); | |
| 176 cache_.SetClient(kOrigin1, kEndpoint2, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 177 kGroup2, kExpires1); | |
| 178 cache_.SetClient(kOrigin2, kEndpoint1, ReportingClient::EXCLUDE_SUBDOMAINS, | |
| 179 kGroup1, kExpires1); | |
| 180 | |
| 181 cache_.RemoveClientsForEndpoint(kEndpoint1); | |
| 182 | |
| 183 std::vector<const ReportingClient*> clients; | |
| 184 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup1, &clients); | |
| 185 EXPECT_TRUE(clients.empty()); | |
| 186 | |
| 187 cache_.GetClientsForOriginAndGroup(kOrigin1, kGroup2, &clients); | |
| 188 EXPECT_EQ(1u, clients.size()); | |
| 189 | |
| 190 cache_.GetClientsForOriginAndGroup(kOrigin2, kGroup1, &clients); | |
| 191 EXPECT_TRUE(clients.empty()); | |
| 192 } | |
| 193 | |
| 194 } // namespace | |
| 195 } // namespace net | |
| OLD | NEW |