OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/rappor/rappor_metric.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 |
| 9 #include "base/rand_util.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace rappor { |
| 14 |
| 15 const RapporParameters kTestRapporParameters = { |
| 16 16 /* Bloom filter size bytes */, |
| 17 4 /* Bloom filter hash count */, |
| 18 PROBABILITY_75 /* Fake data probability */, |
| 19 PROBABILITY_50 /* Fake one probability */, |
| 20 PROBABILITY_75 /* One coin probability */, |
| 21 PROBABILITY_50 /* Zero coin probability */ |
| 22 }; |
| 23 |
| 24 const RapporParameters kTestStatsRapporParameters = { |
| 25 50 /* Bloom filter size bytes */, |
| 26 4 /* Bloom filter hash count */, |
| 27 PROBABILITY_75 /* Fake data probability */, |
| 28 PROBABILITY_50 /* Fake one probability */, |
| 29 PROBABILITY_75 /* One coin probability */, |
| 30 PROBABILITY_50 /* Zero coin probability */ |
| 31 }; |
| 32 |
| 33 // Check for basic syntax and use. |
| 34 TEST(RapporMetricTest, BasicTest) { |
| 35 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0); |
| 36 testMetric.AddSample("Foo"); |
| 37 testMetric.AddSample("Bar"); |
| 38 EXPECT_EQ(0x80, testMetric.bytes()[1]); |
| 39 } |
| 40 |
| 41 TEST(RapporMetricTest, GetReportTest) { |
| 42 RapporMetric metric("MyRappor", kTestRapporParameters, 0); |
| 43 |
| 44 ByteVector report = metric.GetReport("MySecret"); |
| 45 EXPECT_EQ(16u, report.size()); |
| 46 } |
| 47 |
| 48 TEST(RapporMetricTest, GetReportStatistical) { |
| 49 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0); |
| 50 |
| 51 for (char i = 0; i < 50; i++) { |
| 52 metric.AddSample(base::StringPrintf("%d", i)); |
| 53 } |
| 54 ByteVector real_bits = metric.bytes(); |
| 55 int real_bit_count = CountBits(real_bits); |
| 56 EXPECT_EQ(real_bit_count, 152); |
| 57 |
| 58 std::string secret = base::RandBytesAsString(128); |
| 59 ByteVector report = metric.GetReport(secret); |
| 60 |
| 61 // For the bits we actually set in the bloom filter, get a count of how |
| 62 // many of them reported true. |
| 63 ByteVector from_true_reports = report; |
| 64 // Real bits AND report bits. |
| 65 ByteVectorMerge(real_bits, real_bits, &from_true_reports); |
| 66 int true_from_true_count = CountBits(from_true_reports); |
| 67 |
| 68 // For the bits we didn't set in the bloom filter, get a count of how |
| 69 // many of them reported true. |
| 70 ByteVector from_false_reports = report; |
| 71 ByteVectorOr(real_bits, &from_false_reports); |
| 72 int true_from_false_count = CountBits(from_false_reports) - real_bit_count; |
| 73 |
| 74 // The probability of a true bit being true after redaction = |
| 75 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) = |
| 76 // .75 * .5 + (1-.75) = .625 |
| 77 // The probablity of a false bit being true after redaction = |
| 78 // [fake_prob]*[fake_true_prob] = .375 |
| 79 // The probability of a bit reporting true = |
| 80 // [redacted_prob] * .75 + (1-[redacted_prob] * .5) = |
| 81 // 0.65625 for true bits |
| 82 // 0.59375 for false bits |
| 83 |
| 84 // stats.binom(152, 0.65625).ppf(0.000005) = 73 |
| 85 EXPECT_GT(true_from_true_count, 73); |
| 86 // stats.binom(152, 0.65625).ppf(0.999995) = 124 |
| 87 EXPECT_LE(true_from_true_count, 124); |
| 88 |
| 89 // stats.binom(248, 0.59375).ppf(.000005) = 113 |
| 90 EXPECT_GT(true_from_false_count, 113); |
| 91 // stats.binom(248, 0.59375).ppf(.999995) = 181 |
| 92 EXPECT_LE(true_from_false_count, 181); |
| 93 } |
| 94 |
| 95 } // namespace rappor |
OLD | NEW |