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"
17 class TestRapporService : public RapporService {
19 TestRapporService(ReportingLevel reporting_level) : RapporService(&prefs) {
20 RegisterPrefs(prefs.registry());
22 HmacByteVectorGenerator::GenerateEntropyInput(),
26 void GetReports(RapporReports* reports) {
27 ExportMetrics(reports);
30 int32_t TestLoadCohort() {
34 std::string TestLoadSecret() {
38 void TestRecordSample(const std::string& metric_name,
39 const RapporParameters& parameters,
40 const std::string& sample) {
41 RecordSampleInternal(metric_name, parameters, sample);
44 TestingPrefServiceSimple prefs;
47 DISALLOW_COPY_AND_ASSIGN(TestRapporService);
50 TEST(RapporServiceTest, LoadCohort) {
51 TestRapporService rappor_service(REPORTING_DISABLED);
52 rappor_service.prefs.SetInteger(prefs::kRapporCohortSeed, 1);
53 EXPECT_EQ(1, rappor_service.TestLoadCohort());
56 TEST(RapporServiceTest, LoadSecret) {
57 TestRapporService rappor_service(REPORTING_DISABLED);
58 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
59 std::string secret_base64;
60 base::Base64Encode(secret, &secret_base64);
61 rappor_service.prefs.SetString(prefs::kRapporSecret, secret_base64);
62 EXPECT_EQ(secret, rappor_service.TestLoadSecret());
65 // Check that samples can be recorded and exported.
66 TEST(RapporServiceTest, RecordAndExportMetrics) {
67 const RapporParameters kTestRapporParameters = {
69 16 /* Bloom filter size bytes */,
70 4 /* Bloom filter hash count */,
71 PROBABILITY_75 /* Fake data probability */,
72 PROBABILITY_50 /* Fake one probability */,
73 PROBABILITY_75 /* One coin probability */,
74 PROBABILITY_50 /* Zero coin probability */,
77 TestRapporService rappor_service(COARSE_LEVEL);
79 // Multiple samples for the same metric should only generate one report.
80 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo");
81 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar");
83 RapporReports reports;
84 rappor_service.GetReports(&reports);
85 EXPECT_EQ(1, reports.report_size());
87 const RapporReports::Report& report = reports.report(0);
88 EXPECT_TRUE(report.name_hash());
89 EXPECT_EQ(16u, report.bits().size());
92 // Check that the reporting level is respected.
93 TEST(RapporServiceTest, ReportingLevel) {
94 const RapporParameters kFineRapporParameters = {
96 16 /* Bloom filter size bytes */,
97 4 /* Bloom filter hash count */,
98 PROBABILITY_75 /* Fake data probability */,
99 PROBABILITY_50 /* Fake one probability */,
100 PROBABILITY_75 /* One coin probability */,
101 PROBABILITY_50 /* Zero coin probability */,
104 TestRapporService rappor_service(COARSE_LEVEL);
106 rappor_service.TestRecordSample("FineMetric", kFineRapporParameters, "foo");
108 RapporReports reports;
109 rappor_service.GetReports(&reports);
110 EXPECT_EQ(0, reports.report_size());
113 } // namespace rappor