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

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

Powered by Google App Engine
This is Rietveld 408576698