reporting_test_util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #ifndef NET_REPORTING_REPORTING_TEST_UTIL_H_
  5. #define NET_REPORTING_REPORTING_TEST_UTIL_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/test/simple_test_clock.h"
  12. #include "base/test/simple_test_tick_clock.h"
  13. #include "base/unguessable_token.h"
  14. #include "net/base/network_isolation_key.h"
  15. #include "net/base/rand_callback.h"
  16. #include "net/reporting/reporting_cache.h"
  17. #include "net/reporting/reporting_context.h"
  18. #include "net/reporting/reporting_delegate.h"
  19. #include "net/reporting/reporting_service.h"
  20. #include "net/reporting/reporting_uploader.h"
  21. #include "net/test/test_with_task_environment.h"
  22. #include "testing/gmock/include/gmock/gmock.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. #include "third_party/abseil-cpp/absl/types/optional.h"
  25. #include "url/gurl.h"
  26. namespace base {
  27. class MockOneShotTimer;
  28. class SimpleTestClock;
  29. class SimpleTestTickClock;
  30. class Value;
  31. } // namespace base
  32. namespace url {
  33. class Origin;
  34. } // namespace url
  35. namespace net {
  36. class IsolationInfo;
  37. struct ReportingEndpoint;
  38. class ReportingGarbageCollector;
  39. // A matcher for ReportingReports, which checks that the url of the report is
  40. // the given url.
  41. // Usage: EXPECT_THAT(report, ReportUrlIs(url));
  42. // EXPECT_THAT(reports(),
  43. // testing::ElementsAre(ReportUrlIs(url1), ReportUrlIs(url2)));
  44. MATCHER_P(ReportUrlIs, url, "") {
  45. return arg.url == url;
  46. }
  47. RandIntCallback TestReportingRandIntCallback();
  48. // A test implementation of ReportingUploader that holds uploads for tests to
  49. // examine and complete with a specified outcome.
  50. class TestReportingUploader : public ReportingUploader {
  51. public:
  52. class PendingUpload {
  53. public:
  54. virtual ~PendingUpload();
  55. virtual const url::Origin& report_origin() const = 0;
  56. virtual const GURL& url() const = 0;
  57. virtual const std::string& json() const = 0;
  58. virtual absl::optional<base::Value> GetValue() const = 0;
  59. virtual void Complete(Outcome outcome) = 0;
  60. protected:
  61. PendingUpload();
  62. };
  63. TestReportingUploader();
  64. TestReportingUploader(const TestReportingUploader&) = delete;
  65. TestReportingUploader& operator=(const TestReportingUploader&) = delete;
  66. ~TestReportingUploader() override;
  67. const std::vector<std::unique_ptr<PendingUpload>>& pending_uploads() const {
  68. return pending_uploads_;
  69. }
  70. // ReportingUploader implementation:
  71. void StartUpload(const url::Origin& report_origin,
  72. const GURL& url,
  73. const IsolationInfo& isolation_info,
  74. const std::string& json,
  75. int max_depth,
  76. bool eligible_for_credentials,
  77. UploadCallback callback) override;
  78. void OnShutdown() override;
  79. int GetPendingUploadCountForTesting() const override;
  80. private:
  81. std::vector<std::unique_ptr<PendingUpload>> pending_uploads_;
  82. };
  83. // Allows all permissions unless set_disallow_report_uploads is called; uses
  84. // the real ReportingDelegate for JSON parsing to exercise depth and size
  85. // limits.
  86. class TestReportingDelegate : public ReportingDelegate {
  87. public:
  88. TestReportingDelegate();
  89. TestReportingDelegate(const TestReportingDelegate&) = delete;
  90. TestReportingDelegate& operator=(const TestReportingDelegate&) = delete;
  91. // ReportingDelegate implementation:
  92. ~TestReportingDelegate() override;
  93. void set_disallow_report_uploads(bool disallow_report_uploads) {
  94. disallow_report_uploads_ = disallow_report_uploads;
  95. }
  96. void set_pause_permissions_check(bool pause_permissions_check) {
  97. pause_permissions_check_ = pause_permissions_check;
  98. }
  99. bool CanQueueReport(const url::Origin& origin) const override;
  100. void CanSendReports(std::set<url::Origin> origins,
  101. base::OnceCallback<void(std::set<url::Origin>)>
  102. result_callback) const override;
  103. bool PermissionsCheckPaused() const;
  104. void ResumePermissionsCheck();
  105. bool CanSetClient(const url::Origin& origin,
  106. const GURL& endpoint) const override;
  107. bool CanUseClient(const url::Origin& origin,
  108. const GURL& endpoint) const override;
  109. private:
  110. bool disallow_report_uploads_ = false;
  111. bool pause_permissions_check_ = false;
  112. mutable std::set<url::Origin> saved_origins_;
  113. mutable base::OnceCallback<void(std::set<url::Origin>)>
  114. permissions_check_callback_;
  115. };
  116. // A test implementation of ReportingContext that uses test versions of
  117. // Clock, TickClock, Timer, and ReportingUploader.
  118. class TestReportingContext : public ReportingContext {
  119. public:
  120. TestReportingContext(
  121. base::Clock* clock,
  122. const base::TickClock* tick_clock,
  123. const ReportingPolicy& policy,
  124. ReportingCache::PersistentReportingStore* store = nullptr);
  125. TestReportingContext(const TestReportingContext&) = delete;
  126. TestReportingContext& operator=(const TestReportingContext&) = delete;
  127. ~TestReportingContext();
  128. base::MockOneShotTimer* test_delivery_timer() { return delivery_timer_; }
  129. base::MockOneShotTimer* test_garbage_collection_timer() {
  130. return garbage_collection_timer_;
  131. }
  132. TestReportingUploader* test_uploader() {
  133. return reinterpret_cast<TestReportingUploader*>(uploader());
  134. }
  135. TestReportingDelegate* test_delegate() {
  136. return reinterpret_cast<TestReportingDelegate*>(delegate());
  137. }
  138. private:
  139. // Owned by the DeliveryAgent and GarbageCollector, respectively, but
  140. // referenced here to preserve type:
  141. raw_ptr<base::MockOneShotTimer> delivery_timer_;
  142. raw_ptr<base::MockOneShotTimer> garbage_collection_timer_;
  143. };
  144. // A unit test base class that provides a TestReportingContext and shorthand
  145. // getters.
  146. class ReportingTestBase : public TestWithTaskEnvironment {
  147. public:
  148. ReportingTestBase(const ReportingTestBase&) = delete;
  149. ReportingTestBase& operator=(const ReportingTestBase&) = delete;
  150. protected:
  151. ReportingTestBase();
  152. ~ReportingTestBase() override;
  153. void UsePolicy(const ReportingPolicy& policy);
  154. void UseStore(ReportingCache::PersistentReportingStore* store);
  155. // Finds a particular endpoint in the cache and returns it (or an invalid
  156. // ReportingEndpoint, if not found).
  157. const ReportingEndpoint FindEndpointInCache(
  158. const ReportingEndpointGroupKey& group_key,
  159. const GURL& url);
  160. // Sets an endpoint with the given properties in a group with the given
  161. // properties, bypassing header parsing. Note that the endpoint is not
  162. // guaranteed to exist in the cache after calling this function, if endpoint
  163. // eviction is triggered. Returns whether the endpoint was successfully set.
  164. bool SetEndpointInCache(
  165. const ReportingEndpointGroupKey& group_key,
  166. const GURL& url,
  167. base::Time expires,
  168. OriginSubdomains include_subdomains = OriginSubdomains::DEFAULT,
  169. int priority = ReportingEndpoint::EndpointInfo::kDefaultPriority,
  170. int weight = ReportingEndpoint::EndpointInfo::kDefaultWeight);
  171. // Sets an endpoint with the given group_key and url as origin in the document
  172. // endpoints map using |reporting_source| as key.
  173. void SetV1EndpointInCache(const ReportingEndpointGroupKey& group_key,
  174. const base::UnguessableToken& reporting_source,
  175. const IsolationInfo& isolation_info,
  176. const GURL& url);
  177. // Returns whether an endpoint with the given properties exists in the cache.
  178. bool EndpointExistsInCache(const ReportingEndpointGroupKey& group_key,
  179. const GURL& url);
  180. // Gets the statistics for a given endpoint, if it exists.
  181. ReportingEndpoint::Statistics GetEndpointStatistics(
  182. const ReportingEndpointGroupKey& group_key,
  183. const GURL& url);
  184. // Returns whether an endpoint group with exactly the given properties exists
  185. // in the cache. |expires| can be omitted, in which case it will not be
  186. // checked.
  187. bool EndpointGroupExistsInCache(const ReportingEndpointGroupKey& group_key,
  188. OriginSubdomains include_subdomains,
  189. base::Time expires = base::Time());
  190. // Returns whether a client for the given origin exists in the cache.
  191. bool ClientExistsInCacheForOrigin(const url::Origin& origin);
  192. // Makes a unique URL with the provided index.
  193. GURL MakeURL(size_t index);
  194. // Simulates an embedder restart, preserving the ReportingPolicy.
  195. //
  196. // Advances the Clock by |delta|, and the TickClock by |delta_ticks|. Both can
  197. // be zero or negative.
  198. void SimulateRestart(base::TimeDelta delta, base::TimeDelta delta_ticks);
  199. TestReportingContext* context() { return context_.get(); }
  200. const ReportingPolicy& policy() { return context_->policy(); }
  201. base::SimpleTestClock* clock() { return &clock_; }
  202. base::SimpleTestTickClock* tick_clock() { return &tick_clock_; }
  203. base::MockOneShotTimer* delivery_timer() {
  204. return context_->test_delivery_timer();
  205. }
  206. base::MockOneShotTimer* garbage_collection_timer() {
  207. return context_->test_garbage_collection_timer();
  208. }
  209. TestReportingUploader* uploader() { return context_->test_uploader(); }
  210. ReportingCache* cache() { return context_->cache(); }
  211. ReportingDeliveryAgent* delivery_agent() {
  212. return context_->delivery_agent();
  213. }
  214. ReportingGarbageCollector* garbage_collector() {
  215. return context_->garbage_collector();
  216. }
  217. ReportingCache::PersistentReportingStore* store() { return store_; }
  218. base::TimeTicks yesterday();
  219. base::TimeTicks now();
  220. base::TimeTicks tomorrow();
  221. const std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>&
  222. pending_uploads() {
  223. return uploader()->pending_uploads();
  224. }
  225. private:
  226. void CreateContext(const ReportingPolicy& policy,
  227. base::Time now,
  228. base::TimeTicks now_ticks);
  229. base::SimpleTestClock clock_;
  230. base::SimpleTestTickClock tick_clock_;
  231. std::unique_ptr<TestReportingContext> context_;
  232. raw_ptr<ReportingCache::PersistentReportingStore> store_ = nullptr;
  233. };
  234. class TestReportingService : public ReportingService {
  235. public:
  236. struct Report {
  237. Report();
  238. Report(const Report&) = delete;
  239. Report(Report&& other);
  240. Report(const GURL& url,
  241. const NetworkIsolationKey& network_isolation_key,
  242. const std::string& user_agent,
  243. const std::string& group,
  244. const std::string& type,
  245. std::unique_ptr<const base::Value> body,
  246. int depth);
  247. ~Report();
  248. GURL url;
  249. NetworkIsolationKey network_isolation_key;
  250. std::string user_agent;
  251. std::string group;
  252. std::string type;
  253. std::unique_ptr<const base::Value> body;
  254. int depth;
  255. };
  256. TestReportingService();
  257. TestReportingService(const TestReportingService&) = delete;
  258. TestReportingService& operator=(const TestReportingService&) = delete;
  259. const std::vector<Report>& reports() const { return reports_; }
  260. // ReportingService implementation:
  261. ~TestReportingService() override;
  262. void SetDocumentReportingEndpoints(
  263. const base::UnguessableToken& reporting_source,
  264. const url::Origin& origin,
  265. const IsolationInfo& isolation_info,
  266. const base::flat_map<std::string, std::string>& endpoints) override {}
  267. void SendReportsAndRemoveSource(
  268. const base::UnguessableToken& reporting_source) override {}
  269. void QueueReport(
  270. const GURL& url,
  271. const absl::optional<base::UnguessableToken>& reporting_source,
  272. const NetworkIsolationKey& network_isolation_key,
  273. const std::string& user_agent,
  274. const std::string& group,
  275. const std::string& type,
  276. base::Value::Dict body,
  277. int depth) override;
  278. void ProcessReportToHeader(const url::Origin& url,
  279. const NetworkIsolationKey& network_isolation_key,
  280. const std::string& header_value) override;
  281. void RemoveBrowsingData(
  282. uint64_t data_type_mask,
  283. const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter)
  284. override;
  285. void RemoveAllBrowsingData(uint64_t data_type_mask) override;
  286. void OnShutdown() override;
  287. const ReportingPolicy& GetPolicy() const override;
  288. ReportingContext* GetContextForTesting() const override;
  289. std::vector<const ReportingReport*> GetReports() const override;
  290. base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
  291. GetV1ReportingEndpointsByOrigin() const override;
  292. void AddReportingCacheObserver(ReportingCacheObserver* observer) override;
  293. void RemoveReportingCacheObserver(ReportingCacheObserver* observer) override;
  294. private:
  295. std::vector<Report> reports_;
  296. ReportingPolicy dummy_policy_;
  297. };
  298. } // namespace net
  299. #endif // NET_REPORTING_REPORTING_TEST_UTIL_H_