Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: components/rappor/rappor_metric_unittest.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, BasicMetric) {
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, GetReport) {
42 RapporMetric metric("MyRappor", kTestRapporParameters, 0);
43
44 ByteVector report = metric.GetReport(
45 HmacByteVectorGenerator::GenerateEntropyInput());
46 EXPECT_EQ(16u, report.size());
47 }
48
49 TEST(RapporMetricTest, GetReportStatistics) {
50 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0);
51
52 for (char i = 0; i < 50; i++) {
53 metric.AddSample(base::StringPrintf("%d", i));
54 }
55 ByteVector real_bits = metric.bytes();
56 int real_bit_count = CountBits(real_bits);
57 EXPECT_EQ(real_bit_count, 152);
58
59 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
60 ByteVector report = metric.GetReport(secret);
61
62 // For the bits we actually set in the bloom filter, get a count of how
63 // many of them reported true.
64 ByteVector from_true_reports = report;
65 // Real bits AND report bits.
66 ByteVectorMerge(real_bits, real_bits, &from_true_reports);
67 int true_from_true_count = CountBits(from_true_reports);
68
69 // For the bits we didn't set in the bloom filter, get a count of how
70 // many of them reported true.
71 ByteVector from_false_reports = report;
72 ByteVectorOr(real_bits, &from_false_reports);
73 int true_from_false_count = CountBits(from_false_reports) - real_bit_count;
74
75 // The probability of a true bit being true after redaction =
76 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) =
77 // .75 * .5 + (1-.75) = .625
78 // The probablity of a false bit being true after redaction =
79 // [fake_prob]*[fake_true_prob] = .375
80 // The probability of a bit reporting true =
81 // [redacted_prob] * .75 + (1-[redacted_prob] * .5) =
Ilya Sherman 2014/02/13 01:39:03 nit: It would help to explain where the .75 and th
Steven Holte 2014/02/13 05:11:12 Done.
82 // 0.65625 for true bits
83 // 0.59375 for false bits
84
85 // stats.binom(152, 0.65625).ppf(0.000005) = 73
86 EXPECT_GT(true_from_true_count, 73);
87 // stats.binom(152, 0.65625).ppf(0.999995) = 124
88 EXPECT_LE(true_from_true_count, 124);
89
90 // stats.binom(248, 0.59375).ppf(.000005) = 113
91 EXPECT_GT(true_from_false_count, 113);
92 // stats.binom(248, 0.59375).ppf(.999995) = 181
93 EXPECT_LE(true_from_false_count, 181);
94 }
95
96 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698