blob: 946e1e0bd5e9044575872b59ab96ef497b2e60d8 [file] [log] [blame]
juliatuttle586843332017-03-27 16:22:371// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/reporting/reporting_test_util.h"
6
juliatuttleee4b55e2017-04-07 17:09:457#include <memory>
8#include <string>
juliatuttle586843332017-03-27 16:22:379#include <vector>
10
juliatuttleee4b55e2017-04-07 17:09:4511#include "base/bind.h"
12#include "base/json/json_reader.h"
13#include "base/memory/ptr_util.h"
14#include "base/test/simple_test_clock.h"
15#include "base/test/simple_test_tick_clock.h"
juliatuttle586843332017-03-27 16:22:3716#include "net/reporting/reporting_cache.h"
17#include "net/reporting/reporting_client.h"
juliatuttleee4b55e2017-04-07 17:09:4518#include "net/reporting/reporting_context.h"
19#include "net/reporting/reporting_delegate.h"
20#include "net/reporting/reporting_policy.h"
21#include "net/reporting/reporting_uploader.h"
22#include "testing/gtest/include/gtest/gtest.h"
juliatuttle586843332017-03-27 16:22:3723#include "url/gurl.h"
24#include "url/origin.h"
25
26namespace net {
27
juliatuttleee4b55e2017-04-07 17:09:4528namespace {
29
30class PendingUploadImpl : public TestReportingUploader::PendingUpload {
31 public:
32 PendingUploadImpl(
33 const GURL& url,
34 const std::string& json,
35 const ReportingUploader::Callback& callback,
36 const base::Callback<void(PendingUpload*)>& complete_callback)
37 : url_(url),
38 json_(json),
39 callback_(callback),
40 complete_callback_(complete_callback) {}
41
42 ~PendingUploadImpl() override {}
43
44 // PendingUpload implementationP:
45 const GURL& url() const override { return url_; }
46 const std::string& json() const override { return json_; }
47 std::unique_ptr<base::Value> GetValue() const override {
48 return base::JSONReader::Read(json_);
49 }
50
51 void Complete(ReportingUploader::Outcome outcome) override {
52 callback_.Run(outcome);
53 // Deletes |this|.
54 complete_callback_.Run(this);
55 }
56
57 private:
58 GURL url_;
59 std::string json_;
60 ReportingUploader::Callback callback_;
61 base::Callback<void(PendingUpload*)> complete_callback_;
62};
63
64void ErasePendingUpload(
65 std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads,
66 TestReportingUploader::PendingUpload* upload) {
67 for (auto it = uploads->begin(); it != uploads->end(); ++it) {
68 if (it->get() == upload) {
69 uploads->erase(it);
70 return;
71 }
72 }
73 NOTREACHED();
74}
75
76} // namespace
77
juliatuttle586843332017-03-27 16:22:3778const ReportingClient* FindClientInCache(const ReportingCache* cache,
79 const url::Origin& origin,
80 const GURL& endpoint) {
81 std::vector<const ReportingClient*> clients;
82 cache->GetClients(&clients);
83 for (const ReportingClient* client : clients) {
84 if (client->origin == origin && client->endpoint == endpoint)
85 return client;
86 }
87 return nullptr;
88}
89
juliatuttleee4b55e2017-04-07 17:09:4590TestReportingDelegate::TestReportingDelegate() {}
91TestReportingDelegate::~TestReportingDelegate() {}
92
93void TestReportingDelegate::PersistData(
94 std::unique_ptr<const base::Value> persisted_data) {
95 persisted_data_ = std::move(persisted_data);
96}
97
98std::unique_ptr<const base::Value> TestReportingDelegate::GetPersistedData() {
99 if (!persisted_data_)
100 return std::unique_ptr<const base::Value>();
101 return persisted_data_->CreateDeepCopy();
102}
103
104TestReportingUploader::PendingUpload::~PendingUpload() {}
105TestReportingUploader::PendingUpload::PendingUpload() {}
106
107TestReportingUploader::TestReportingUploader() {}
108TestReportingUploader::~TestReportingUploader() {}
109
110void TestReportingUploader::StartUpload(const GURL& url,
111 const std::string& json,
112 const Callback& callback) {
113 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>(
114 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_)));
115}
116
117TestReportingContext::TestReportingContext(const ReportingPolicy& policy)
118 : ReportingContext(policy,
119 base::MakeUnique<TestReportingDelegate>(),
120 base::MakeUnique<base::SimpleTestClock>(),
121 base::MakeUnique<base::SimpleTestTickClock>(),
122 base::MakeUnique<TestReportingUploader>()) {}
123
124TestReportingContext::~TestReportingContext() {}
125
126ReportingTestBase::ReportingTestBase() {
127 // For tests, disable jitter.
128 ReportingPolicy policy;
129 policy.endpoint_backoff_policy.jitter_factor = 0.0;
130 UsePolicy(policy);
131}
132
133ReportingTestBase::~ReportingTestBase() {}
134
135void ReportingTestBase::UsePolicy(const ReportingPolicy& policy) {
136 context_ = base::MakeUnique<TestReportingContext>(policy);
137}
138
139base::TimeTicks ReportingTestBase::yesterday() {
140 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1);
141}
142
143base::TimeTicks ReportingTestBase::tomorrow() {
144 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1);
145}
146
juliatuttle586843332017-03-27 16:22:37147} // namespace net