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 // Change for readability |
| 6 |
5 #include "components/rappor/rappor_metric.h" | 7 #include "components/rappor/rappor_metric.h" |
6 | 8 |
7 #include <stdlib.h> | 9 #include <stdlib.h> |
8 | 10 |
9 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace rappor { | 15 namespace rappor { |
14 | 16 |
(...skipping 19 matching lines...) Expand all Loading... |
34 TEST(RapporMetricTest, BasicMetric) { | 36 TEST(RapporMetricTest, BasicMetric) { |
35 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); | 37 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); |
36 testMetric.AddSample("Foo"); | 38 testMetric.AddSample("Foo"); |
37 testMetric.AddSample("Bar"); | 39 testMetric.AddSample("Bar"); |
38 EXPECT_EQ(0x80, testMetric.bytes()[1]); | 40 EXPECT_EQ(0x80, testMetric.bytes()[1]); |
39 } | 41 } |
40 | 42 |
41 TEST(RapporMetricTest, GetReport) { | 43 TEST(RapporMetricTest, GetReport) { |
42 RapporMetric metric("MyRappor", kTestRapporParameters, 0); | 44 RapporMetric metric("MyRappor", kTestRapporParameters, 0); |
43 | 45 |
44 ByteVector report = metric.GetReport( | 46 const ByteVector report = metric.GetReport( |
45 HmacByteVectorGenerator::GenerateEntropyInput()); | 47 HmacByteVectorGenerator::GenerateEntropyInput()); |
46 EXPECT_EQ(16u, report.size()); | 48 EXPECT_EQ(16u, report.size()); |
47 } | 49 } |
48 | 50 |
49 TEST(RapporMetricTest, GetReportStatistics) { | 51 TEST(RapporMetricTest, GetReportStatistics) { |
50 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); | 52 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); |
51 | 53 |
52 for (char i = 0; i < 50; i++) { | 54 for (char i = 0; i < 50; i++) { |
53 metric.AddSample(base::StringPrintf("%d", i)); | 55 metric.AddSample(base::StringPrintf("%d", i)); |
54 } | 56 } |
55 ByteVector real_bits = metric.bytes(); | 57 const ByteVector real_bits = metric.bytes(); |
56 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 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 61 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
60 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. |
66 ByteVectorMerge(real_bits, real_bits, &from_true_reports); | 68 ByteVectorMerge(real_bits, real_bits, &from_true_reports); |
67 int true_from_true_count = CountBits(from_true_reports); | 69 const int true_from_true_count = CountBits(from_true_reports); |
68 | 70 |
69 // For the bits we didn't set in the bloom filter, get a count of how | 71 // For the bits we didn't set in the Bloom filter, get a count of how |
70 // many of them reported true. | 72 // many of them reported true. |
71 ByteVector from_false_reports = report; | 73 ByteVector from_false_reports = report; |
72 ByteVectorOr(real_bits, &from_false_reports); | 74 ByteVectorOr(real_bits, &from_false_reports); |
73 int true_from_false_count = CountBits(from_false_reports) - real_bit_count; | 75 const int true_from_false_count = |
| 76 CountBits(from_false_reports) - real_bit_count; |
74 | 77 |
75 // The probability of a true bit being true after redaction = | 78 // The probability of a true bit being true after redaction = |
76 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) = | 79 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) = |
77 // .75 * .5 + (1-.75) = .625 | 80 // .75 * .5 + (1-.75) = .625 |
78 // The probablity of a false bit being true after redaction = | 81 // The probablity of a false bit being true after redaction = |
79 // [fake_prob]*[fake_true_prob] = .375 | 82 // [fake_prob]*[fake_true_prob] = .375 |
80 // The probability of a bit reporting true = | 83 // The probability of a bit reporting true = |
81 // [redacted_prob] * [one_coin_prob:.75] + | 84 // [redacted_prob] * [one_coin_prob:.75] + |
82 // (1-[redacted_prob]) * [zero_coin_prob:.5] = | 85 // (1-[redacted_prob]) * [zero_coin_prob:.5] = |
83 // 0.65625 for true bits | 86 // 0.65625 for true bits |
84 // 0.59375 for false bits | 87 // 0.59375 for false bits |
85 | 88 |
86 // stats.binom(152, 0.65625).ppf(0.000005) = 73 | 89 // stats.binom(152, 0.65625).ppf(0.000005) = 73 |
87 EXPECT_GT(true_from_true_count, 73); | 90 EXPECT_GT(true_from_true_count, 73); |
88 // stats.binom(152, 0.65625).ppf(0.999995) = 124 | 91 // stats.binom(152, 0.65625).ppf(0.999995) = 124 |
89 EXPECT_LE(true_from_true_count, 124); | 92 EXPECT_LE(true_from_true_count, 124); |
90 | 93 |
91 // stats.binom(248, 0.59375).ppf(.000005) = 113 | 94 // stats.binom(248, 0.59375).ppf(.000005) = 113 |
92 EXPECT_GT(true_from_false_count, 113); | 95 EXPECT_GT(true_from_false_count, 113); |
93 // stats.binom(248, 0.59375).ppf(.999995) = 181 | 96 // stats.binom(248, 0.59375).ppf(.999995) = 181 |
94 EXPECT_LE(true_from_false_count, 181); | 97 EXPECT_LE(true_from_false_count, 181); |
95 } | 98 } |
96 | 99 |
97 } // namespace rappor | 100 } // namespace rappor |
OLD | NEW |