OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/sampler.h" | 5 #include "components/rappor/sampler.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/metrics/metrics_hashes.h" | 10 #include "base/metrics/metrics_hashes.h" |
10 #include "components/rappor/byte_vector_utils.h" | 11 #include "components/rappor/byte_vector_utils.h" |
11 #include "components/rappor/proto/rappor_metric.pb.h" | 12 #include "components/rappor/proto/rappor_metric.pb.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace rappor { | 15 namespace rappor { |
15 | 16 |
16 const RapporParameters kTestRapporParameters = { | 17 const RapporParameters kTestRapporParameters = { |
17 1 /* Num cohorts */, | 18 1 /* Num cohorts */, |
18 1 /* Bloom filter size bytes */, | 19 1 /* Bloom filter size bytes */, |
19 4 /* Bloom filter hash count */, | 20 4 /* Bloom filter hash count */, |
20 NORMAL_NOISE /* Noise level */, | 21 NORMAL_NOISE /* Noise level */, |
21 UMA_RAPPOR_GROUP /* Recording group (not used) */}; | 22 UMA_RAPPOR_GROUP /* Recording group (not used) */}; |
22 | 23 |
23 class TestSamplerFactory { | 24 class TestSamplerFactory { |
24 public: | 25 public: |
25 static scoped_ptr<Sample> CreateSample() { | 26 static std::unique_ptr<Sample> CreateSample() { |
26 return scoped_ptr<Sample>(new Sample(0, kTestRapporParameters)); | 27 return std::unique_ptr<Sample>(new Sample(0, kTestRapporParameters)); |
27 } | 28 } |
28 }; | 29 }; |
29 | 30 |
30 namespace internal { | 31 namespace internal { |
31 | 32 |
32 // Test that exporting deletes samples. | 33 // Test that exporting deletes samples. |
33 TEST(RapporSamplerTest, TestExport) { | 34 TEST(RapporSamplerTest, TestExport) { |
34 Sampler sampler; | 35 Sampler sampler; |
35 | 36 |
36 scoped_ptr<Sample> sample1 = TestSamplerFactory::CreateSample(); | 37 std::unique_ptr<Sample> sample1 = TestSamplerFactory::CreateSample(); |
37 sample1->SetStringField("Foo", "Junk"); | 38 sample1->SetStringField("Foo", "Junk"); |
38 sampler.AddSample("Metric1", std::move(sample1)); | 39 sampler.AddSample("Metric1", std::move(sample1)); |
39 | 40 |
40 scoped_ptr<Sample> sample2 = TestSamplerFactory::CreateSample(); | 41 std::unique_ptr<Sample> sample2 = TestSamplerFactory::CreateSample(); |
41 sample2->SetStringField("Foo", "Junk2"); | 42 sample2->SetStringField("Foo", "Junk2"); |
42 sampler.AddSample("Metric1", std::move(sample2)); | 43 sampler.AddSample("Metric1", std::move(sample2)); |
43 | 44 |
44 // Since the two samples were for one metric, we should randomly get one | 45 // Since the two samples were for one metric, we should randomly get one |
45 // of the two. | 46 // of the two. |
46 RapporReports reports; | 47 RapporReports reports; |
47 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 48 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
48 sampler.ExportMetrics(secret, &reports); | 49 sampler.ExportMetrics(secret, &reports); |
49 EXPECT_EQ(1, reports.report_size()); | 50 EXPECT_EQ(1, reports.report_size()); |
50 EXPECT_EQ(1u, reports.report(0).bits().size()); | 51 EXPECT_EQ(1u, reports.report(0).bits().size()); |
51 | 52 |
52 // First export should clear the metric. | 53 // First export should clear the metric. |
53 RapporReports reports2; | 54 RapporReports reports2; |
54 sampler.ExportMetrics(secret, &reports2); | 55 sampler.ExportMetrics(secret, &reports2); |
55 EXPECT_EQ(0, reports2.report_size()); | 56 EXPECT_EQ(0, reports2.report_size()); |
56 } | 57 } |
57 | 58 |
58 // Test exporting fields with NO_NOISE. | 59 // Test exporting fields with NO_NOISE. |
59 TEST(RapporSamplerTest, TestNoNoise) { | 60 TEST(RapporSamplerTest, TestNoNoise) { |
60 Sampler sampler; | 61 Sampler sampler; |
61 | 62 |
62 scoped_ptr<Sample> sample1 = TestSamplerFactory::CreateSample(); | 63 std::unique_ptr<Sample> sample1 = TestSamplerFactory::CreateSample(); |
63 sample1->SetFlagsField("Foo", 0xde, 8, NO_NOISE); | 64 sample1->SetFlagsField("Foo", 0xde, 8, NO_NOISE); |
64 sample1->SetUInt64Field("Bar", 0x0011223344aabbccdd, NO_NOISE); | 65 sample1->SetUInt64Field("Bar", 0x0011223344aabbccdd, NO_NOISE); |
65 sampler.AddSample("Metric1", std::move(sample1)); | 66 sampler.AddSample("Metric1", std::move(sample1)); |
66 | 67 |
67 RapporReports reports; | 68 RapporReports reports; |
68 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 69 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
69 sampler.ExportMetrics(secret, &reports); | 70 sampler.ExportMetrics(secret, &reports); |
70 EXPECT_EQ(2, reports.report_size()); | 71 EXPECT_EQ(2, reports.report_size()); |
71 | 72 |
72 uint64_t hash1 = base::HashMetricName("Metric1.Foo"); | 73 uint64_t hash1 = base::HashMetricName("Metric1.Foo"); |
73 bool order = reports.report(0).name_hash() == hash1; | 74 bool order = reports.report(0).name_hash() == hash1; |
74 const RapporReports::Report& report1 = reports.report(order ? 0 : 1); | 75 const RapporReports::Report& report1 = reports.report(order ? 0 : 1); |
75 EXPECT_EQ("\xde", report1.bits()); | 76 EXPECT_EQ("\xde", report1.bits()); |
76 const RapporReports::Report& report2 = reports.report(order ? 1 : 0); | 77 const RapporReports::Report& report2 = reports.report(order ? 1 : 0); |
77 EXPECT_EQ("\xdd\xcc\xbb\xaa\x44\x33\x22\x11\x00", report2.bits()); | 78 EXPECT_EQ("\xdd\xcc\xbb\xaa\x44\x33\x22\x11\x00", report2.bits()); |
78 } | 79 } |
79 | 80 |
80 } // namespace internal | 81 } // namespace internal |
81 | 82 |
82 } // namespace rappor | 83 } // namespace rappor |
OLD | NEW |