reporting_header_parser_unittest.cc 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. #include "net/reporting/reporting_header_parser.h"
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/json/json_reader.h"
  10. #include "base/strings/strcat.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/test/metrics/histogram_tester.h"
  13. #include "base/test/scoped_feature_list.h"
  14. #include "base/test/simple_test_tick_clock.h"
  15. #include "base/time/time.h"
  16. #include "base/values.h"
  17. #include "net/base/features.h"
  18. #include "net/base/isolation_info.h"
  19. #include "net/base/schemeful_site.h"
  20. #include "net/reporting/mock_persistent_reporting_store.h"
  21. #include "net/reporting/reporting_cache.h"
  22. #include "net/reporting/reporting_endpoint.h"
  23. #include "net/reporting/reporting_test_util.h"
  24. #include "testing/gtest/include/gtest/gtest.h"
  25. #include "third_party/abseil-cpp/absl/types/optional.h"
  26. #include "url/gurl.h"
  27. #include "url/origin.h"
  28. namespace net {
  29. namespace {
  30. using CommandType = MockPersistentReportingStore::Command::Type;
  31. using Dictionary = structured_headers::Dictionary;
  32. constexpr char kReportingHeaderTypeHistogram[] = "Net.Reporting.HeaderType";
  33. class ReportingHeaderParserTestBase
  34. : public ReportingTestBase,
  35. public ::testing::WithParamInterface<bool> {
  36. protected:
  37. ReportingHeaderParserTestBase() {
  38. ReportingPolicy policy;
  39. policy.max_endpoints_per_origin = 10;
  40. policy.max_endpoint_count = 20;
  41. UsePolicy(policy);
  42. if (GetParam())
  43. store_ = std::make_unique<MockPersistentReportingStore>();
  44. else
  45. store_ = nullptr;
  46. UseStore(store_.get());
  47. }
  48. ~ReportingHeaderParserTestBase() override = default;
  49. void SetUp() override {
  50. // All ReportingCache methods assume that the store has been initialized.
  51. if (mock_store()) {
  52. mock_store()->LoadReportingClients(
  53. base::BindOnce(&ReportingCache::AddClientsLoadedFromStore,
  54. base::Unretained(cache())));
  55. mock_store()->FinishLoading(true);
  56. }
  57. }
  58. MockPersistentReportingStore* mock_store() { return store_.get(); }
  59. base::test::ScopedFeatureList feature_list_;
  60. const GURL kUrl1_ = GURL("https://origin1.test/path");
  61. const url::Origin kOrigin1_ = url::Origin::Create(kUrl1_);
  62. const GURL kUrl2_ = GURL("https://origin2.test/path");
  63. const url::Origin kOrigin2_ = url::Origin::Create(kUrl2_);
  64. const NetworkIsolationKey kNik_ =
  65. NetworkIsolationKey(SchemefulSite(kOrigin1_), SchemefulSite(kOrigin1_));
  66. const NetworkIsolationKey kOtherNik_ =
  67. NetworkIsolationKey(SchemefulSite(kOrigin2_), SchemefulSite(kOrigin2_));
  68. const IsolationInfo kIsolationInfo_ =
  69. IsolationInfo::Create(IsolationInfo::RequestType::kOther,
  70. kOrigin1_,
  71. kOrigin1_,
  72. SiteForCookies::FromOrigin(kOrigin1_));
  73. const GURL kUrlEtld_ = GURL("https://co.uk/foo.html/");
  74. const url::Origin kOriginEtld_ = url::Origin::Create(kUrlEtld_);
  75. const GURL kEndpoint1_ = GURL("https://endpoint1.test/");
  76. const GURL kEndpoint2_ = GURL("https://endpoint2.test/");
  77. const GURL kEndpoint3_ = GURL("https://endpoint3.test/");
  78. const GURL kEndpointPathAbsolute_ =
  79. GURL("https://origin1.test/path-absolute-url");
  80. const std::string kGroup1_ = "group1";
  81. const std::string kGroup2_ = "group2";
  82. // There are 2^3 = 8 of these to test the different combinations of matching
  83. // vs mismatching NIK, origin, and group.
  84. const ReportingEndpointGroupKey kGroupKey11_ =
  85. ReportingEndpointGroupKey(kNik_, kOrigin1_, kGroup1_);
  86. const ReportingEndpointGroupKey kGroupKey21_ =
  87. ReportingEndpointGroupKey(kNik_, kOrigin2_, kGroup1_);
  88. const ReportingEndpointGroupKey kGroupKey12_ =
  89. ReportingEndpointGroupKey(kNik_, kOrigin1_, kGroup2_);
  90. const ReportingEndpointGroupKey kGroupKey22_ =
  91. ReportingEndpointGroupKey(kNik_, kOrigin2_, kGroup2_);
  92. private:
  93. std::unique_ptr<MockPersistentReportingStore> store_;
  94. };
  95. // This test is parametrized on a boolean that represents whether to use a
  96. // MockPersistentReportingStore.
  97. class ReportingHeaderParserTest : public ReportingHeaderParserTestBase {
  98. protected:
  99. ReportingHeaderParserTest() {
  100. // This is a private API of the reporting service, so no need to test the
  101. // case kPartitionNelAndReportingByNetworkIsolationKey is disabled - the
  102. // feature is only applied at the entry points of the service.
  103. feature_list_.InitAndEnableFeature(
  104. features::kPartitionNelAndReportingByNetworkIsolationKey);
  105. }
  106. ReportingEndpointGroup MakeEndpointGroup(
  107. const std::string& name,
  108. const std::vector<ReportingEndpoint::EndpointInfo>& endpoints,
  109. OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT,
  110. base::TimeDelta ttl = base::Days(1),
  111. url::Origin origin = url::Origin()) {
  112. ReportingEndpointGroupKey group_key(kNik_ /* unused */,
  113. url::Origin() /* unused */, name);
  114. ReportingEndpointGroup group;
  115. group.group_key = group_key;
  116. group.include_subdomains = include_subdomains;
  117. group.ttl = ttl;
  118. group.endpoints = std::move(endpoints);
  119. return group;
  120. }
  121. // Constructs a string which would represent a single group in a Report-To
  122. // header. If |group_name| is an empty string, the group name will be omitted
  123. // (and thus default to "default" when parsed). Setting |omit_defaults| omits
  124. // the priority, weight, and include_subdomains fields if they are default,
  125. // otherwise they are spelled out fully.
  126. std::string ConstructHeaderGroupString(const ReportingEndpointGroup& group,
  127. bool omit_defaults = true) {
  128. std::ostringstream s;
  129. s << "{ ";
  130. if (!group.group_key.group_name.empty()) {
  131. s << "\"group\": \"" << group.group_key.group_name << "\", ";
  132. }
  133. s << "\"max_age\": " << group.ttl.InSeconds() << ", ";
  134. if (group.include_subdomains != OriginSubdomains::DEFAULT) {
  135. s << "\"include_subdomains\": true, ";
  136. } else if (!omit_defaults) {
  137. s << "\"include_subdomains\": false, ";
  138. }
  139. s << "\"endpoints\": [";
  140. for (const ReportingEndpoint::EndpointInfo& endpoint_info :
  141. group.endpoints) {
  142. s << "{ ";
  143. s << "\"url\": \"" << endpoint_info.url.spec() << "\"";
  144. if (!omit_defaults ||
  145. endpoint_info.priority !=
  146. ReportingEndpoint::EndpointInfo::kDefaultPriority) {
  147. s << ", \"priority\": " << endpoint_info.priority;
  148. }
  149. if (!omit_defaults ||
  150. endpoint_info.weight !=
  151. ReportingEndpoint::EndpointInfo::kDefaultWeight) {
  152. s << ", \"weight\": " << endpoint_info.weight;
  153. }
  154. s << " }, ";
  155. }
  156. if (!group.endpoints.empty())
  157. s.seekp(-2, s.cur); // Overwrite trailing comma and space.
  158. s << "]";
  159. s << " }";
  160. return s.str();
  161. }
  162. void ParseHeader(const NetworkIsolationKey& network_isolation_key,
  163. const url::Origin& origin,
  164. const std::string& json) {
  165. absl::optional<base::Value> value =
  166. base::JSONReader::Read("[" + json + "]");
  167. if (value) {
  168. ReportingHeaderParser::ParseReportToHeader(
  169. context(), network_isolation_key, origin, value->GetList());
  170. }
  171. }
  172. };
  173. // TODO(juliatuttle): Ideally these tests should be expecting that JSON parsing
  174. // (and therefore header parsing) may happen asynchronously, but the entire
  175. // pipeline is also tested by NetworkErrorLoggingEndToEndTest.
  176. TEST_P(ReportingHeaderParserTest, Invalid) {
  177. static const struct {
  178. const char* header_value;
  179. const char* description;
  180. } kInvalidHeaderTestCases[] = {
  181. {"{\"max_age\":1, \"endpoints\": [{}]}", "missing url"},
  182. {"{\"max_age\":1, \"endpoints\": [{\"url\":0}]}", "non-string url"},
  183. {"{\"max_age\":1, \"endpoints\": [{\"url\":\"//scheme/relative\"}]}",
  184. "scheme-relative url"},
  185. {"{\"max_age\":1, \"endpoints\": [{\"url\":\"relative/path\"}]}",
  186. "path relative url"},
  187. {"{\"max_age\":1, \"endpoints\": [{\"url\":\"http://insecure/\"}]}",
  188. "insecure url"},
  189. {"{\"endpoints\": [{\"url\":\"https://endpoint/\"}]}", "missing max_age"},
  190. {"{\"max_age\":\"\", \"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
  191. "non-integer max_age"},
  192. {"{\"max_age\":-1, \"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
  193. "negative max_age"},
  194. {"{\"max_age\":1, \"group\":0, "
  195. "\"endpoints\": [{\"url\":\"https://endpoint/\"}]}",
  196. "non-string group"},
  197. // Note that a non-boolean include_subdomains field is *not* invalid, per
  198. // the spec.
  199. // Priority should be a nonnegative integer.
  200. {"{\"max_age\":1, "
  201. "\"endpoints\": [{\"url\":\"https://endpoint/\",\"priority\":\"\"}]}",
  202. "non-integer priority"},
  203. {"{\"max_age\":1, "
  204. "\"endpoints\": [{\"url\":\"https://endpoint/\",\"priority\":-1}]}",
  205. "negative priority"},
  206. // Weight should be a non-negative integer.
  207. {"{\"max_age\":1, "
  208. "\"endpoints\": [{\"url\":\"https://endpoint/\",\"weight\":\"\"}]}",
  209. "non-integer weight"},
  210. {"{\"max_age\":1, "
  211. "\"endpoints\": [{\"url\":\"https://endpoint/\",\"weight\":-1}]}",
  212. "negative weight"},
  213. {"[{\"max_age\":1, \"endpoints\": [{\"url\":\"https://a/\"}]},"
  214. "{\"max_age\":1, \"endpoints\": [{\"url\":\"https://b/\"}]}]",
  215. "wrapped in list"}};
  216. base::HistogramTester histograms;
  217. int invalid_case_count = 0;
  218. for (const auto& test_case : kInvalidHeaderTestCases) {
  219. ParseHeader(kNik_, kOrigin1_, test_case.header_value);
  220. invalid_case_count++;
  221. EXPECT_EQ(0u, cache()->GetEndpointCount())
  222. << "Invalid Report-To header (" << test_case.description << ": \""
  223. << test_case.header_value << "\") parsed as valid.";
  224. histograms.ExpectBucketCount(
  225. kReportingHeaderTypeHistogram,
  226. ReportingHeaderParser::ReportingHeaderType::kReportToInvalid,
  227. invalid_case_count);
  228. if (mock_store()) {
  229. mock_store()->Flush();
  230. EXPECT_EQ(0, mock_store()->StoredEndpointsCount());
  231. EXPECT_EQ(0, mock_store()->StoredEndpointGroupsCount());
  232. }
  233. }
  234. histograms.ExpectBucketCount(
  235. kReportingHeaderTypeHistogram,
  236. ReportingHeaderParser::ReportingHeaderType::kReportTo, 0);
  237. }
  238. TEST_P(ReportingHeaderParserTest, Basic) {
  239. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  240. base::HistogramTester histograms;
  241. std::string header =
  242. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  243. ParseHeader(kNik_, kOrigin1_, header);
  244. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  245. histograms.ExpectBucketCount(
  246. kReportingHeaderTypeHistogram,
  247. ReportingHeaderParser::ReportingHeaderType::kReportTo, 1);
  248. EXPECT_TRUE(
  249. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  250. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  251. EXPECT_EQ(1u, cache()->GetEndpointCount());
  252. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  253. ASSERT_TRUE(endpoint);
  254. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  255. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  256. EXPECT_EQ(kEndpoint1_, endpoint.info.url);
  257. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  258. endpoint.info.priority);
  259. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  260. endpoint.info.weight);
  261. if (mock_store()) {
  262. mock_store()->Flush();
  263. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  264. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  265. MockPersistentReportingStore::CommandList expected_commands;
  266. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  267. kGroupKey11_, kEndpoint1_);
  268. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  269. kGroupKey11_);
  270. EXPECT_THAT(mock_store()->GetAllCommands(),
  271. testing::IsSupersetOf(expected_commands));
  272. }
  273. }
  274. TEST_P(ReportingHeaderParserTest, PathAbsoluteURLEndpoint) {
  275. std::string header =
  276. "{\"group\": \"group1\", \"max_age\":1, \"endpoints\": "
  277. "[{\"url\":\"/path-absolute-url\"}]}";
  278. base::HistogramTester histograms;
  279. ParseHeader(kNik_, kOrigin1_, header);
  280. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  281. histograms.ExpectBucketCount(
  282. kReportingHeaderTypeHistogram,
  283. ReportingHeaderParser::ReportingHeaderType::kReportTo, 1);
  284. EXPECT_TRUE(
  285. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  286. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  287. EXPECT_EQ(1u, cache()->GetEndpointCount());
  288. ReportingEndpoint endpoint =
  289. FindEndpointInCache(kGroupKey11_, kEndpointPathAbsolute_);
  290. ASSERT_TRUE(endpoint);
  291. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  292. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  293. EXPECT_EQ(kEndpointPathAbsolute_, endpoint.info.url);
  294. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  295. endpoint.info.priority);
  296. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  297. endpoint.info.weight);
  298. if (mock_store()) {
  299. mock_store()->Flush();
  300. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  301. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  302. MockPersistentReportingStore::CommandList expected_commands;
  303. expected_commands.emplace_back(
  304. CommandType::ADD_REPORTING_ENDPOINT,
  305. ReportingEndpoint(kGroupKey11_, ReportingEndpoint::EndpointInfo{
  306. kEndpointPathAbsolute_}));
  307. expected_commands.emplace_back(
  308. CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  309. CachedReportingEndpointGroup(
  310. kGroupKey11_, OriginSubdomains::DEFAULT /* irrelevant */,
  311. base::Time() /* irrelevant */, base::Time() /* irrelevant */));
  312. EXPECT_THAT(mock_store()->GetAllCommands(),
  313. testing::IsSupersetOf(expected_commands));
  314. }
  315. }
  316. TEST_P(ReportingHeaderParserTest, OmittedGroupName) {
  317. ReportingEndpointGroupKey kGroupKey(kNik_, kOrigin1_, "default");
  318. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  319. std::string header =
  320. ConstructHeaderGroupString(MakeEndpointGroup(std::string(), endpoints));
  321. ParseHeader(kNik_, kOrigin1_, header);
  322. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  323. EXPECT_TRUE(EndpointGroupExistsInCache(kGroupKey, OriginSubdomains::DEFAULT));
  324. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  325. EXPECT_EQ(1u, cache()->GetEndpointCount());
  326. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey, kEndpoint1_);
  327. ASSERT_TRUE(endpoint);
  328. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  329. EXPECT_EQ("default", endpoint.group_key.group_name);
  330. EXPECT_EQ(kEndpoint1_, endpoint.info.url);
  331. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  332. endpoint.info.priority);
  333. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  334. endpoint.info.weight);
  335. if (mock_store()) {
  336. mock_store()->Flush();
  337. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  338. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  339. MockPersistentReportingStore::CommandList expected_commands;
  340. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  341. kGroupKey, kEndpoint1_);
  342. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  343. kGroupKey);
  344. EXPECT_THAT(mock_store()->GetAllCommands(),
  345. testing::IsSupersetOf(expected_commands));
  346. }
  347. }
  348. TEST_P(ReportingHeaderParserTest, IncludeSubdomainsTrue) {
  349. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  350. std::string header = ConstructHeaderGroupString(
  351. MakeEndpointGroup(kGroup1_, endpoints, OriginSubdomains::INCLUDE));
  352. ParseHeader(kNik_, kOrigin1_, header);
  353. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  354. EXPECT_TRUE(
  355. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::INCLUDE));
  356. EXPECT_EQ(1u, cache()->GetEndpointCount());
  357. EXPECT_TRUE(EndpointExistsInCache(kGroupKey11_, kEndpoint1_));
  358. if (mock_store()) {
  359. mock_store()->Flush();
  360. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  361. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  362. MockPersistentReportingStore::CommandList expected_commands;
  363. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  364. kGroupKey11_, kEndpoint1_);
  365. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  366. kGroupKey11_);
  367. EXPECT_THAT(mock_store()->GetAllCommands(),
  368. testing::IsSupersetOf(expected_commands));
  369. }
  370. }
  371. TEST_P(ReportingHeaderParserTest, IncludeSubdomainsFalse) {
  372. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  373. std::string header = ConstructHeaderGroupString(
  374. MakeEndpointGroup(kGroup1_, endpoints, OriginSubdomains::EXCLUDE),
  375. false /* omit_defaults */);
  376. ParseHeader(kNik_, kOrigin1_, header);
  377. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  378. EXPECT_TRUE(
  379. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::EXCLUDE));
  380. EXPECT_EQ(1u, cache()->GetEndpointCount());
  381. EXPECT_TRUE(EndpointExistsInCache(kGroupKey11_, kEndpoint1_));
  382. if (mock_store()) {
  383. mock_store()->Flush();
  384. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  385. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  386. MockPersistentReportingStore::CommandList expected_commands;
  387. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  388. kGroupKey11_, kEndpoint1_);
  389. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  390. kGroupKey11_);
  391. EXPECT_THAT(mock_store()->GetAllCommands(),
  392. testing::IsSupersetOf(expected_commands));
  393. }
  394. }
  395. TEST_P(ReportingHeaderParserTest, IncludeSubdomainsEtldRejected) {
  396. ReportingEndpointGroupKey kGroupKey(kNik_, kOriginEtld_, kGroup1_);
  397. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  398. std::string header = ConstructHeaderGroupString(
  399. MakeEndpointGroup(kGroup1_, endpoints, OriginSubdomains::INCLUDE));
  400. ParseHeader(kNik_, kOriginEtld_, header);
  401. EXPECT_EQ(0u, cache()->GetEndpointGroupCountForTesting());
  402. EXPECT_FALSE(
  403. EndpointGroupExistsInCache(kGroupKey, OriginSubdomains::INCLUDE));
  404. EXPECT_EQ(0u, cache()->GetEndpointCount());
  405. EXPECT_FALSE(EndpointExistsInCache(kGroupKey, kEndpoint1_));
  406. }
  407. TEST_P(ReportingHeaderParserTest, NonIncludeSubdomainsEtldAccepted) {
  408. ReportingEndpointGroupKey kGroupKey(kNik_, kOriginEtld_, kGroup1_);
  409. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  410. std::string header = ConstructHeaderGroupString(
  411. MakeEndpointGroup(kGroup1_, endpoints, OriginSubdomains::EXCLUDE));
  412. ParseHeader(kNik_, kOriginEtld_, header);
  413. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  414. EXPECT_TRUE(EndpointGroupExistsInCache(kGroupKey, OriginSubdomains::EXCLUDE));
  415. EXPECT_EQ(1u, cache()->GetEndpointCount());
  416. EXPECT_TRUE(EndpointExistsInCache(kGroupKey, kEndpoint1_));
  417. }
  418. TEST_P(ReportingHeaderParserTest, IncludeSubdomainsNotBoolean) {
  419. std::string header =
  420. "{\"group\": \"" + kGroup1_ +
  421. "\", "
  422. "\"max_age\":86400, \"include_subdomains\": \"NotABoolean\", "
  423. "\"endpoints\": [{\"url\":\"" +
  424. kEndpoint1_.spec() + "\"}]}";
  425. ParseHeader(kNik_, kOrigin1_, header);
  426. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  427. EXPECT_TRUE(
  428. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  429. EXPECT_EQ(1u, cache()->GetEndpointCount());
  430. EXPECT_TRUE(EndpointExistsInCache(kGroupKey11_, kEndpoint1_));
  431. if (mock_store()) {
  432. mock_store()->Flush();
  433. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  434. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  435. MockPersistentReportingStore::CommandList expected_commands;
  436. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  437. kGroupKey11_, kEndpoint1_);
  438. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  439. kGroupKey11_);
  440. EXPECT_THAT(mock_store()->GetAllCommands(),
  441. testing::IsSupersetOf(expected_commands));
  442. }
  443. }
  444. TEST_P(ReportingHeaderParserTest, NonDefaultPriority) {
  445. const int kNonDefaultPriority = 10;
  446. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {
  447. {kEndpoint1_, kNonDefaultPriority}};
  448. std::string header =
  449. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  450. ParseHeader(kNik_, kOrigin1_, header);
  451. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  452. EXPECT_TRUE(
  453. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  454. EXPECT_EQ(1u, cache()->GetEndpointCount());
  455. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  456. ASSERT_TRUE(endpoint);
  457. EXPECT_EQ(kNonDefaultPriority, endpoint.info.priority);
  458. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  459. endpoint.info.weight);
  460. if (mock_store()) {
  461. mock_store()->Flush();
  462. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  463. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  464. MockPersistentReportingStore::CommandList expected_commands;
  465. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  466. kGroupKey11_, kEndpoint1_);
  467. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  468. kGroupKey11_);
  469. EXPECT_THAT(mock_store()->GetAllCommands(),
  470. testing::IsSupersetOf(expected_commands));
  471. }
  472. }
  473. TEST_P(ReportingHeaderParserTest, NonDefaultWeight) {
  474. const int kNonDefaultWeight = 10;
  475. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {
  476. {kEndpoint1_, ReportingEndpoint::EndpointInfo::kDefaultPriority,
  477. kNonDefaultWeight}};
  478. std::string header =
  479. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  480. ParseHeader(kNik_, kOrigin1_, header);
  481. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  482. EXPECT_TRUE(
  483. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  484. EXPECT_EQ(1u, cache()->GetEndpointCount());
  485. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  486. ASSERT_TRUE(endpoint);
  487. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  488. endpoint.info.priority);
  489. EXPECT_EQ(kNonDefaultWeight, endpoint.info.weight);
  490. if (mock_store()) {
  491. mock_store()->Flush();
  492. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  493. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  494. MockPersistentReportingStore::CommandList expected_commands;
  495. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  496. kGroupKey11_, kEndpoint1_);
  497. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  498. kGroupKey11_);
  499. EXPECT_THAT(mock_store()->GetAllCommands(),
  500. testing::IsSupersetOf(expected_commands));
  501. }
  502. }
  503. TEST_P(ReportingHeaderParserTest, MaxAge) {
  504. const int kMaxAgeSecs = 100;
  505. base::TimeDelta ttl = base::Seconds(kMaxAgeSecs);
  506. base::Time expires = clock()->Now() + ttl;
  507. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  508. std::string header = ConstructHeaderGroupString(
  509. MakeEndpointGroup(kGroup1_, endpoints, OriginSubdomains::DEFAULT, ttl));
  510. ParseHeader(kNik_, kOrigin1_, header);
  511. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  512. EXPECT_TRUE(EndpointGroupExistsInCache(kGroupKey11_,
  513. OriginSubdomains::DEFAULT, expires));
  514. if (mock_store()) {
  515. mock_store()->Flush();
  516. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  517. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  518. MockPersistentReportingStore::CommandList expected_commands;
  519. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  520. kGroupKey11_, kEndpoint1_);
  521. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  522. kGroupKey11_);
  523. EXPECT_THAT(mock_store()->GetAllCommands(),
  524. testing::IsSupersetOf(expected_commands));
  525. }
  526. }
  527. TEST_P(ReportingHeaderParserTest, MultipleEndpointsSameGroup) {
  528. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_},
  529. {kEndpoint2_}};
  530. std::string header =
  531. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  532. ParseHeader(kNik_, kOrigin1_, header);
  533. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  534. EXPECT_TRUE(
  535. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  536. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  537. EXPECT_EQ(2u, cache()->GetEndpointCount());
  538. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  539. ASSERT_TRUE(endpoint);
  540. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  541. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  542. EXPECT_EQ(kEndpoint1_, endpoint.info.url);
  543. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  544. endpoint.info.priority);
  545. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  546. endpoint.info.weight);
  547. ReportingEndpoint endpoint2 = FindEndpointInCache(kGroupKey11_, kEndpoint2_);
  548. ASSERT_TRUE(endpoint2);
  549. EXPECT_EQ(kOrigin1_, endpoint2.group_key.origin);
  550. EXPECT_EQ(kGroup1_, endpoint2.group_key.group_name);
  551. EXPECT_EQ(kEndpoint2_, endpoint2.info.url);
  552. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  553. endpoint2.info.priority);
  554. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  555. endpoint2.info.weight);
  556. if (mock_store()) {
  557. mock_store()->Flush();
  558. EXPECT_EQ(2, mock_store()->StoredEndpointsCount());
  559. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  560. MockPersistentReportingStore::CommandList expected_commands;
  561. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  562. kGroupKey11_, kEndpoint1_);
  563. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  564. kGroupKey11_, kEndpoint2_);
  565. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  566. kGroupKey11_);
  567. EXPECT_THAT(mock_store()->GetAllCommands(),
  568. testing::IsSupersetOf(expected_commands));
  569. }
  570. }
  571. TEST_P(ReportingHeaderParserTest, MultipleEndpointsDifferentGroups) {
  572. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_}};
  573. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint1_}};
  574. std::string header =
  575. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  576. ", " +
  577. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints2));
  578. ParseHeader(kNik_, kOrigin1_, header);
  579. EXPECT_EQ(2u, cache()->GetEndpointGroupCountForTesting());
  580. EXPECT_TRUE(
  581. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  582. EXPECT_TRUE(
  583. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  584. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  585. EXPECT_EQ(2u, cache()->GetEndpointCount());
  586. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  587. ASSERT_TRUE(endpoint);
  588. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  589. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  590. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  591. endpoint.info.priority);
  592. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  593. endpoint.info.weight);
  594. ReportingEndpoint endpoint2 = FindEndpointInCache(kGroupKey12_, kEndpoint1_);
  595. ASSERT_TRUE(endpoint2);
  596. EXPECT_EQ(kOrigin1_, endpoint2.group_key.origin);
  597. EXPECT_EQ(kGroup2_, endpoint2.group_key.group_name);
  598. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  599. endpoint2.info.priority);
  600. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  601. endpoint2.info.weight);
  602. if (mock_store()) {
  603. mock_store()->Flush();
  604. EXPECT_EQ(2, mock_store()->StoredEndpointsCount());
  605. EXPECT_EQ(2, mock_store()->StoredEndpointGroupsCount());
  606. MockPersistentReportingStore::CommandList expected_commands;
  607. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  608. kGroupKey11_, kEndpoint1_);
  609. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  610. kGroupKey12_, kEndpoint1_);
  611. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  612. kGroupKey11_);
  613. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  614. kGroupKey12_);
  615. EXPECT_THAT(mock_store()->GetAllCommands(),
  616. testing::IsSupersetOf(expected_commands));
  617. }
  618. }
  619. TEST_P(ReportingHeaderParserTest, MultipleHeadersFromDifferentOrigins) {
  620. // First origin sets a header with two endpoints in the same group.
  621. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_},
  622. {kEndpoint2_}};
  623. std::string header1 =
  624. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1));
  625. ParseHeader(kNik_, kOrigin1_, header1);
  626. // Second origin has two endpoint groups.
  627. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint1_}};
  628. std::vector<ReportingEndpoint::EndpointInfo> endpoints3 = {{kEndpoint2_}};
  629. std::string header2 =
  630. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints2)) +
  631. ", " +
  632. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints3));
  633. ParseHeader(kNik_, kOrigin2_, header2);
  634. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  635. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin2_));
  636. EXPECT_EQ(3u, cache()->GetEndpointGroupCountForTesting());
  637. EXPECT_TRUE(
  638. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  639. EXPECT_TRUE(
  640. EndpointGroupExistsInCache(kGroupKey21_, OriginSubdomains::DEFAULT));
  641. EXPECT_TRUE(
  642. EndpointGroupExistsInCache(kGroupKey22_, OriginSubdomains::DEFAULT));
  643. EXPECT_EQ(4u, cache()->GetEndpointCount());
  644. EXPECT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  645. EXPECT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint2_));
  646. EXPECT_TRUE(FindEndpointInCache(kGroupKey21_, kEndpoint1_));
  647. EXPECT_TRUE(FindEndpointInCache(kGroupKey22_, kEndpoint2_));
  648. if (mock_store()) {
  649. mock_store()->Flush();
  650. EXPECT_EQ(4, mock_store()->StoredEndpointsCount());
  651. EXPECT_EQ(3, mock_store()->StoredEndpointGroupsCount());
  652. MockPersistentReportingStore::CommandList expected_commands;
  653. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  654. kGroupKey11_, kEndpoint1_);
  655. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  656. kGroupKey11_, kEndpoint2_);
  657. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  658. kGroupKey21_, kEndpoint1_);
  659. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  660. kGroupKey22_, kEndpoint2_);
  661. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  662. kGroupKey11_);
  663. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  664. kGroupKey21_);
  665. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  666. kGroupKey22_);
  667. EXPECT_THAT(mock_store()->GetAllCommands(),
  668. testing::IsSupersetOf(expected_commands));
  669. }
  670. }
  671. // Test that each combination of NIK, origin, and group name is considered
  672. // distinct.
  673. // See also: ReportingCacheTest.ClientsKeyedByEndpointGroupKey
  674. TEST_P(ReportingHeaderParserTest, EndpointGroupKey) {
  675. // Raise the endpoint limits for this test.
  676. ReportingPolicy policy;
  677. policy.max_endpoints_per_origin = 5; // This test should use 4.
  678. policy.max_endpoint_count = 20; // This test should use 16.
  679. UsePolicy(policy);
  680. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_},
  681. {kEndpoint2_}};
  682. std::string header1 =
  683. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  684. ", " +
  685. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints1));
  686. const ReportingEndpointGroupKey kOtherGroupKey11 =
  687. ReportingEndpointGroupKey(kOtherNik_, kOrigin1_, kGroup1_);
  688. const ReportingEndpointGroupKey kOtherGroupKey21 =
  689. ReportingEndpointGroupKey(kOtherNik_, kOrigin2_, kGroup1_);
  690. const ReportingEndpointGroupKey kOtherGroupKey12 =
  691. ReportingEndpointGroupKey(kOtherNik_, kOrigin1_, kGroup2_);
  692. const ReportingEndpointGroupKey kOtherGroupKey22 =
  693. ReportingEndpointGroupKey(kOtherNik_, kOrigin2_, kGroup2_);
  694. const struct {
  695. NetworkIsolationKey network_isolation_key;
  696. GURL url;
  697. ReportingEndpointGroupKey group1_key;
  698. ReportingEndpointGroupKey group2_key;
  699. } kHeaderSources[] = {
  700. {kNik_, kUrl1_, kGroupKey11_, kGroupKey12_},
  701. {kNik_, kUrl2_, kGroupKey21_, kGroupKey22_},
  702. {kOtherNik_, kUrl1_, kOtherGroupKey11, kOtherGroupKey12},
  703. {kOtherNik_, kUrl2_, kOtherGroupKey21, kOtherGroupKey22},
  704. };
  705. size_t endpoint_group_count = 0u;
  706. size_t endpoint_count = 0u;
  707. MockPersistentReportingStore::CommandList expected_commands;
  708. // Set 2 endpoints in each of 2 groups for each of 2x2 combinations of
  709. // (NIK, origin).
  710. for (const auto& source : kHeaderSources) {
  711. // Verify pre-parsing state
  712. EXPECT_FALSE(FindEndpointInCache(source.group1_key, kEndpoint1_));
  713. EXPECT_FALSE(FindEndpointInCache(source.group1_key, kEndpoint2_));
  714. EXPECT_FALSE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  715. EXPECT_FALSE(FindEndpointInCache(source.group2_key, kEndpoint2_));
  716. EXPECT_FALSE(EndpointGroupExistsInCache(source.group1_key,
  717. OriginSubdomains::DEFAULT));
  718. EXPECT_FALSE(EndpointGroupExistsInCache(source.group2_key,
  719. OriginSubdomains::DEFAULT));
  720. ParseHeader(source.network_isolation_key, url::Origin::Create(source.url),
  721. header1);
  722. endpoint_group_count += 2u;
  723. endpoint_count += 4u;
  724. EXPECT_EQ(endpoint_group_count, cache()->GetEndpointGroupCountForTesting());
  725. EXPECT_EQ(endpoint_count, cache()->GetEndpointCount());
  726. // Verify post-parsing state
  727. EXPECT_TRUE(FindEndpointInCache(source.group1_key, kEndpoint1_));
  728. EXPECT_TRUE(FindEndpointInCache(source.group1_key, kEndpoint2_));
  729. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  730. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint2_));
  731. EXPECT_TRUE(EndpointGroupExistsInCache(source.group1_key,
  732. OriginSubdomains::DEFAULT));
  733. EXPECT_TRUE(EndpointGroupExistsInCache(source.group2_key,
  734. OriginSubdomains::DEFAULT));
  735. if (mock_store()) {
  736. mock_store()->Flush();
  737. EXPECT_EQ(static_cast<int>(endpoint_count),
  738. mock_store()->StoredEndpointsCount());
  739. EXPECT_EQ(static_cast<int>(endpoint_group_count),
  740. mock_store()->StoredEndpointGroupsCount());
  741. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  742. source.group1_key, kEndpoint1_);
  743. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  744. source.group1_key, kEndpoint2_);
  745. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  746. source.group1_key);
  747. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  748. source.group2_key, kEndpoint1_);
  749. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  750. source.group2_key, kEndpoint2_);
  751. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  752. source.group2_key);
  753. EXPECT_THAT(mock_store()->GetAllCommands(),
  754. testing::IsSupersetOf(expected_commands));
  755. }
  756. }
  757. // Check that expected data is present in the ReportingCache at the end.
  758. for (const auto& source : kHeaderSources) {
  759. EXPECT_TRUE(FindEndpointInCache(source.group1_key, kEndpoint1_));
  760. EXPECT_TRUE(FindEndpointInCache(source.group1_key, kEndpoint2_));
  761. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  762. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint2_));
  763. EXPECT_TRUE(EndpointGroupExistsInCache(source.group1_key,
  764. OriginSubdomains::DEFAULT));
  765. EXPECT_TRUE(EndpointGroupExistsInCache(source.group2_key,
  766. OriginSubdomains::DEFAULT));
  767. EXPECT_TRUE(cache()->ClientExistsForTesting(
  768. source.network_isolation_key, url::Origin::Create(source.url)));
  769. }
  770. // Test updating existing configurations
  771. // This removes endpoint 1, updates the priority of endpoint 2, and adds
  772. // endpoint 3.
  773. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint2_, 2},
  774. {kEndpoint3_}};
  775. // Removes group 1, updates include_subdomains for group 2.
  776. std::string header2 = ConstructHeaderGroupString(
  777. MakeEndpointGroup(kGroup2_, endpoints2, OriginSubdomains::INCLUDE));
  778. for (const auto& source : kHeaderSources) {
  779. // Verify pre-update state
  780. EXPECT_TRUE(EndpointGroupExistsInCache(source.group1_key,
  781. OriginSubdomains::DEFAULT));
  782. EXPECT_TRUE(EndpointGroupExistsInCache(source.group2_key,
  783. OriginSubdomains::DEFAULT));
  784. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  785. ReportingEndpoint endpoint =
  786. FindEndpointInCache(source.group2_key, kEndpoint2_);
  787. EXPECT_TRUE(endpoint);
  788. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  789. endpoint.info.priority);
  790. EXPECT_FALSE(FindEndpointInCache(source.group2_key, kEndpoint3_));
  791. ParseHeader(source.network_isolation_key, url::Origin::Create(source.url),
  792. header2);
  793. endpoint_group_count--;
  794. endpoint_count -= 2;
  795. EXPECT_EQ(endpoint_group_count, cache()->GetEndpointGroupCountForTesting());
  796. EXPECT_EQ(endpoint_count, cache()->GetEndpointCount());
  797. // Verify post-update state
  798. EXPECT_FALSE(EndpointGroupExistsInCache(source.group1_key,
  799. OriginSubdomains::DEFAULT));
  800. EXPECT_TRUE(EndpointGroupExistsInCache(source.group2_key,
  801. OriginSubdomains::INCLUDE));
  802. EXPECT_FALSE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  803. endpoint = FindEndpointInCache(source.group2_key, kEndpoint2_);
  804. EXPECT_TRUE(endpoint);
  805. EXPECT_EQ(2, endpoint.info.priority);
  806. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint3_));
  807. if (mock_store()) {
  808. mock_store()->Flush();
  809. EXPECT_EQ(static_cast<int>(endpoint_count),
  810. mock_store()->StoredEndpointsCount());
  811. EXPECT_EQ(static_cast<int>(endpoint_group_count),
  812. mock_store()->StoredEndpointGroupsCount());
  813. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  814. source.group1_key, kEndpoint1_);
  815. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  816. source.group1_key, kEndpoint2_);
  817. expected_commands.emplace_back(
  818. CommandType::DELETE_REPORTING_ENDPOINT_GROUP, source.group1_key);
  819. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  820. source.group2_key, kEndpoint1_);
  821. expected_commands.emplace_back(
  822. CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS, source.group2_key,
  823. kEndpoint2_);
  824. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  825. source.group2_key, kEndpoint3_);
  826. expected_commands.emplace_back(
  827. CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS,
  828. source.group2_key);
  829. EXPECT_THAT(mock_store()->GetAllCommands(),
  830. testing::IsSupersetOf(expected_commands));
  831. }
  832. }
  833. // Check that expected data is present in the ReportingCache at the end.
  834. for (const auto& source : kHeaderSources) {
  835. EXPECT_FALSE(FindEndpointInCache(source.group1_key, kEndpoint1_));
  836. EXPECT_FALSE(FindEndpointInCache(source.group1_key, kEndpoint2_));
  837. EXPECT_FALSE(FindEndpointInCache(source.group2_key, kEndpoint1_));
  838. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint2_));
  839. EXPECT_TRUE(FindEndpointInCache(source.group2_key, kEndpoint3_));
  840. EXPECT_FALSE(EndpointGroupExistsInCache(source.group1_key,
  841. OriginSubdomains::DEFAULT));
  842. EXPECT_TRUE(EndpointGroupExistsInCache(source.group2_key,
  843. OriginSubdomains::INCLUDE));
  844. EXPECT_TRUE(cache()->ClientExistsForTesting(
  845. source.network_isolation_key, url::Origin::Create(source.url)));
  846. }
  847. }
  848. TEST_P(ReportingHeaderParserTest,
  849. HeaderErroneouslyContainsMultipleGroupsOfSameName) {
  850. // Add a preexisting header to test that a header with multiple groups of the
  851. // same name is treated as if it specified a single group with the combined
  852. // set of specified endpoints. In particular, it must overwrite/update any
  853. // preexisting group all at once. See https://crbug.com/1116529.
  854. std::vector<ReportingEndpoint::EndpointInfo> preexisting = {{kEndpoint1_}};
  855. std::string preexisting_header =
  856. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, preexisting));
  857. ParseHeader(kNik_, kOrigin1_, preexisting_header);
  858. EXPECT_TRUE(
  859. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  860. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  861. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  862. EXPECT_EQ(1u, cache()->GetEndpointCount());
  863. ReportingEndpoint endpoint = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  864. ASSERT_TRUE(endpoint);
  865. if (mock_store()) {
  866. mock_store()->Flush();
  867. EXPECT_EQ(1, mock_store()->StoredEndpointsCount());
  868. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  869. MockPersistentReportingStore::CommandList expected_commands;
  870. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  871. kGroupKey11_, kEndpoint1_);
  872. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  873. kGroupKey11_);
  874. EXPECT_THAT(mock_store()->GetAllCommands(),
  875. testing::IsSupersetOf(expected_commands));
  876. // Reset commands so we can check that the next part, adding the header with
  877. // duplicate groups, does not cause clearing of preexisting endpoints twice.
  878. mock_store()->ClearCommands();
  879. }
  880. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_}};
  881. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint2_}};
  882. std::string duplicate_groups_header =
  883. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  884. ", " +
  885. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints2));
  886. ParseHeader(kNik_, kOrigin1_, duplicate_groups_header);
  887. // Result is as if they set the two groups with the same name as one group.
  888. EXPECT_TRUE(
  889. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  890. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  891. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  892. EXPECT_EQ(2u, cache()->GetEndpointCount());
  893. ReportingEndpoint endpoint1 = FindEndpointInCache(kGroupKey11_, kEndpoint1_);
  894. ASSERT_TRUE(endpoint);
  895. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  896. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  897. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  898. endpoint.info.priority);
  899. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  900. endpoint.info.weight);
  901. ReportingEndpoint endpoint2 = FindEndpointInCache(kGroupKey11_, kEndpoint2_);
  902. ASSERT_TRUE(endpoint2);
  903. EXPECT_EQ(kOrigin1_, endpoint2.group_key.origin);
  904. EXPECT_EQ(kGroup1_, endpoint2.group_key.group_name);
  905. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  906. endpoint2.info.priority);
  907. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  908. endpoint2.info.weight);
  909. if (mock_store()) {
  910. mock_store()->Flush();
  911. EXPECT_EQ(2, mock_store()->StoredEndpointsCount());
  912. EXPECT_EQ(1, mock_store()->StoredEndpointGroupsCount());
  913. MockPersistentReportingStore::CommandList expected_commands;
  914. expected_commands.emplace_back(
  915. CommandType::UPDATE_REPORTING_ENDPOINT_DETAILS, kGroupKey11_,
  916. kEndpoint1_);
  917. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  918. kGroupKey11_, kEndpoint2_);
  919. expected_commands.emplace_back(
  920. CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_DETAILS, kGroupKey11_);
  921. MockPersistentReportingStore::CommandList actual_commands =
  922. mock_store()->GetAllCommands();
  923. EXPECT_THAT(actual_commands, testing::IsSupersetOf(expected_commands));
  924. for (const auto& command : actual_commands) {
  925. EXPECT_NE(CommandType::DELETE_REPORTING_ENDPOINT, command.type);
  926. EXPECT_NE(CommandType::DELETE_REPORTING_ENDPOINT_GROUP, command.type);
  927. // The endpoint with URL kEndpoint1_ is only ever updated, not added anew.
  928. EXPECT_NE(
  929. MockPersistentReportingStore::Command(
  930. CommandType::ADD_REPORTING_ENDPOINT, kGroupKey11_, kEndpoint1_),
  931. command);
  932. // The group is only ever updated, not added anew.
  933. EXPECT_NE(MockPersistentReportingStore::Command(
  934. CommandType::ADD_REPORTING_ENDPOINT_GROUP, kGroupKey11_),
  935. command);
  936. }
  937. }
  938. }
  939. TEST_P(ReportingHeaderParserTest,
  940. HeaderErroneouslyContainsGroupsWithRedundantEndpoints) {
  941. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_},
  942. {kEndpoint1_}};
  943. std::string header =
  944. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  945. ParseHeader(kNik_, kOrigin1_, header);
  946. // We should dedupe the identical endpoint URLs.
  947. EXPECT_EQ(1u, cache()->GetEndpointCount());
  948. ASSERT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  949. EXPECT_TRUE(
  950. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  951. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  952. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  953. }
  954. TEST_P(ReportingHeaderParserTest,
  955. HeaderErroneouslyContainsMultipleGroupsOfSameNameAndEndpoints) {
  956. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  957. std::string header =
  958. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints)) +
  959. ", " + ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  960. ParseHeader(kNik_, kOrigin1_, header);
  961. // We should dedupe the identical endpoint URLs, even when they're in
  962. // different group.
  963. EXPECT_EQ(1u, cache()->GetEndpointCount());
  964. ASSERT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  965. EXPECT_TRUE(
  966. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  967. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  968. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  969. }
  970. TEST_P(ReportingHeaderParserTest,
  971. HeaderErroneouslyContainsGroupsOfSameNameAndOverlappingEndpoints) {
  972. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_},
  973. {kEndpoint2_}};
  974. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint1_},
  975. {kEndpoint3_}};
  976. std::string header =
  977. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  978. ", " +
  979. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints2));
  980. ParseHeader(kNik_, kOrigin1_, header);
  981. // We should dedupe the identical endpoint URLs, even when they're in
  982. // different group.
  983. EXPECT_EQ(3u, cache()->GetEndpointCount());
  984. ASSERT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  985. ASSERT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint2_));
  986. ASSERT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint3_));
  987. EXPECT_TRUE(
  988. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  989. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  990. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  991. }
  992. TEST_P(ReportingHeaderParserTest, OverwriteOldHeader) {
  993. // First, the origin sets a header with two endpoints in the same group.
  994. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {
  995. {kEndpoint1_, 10 /* priority */}, {kEndpoint2_}};
  996. std::string header1 =
  997. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1));
  998. ParseHeader(kNik_, kOrigin1_, header1);
  999. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1000. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  1001. EXPECT_TRUE(
  1002. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1003. EXPECT_EQ(2u, cache()->GetEndpointCount());
  1004. EXPECT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  1005. EXPECT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint2_));
  1006. if (mock_store()) {
  1007. mock_store()->Flush();
  1008. EXPECT_EQ(2,
  1009. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1010. EXPECT_EQ(1, mock_store()->CountCommands(
  1011. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1012. MockPersistentReportingStore::CommandList expected_commands;
  1013. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1014. kGroupKey11_, kEndpoint1_);
  1015. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1016. kGroupKey11_, kEndpoint2_);
  1017. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1018. kGroupKey11_);
  1019. EXPECT_THAT(mock_store()->GetAllCommands(),
  1020. testing::IsSupersetOf(expected_commands));
  1021. }
  1022. // Second header from the same origin should overwrite the previous one.
  1023. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {
  1024. // This endpoint should update the priority of the existing one.
  1025. {kEndpoint1_, 20 /* priority */}};
  1026. // The second endpoint in this group will be deleted.
  1027. // This group is new.
  1028. std::vector<ReportingEndpoint::EndpointInfo> endpoints3 = {{kEndpoint2_}};
  1029. std::string header2 =
  1030. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints2)) +
  1031. ", " +
  1032. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints3));
  1033. ParseHeader(kNik_, kOrigin1_, header2);
  1034. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1035. EXPECT_TRUE(
  1036. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1037. EXPECT_TRUE(
  1038. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1039. EXPECT_EQ(2u, cache()->GetEndpointCount());
  1040. EXPECT_TRUE(FindEndpointInCache(kGroupKey11_, kEndpoint1_));
  1041. EXPECT_EQ(20, FindEndpointInCache(kGroupKey11_, kEndpoint1_).info.priority);
  1042. EXPECT_FALSE(FindEndpointInCache(kGroupKey11_, kEndpoint2_));
  1043. EXPECT_TRUE(FindEndpointInCache(kGroupKey12_, kEndpoint2_));
  1044. if (mock_store()) {
  1045. mock_store()->Flush();
  1046. EXPECT_EQ(2 + 1,
  1047. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1048. EXPECT_EQ(1 + 1, mock_store()->CountCommands(
  1049. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1050. EXPECT_EQ(
  1051. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1052. MockPersistentReportingStore::CommandList expected_commands;
  1053. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1054. kGroupKey12_, kEndpoint2_);
  1055. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1056. kGroupKey12_);
  1057. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1058. kGroupKey11_, kEndpoint2_);
  1059. EXPECT_THAT(mock_store()->GetAllCommands(),
  1060. testing::IsSupersetOf(expected_commands));
  1061. }
  1062. }
  1063. TEST_P(ReportingHeaderParserTest, OverwriteOldHeaderWithCompletelyNew) {
  1064. ReportingEndpointGroupKey kGroupKey1(kNik_, kOrigin1_, "1");
  1065. ReportingEndpointGroupKey kGroupKey2(kNik_, kOrigin1_, "2");
  1066. ReportingEndpointGroupKey kGroupKey3(kNik_, kOrigin1_, "3");
  1067. ReportingEndpointGroupKey kGroupKey4(kNik_, kOrigin1_, "4");
  1068. ReportingEndpointGroupKey kGroupKey5(kNik_, kOrigin1_, "5");
  1069. std::vector<ReportingEndpoint::EndpointInfo> endpoints1_1 = {{MakeURL(10)},
  1070. {MakeURL(11)}};
  1071. std::vector<ReportingEndpoint::EndpointInfo> endpoints2_1 = {{MakeURL(20)},
  1072. {MakeURL(21)}};
  1073. std::vector<ReportingEndpoint::EndpointInfo> endpoints3_1 = {{MakeURL(30)},
  1074. {MakeURL(31)}};
  1075. std::string header1 =
  1076. ConstructHeaderGroupString(MakeEndpointGroup("1", endpoints1_1)) + ", " +
  1077. ConstructHeaderGroupString(MakeEndpointGroup("2", endpoints2_1)) + ", " +
  1078. ConstructHeaderGroupString(MakeEndpointGroup("3", endpoints3_1));
  1079. ParseHeader(kNik_, kOrigin1_, header1);
  1080. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1081. EXPECT_EQ(3u, cache()->GetEndpointGroupCountForTesting());
  1082. EXPECT_TRUE(
  1083. EndpointGroupExistsInCache(kGroupKey1, OriginSubdomains::DEFAULT));
  1084. EXPECT_TRUE(
  1085. EndpointGroupExistsInCache(kGroupKey2, OriginSubdomains::DEFAULT));
  1086. EXPECT_TRUE(
  1087. EndpointGroupExistsInCache(kGroupKey3, OriginSubdomains::DEFAULT));
  1088. EXPECT_EQ(6u, cache()->GetEndpointCount());
  1089. if (mock_store()) {
  1090. mock_store()->Flush();
  1091. EXPECT_EQ(6,
  1092. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1093. EXPECT_EQ(3, mock_store()->CountCommands(
  1094. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1095. MockPersistentReportingStore::CommandList expected_commands;
  1096. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1097. kGroupKey1, endpoints1_1[0].url);
  1098. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1099. kGroupKey1, endpoints1_1[1].url);
  1100. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1101. kGroupKey2, endpoints2_1[0].url);
  1102. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1103. kGroupKey2, endpoints2_1[1].url);
  1104. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1105. kGroupKey3, endpoints3_1[0].url);
  1106. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1107. kGroupKey3, endpoints3_1[1].url);
  1108. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1109. kGroupKey1);
  1110. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1111. kGroupKey2);
  1112. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1113. kGroupKey3);
  1114. EXPECT_THAT(mock_store()->GetAllCommands(),
  1115. testing::IsSupersetOf(expected_commands));
  1116. }
  1117. // Replace endpoints in each group with completely new endpoints.
  1118. std::vector<ReportingEndpoint::EndpointInfo> endpoints1_2 = {{MakeURL(12)}};
  1119. std::vector<ReportingEndpoint::EndpointInfo> endpoints2_2 = {{MakeURL(22)}};
  1120. std::vector<ReportingEndpoint::EndpointInfo> endpoints3_2 = {{MakeURL(32)}};
  1121. std::string header2 =
  1122. ConstructHeaderGroupString(MakeEndpointGroup("1", endpoints1_2)) + ", " +
  1123. ConstructHeaderGroupString(MakeEndpointGroup("2", endpoints2_2)) + ", " +
  1124. ConstructHeaderGroupString(MakeEndpointGroup("3", endpoints3_2));
  1125. ParseHeader(kNik_, kOrigin1_, header2);
  1126. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1127. EXPECT_EQ(3u, cache()->GetEndpointGroupCountForTesting());
  1128. EXPECT_TRUE(
  1129. EndpointGroupExistsInCache(kGroupKey1, OriginSubdomains::DEFAULT));
  1130. EXPECT_TRUE(
  1131. EndpointGroupExistsInCache(kGroupKey2, OriginSubdomains::DEFAULT));
  1132. EXPECT_TRUE(
  1133. EndpointGroupExistsInCache(kGroupKey3, OriginSubdomains::DEFAULT));
  1134. EXPECT_EQ(3u, cache()->GetEndpointCount());
  1135. EXPECT_TRUE(FindEndpointInCache(kGroupKey1, MakeURL(12)));
  1136. EXPECT_FALSE(FindEndpointInCache(kGroupKey1, MakeURL(10)));
  1137. EXPECT_FALSE(FindEndpointInCache(kGroupKey1, MakeURL(11)));
  1138. EXPECT_TRUE(FindEndpointInCache(kGroupKey2, MakeURL(22)));
  1139. EXPECT_FALSE(FindEndpointInCache(kGroupKey2, MakeURL(20)));
  1140. EXPECT_FALSE(FindEndpointInCache(kGroupKey2, MakeURL(21)));
  1141. EXPECT_TRUE(FindEndpointInCache(kGroupKey3, MakeURL(32)));
  1142. EXPECT_FALSE(FindEndpointInCache(kGroupKey3, MakeURL(30)));
  1143. EXPECT_FALSE(FindEndpointInCache(kGroupKey3, MakeURL(31)));
  1144. if (mock_store()) {
  1145. mock_store()->Flush();
  1146. EXPECT_EQ(6 + 3,
  1147. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1148. EXPECT_EQ(3, mock_store()->CountCommands(
  1149. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1150. EXPECT_EQ(
  1151. 6, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1152. EXPECT_EQ(0, mock_store()->CountCommands(
  1153. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1154. MockPersistentReportingStore::CommandList expected_commands;
  1155. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1156. kGroupKey1, endpoints1_2[0].url);
  1157. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1158. kGroupKey2, endpoints2_2[0].url);
  1159. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1160. kGroupKey3, endpoints3_2[0].url);
  1161. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1162. kGroupKey1, endpoints1_1[0].url);
  1163. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1164. kGroupKey1, endpoints1_1[1].url);
  1165. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1166. kGroupKey2, endpoints2_1[0].url);
  1167. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1168. kGroupKey2, endpoints2_1[1].url);
  1169. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1170. kGroupKey3, endpoints3_1[0].url);
  1171. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1172. kGroupKey3, endpoints3_1[1].url);
  1173. EXPECT_THAT(mock_store()->GetAllCommands(),
  1174. testing::IsSupersetOf(expected_commands));
  1175. }
  1176. // Replace all the groups with completely new groups.
  1177. std::vector<ReportingEndpoint::EndpointInfo> endpoints4_3 = {{MakeURL(40)}};
  1178. std::vector<ReportingEndpoint::EndpointInfo> endpoints5_3 = {{MakeURL(50)}};
  1179. std::string header3 =
  1180. ConstructHeaderGroupString(MakeEndpointGroup("4", endpoints4_3)) + ", " +
  1181. ConstructHeaderGroupString(MakeEndpointGroup("5", endpoints5_3));
  1182. ParseHeader(kNik_, kOrigin1_, header3);
  1183. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1184. EXPECT_EQ(2u, cache()->GetEndpointGroupCountForTesting());
  1185. EXPECT_TRUE(
  1186. EndpointGroupExistsInCache(kGroupKey4, OriginSubdomains::DEFAULT));
  1187. EXPECT_TRUE(
  1188. EndpointGroupExistsInCache(kGroupKey4, OriginSubdomains::DEFAULT));
  1189. EXPECT_FALSE(
  1190. EndpointGroupExistsInCache(kGroupKey1, OriginSubdomains::DEFAULT));
  1191. EXPECT_FALSE(
  1192. EndpointGroupExistsInCache(kGroupKey2, OriginSubdomains::DEFAULT));
  1193. EXPECT_FALSE(
  1194. EndpointGroupExistsInCache(kGroupKey3, OriginSubdomains::DEFAULT));
  1195. EXPECT_EQ(2u, cache()->GetEndpointCount());
  1196. if (mock_store()) {
  1197. mock_store()->Flush();
  1198. EXPECT_EQ(6 + 3 + 2,
  1199. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1200. EXPECT_EQ(3 + 2, mock_store()->CountCommands(
  1201. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1202. EXPECT_EQ(6 + 3, mock_store()->CountCommands(
  1203. CommandType::DELETE_REPORTING_ENDPOINT));
  1204. EXPECT_EQ(3, mock_store()->CountCommands(
  1205. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1206. MockPersistentReportingStore::CommandList expected_commands;
  1207. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1208. kGroupKey4, endpoints4_3[0].url);
  1209. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1210. kGroupKey5, endpoints5_3[0].url);
  1211. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1212. kGroupKey4);
  1213. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1214. kGroupKey5);
  1215. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1216. kGroupKey1, endpoints1_2[0].url);
  1217. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1218. kGroupKey2, endpoints2_2[0].url);
  1219. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1220. kGroupKey3, endpoints3_2[0].url);
  1221. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1222. kGroupKey1);
  1223. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1224. kGroupKey2);
  1225. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1226. kGroupKey3);
  1227. EXPECT_THAT(mock_store()->GetAllCommands(),
  1228. testing::IsSupersetOf(expected_commands));
  1229. }
  1230. }
  1231. TEST_P(ReportingHeaderParserTest, ZeroMaxAgeRemovesEndpointGroup) {
  1232. // Without a pre-existing client, max_age: 0 should do nothing.
  1233. ASSERT_EQ(0u, cache()->GetEndpointCount());
  1234. ParseHeader(kNik_, kOrigin1_,
  1235. "{\"endpoints\":[{\"url\":\"" + kEndpoint1_.spec() +
  1236. "\"}],\"max_age\":0}");
  1237. EXPECT_EQ(0u, cache()->GetEndpointCount());
  1238. if (mock_store()) {
  1239. mock_store()->Flush();
  1240. EXPECT_EQ(0,
  1241. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1242. EXPECT_EQ(0, mock_store()->CountCommands(
  1243. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1244. }
  1245. // Set a header with two endpoint groups.
  1246. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_}};
  1247. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint2_}};
  1248. std::string header1 =
  1249. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  1250. ", " +
  1251. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints2));
  1252. ParseHeader(kNik_, kOrigin1_, header1);
  1253. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1254. EXPECT_EQ(2u, cache()->GetEndpointGroupCountForTesting());
  1255. EXPECT_TRUE(
  1256. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1257. EXPECT_TRUE(
  1258. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1259. EXPECT_EQ(2u, cache()->GetEndpointCount());
  1260. if (mock_store()) {
  1261. mock_store()->Flush();
  1262. EXPECT_EQ(2,
  1263. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1264. EXPECT_EQ(2, mock_store()->CountCommands(
  1265. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1266. MockPersistentReportingStore::CommandList expected_commands;
  1267. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1268. kGroupKey11_, kEndpoint1_);
  1269. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1270. kGroupKey12_, kEndpoint2_);
  1271. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1272. kGroupKey11_);
  1273. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1274. kGroupKey12_);
  1275. EXPECT_THAT(mock_store()->GetAllCommands(),
  1276. testing::IsSupersetOf(expected_commands));
  1277. }
  1278. // Set another header with max_age: 0 to delete one of the groups.
  1279. std::string header2 =
  1280. ConstructHeaderGroupString(MakeEndpointGroup(
  1281. kGroup1_, endpoints1, OriginSubdomains::DEFAULT, base::Seconds(0))) +
  1282. ", " +
  1283. ConstructHeaderGroupString(
  1284. MakeEndpointGroup(kGroup2_, endpoints2)); // Other group stays.
  1285. ParseHeader(kNik_, kOrigin1_, header2);
  1286. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1287. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  1288. // Group was deleted.
  1289. EXPECT_FALSE(
  1290. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1291. // Other group remains in the cache.
  1292. EXPECT_TRUE(
  1293. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1294. EXPECT_EQ(1u, cache()->GetEndpointCount());
  1295. if (mock_store()) {
  1296. mock_store()->Flush();
  1297. EXPECT_EQ(2,
  1298. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1299. EXPECT_EQ(2, mock_store()->CountCommands(
  1300. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1301. EXPECT_EQ(
  1302. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1303. EXPECT_EQ(1, mock_store()->CountCommands(
  1304. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1305. MockPersistentReportingStore::CommandList expected_commands;
  1306. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1307. kGroupKey11_, kEndpoint1_);
  1308. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1309. kGroupKey11_);
  1310. EXPECT_THAT(mock_store()->GetAllCommands(),
  1311. testing::IsSupersetOf(expected_commands));
  1312. }
  1313. // Set another header with max_age: 0 to delete the other group. (Should work
  1314. // even if the endpoints field is an empty list.)
  1315. std::string header3 = ConstructHeaderGroupString(MakeEndpointGroup(
  1316. kGroup2_, std::vector<ReportingEndpoint::EndpointInfo>(),
  1317. OriginSubdomains::DEFAULT, base::Seconds(0)));
  1318. ParseHeader(kNik_, kOrigin1_, header3);
  1319. // Deletion of the last remaining group also deletes the client for this
  1320. // origin.
  1321. EXPECT_FALSE(ClientExistsInCacheForOrigin(kOrigin1_));
  1322. EXPECT_EQ(0u, cache()->GetEndpointGroupCountForTesting());
  1323. EXPECT_EQ(0u, cache()->GetEndpointCount());
  1324. if (mock_store()) {
  1325. mock_store()->Flush();
  1326. EXPECT_EQ(2,
  1327. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1328. EXPECT_EQ(2, mock_store()->CountCommands(
  1329. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1330. EXPECT_EQ(1 + 1, mock_store()->CountCommands(
  1331. CommandType::DELETE_REPORTING_ENDPOINT));
  1332. EXPECT_EQ(1 + 1, mock_store()->CountCommands(
  1333. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1334. MockPersistentReportingStore::CommandList expected_commands;
  1335. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1336. kGroupKey12_, kEndpoint2_);
  1337. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1338. kGroupKey12_);
  1339. EXPECT_THAT(mock_store()->GetAllCommands(),
  1340. testing::IsSupersetOf(expected_commands));
  1341. }
  1342. }
  1343. // Invalid advertisements that parse as JSON should remove an endpoint group,
  1344. // while those that don't are ignored.
  1345. TEST_P(ReportingHeaderParserTest, InvalidAdvertisementRemovesEndpointGroup) {
  1346. std::string invalid_non_json_header = "Goats should wear hats.";
  1347. std::string invalid_json_header = "\"Goats should wear hats.\"";
  1348. // Without a pre-existing client, neither invalid header does anything.
  1349. ASSERT_EQ(0u, cache()->GetEndpointCount());
  1350. ParseHeader(kNik_, kOrigin1_, invalid_non_json_header);
  1351. EXPECT_EQ(0u, cache()->GetEndpointCount());
  1352. if (mock_store()) {
  1353. mock_store()->Flush();
  1354. EXPECT_EQ(0,
  1355. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1356. EXPECT_EQ(0, mock_store()->CountCommands(
  1357. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1358. }
  1359. ASSERT_EQ(0u, cache()->GetEndpointCount());
  1360. ParseHeader(kNik_, kOrigin1_, invalid_json_header);
  1361. EXPECT_EQ(0u, cache()->GetEndpointCount());
  1362. if (mock_store()) {
  1363. mock_store()->Flush();
  1364. EXPECT_EQ(0,
  1365. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1366. EXPECT_EQ(0, mock_store()->CountCommands(
  1367. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1368. }
  1369. // Set a header with two endpoint groups.
  1370. std::vector<ReportingEndpoint::EndpointInfo> endpoints1 = {{kEndpoint1_}};
  1371. std::vector<ReportingEndpoint::EndpointInfo> endpoints2 = {{kEndpoint2_}};
  1372. std::string header1 =
  1373. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints1)) +
  1374. ", " +
  1375. ConstructHeaderGroupString(MakeEndpointGroup(kGroup2_, endpoints2));
  1376. ParseHeader(kNik_, kOrigin1_, header1);
  1377. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1378. EXPECT_EQ(2u, cache()->GetEndpointGroupCountForTesting());
  1379. EXPECT_TRUE(
  1380. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1381. EXPECT_TRUE(
  1382. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1383. EXPECT_EQ(2u, cache()->GetEndpointCount());
  1384. if (mock_store()) {
  1385. mock_store()->Flush();
  1386. EXPECT_EQ(2,
  1387. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1388. EXPECT_EQ(2, mock_store()->CountCommands(
  1389. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1390. MockPersistentReportingStore::CommandList expected_commands;
  1391. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1392. kGroupKey11_, kEndpoint1_);
  1393. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT,
  1394. kGroupKey12_, kEndpoint2_);
  1395. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1396. kGroupKey11_);
  1397. expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP,
  1398. kGroupKey12_);
  1399. EXPECT_THAT(mock_store()->GetAllCommands(),
  1400. testing::IsSupersetOf(expected_commands));
  1401. }
  1402. // Set another header with max_age: 0 to delete one of the groups.
  1403. std::string header2 =
  1404. ConstructHeaderGroupString(MakeEndpointGroup(
  1405. kGroup1_, endpoints1, OriginSubdomains::DEFAULT, base::Seconds(0))) +
  1406. ", " +
  1407. ConstructHeaderGroupString(
  1408. MakeEndpointGroup(kGroup2_, endpoints2)); // Other group stays.
  1409. ParseHeader(kNik_, kOrigin1_, header2);
  1410. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1411. EXPECT_EQ(1u, cache()->GetEndpointGroupCountForTesting());
  1412. // Group was deleted.
  1413. EXPECT_FALSE(
  1414. EndpointGroupExistsInCache(kGroupKey11_, OriginSubdomains::DEFAULT));
  1415. // Other group remains in the cache.
  1416. EXPECT_TRUE(
  1417. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1418. EXPECT_EQ(1u, cache()->GetEndpointCount());
  1419. if (mock_store()) {
  1420. mock_store()->Flush();
  1421. EXPECT_EQ(2,
  1422. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1423. EXPECT_EQ(2, mock_store()->CountCommands(
  1424. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1425. EXPECT_EQ(
  1426. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1427. EXPECT_EQ(1, mock_store()->CountCommands(
  1428. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1429. MockPersistentReportingStore::CommandList expected_commands;
  1430. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1431. kGroupKey11_, kEndpoint1_);
  1432. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1433. kGroupKey11_);
  1434. EXPECT_THAT(mock_store()->GetAllCommands(),
  1435. testing::IsSupersetOf(expected_commands));
  1436. }
  1437. // Invalid header values that are not JSON lists (without the outer brackets)
  1438. // are ignored.
  1439. ParseHeader(kNik_, kOrigin1_, invalid_non_json_header);
  1440. EXPECT_TRUE(ClientExistsInCacheForOrigin(kOrigin1_));
  1441. EXPECT_TRUE(
  1442. EndpointGroupExistsInCache(kGroupKey12_, OriginSubdomains::DEFAULT));
  1443. EXPECT_EQ(1u, cache()->GetEndpointCount());
  1444. if (mock_store()) {
  1445. mock_store()->Flush();
  1446. EXPECT_EQ(2,
  1447. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1448. EXPECT_EQ(2, mock_store()->CountCommands(
  1449. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1450. EXPECT_EQ(
  1451. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1452. EXPECT_EQ(1, mock_store()->CountCommands(
  1453. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1454. MockPersistentReportingStore::CommandList expected_commands;
  1455. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1456. kGroupKey11_, kEndpoint1_);
  1457. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1458. kGroupKey11_);
  1459. EXPECT_THAT(mock_store()->GetAllCommands(),
  1460. testing::IsSupersetOf(expected_commands));
  1461. }
  1462. // Invalid headers that do parse as JSON should delete the corresponding
  1463. // client.
  1464. ParseHeader(kNik_, kOrigin1_, invalid_json_header);
  1465. // Deletion of the last remaining group also deletes the client for this
  1466. // origin.
  1467. EXPECT_FALSE(ClientExistsInCacheForOrigin(kOrigin1_));
  1468. EXPECT_EQ(0u, cache()->GetEndpointGroupCountForTesting());
  1469. EXPECT_EQ(0u, cache()->GetEndpointCount());
  1470. if (mock_store()) {
  1471. mock_store()->Flush();
  1472. EXPECT_EQ(2,
  1473. mock_store()->CountCommands(CommandType::ADD_REPORTING_ENDPOINT));
  1474. EXPECT_EQ(2, mock_store()->CountCommands(
  1475. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1476. EXPECT_EQ(1 + 1, mock_store()->CountCommands(
  1477. CommandType::DELETE_REPORTING_ENDPOINT));
  1478. EXPECT_EQ(1 + 1, mock_store()->CountCommands(
  1479. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1480. MockPersistentReportingStore::CommandList expected_commands;
  1481. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT,
  1482. kGroupKey12_, kEndpoint2_);
  1483. expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP,
  1484. kGroupKey12_);
  1485. EXPECT_THAT(mock_store()->GetAllCommands(),
  1486. testing::IsSupersetOf(expected_commands));
  1487. }
  1488. }
  1489. TEST_P(ReportingHeaderParserTest, EvictEndpointsOverPerOriginLimit1) {
  1490. // Set a header with too many endpoints, all in the same group.
  1491. std::vector<ReportingEndpoint::EndpointInfo> endpoints;
  1492. for (size_t i = 0; i < policy().max_endpoints_per_origin + 1; ++i) {
  1493. endpoints.push_back({MakeURL(i)});
  1494. }
  1495. std::string header =
  1496. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  1497. ParseHeader(kNik_, kOrigin1_, header);
  1498. // Endpoint count should be at most the limit.
  1499. EXPECT_GE(policy().max_endpoints_per_origin, cache()->GetEndpointCount());
  1500. if (mock_store()) {
  1501. mock_store()->Flush();
  1502. EXPECT_EQ(policy().max_endpoints_per_origin + 1,
  1503. static_cast<unsigned long>(mock_store()->CountCommands(
  1504. CommandType::ADD_REPORTING_ENDPOINT)));
  1505. EXPECT_EQ(1, mock_store()->CountCommands(
  1506. CommandType::ADD_REPORTING_ENDPOINT_GROUP));
  1507. EXPECT_EQ(
  1508. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1509. }
  1510. }
  1511. TEST_P(ReportingHeaderParserTest, EvictEndpointsOverPerOriginLimit2) {
  1512. // Set a header with too many endpoints, in different groups.
  1513. std::string header;
  1514. for (size_t i = 0; i < policy().max_endpoints_per_origin + 1; ++i) {
  1515. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{MakeURL(i)}};
  1516. header = header + ConstructHeaderGroupString(MakeEndpointGroup(
  1517. base::NumberToString(i), endpoints));
  1518. if (i != policy().max_endpoints_per_origin)
  1519. header = header + ", ";
  1520. }
  1521. ParseHeader(kNik_, kOrigin1_, header);
  1522. // Endpoint count should be at most the limit.
  1523. EXPECT_GE(policy().max_endpoints_per_origin, cache()->GetEndpointCount());
  1524. if (mock_store()) {
  1525. mock_store()->Flush();
  1526. EXPECT_EQ(policy().max_endpoints_per_origin + 1,
  1527. static_cast<unsigned long>(mock_store()->CountCommands(
  1528. CommandType::ADD_REPORTING_ENDPOINT)));
  1529. EXPECT_EQ(policy().max_endpoints_per_origin + 1,
  1530. static_cast<unsigned long>(mock_store()->CountCommands(
  1531. CommandType::ADD_REPORTING_ENDPOINT_GROUP)));
  1532. EXPECT_EQ(
  1533. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1534. EXPECT_EQ(1, mock_store()->CountCommands(
  1535. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1536. }
  1537. }
  1538. TEST_P(ReportingHeaderParserTest, EvictEndpointsOverGlobalLimit) {
  1539. // Set headers from different origins up to the global limit.
  1540. for (size_t i = 0; i < policy().max_endpoint_count; ++i) {
  1541. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{MakeURL(i)}};
  1542. std::string header =
  1543. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  1544. ParseHeader(kNik_, url::Origin::Create(MakeURL(i)), header);
  1545. }
  1546. EXPECT_EQ(policy().max_endpoint_count, cache()->GetEndpointCount());
  1547. // Parse one more header to trigger eviction.
  1548. ParseHeader(kNik_, kOrigin1_,
  1549. "{\"endpoints\":[{\"url\":\"" + kEndpoint1_.spec() +
  1550. "\"}],\"max_age\":1}");
  1551. // Endpoint count should be at most the limit.
  1552. EXPECT_GE(policy().max_endpoint_count, cache()->GetEndpointCount());
  1553. if (mock_store()) {
  1554. mock_store()->Flush();
  1555. EXPECT_EQ(policy().max_endpoint_count + 1,
  1556. static_cast<unsigned long>(mock_store()->CountCommands(
  1557. CommandType::ADD_REPORTING_ENDPOINT)));
  1558. EXPECT_EQ(policy().max_endpoint_count + 1,
  1559. static_cast<unsigned long>(mock_store()->CountCommands(
  1560. CommandType::ADD_REPORTING_ENDPOINT_GROUP)));
  1561. EXPECT_EQ(
  1562. 1, mock_store()->CountCommands(CommandType::DELETE_REPORTING_ENDPOINT));
  1563. EXPECT_EQ(1, mock_store()->CountCommands(
  1564. CommandType::DELETE_REPORTING_ENDPOINT_GROUP));
  1565. }
  1566. }
  1567. INSTANTIATE_TEST_SUITE_P(ReportingHeaderParserStoreTest,
  1568. ReportingHeaderParserTest,
  1569. testing::Bool());
  1570. // This test is parametrized on a boolean that represents whether to use a
  1571. // MockPersistentReportingStore.
  1572. class ReportingHeaderParserStructuredHeaderTest
  1573. : public ReportingHeaderParserTestBase {
  1574. protected:
  1575. ReportingHeaderParserStructuredHeaderTest() {
  1576. // Enable kDocumentReporting to support new StructuredHeader-based
  1577. // Reporting-Endpoints header.
  1578. feature_list_.InitWithFeatures(
  1579. {features::kPartitionNelAndReportingByNetworkIsolationKey,
  1580. features::kDocumentReporting},
  1581. {});
  1582. }
  1583. ~ReportingHeaderParserStructuredHeaderTest() override = default;
  1584. ReportingEndpointGroup MakeEndpointGroup(
  1585. const std::string& name,
  1586. const std::vector<ReportingEndpoint::EndpointInfo>& endpoints,
  1587. url::Origin origin = url::Origin()) {
  1588. ReportingEndpointGroupKey group_key(kNik_ /* unused */,
  1589. url::Origin() /* unused */, name);
  1590. ReportingEndpointGroup group;
  1591. group.group_key = group_key;
  1592. group.include_subdomains = OriginSubdomains::EXCLUDE;
  1593. group.ttl = base::Days(30);
  1594. group.endpoints = std::move(endpoints);
  1595. return group;
  1596. }
  1597. // Constructs a string which would represent a single endpoint in a
  1598. // Reporting-Endpoints header.
  1599. std::string ConstructHeaderGroupString(const ReportingEndpointGroup& group) {
  1600. std::string header = group.group_key.group_name;
  1601. if (header.empty())
  1602. return header;
  1603. base::StrAppend(&header, {"="});
  1604. if (group.endpoints.empty())
  1605. return header;
  1606. base::StrAppend(&header, {"\"", group.endpoints.front().url.spec(), "\""});
  1607. return header;
  1608. }
  1609. void ParseHeader(const base::UnguessableToken& reporting_source,
  1610. const IsolationInfo& isolation_info,
  1611. const url::Origin& origin,
  1612. const std::string& header_string) {
  1613. absl::optional<base::flat_map<std::string, std::string>> header_map =
  1614. ParseReportingEndpoints(header_string);
  1615. if (header_map) {
  1616. ReportingHeaderParser::ProcessParsedReportingEndpointsHeader(
  1617. context(), reporting_source, isolation_info,
  1618. isolation_info.network_isolation_key(), origin, *header_map);
  1619. }
  1620. }
  1621. void ProcessParsedHeader(
  1622. const base::UnguessableToken& reporting_source,
  1623. const IsolationInfo& isolation_info,
  1624. const url::Origin& origin,
  1625. const absl::optional<base::flat_map<std::string, std::string>>&
  1626. header_map) {
  1627. ReportingHeaderParser::ProcessParsedReportingEndpointsHeader(
  1628. context(), reporting_source, isolation_info,
  1629. isolation_info.network_isolation_key(), origin, *header_map);
  1630. }
  1631. const base::UnguessableToken kReportingSource_ =
  1632. base::UnguessableToken::Create();
  1633. };
  1634. TEST_P(ReportingHeaderParserStructuredHeaderTest, ParseInvalid) {
  1635. static const struct {
  1636. const char* header_value;
  1637. const char* description;
  1638. } kInvalidHeaderTestCases[] = {
  1639. {"default=", "missing url"},
  1640. {"default=1", "non-string url"},
  1641. };
  1642. for (auto& test_case : kInvalidHeaderTestCases) {
  1643. auto parsed_result = ParseReportingEndpoints(test_case.header_value);
  1644. EXPECT_FALSE(parsed_result.has_value())
  1645. << "Invalid Reporting-Endpoints header (" << test_case.description
  1646. << ": \"" << test_case.header_value << "\") parsed as valid.";
  1647. }
  1648. }
  1649. TEST_P(ReportingHeaderParserStructuredHeaderTest, ProcessInvalid) {
  1650. static const struct {
  1651. const char* header_value;
  1652. const char* description;
  1653. } kInvalidHeaderTestCases[] = {
  1654. {"default=\"//scheme/relative\"", "scheme-relative url"},
  1655. {"default=\"relative/path\"", "path relative url"},
  1656. {"default=\"http://insecure/\"", "insecure url"}};
  1657. base::HistogramTester histograms;
  1658. int invalid_case_count = 0;
  1659. for (auto& test_case : kInvalidHeaderTestCases) {
  1660. auto parsed_result = ParseReportingEndpoints(test_case.header_value);
  1661. EXPECT_TRUE(parsed_result.has_value())
  1662. << "Syntactically valid Reporting-Endpoints header (\""
  1663. << test_case.description << ": \"" << test_case.header_value
  1664. << "\") parsed as invalid.";
  1665. ProcessParsedHeader(kReportingSource_, kIsolationInfo_, kOrigin1_,
  1666. parsed_result);
  1667. invalid_case_count++;
  1668. histograms.ExpectBucketCount(
  1669. kReportingHeaderTypeHistogram,
  1670. ReportingHeaderParser::ReportingHeaderType::kReportingEndpointsInvalid,
  1671. invalid_case_count);
  1672. // The endpoint should not have been set up in the cache.
  1673. ReportingEndpoint endpoint =
  1674. cache()->GetV1EndpointForTesting(kReportingSource_, "default");
  1675. EXPECT_FALSE(endpoint);
  1676. }
  1677. histograms.ExpectBucketCount(
  1678. kReportingHeaderTypeHistogram,
  1679. ReportingHeaderParser::ReportingHeaderType::kReportingEndpoints, 0);
  1680. }
  1681. TEST_P(ReportingHeaderParserStructuredHeaderTest, ParseBasic) {
  1682. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  1683. std::string header =
  1684. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  1685. auto parsed_result = ParseReportingEndpoints(header);
  1686. EXPECT_TRUE(parsed_result.has_value())
  1687. << "Valid Reporting-Endpoints header (\"" << header
  1688. << "\") parsed as invalid.";
  1689. EXPECT_EQ(1u, parsed_result->size());
  1690. EXPECT_EQ(parsed_result->at(kGroup1_), kEndpoint1_.spec());
  1691. }
  1692. TEST_P(ReportingHeaderParserStructuredHeaderTest, Basic) {
  1693. base::HistogramTester histograms;
  1694. std::vector<ReportingEndpoint::EndpointInfo> endpoints = {{kEndpoint1_}};
  1695. std::string header =
  1696. ConstructHeaderGroupString(MakeEndpointGroup(kGroup1_, endpoints));
  1697. auto parsed_result = ParseReportingEndpoints(header);
  1698. ProcessParsedHeader(kReportingSource_, kIsolationInfo_, kOrigin1_,
  1699. parsed_result);
  1700. // Ensure that the endpoint was not inserted into the persistent endpoint
  1701. // groups used for v0 reporting.
  1702. EXPECT_EQ(0u, cache()->GetEndpointGroupCountForTesting());
  1703. ReportingEndpoint endpoint =
  1704. cache()->GetV1EndpointForTesting(kReportingSource_, kGroup1_);
  1705. EXPECT_TRUE(endpoint);
  1706. IsolationInfo isolation_info = cache()->GetIsolationInfoForEndpoint(endpoint);
  1707. EXPECT_TRUE(isolation_info.IsEqualForTesting(kIsolationInfo_));
  1708. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  1709. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  1710. EXPECT_EQ(kEndpoint1_, endpoint.info.url);
  1711. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  1712. endpoint.info.priority);
  1713. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  1714. endpoint.info.weight);
  1715. histograms.ExpectBucketCount(
  1716. kReportingHeaderTypeHistogram,
  1717. ReportingHeaderParser::ReportingHeaderType::kReportingEndpoints, 1);
  1718. // Ephemeral endpoints should not be persisted in the store
  1719. if (mock_store()) {
  1720. mock_store()->Flush();
  1721. EXPECT_EQ(0, mock_store()->StoredEndpointsCount());
  1722. EXPECT_EQ(0, mock_store()->StoredEndpointGroupsCount());
  1723. }
  1724. }
  1725. TEST_P(ReportingHeaderParserStructuredHeaderTest, PathAbsoluteURLEndpoint) {
  1726. base::HistogramTester histograms;
  1727. std::string header = "group1=\"/path-absolute-url\"";
  1728. auto parsed_result = ParseReportingEndpoints(header);
  1729. ProcessParsedHeader(kReportingSource_, kIsolationInfo_, kOrigin1_,
  1730. parsed_result);
  1731. // Ensure that the endpoint was not inserted into the persistent endpoint
  1732. // groups used for v0 reporting.
  1733. EXPECT_EQ(0u, cache()->GetEndpointGroupCountForTesting());
  1734. ReportingEndpoint endpoint =
  1735. cache()->GetV1EndpointForTesting(kReportingSource_, kGroup1_);
  1736. EXPECT_TRUE(endpoint);
  1737. EXPECT_EQ(kOrigin1_, endpoint.group_key.origin);
  1738. EXPECT_EQ(kGroup1_, endpoint.group_key.group_name);
  1739. EXPECT_EQ(kEndpointPathAbsolute_, endpoint.info.url);
  1740. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultPriority,
  1741. endpoint.info.priority);
  1742. EXPECT_EQ(ReportingEndpoint::EndpointInfo::kDefaultWeight,
  1743. endpoint.info.weight);
  1744. histograms.ExpectBucketCount(
  1745. kReportingHeaderTypeHistogram,
  1746. ReportingHeaderParser::ReportingHeaderType::kReportingEndpoints, 1);
  1747. // Ephemeral endpoints should not be persisted in the store
  1748. if (mock_store()) {
  1749. mock_store()->Flush();
  1750. EXPECT_EQ(0, mock_store()->StoredEndpointsCount());
  1751. EXPECT_EQ(0, mock_store()->StoredEndpointGroupsCount());
  1752. }
  1753. }
  1754. INSTANTIATE_TEST_SUITE_P(ReportingHeaderParserStoreTest,
  1755. ReportingHeaderParserStructuredHeaderTest,
  1756. testing::Bool());
  1757. } // namespace
  1758. } // namespace net