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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 50 /* Bloom filter size bytes */, | 26 50 /* Bloom filter size bytes */, |
27 4 /* Bloom filter hash count */, | 27 4 /* Bloom filter hash count */, |
28 PROBABILITY_75 /* Fake data probability */, | 28 PROBABILITY_75 /* Fake data probability */, |
29 PROBABILITY_50 /* Fake one probability */, | 29 PROBABILITY_50 /* Fake one probability */, |
30 PROBABILITY_75 /* One coin probability */, | 30 PROBABILITY_75 /* One coin probability */, |
31 PROBABILITY_50 /* Zero coin probability */}; | 31 PROBABILITY_50 /* Zero coin probability */}; |
32 | 32 |
33 // Check for basic syntax and use. | 33 // Check for basic syntax and use. |
34 TEST(RapporMetricTest, BasicMetric) { | 34 TEST(RapporMetricTest, BasicMetric) { |
35 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); | 35 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); |
36 testMetric.AddSample("Foo"); | |
37 testMetric.AddSample("Bar"); | 36 testMetric.AddSample("Bar"); |
38 EXPECT_EQ(0x80, testMetric.bytes()[1]); | 37 EXPECT_EQ(0x80, testMetric.bytes()[1]); |
39 } | 38 } |
40 | 39 |
41 TEST(RapporMetricTest, GetReport) { | 40 TEST(RapporMetricTest, GetReport) { |
42 RapporMetric metric("MyRappor", kTestRapporParameters, 0); | 41 RapporMetric metric("MyRappor", kTestRapporParameters, 0); |
43 | 42 |
44 const ByteVector report = metric.GetReport( | 43 const ByteVector report = metric.GetReport( |
45 HmacByteVectorGenerator::GenerateEntropyInput()); | 44 HmacByteVectorGenerator::GenerateEntropyInput()); |
46 EXPECT_EQ(16u, report.size()); | 45 EXPECT_EQ(16u, report.size()); |
47 } | 46 } |
48 | 47 |
49 TEST(RapporMetricTest, GetReportStatistics) { | 48 TEST(RapporMetricTest, GetReportStatistics) { |
50 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); | 49 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); |
51 | 50 |
52 for (char i = 0; i < 50; i++) { | 51 ByteVector real_bits(kTestStatsRapporParameters.bloom_filter_size_bytes); |
53 metric.AddSample(base::StringPrintf("%d", i)); | 52 // Set 152 bits (19 bytes) |
| 53 for (char i = 0; i < 19; i++) { |
| 54 real_bits[i] = 0xff; |
54 } | 55 } |
55 const ByteVector real_bits = metric.bytes(); | 56 metric.SetBytesForTesting(real_bits); |
56 const int real_bit_count = CountBits(real_bits); | 57 const int real_bit_count = CountBits(real_bits); |
57 EXPECT_EQ(real_bit_count, 152); | 58 EXPECT_EQ(real_bit_count, 152); |
58 | 59 |
59 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 60 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
60 const ByteVector report = metric.GetReport(secret); | 61 const ByteVector report = metric.GetReport(secret); |
61 | 62 |
62 // For the bits we actually set in the Bloom filter, get a count of how | 63 // For the bits we actually set in the Bloom filter, get a count of how |
63 // many of them reported true. | 64 // many of them reported true. |
64 ByteVector from_true_reports = report; | 65 ByteVector from_true_reports = report; |
65 // Real bits AND report bits. | 66 // Real bits AND report bits. |
(...skipping 23 matching lines...) Expand all Loading... |
89 // stats.binom(152, 0.65625).ppf(0.999995) = 124 | 90 // stats.binom(152, 0.65625).ppf(0.999995) = 124 |
90 EXPECT_LE(true_from_true_count, 124); | 91 EXPECT_LE(true_from_true_count, 124); |
91 | 92 |
92 // stats.binom(248, 0.59375).ppf(.000005) = 113 | 93 // stats.binom(248, 0.59375).ppf(.000005) = 113 |
93 EXPECT_GT(true_from_false_count, 113); | 94 EXPECT_GT(true_from_false_count, 113); |
94 // stats.binom(248, 0.59375).ppf(.999995) = 181 | 95 // stats.binom(248, 0.59375).ppf(.999995) = 181 |
95 EXPECT_LE(true_from_false_count, 181); | 96 EXPECT_LE(true_from_false_count, 181); |
96 } | 97 } |
97 | 98 |
98 } // namespace rappor | 99 } // namespace rappor |
OLD | NEW |