OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service.h" |
6 | 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/prefs/testing_pref_service.h" |
7 #include "components/rappor/byte_vector_utils.h" | 9 #include "components/rappor/byte_vector_utils.h" |
8 #include "components/rappor/proto/rappor_metric.pb.h" | 10 #include "components/rappor/proto/rappor_metric.pb.h" |
9 #include "components/rappor/rappor_parameters.h" | 11 #include "components/rappor/rappor_parameters.h" |
| 12 #include "components/rappor/rappor_pref_names.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 namespace rappor { | 15 namespace rappor { |
13 | 16 |
14 class TestRapporService : public RapporService { | 17 class TestRapporService : public RapporService { |
15 public: | 18 public: |
| 19 TestRapporService() : RapporService(&prefs_) { |
| 20 RegisterPrefs(prefs_.registry()); |
| 21 prefs_.SetInteger(prefs::kRapporCohortSeed, 0); |
| 22 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
| 23 std::string secret_base64; |
| 24 base::Base64Encode(secret, &secret_base64); |
| 25 prefs_.SetString(prefs::kRapporSecret, secret_base64); |
| 26 LoadCohort(); |
| 27 LoadSecret(); |
| 28 } |
| 29 |
16 void GetReports(RapporReports* reports) { | 30 void GetReports(RapporReports* reports) { |
17 ExportMetrics(reports); | 31 ExportMetrics(reports); |
18 } | 32 } |
| 33 |
19 void TestRecordSample(const std::string& metric_name, | 34 void TestRecordSample(const std::string& metric_name, |
20 const RapporParameters& parameters, | 35 const RapporParameters& parameters, |
21 const std::string& sample) { | 36 const std::string& sample) { |
22 RecordSampleInternal(metric_name, parameters, sample); | 37 RecordSampleInternal(metric_name, parameters, sample); |
23 } | 38 } |
| 39 |
| 40 protected: |
| 41 TestingPrefServiceSimple prefs_; |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(TestRapporService); |
24 }; | 45 }; |
25 | 46 |
26 TEST(RapporServiceTest, RecordAndExportMetrics) { | 47 TEST(RapporServiceTest, RecordAndExportMetrics) { |
27 const RapporParameters kTestRapporParameters = { | 48 const RapporParameters kTestRapporParameters = { |
28 1 /* Num cohorts */, | 49 1 /* Num cohorts */, |
29 16 /* Bloom filter size bytes */, | 50 16 /* Bloom filter size bytes */, |
30 4 /* Bloom filter hash count */, | 51 4 /* Bloom filter hash count */, |
31 PROBABILITY_75 /* Fake data probability */, | 52 PROBABILITY_75 /* Fake data probability */, |
32 PROBABILITY_50 /* Fake one probability */, | 53 PROBABILITY_50 /* Fake one probability */, |
33 PROBABILITY_75 /* One coin probability */, | 54 PROBABILITY_75 /* One coin probability */, |
34 PROBABILITY_50 /* Zero coin probability */}; | 55 PROBABILITY_50 /* Zero coin probability */}; |
35 | 56 |
36 TestRapporService rappor_service; | 57 TestRapporService rappor_service; |
37 rappor_service.SetCohortForTesting(0); | |
38 rappor_service.SetSecretForTesting( | |
39 HmacByteVectorGenerator::GenerateEntropyInput()); | |
40 | 58 |
41 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo"); | 59 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo"); |
42 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar"); | 60 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar"); |
43 | 61 |
44 RapporReports reports; | 62 RapporReports reports; |
45 rappor_service.GetReports(&reports); | 63 rappor_service.GetReports(&reports); |
46 EXPECT_EQ(1, reports.report_size()); | 64 EXPECT_EQ(1, reports.report_size()); |
47 | 65 |
48 const RapporReports::Report& report = reports.report(0); | 66 const RapporReports::Report& report = reports.report(0); |
49 EXPECT_TRUE(report.name_hash()); | 67 EXPECT_TRUE(report.name_hash()); |
50 EXPECT_EQ(16u, report.bits().size()); | 68 EXPECT_EQ(16u, report.bits().size()); |
51 } | 69 } |
52 | 70 |
53 } // namespace rappor | 71 } // namespace rappor |
OLD | NEW |