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

Unified Diff: components/rappor/byte_vector_utils_unittest.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: RapporMetrics StatsTest 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 side-by-side diff with in-line comments
Download patch
Index: components/rappor/byte_vector_utils_unittest.cc
diff --git a/components/rappor/byte_vector_utils_unittest.cc b/components/rappor/byte_vector_utils_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..45145c31ae53e5e1b7bc925fb5f695ce24bcef8f
--- /dev/null
+++ b/components/rappor/byte_vector_utils_unittest.cc
@@ -0,0 +1,104 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/rappor/byte_vector_utils.h"
+
+#include "base/rand_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+
Alexei Svitkine (slow) 2014/01/21 18:14:04 Nit: Remove one blank line.
Steven Holte 2014/01/21 20:25:14 Done.
+namespace rappor {
+
+namespace {
+
+int CountBits(const ByteVector& v) {
+ int bit_count = 0;
+ for(size_t i = 0; i < v.size(); i++) {
Alexei Svitkine (slow) 2014/01/21 18:14:04 Nit: space after for. also after "if" below. Also
Steven Holte 2014/01/21 20:25:14 Done.
+ uint8_t byte = v[i];
+ for (int j = 0; j < 8 ; j++) {
+ if(byte & 1 << j) {
Alexei Svitkine (slow) 2014/01/21 18:14:04 Nit: No {}'s. Also, please but parens around one
Steven Holte 2014/01/21 20:25:14 Done.
+ bit_count++;
+ }
+ }
+ }
+ return bit_count;
+}
+
+} // namespace
+
+TEST(ByteVectorTest, TestOr) {
+ ByteVector lhs(2);
+ lhs[1] = 0x12;
+ ByteVector rhs(2);
+ rhs[1] = 0x03;
+
+ EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]);
+}
+
+TEST(ByteVectorTest, TestMerge) {
+ ByteVector lhs(2);
+ lhs[1] = 0x33;
+ ByteVector rhs(2);
+ rhs[1] = 0x55;
+ ByteVector mask(2);
+ mask[1] = 0x0f;
+
+ EXPECT_EQ(0x35, (*ByteVectorMerge(mask, lhs, &rhs))[1]);
+}
+
+TEST(ByteVectorTest, TestGenerator) {
+ ByteVectorGenerator generator(2u);
+ ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50);
+ EXPECT_EQ(random_50.size(), 2u);
+ ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75);
+ EXPECT_EQ(random_75.size(), 2u);
+}
+
+TEST(ByteVectorTest, TestHmacGeneratorDeterminism) {
+ HmacByteVectorGenerator generator(2u, "MySecret");
+ ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50);
+ EXPECT_EQ(random_50.size(), 2u);
+ EXPECT_EQ(random_50[0], 0x16);
+ ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75);
+ EXPECT_EQ(random_75.size(), 2u);
+ EXPECT_EQ(random_75[0], 0xef);
+}
+
+TEST(ByteVectorTest, TestStatistic50) {
+ ByteVectorGenerator generator(50u);
+ ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50);
+ int bit_count = CountBits(random);
+ // Check bounds on bit counts that are true with 99.999% probability.
+ EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
+ EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
+}
+
+TEST(ByteVectorTest, TestStatistic75) {
+ ByteVectorGenerator generator(50u);
+ ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75);
+ int bit_count = CountBits(random);
+ // Check bounds on bit counts that are true with 99.999% probability.
+ EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
+ EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
+}
+
+TEST(ByteVectorTest, TestHmacStatistic50) {
+ HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128));
+ ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50);
+ int bit_count = CountBits(random);
+ // Check bounds on bit counts that are true with 99.999% probability.
+ EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
+ EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
+}
+
+TEST(ByteVectorTest, TestHmacStatistic75) {
+ HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128));
+ ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75);
+ int bit_count = CountBits(random);
+ // Check bounds on bit counts that are true with 99.999% probability.
+ EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
+ EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
+}
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698