blob: a561f9985b122fd87cf7470071dd0758daae09b2 [file] [log] [blame]
holteca6486b2014-11-06 02:30:071// Copyright 2014 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 "components/rappor/test_rappor_service.h"
6
dcheng51ace48a2015-12-26 22:45:177#include <utility>
8
csharrisonb50320c32015-11-02 15:59:579#include "base/logging.h"
dcheng82beb4f2016-04-26 00:35:0210#include "base/memory/ptr_util.h"
holteca6486b2014-11-06 02:30:0711#include "components/rappor/byte_vector_utils.h"
12#include "components/rappor/proto/rappor_metric.pb.h"
nzolghadrd87a308d2016-12-07 15:45:5613#include "components/rappor/public/rappor_parameters.h"
holte2a1a5472015-01-15 00:42:3814#include "components/rappor/rappor_prefs.h"
holte5a7ed7c2015-01-09 23:52:4615#include "components/rappor/test_log_uploader.h"
holteca6486b2014-11-06 02:30:0716
17namespace rappor {
18
holte79705c82014-11-14 23:41:0419namespace {
20
holte5a7ed7c2015-01-09 23:52:4621bool MockIsIncognito(bool* is_incognito) {
22 return *is_incognito;
holte79705c82014-11-14 23:41:0423}
24
25} // namespace
26
csharrisonb50320c32015-11-02 15:59:5727TestSample::TestSample(RapporType type)
28 : Sample(0, internal::kRapporParametersForType[type]), shadow_(type) {}
29
30TestSample::~TestSample() {}
31
32void TestSample::SetStringField(const std::string& field_name,
33 const std::string& value) {
34 shadow_.string_fields[field_name] = value;
35 Sample::SetStringField(field_name, value);
36}
37
38void TestSample::SetFlagsField(const std::string& field_name,
39 uint64_t flags,
40 size_t num_flags) {
41 shadow_.flag_fields[field_name] = flags;
42 Sample::SetFlagsField(field_name, flags, num_flags);
43}
44
nzolghadr3fddfc52016-12-13 21:52:1245void TestSample::SetUInt64Field(const std::string& field_name,
46 uint64_t value,
47 NoiseLevel noise_level) {
48 shadow_.uint64_fields[field_name] =
49 std::pair<std::uint64_t, NoiseLevel>(value, noise_level);
50 Sample::SetUInt64Field(field_name, value, noise_level);
51}
52
csharrisonb50320c32015-11-02 15:59:5753TestSample::Shadow::Shadow(RapporType type) : type(type) {}
54
55TestSample::Shadow::Shadow(const TestSample::Shadow& other) {
56 type = other.type;
57 flag_fields = other.flag_fields;
58 string_fields = other.string_fields;
nzolghadr3fddfc52016-12-13 21:52:1259 uint64_fields = other.uint64_fields;
csharrisonb50320c32015-11-02 15:59:5760}
61
62TestSample::Shadow::~Shadow() {}
63
nzolghadrd87a308d2016-12-07 15:45:5664TestRapporServiceImpl::TestRapporServiceImpl()
65 : RapporServiceImpl(&test_prefs_,
66 base::Bind(&MockIsIncognito, &is_incognito_)),
holte5a7ed7c2015-01-09 23:52:4667 next_rotation_(base::TimeDelta()),
68 is_incognito_(false) {
69 RegisterPrefs(test_prefs_.registry());
70 test_uploader_ = new TestLogUploader();
dcheng82beb4f2016-04-26 00:35:0271 InitializeInternal(base::WrapUnique(test_uploader_), 0,
holte5a7ed7c2015-01-09 23:52:4672 HmacByteVectorGenerator::GenerateEntropyInput());
holte0036d9b2017-03-15 21:49:1673 Update(true, true);
holteca6486b2014-11-06 02:30:0774}
75
nzolghadrd87a308d2016-12-07 15:45:5676TestRapporServiceImpl::~TestRapporServiceImpl() {}
holteca6486b2014-11-06 02:30:0777
nzolghadrd87a308d2016-12-07 15:45:5678std::unique_ptr<Sample> TestRapporServiceImpl::CreateSample(RapporType type) {
dcheng82beb4f2016-04-26 00:35:0279 std::unique_ptr<TestSample> test_sample(new TestSample(type));
dcheng51ace48a2015-12-26 22:45:1780 return std::move(test_sample);
csharrisonb50320c32015-11-02 15:59:5781}
82
83// Intercepts the sample being recorded and saves it in a test structure.
nzolghadrd87a308d2016-12-07 15:45:5684void TestRapporServiceImpl::RecordSample(const std::string& metric_name,
85 std::unique_ptr<Sample> sample) {
csharrisonb50320c32015-11-02 15:59:5786 TestSample* test_sample = static_cast<TestSample*>(sample.get());
87 // Erase the previous sample if we logged one.
88 shadows_.erase(metric_name);
89 shadows_.insert(std::pair<std::string, TestSample::Shadow>(
90 metric_name, test_sample->GetShadow()));
91 // Original version is still called.
nzolghadrd87a308d2016-12-07 15:45:5692 RapporServiceImpl::RecordSample(metric_name, std::move(sample));
csharrisonb50320c32015-11-02 15:59:5793}
94
nzolghadrd87a308d2016-12-07 15:45:5695void TestRapporServiceImpl::RecordSampleString(const std::string& metric_name,
96 RapporType type,
97 const std::string& sample) {
mathp7deb9df2015-03-13 14:44:1698 // Save the recorded sample to the local structure.
99 RapporSample rappor_sample;
100 rappor_sample.type = type;
101 rappor_sample.value = sample;
102 samples_[metric_name] = rappor_sample;
103 // Original version is still called.
nzolghadrd87a308d2016-12-07 15:45:56104 RapporServiceImpl::RecordSampleString(metric_name, type, sample);
mathp7deb9df2015-03-13 14:44:16105}
106
nzolghadrd87a308d2016-12-07 15:45:56107int TestRapporServiceImpl::GetReportsCount() {
holteca6486b2014-11-06 02:30:07108 RapporReports reports;
109 ExportMetrics(&reports);
110 return reports.report_size();
111}
112
nzolghadrd87a308d2016-12-07 15:45:56113void TestRapporServiceImpl::GetReports(RapporReports* reports) {
holte5a7ed7c2015-01-09 23:52:46114 ExportMetrics(reports);
115}
116
nzolghadrd87a308d2016-12-07 15:45:56117TestSample::Shadow* TestRapporServiceImpl::GetRecordedSampleForMetric(
csharrisonb50320c32015-11-02 15:59:57118 const std::string& metric_name) {
jdoerrie3feb1852018-10-05 12:16:44119 auto it = shadows_.find(metric_name);
csharrisonb50320c32015-11-02 15:59:57120 if (it == shadows_.end())
121 return nullptr;
122 return &it->second;
123}
124
nzolghadrd87a308d2016-12-07 15:45:56125bool TestRapporServiceImpl::GetRecordedSampleForMetric(
mathp7deb9df2015-03-13 14:44:16126 const std::string& metric_name,
127 std::string* sample,
128 RapporType* type) {
jdoerrie3feb1852018-10-05 12:16:44129 auto it = samples_.find(metric_name);
mathp7deb9df2015-03-13 14:44:16130 if (it == samples_.end())
131 return false;
132 *sample = it->second.value;
133 *type = it->second.type;
134 return true;
135}
136
holte5a7ed7c2015-01-09 23:52:46137// Cancel the next call to OnLogInterval.
nzolghadrd87a308d2016-12-07 15:45:56138void TestRapporServiceImpl::CancelNextLogRotation() {
holte5a7ed7c2015-01-09 23:52:46139 next_rotation_ = base::TimeDelta();
140}
141
142// Schedule the next call to OnLogInterval.
nzolghadrd87a308d2016-12-07 15:45:56143void TestRapporServiceImpl::ScheduleNextLogRotation(base::TimeDelta interval) {
holte5a7ed7c2015-01-09 23:52:46144 next_rotation_ = interval;
145}
146
holteca6486b2014-11-06 02:30:07147} // namespace rappor