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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 RapporMetric metric("MyRappor", kTestRapporParameters, 0); | 42 RapporMetric metric("MyRappor", kTestRapporParameters, 0); |
43 | 43 |
44 const ByteVector report = metric.GetReport( | 44 const ByteVector report = metric.GetReport( |
45 HmacByteVectorGenerator::GenerateEntropyInput()); | 45 HmacByteVectorGenerator::GenerateEntropyInput()); |
46 EXPECT_EQ(16u, report.size()); | 46 EXPECT_EQ(16u, report.size()); |
47 } | 47 } |
48 | 48 |
49 TEST(RapporMetricTest, GetReportStatistics) { | 49 TEST(RapporMetricTest, GetReportStatistics) { |
50 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); | 50 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); |
51 | 51 |
52 for (char i = 0; i < 50; i++) { | 52 ByteVector real_bits(kTestStatsRapporParameters.bloom_filter_size_bytes); |
53 metric.AddSample(base::StringPrintf("%d", i)); | 53 // Set 152 bits (19 bytes) |
| 54 for (char i = 0; i < 19; i++) { |
| 55 real_bits[i] = 0xff; |
54 } | 56 } |
55 const ByteVector real_bits = metric.bytes(); | 57 metric.SetBytesForTesting(real_bits); |
56 const int real_bit_count = CountBits(real_bits); | 58 const int real_bit_count = CountBits(real_bits); |
57 EXPECT_EQ(real_bit_count, 152); | 59 EXPECT_EQ(real_bit_count, 152); |
58 | 60 |
59 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 61 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
60 const ByteVector report = metric.GetReport(secret); | 62 const ByteVector report = metric.GetReport(secret); |
61 | 63 |
62 // For the bits we actually set in the Bloom filter, get a count of how | 64 // For the bits we actually set in the Bloom filter, get a count of how |
63 // many of them reported true. | 65 // many of them reported true. |
64 ByteVector from_true_reports = report; | 66 ByteVector from_true_reports = report; |
65 // Real bits AND report bits. | 67 // Real bits AND report bits. |
(...skipping 23 matching lines...) Expand all Loading... |
89 // stats.binom(152, 0.65625).ppf(0.999995) = 124 | 91 // stats.binom(152, 0.65625).ppf(0.999995) = 124 |
90 EXPECT_LE(true_from_true_count, 124); | 92 EXPECT_LE(true_from_true_count, 124); |
91 | 93 |
92 // stats.binom(248, 0.59375).ppf(.000005) = 113 | 94 // stats.binom(248, 0.59375).ppf(.000005) = 113 |
93 EXPECT_GT(true_from_false_count, 113); | 95 EXPECT_GT(true_from_false_count, 113); |
94 // stats.binom(248, 0.59375).ppf(.999995) = 181 | 96 // stats.binom(248, 0.59375).ppf(.999995) = 181 |
95 EXPECT_LE(true_from_false_count, 181); | 97 EXPECT_LE(true_from_false_count, 181); |
96 } | 98 } |
97 | 99 |
98 } // namespace rappor | 100 } // namespace rappor |
OLD | NEW |