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_metric.h" | 5 #include "components/rappor/rappor_metric.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace rappor { | 13 namespace rappor { |
14 | 14 |
15 const RapporParameters kTestRapporParameters = { | 15 const RapporParameters kTestRapporParameters = { |
16 1 /* Num cohorts */, | 16 1 /* Num cohorts */, |
17 16 /* Bloom filter size bytes */, | 17 4 /* Bloom filter size bytes */, |
18 4 /* Bloom filter hash count */, | 18 2 /* Bloom filter hash count */, |
| 19 4, /* Flag bytes */ |
19 PROBABILITY_75 /* Fake data probability */, | 20 PROBABILITY_75 /* Fake data probability */, |
20 PROBABILITY_50 /* Fake one probability */, | 21 PROBABILITY_50 /* Fake one probability */, |
21 PROBABILITY_75 /* One coin probability */, | 22 PROBABILITY_75 /* One coin probability */, |
22 PROBABILITY_50 /* Zero coin probability */, | 23 PROBABILITY_50 /* Zero coin probability */, |
23 FINE_LEVEL /* Reporting level (not used) */}; | 24 FINE_LEVEL /* Reporting level (not used) */}; |
24 | 25 |
25 const RapporParameters kTestStatsRapporParameters = { | 26 const RapporParameters kTestStatsRapporParameters = { |
26 1 /* Num cohorts */, | 27 1 /* Num cohorts */, |
27 50 /* Bloom filter size bytes */, | 28 50 /* Bloom filter size bytes */, |
28 4 /* Bloom filter hash count */, | 29 4 /* Bloom filter hash count */, |
| 30 0, /* Flag bytes */ |
29 PROBABILITY_75 /* Fake data probability */, | 31 PROBABILITY_75 /* Fake data probability */, |
30 PROBABILITY_50 /* Fake one probability */, | 32 PROBABILITY_50 /* Fake one probability */, |
31 PROBABILITY_75 /* One coin probability */, | 33 PROBABILITY_75 /* One coin probability */, |
32 PROBABILITY_50 /* Zero coin probability */, | 34 PROBABILITY_50 /* Zero coin probability */, |
33 FINE_LEVEL /* Reporting level (not used) */}; | 35 FINE_LEVEL /* Reporting level (not used) */}; |
34 | 36 |
35 // Check for basic syntax and use. | 37 TEST(RapporMetricTest, SetSampleBits) { |
36 TEST(RapporMetricTest, BasicMetric) { | 38 internal::Sample sample; |
37 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); | 39 internal::SetSampleBits(kTestRapporParameters, 0, "Foo", 0x12, &sample); |
38 testMetric.AddSample("Bar"); | 40 EXPECT_EQ(8u, sample.size()); |
39 EXPECT_EQ(0x80, testMetric.bytes()[1]); | 41 EXPECT_EQ(0x12, sample[4]); |
40 } | 42 } |
41 | 43 |
42 TEST(RapporMetricTest, GetReport) { | 44 TEST(RapporMetricTest, GetReport) { |
43 RapporMetric metric("MyRappor", kTestRapporParameters, 0); | 45 internal::RapporMetric metric("MyRappor", kTestRapporParameters); |
44 | 46 |
45 const ByteVector report = metric.GetReport( | 47 const ByteVector report = metric.GetReport( |
46 HmacByteVectorGenerator::GenerateEntropyInput()); | 48 HmacByteVectorGenerator::GenerateEntropyInput()); |
47 EXPECT_EQ(16u, report.size()); | 49 EXPECT_EQ(8u, report.size()); |
48 } | 50 } |
49 | 51 |
50 TEST(RapporMetricTest, GetReportStatistics) { | 52 TEST(RapporMetricTest, GetReportStatistics) { |
51 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); | 53 internal::RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters); |
52 | 54 |
53 ByteVector real_bits(kTestStatsRapporParameters.bloom_filter_size_bytes); | 55 ByteVector real_bits(kTestStatsRapporParameters.bloom_filter_size_bytes); |
54 // Set 152 bits (19 bytes) | 56 // Set 152 bits (19 bytes) |
55 for (char i = 0; i < 19; i++) { | 57 for (char i = 0; i < 19; i++) { |
56 real_bits[i] = 0xff; | 58 real_bits[i] = 0xff; |
57 } | 59 } |
58 metric.SetBytesForTesting(real_bits); | 60 metric.AddSample(real_bits); |
59 const int real_bit_count = CountBits(real_bits); | 61 const int real_bit_count = CountBits(real_bits); |
60 EXPECT_EQ(real_bit_count, 152); | 62 EXPECT_EQ(real_bit_count, 152); |
61 | 63 |
62 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 64 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
63 const ByteVector report = metric.GetReport(secret); | 65 const ByteVector report = metric.GetReport(secret); |
64 | 66 |
65 // For the bits we actually set in the Bloom filter, get a count of how | 67 // For the bits we actually set in the Bloom filter, get a count of how |
66 // many of them reported true. | 68 // many of them reported true. |
67 ByteVector from_true_reports = report; | 69 ByteVector from_true_reports = report; |
68 // Real bits AND report bits. | 70 // Real bits AND report bits. |
(...skipping 23 matching lines...) Expand all Loading... |
92 // stats.binom(152, 0.65625).ppf(0.999995) = 124 | 94 // stats.binom(152, 0.65625).ppf(0.999995) = 124 |
93 EXPECT_LE(true_from_true_count, 124); | 95 EXPECT_LE(true_from_true_count, 124); |
94 | 96 |
95 // stats.binom(248, 0.59375).ppf(.000005) = 113 | 97 // stats.binom(248, 0.59375).ppf(.000005) = 113 |
96 EXPECT_GT(true_from_false_count, 113); | 98 EXPECT_GT(true_from_false_count, 113); |
97 // stats.binom(248, 0.59375).ppf(.999995) = 181 | 99 // stats.binom(248, 0.59375).ppf(.999995) = 181 |
98 EXPECT_LE(true_from_false_count, 181); | 100 EXPECT_LE(true_from_false_count, 181); |
99 } | 101 } |
100 | 102 |
101 } // namespace rappor | 103 } // namespace rappor |
OLD | NEW |