Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / rappor / rappor_service_unittest.cc
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.
4
5 #include "components/rappor/rappor_service.h"
6
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"
14
15 namespace rappor {
16
17 class TestRapporService : public RapporService {
18  public:
19   TestRapporService(ReportingLevel reporting_level) : RapporService(&prefs) {
20     RegisterPrefs(prefs.registry());
21     Initialize(0,
22                HmacByteVectorGenerator::GenerateEntropyInput(),
23                reporting_level);
24   }
25
26   void GetReports(RapporReports* reports) {
27     ExportMetrics(reports);
28   }
29
30   int32_t TestLoadCohort() {
31     return LoadCohort();
32   }
33
34   std::string TestLoadSecret() {
35     return LoadSecret();
36   }
37
38   void TestRecordSample(const std::string& metric_name,
39                         const RapporParameters& parameters,
40                         const std::string& sample) {
41     RecordSampleInternal(metric_name, parameters, sample);
42   }
43
44   TestingPrefServiceSimple prefs;
45
46  private:
47   DISALLOW_COPY_AND_ASSIGN(TestRapporService);
48 };
49
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());
54 }
55
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());
63 }
64
65 // Check that samples can be recorded and exported.
66 TEST(RapporServiceTest, RecordAndExportMetrics) {
67   const RapporParameters kTestRapporParameters = {
68       1 /* Num cohorts */,
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 */,
75       COARSE_LEVEL};
76
77   TestRapporService rappor_service(COARSE_LEVEL);
78
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");
82
83   RapporReports reports;
84   rappor_service.GetReports(&reports);
85   EXPECT_EQ(1, reports.report_size());
86
87   const RapporReports::Report& report = reports.report(0);
88   EXPECT_TRUE(report.name_hash());
89   EXPECT_EQ(16u, report.bits().size());
90 }
91
92 // Check that the reporting level is respected.
93 TEST(RapporServiceTest, ReportingLevel) {
94   const RapporParameters kFineRapporParameters = {
95       1 /* Num cohorts */,
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 */,
102       FINE_LEVEL};
103
104   TestRapporService rappor_service(COARSE_LEVEL);
105
106   rappor_service.TestRecordSample("FineMetric", kFineRapporParameters, "foo");
107
108   RapporReports reports;
109   rappor_service.GetReports(&reports);
110   EXPECT_EQ(0, reports.report_size());
111 }
112
113 }  // namespace rappor