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

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: 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 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..c0dc831d017b56e443145e00b8cab2f55898dcf4
--- /dev/null
+++ b/components/rappor/byte_vector_utils_unittest.cc
@@ -0,0 +1,89 @@
+// 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"
+
+namespace rappor {
+
+TEST(ByteVectorTest, ByteVectorOr) {
+ ByteVector lhs(2);
+ lhs[1] = 0x12;
+ ByteVector rhs(2);
+ rhs[1] = 0x03;
+
+ EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]);
+}
+
+TEST(ByteVectorTest, ByteVectorMerge) {
+ 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, ByteVectorGenerator) {
+ 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, HmacByteVectorGenerator) {
+ HmacByteVectorGenerator generator(1u,
+ std::string(HmacByteVectorGenerator::kEntropyInputSize, 0x00), "");
+ ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50);
+ EXPECT_EQ(random_50.size(), 1u);
+ EXPECT_EQ(random_50[0], 0x26);
+ ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75);
+ EXPECT_EQ(random_75.size(), 1u);
+ EXPECT_EQ(random_75[0], 0xdf);
+}
+
+TEST(ByteVectorTest, WeightedRandomStatistics50) {
+ 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, WeightedRandomStatistics75) {
+ 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, HmacWeightedRandomStatistics50) {
+ HmacByteVectorGenerator generator(50u,
+ HmacByteVectorGenerator::GenerateEntropyInput(), "");
+ 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, HmacWeightedRandomStatistics75) {
+ HmacByteVectorGenerator generator(50u,
+ HmacByteVectorGenerator::GenerateEntropyInput(), "");
+ 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
+}
+
edknapp 2014/02/12 23:09:17 Could you include the relevant sample test vectors
Steven Holte 2014/02/13 21:13:45 As Ed discovered in another thread, the test vecto
Alexei Svitkine (slow) 2014/02/13 21:25:39 If you need to test a function that's in the anon
edknapp 2014/02/13 21:55:41 What do you think of having each call to our GetRa
Steven Holte 2014/02/13 22:01:54 Actually, after thinking about it a bit more, I fi
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698