Fix AfterTyping experiment for Android.
[chromium-blink-merge.git] / components / rappor / rappor_metric_unittest.cc
blob7b0fc2776759e5d478016b803c24e6662ebcf869
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.
5 #include "components/rappor/rappor_metric.h"
7 #include <stdlib.h>
9 #include "base/rand_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace rappor {
15 const RapporParameters kTestRapporParameters = {
16 1 /* Num cohorts */,
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 UMA_RAPPOR_GROUP /* Recording group (not used) */};
25 const RapporParameters kTestStatsRapporParameters = {
26 1 /* Num cohorts */,
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 UMA_RAPPOR_GROUP /* Recording group (not used) */};
35 // Check for basic syntax and use.
36 TEST(RapporMetricTest, BasicMetric) {
37 RapporMetric testMetric("MyRappor", kTestRapporParameters, 0);
38 testMetric.AddSample("Bar");
39 EXPECT_EQ(0x80, testMetric.bytes()[1]);
42 TEST(RapporMetricTest, GetReport) {
43 RapporMetric metric("MyRappor", kTestRapporParameters, 0);
45 const ByteVector report = metric.GetReport(
46 HmacByteVectorGenerator::GenerateEntropyInput());
47 EXPECT_EQ(16u, report.size());
50 TEST(RapporMetricTest, GetReportStatistics) {
51 RapporMetric metric("MyStatsRappor", kTestStatsRapporParameters, 0);
53 ByteVector real_bits(kTestStatsRapporParameters.bloom_filter_size_bytes);
54 // Set 152 bits (19 bytes)
55 for (char i = 0; i < 19; i++) {
56 real_bits[i] = 0xff;
58 metric.SetBytesForTesting(real_bits);
59 const int real_bit_count = CountBits(real_bits);
60 EXPECT_EQ(real_bit_count, 152);
62 const std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
63 const ByteVector report = metric.GetReport(secret);
65 // For the bits we actually set in the Bloom filter, get a count of how
66 // many of them reported true.
67 ByteVector from_true_reports = report;
68 // Real bits AND report bits.
69 ByteVectorMerge(real_bits, real_bits, &from_true_reports);
70 const int true_from_true_count = CountBits(from_true_reports);
72 // For the bits we didn't set in the Bloom filter, get a count of how
73 // many of them reported true.
74 ByteVector from_false_reports = report;
75 ByteVectorOr(real_bits, &from_false_reports);
76 const int true_from_false_count =
77 CountBits(from_false_reports) - real_bit_count;
79 // The probability of a true bit being true after redaction =
80 // [fake_prob]*[fake_true_prob] + (1-[fake_prob]) =
81 // .75 * .5 + (1-.75) = .625
82 // The probablity of a false bit being true after redaction =
83 // [fake_prob]*[fake_true_prob] = .375
84 // The probability of a bit reporting true =
85 // [redacted_prob] * [one_coin_prob:.75] +
86 // (1-[redacted_prob]) * [zero_coin_prob:.5] =
87 // 0.65625 for true bits
88 // 0.59375 for false bits
90 // stats.binom(152, 0.65625).ppf(0.000005) = 73
91 EXPECT_GT(true_from_true_count, 73);
92 // stats.binom(152, 0.65625).ppf(0.999995) = 124
93 EXPECT_LE(true_from_true_count, 124);
95 // stats.binom(248, 0.59375).ppf(.000005) = 113
96 EXPECT_GT(true_from_false_count, 113);
97 // stats.binom(248, 0.59375).ppf(.999995) = 181
98 EXPECT_LE(true_from_false_count, 181);
101 } // namespace rappor