Chromium Code Reviews| 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..d46cd52cee2f35dc1ae3568b5d7d79e3f92efd15 |
| --- /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, TestOr) { |
|
Alexei Svitkine (slow)
2014/02/12 18:13:05
Nit: No need for the actual test case names to sta
Steven Holte
2014/02/12 22:28:50
Done.
|
| + 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(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, 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, |
| + 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, TestHmacStatistic75) { |
| + 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 |
| +} |
| + |
| +} // namespace rappor |