OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/rappor/sampler.h" |
| 6 |
| 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" |
| 10 |
| 11 namespace rappor { |
| 12 |
| 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 FINE_LEVEL /* Reporting level (not used) */}; |
| 22 |
| 23 class TestSamplerFactory { |
| 24 public: |
| 25 static scoped_ptr<Sample> CreateSample() { |
| 26 return scoped_ptr<Sample>(new Sample(0, kTestRapporParameters)); |
| 27 } |
| 28 }; |
| 29 |
| 30 namespace internal { |
| 31 |
| 32 // Test that exporting deletes samples. |
| 33 TEST(RapporSamplerTest, TestExport) { |
| 34 Sampler sampler; |
| 35 |
| 36 scoped_ptr<Sample> sample1 = TestSamplerFactory::CreateSample(); |
| 37 sample1->SetStringField("Foo", "Junk"); |
| 38 sampler.AddSample("Metric1", sample1.Pass()); |
| 39 |
| 40 scoped_ptr<Sample> sample2 = TestSamplerFactory::CreateSample(); |
| 41 sample2->SetStringField("Foo", "Junk2"); |
| 42 sampler.AddSample("Metric1", sample2.Pass()); |
| 43 |
| 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()); |
| 51 |
| 52 // First export should clear the metric. |
| 53 RapporReports reports2; |
| 54 sampler.ExportMetrics(secret, &reports2); |
| 55 EXPECT_EQ(0, reports2.report_size()); |
| 56 } |
| 57 |
| 58 } // namespace internal |
| 59 |
| 60 } // namespace rappor |
OLD | NEW |