Chromium Code Reviews| 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 "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace rappor { | |
| 13 | |
| 14 const RapporParameters kTestRapporParameters = { | |
| 15 "MyRappor", | |
| 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 // Check for basic syntax and use. | |
| 25 TEST(RapporMetricTest, BasicTest) { | |
|
Alexei Svitkine (slow)
2014/01/21 18:14:04
Nit: Keep all the TEST methods together (at the en
Steven Holte
2014/01/21 20:25:14
Done.
| |
| 26 RapporMetric testMetric(kTestRapporParameters); | |
| 27 testMetric.AddSample("Foo"); | |
| 28 testMetric.AddSample("Bar"); | |
| 29 EXPECT_EQ(0x20, testMetric.bytes()[1]); | |
| 30 } | |
| 31 | |
| 32 TEST(RapporMetricTest, GetReportTest) { | |
| 33 RapporMetric metric(kTestRapporParameters); | |
| 34 | |
| 35 ByteVector report = metric.GetReport("MySecret"); | |
| 36 EXPECT_EQ(16u, report.size()); | |
| 37 } | |
| 38 | |
| 39 | |
| 40 const RapporParameters kTestStatsRapporParameters = { | |
| 41 "MyStatsRappor", | |
| 42 50 /* Bloom filter size bytes */, | |
| 43 4 /* Bloom filter hash count */, | |
| 44 PROBABILITY_75 /* Fake data probability */, | |
| 45 PROBABILITY_50 /* Fake one probability */, | |
| 46 PROBABILITY_75 /* One coin probability */, | |
| 47 PROBABILITY_50 /* Zero coin probability */ | |
| 48 }; | |
| 49 | |
| 50 int CountBits(const ByteVector& v) { | |
| 51 int bit_count = 0; | |
| 52 for(size_t i = 0; i < v.size(); i++) { | |
| 53 uint8_t byte = v[i]; | |
| 54 for (int j = 0; j < 8 ; j++) { | |
| 55 if(byte & 1 << j) { | |
| 56 bit_count++; | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 return bit_count; | |
| 61 } | |
| 62 | |
| 63 TEST(RapporMetricTest, GetReportStatistical) { | |
| 64 RapporMetric metric(kTestStatsRapporParameters); | |
| 65 | |
| 66 char sample[2] = "\0"; | |
| 67 for (char i = 0; i < 50; i++) { | |
| 68 sample[0] = i; | |
|
Alexei Svitkine (slow)
2014/01/21 18:14:04
It's kind of weird to generate these non-human rea
Steven Holte
2014/01/21 20:25:14
Done.
| |
| 69 metric.AddSample(sample); | |
| 70 } | |
| 71 ByteVector real_bits = metric.bytes(); | |
| 72 int real_bit_count = CountBits(real_bits); | |
| 73 EXPECT_EQ(real_bit_count, 149); | |
| 74 | |
| 75 std::string secret = base::RandBytesAsString(128); | |
| 76 ByteVector report = metric.GetReport(secret); | |
| 77 | |
| 78 // For the bits we actually set in the bloom filter, get a count of how | |
| 79 // many of them reported true. | |
| 80 ByteVector from_true_reports = report; | |
| 81 // Real bits AND report bits. | |
| 82 ByteVectorMerge(real_bits, real_bits, &from_true_reports); | |
| 83 int true_from_true_count = CountBits(from_true_reports); | |
| 84 | |
| 85 // For the bits we didn't set in the bloom filter, get a count of how | |
| 86 // many of them reported true. | |
| 87 ByteVector from_false_reports = report; | |
| 88 ByteVectorOr(real_bits, &from_false_reports); | |
| 89 int true_from_false_count = CountBits(from_false_reports) - real_bit_count; | |
| 90 | |
| 91 // The probability of a true bit being true after redaction = | |
| 92 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) = | |
| 93 // .75 * .5 + (1-.75) = .625 | |
| 94 // The probablity of a false bit being true after redaction = | |
| 95 // [fake_prob]*[fake_true_prob] = .375 | |
| 96 // The probability of a bit reporting true = | |
| 97 // [redacted_prob] * .75 + (1-[redacted_prob] * .5) = | |
| 98 // 0.65625 for true bits | |
| 99 // 0.59375 for false bits | |
| 100 | |
| 101 // Binomial(149, 0.65625) CDF(71) <= 0.000005 | |
| 102 EXPECT_GT(true_from_true_count, 71); | |
| 103 // Binomial(149, 0.65625) CDF(122) <= 0.999995 | |
| 104 EXPECT_LE(true_from_true_count, 122); | |
| 105 | |
| 106 // Binomial(251, 0.59375) CDF(114) <= 0.000005 | |
| 107 EXPECT_GT(true_from_false_count, 114); | |
| 108 // Binomial(251, 0.59375) CDF(183) <= 0.999995 | |
| 109 EXPECT_LE(true_from_false_count, 183); | |
| 110 } | |
| 111 | |
| 112 } // namespace rappor | |
| OLD | NEW |