Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "components/rappor/byte_vector_utils.h" | |
| 6 | |
| 7 #include "base/rand_util.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace rappor { | |
| 11 | |
| 12 TEST(ByteVectorTest, ByteVectorOr) { | |
| 13 ByteVector lhs(2); | |
| 14 lhs[1] = 0x12; | |
| 15 ByteVector rhs(2); | |
| 16 rhs[1] = 0x03; | |
| 17 | |
| 18 EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]); | |
| 19 } | |
| 20 | |
| 21 TEST(ByteVectorTest, ByteVectorMerge) { | |
| 22 ByteVector lhs(2); | |
| 23 lhs[1] = 0x33; | |
| 24 ByteVector rhs(2); | |
| 25 rhs[1] = 0x55; | |
| 26 ByteVector mask(2); | |
| 27 mask[1] = 0x0f; | |
| 28 | |
| 29 EXPECT_EQ(0x35, (*ByteVectorMerge(mask, lhs, &rhs))[1]); | |
| 30 } | |
| 31 | |
| 32 TEST(ByteVectorTest, ByteVectorGenerator) { | |
| 33 ByteVectorGenerator generator(2u); | |
| 34 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 35 EXPECT_EQ(random_50.size(), 2u); | |
| 36 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 37 EXPECT_EQ(random_75.size(), 2u); | |
| 38 } | |
| 39 | |
| 40 TEST(ByteVectorTest, HmacByteVectorGenerator) { | |
| 41 HmacByteVectorGenerator generator(1u, | |
| 42 std::string(HmacByteVectorGenerator::kEntropyInputSize, 0x00), ""); | |
| 43 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 44 EXPECT_EQ(random_50.size(), 1u); | |
| 45 EXPECT_EQ(random_50[0], 0x26); | |
| 46 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 47 EXPECT_EQ(random_75.size(), 1u); | |
| 48 EXPECT_EQ(random_75[0], 0xdf); | |
| 49 } | |
| 50 | |
| 51 TEST(ByteVectorTest, WeightedRandomStatistics50) { | |
| 52 ByteVectorGenerator generator(50u); | |
| 53 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 54 int bit_count = CountBits(random); | |
| 55 // Check bounds on bit counts that are true with 99.999% probability. | |
| 56 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
| 57 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
| 58 } | |
| 59 | |
| 60 TEST(ByteVectorTest, WeightedRandomStatistics75) { | |
| 61 ByteVectorGenerator generator(50u); | |
| 62 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 63 int bit_count = CountBits(random); | |
| 64 // Check bounds on bit counts that are true with 99.999% probability. | |
| 65 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
| 66 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
| 67 } | |
| 68 | |
| 69 TEST(ByteVectorTest, HmacWeightedRandomStatistics50) { | |
| 70 HmacByteVectorGenerator generator(50u, | |
| 71 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | |
| 72 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 73 int bit_count = CountBits(random); | |
| 74 // Check bounds on bit counts that are true with 99.999% probability. | |
| 75 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
| 76 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
| 77 } | |
| 78 | |
| 79 TEST(ByteVectorTest, HmacWeightedRandomStatistics75) { | |
| 80 HmacByteVectorGenerator generator(50u, | |
| 81 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | |
| 82 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 83 int bit_count = CountBits(random); | |
| 84 // Check bounds on bit counts that are true with 99.999% probability. | |
| 85 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
| 86 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
| 87 } | |
| 88 | |
|
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
| |
| 89 } // namespace rappor | |
| OLD | NEW |