Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / rappor / rappor_service_unittest.cc
blobe9baf02d3543f2d89bd1beceb7ec992dbac8ec43
1 // 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.
5 #include "components/rappor/rappor_service.h"
7 #include "base/base64.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "components/rappor/byte_vector_utils.h"
10 #include "components/rappor/proto/rappor_metric.pb.h"
11 #include "components/rappor/rappor_parameters.h"
12 #include "components/rappor/rappor_pref_names.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace rappor {
17 namespace {
19 bool MockIsIncognito(bool is_incognito) {
20 return is_incognito;
23 } // namespace
25 class TestRapporService : public RapporService {
26 public:
27 TestRapporService(ReportingLevel reporting_level, bool is_incognito)
28 : RapporService(&prefs, base::Bind(&MockIsIncognito, is_incognito)) {
29 RegisterPrefs(prefs.registry());
30 Initialize(0,
31 HmacByteVectorGenerator::GenerateEntropyInput(),
32 reporting_level);
35 void GetReports(RapporReports* reports) {
36 ExportMetrics(reports);
39 int32_t TestLoadCohort() {
40 return LoadCohort();
43 std::string TestLoadSecret() {
44 return LoadSecret();
47 void TestRecordSample(const std::string& metric_name,
48 const RapporParameters& parameters,
49 const std::string& sample) {
50 RecordSampleInternal(metric_name, parameters, sample);
53 TestingPrefServiceSimple prefs;
55 private:
56 DISALLOW_COPY_AND_ASSIGN(TestRapporService);
59 TEST(RapporServiceTest, LoadCohort) {
60 TestRapporService rappor_service(REPORTING_DISABLED, false);
61 rappor_service.prefs.SetInteger(prefs::kRapporCohortSeed, 1);
62 EXPECT_EQ(1, rappor_service.TestLoadCohort());
65 TEST(RapporServiceTest, LoadSecret) {
66 TestRapporService rappor_service(REPORTING_DISABLED, false);
67 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
68 std::string secret_base64;
69 base::Base64Encode(secret, &secret_base64);
70 rappor_service.prefs.SetString(prefs::kRapporSecret, secret_base64);
71 EXPECT_EQ(secret, rappor_service.TestLoadSecret());
74 // Check that samples can be recorded and exported.
75 TEST(RapporServiceTest, RecordAndExportMetrics) {
76 const RapporParameters kTestRapporParameters = {
77 1 /* Num cohorts */,
78 16 /* Bloom filter size bytes */,
79 4 /* Bloom filter hash count */,
80 PROBABILITY_75 /* Fake data probability */,
81 PROBABILITY_50 /* Fake one probability */,
82 PROBABILITY_75 /* One coin probability */,
83 PROBABILITY_50 /* Zero coin probability */,
84 COARSE_LEVEL};
86 TestRapporService rappor_service(COARSE_LEVEL, false);
88 // Multiple samples for the same metric should only generate one report.
89 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo");
90 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar");
92 RapporReports reports;
93 rappor_service.GetReports(&reports);
94 EXPECT_EQ(1, reports.report_size());
96 const RapporReports::Report& report = reports.report(0);
97 EXPECT_TRUE(report.name_hash());
98 EXPECT_EQ(16u, report.bits().size());
101 // Check that the reporting level is respected.
102 TEST(RapporServiceTest, ReportingLevel) {
103 const RapporParameters kFineRapporParameters = {
104 1 /* Num cohorts */,
105 16 /* Bloom filter size bytes */,
106 4 /* Bloom filter hash count */,
107 PROBABILITY_75 /* Fake data probability */,
108 PROBABILITY_50 /* Fake one probability */,
109 PROBABILITY_75 /* One coin probability */,
110 PROBABILITY_50 /* Zero coin probability */,
111 FINE_LEVEL};
113 TestRapporService rappor_service(COARSE_LEVEL, false);
115 rappor_service.TestRecordSample("FineMetric", kFineRapporParameters, "foo");
117 RapporReports reports;
118 rappor_service.GetReports(&reports);
119 EXPECT_EQ(0, reports.report_size());
122 // Check that the incognito is respected.
123 TEST(RapporServiceTest, Incognito) {
124 const RapporParameters kFineRapporParameters = {
125 1 /* Num cohorts */,
126 16 /* Bloom filter size bytes */,
127 4 /* Bloom filter hash count */,
128 PROBABILITY_75 /* Fake data probability */,
129 PROBABILITY_50 /* Fake one probability */,
130 PROBABILITY_75 /* One coin probability */,
131 PROBABILITY_50 /* Zero coin probability */,
132 COARSE_LEVEL};
134 TestRapporService rappor_service(COARSE_LEVEL, true);
136 rappor_service.TestRecordSample("MyMetric", kFineRapporParameters, "foo");
138 RapporReports reports;
139 rappor_service.GetReports(&reports);
140 EXPECT_EQ(0, reports.report_size());
143 } // namespace rappor