Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / rappor / sampler_unittest.cc
blob122abc26009e80da5996e3cb0c3dae989595c768
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
3 // found in the LICENSE file.
5 #include "components/rappor/sampler.h"
7 #include "components/rappor/byte_vector_utils.h"
8 #include "components/rappor/proto/rappor_metric.pb.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace rappor {
13 const RapporParameters kTestRapporParameters = {
14 1 /* Num cohorts */,
15 1 /* Bloom filter size bytes */,
16 4 /* Bloom filter hash count */,
17 PROBABILITY_75 /* Fake data probability */,
18 PROBABILITY_50 /* Fake one probability */,
19 PROBABILITY_75 /* One coin probability */,
20 PROBABILITY_50 /* Zero coin probability */,
21 UMA_RAPPOR_GROUP /* Recording group (not used) */};
23 class TestSamplerFactory {
24 public:
25 static scoped_ptr<Sample> CreateSample() {
26 return scoped_ptr<Sample>(new Sample(0, kTestRapporParameters));
30 namespace internal {
32 // Test that exporting deletes samples.
33 TEST(RapporSamplerTest, TestExport) {
34 Sampler sampler;
36 scoped_ptr<Sample> sample1 = TestSamplerFactory::CreateSample();
37 sample1->SetStringField("Foo", "Junk");
38 sampler.AddSample("Metric1", sample1.Pass());
40 scoped_ptr<Sample> sample2 = TestSamplerFactory::CreateSample();
41 sample2->SetStringField("Foo", "Junk2");
42 sampler.AddSample("Metric1", sample2.Pass());
44 // Since the two samples were for one metric, we should randomly get one
45 // of the two.
46 RapporReports reports;
47 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
48 sampler.ExportMetrics(secret, &reports);
49 EXPECT_EQ(1, reports.report_size());
50 EXPECT_EQ(1u, reports.report(0).bits().size());
52 // First export should clear the metric.
53 RapporReports reports2;
54 sampler.ExportMetrics(secret, &reports2);
55 EXPECT_EQ(0, reports2.report_size());
58 } // namespace internal
60 } // namespace rappor