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..2355cd58c07b7aa172e22cb0b2a3e7d3c427a6b9 |
| --- /dev/null |
| +++ b/components/rappor/byte_vector_utils_unittest.cc |
| @@ -0,0 +1,86 @@ |
| +// 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) { |
| + 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], 0x92); |
| + ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); |
| + EXPECT_EQ(random_75.size(), 2u); |
| + EXPECT_EQ(random_75[0], 0x7f); |
|
wtc
2014/02/11 20:55:29
1. Nit: it seems that we should also test the gene
Steven Holte
2014/02/11 22:08:14
Changed test to generate 1 byte.
|
| +} |
| + |
| +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 |