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 | |
|
Alexei Svitkine (slow)
2014/01/21 18:14:04
Nit: Remove one blank line.
Steven Holte
2014/01/21 20:25:14
Done.
| |
| 11 namespace rappor { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 int CountBits(const ByteVector& v) { | |
| 16 int bit_count = 0; | |
| 17 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.
| |
| 18 uint8_t byte = v[i]; | |
| 19 for (int j = 0; j < 8 ; j++) { | |
| 20 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.
| |
| 21 bit_count++; | |
| 22 } | |
| 23 } | |
| 24 } | |
| 25 return bit_count; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 TEST(ByteVectorTest, TestOr) { | |
| 31 ByteVector lhs(2); | |
| 32 lhs[1] = 0x12; | |
| 33 ByteVector rhs(2); | |
| 34 rhs[1] = 0x03; | |
| 35 | |
| 36 EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]); | |
| 37 } | |
| 38 | |
| 39 TEST(ByteVectorTest, TestMerge) { | |
| 40 ByteVector lhs(2); | |
| 41 lhs[1] = 0x33; | |
| 42 ByteVector rhs(2); | |
| 43 rhs[1] = 0x55; | |
| 44 ByteVector mask(2); | |
| 45 mask[1] = 0x0f; | |
| 46 | |
| 47 EXPECT_EQ(0x35, (*ByteVectorMerge(mask, lhs, &rhs))[1]); | |
| 48 } | |
| 49 | |
| 50 TEST(ByteVectorTest, TestGenerator) { | |
| 51 ByteVectorGenerator generator(2u); | |
| 52 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 53 EXPECT_EQ(random_50.size(), 2u); | |
| 54 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 55 EXPECT_EQ(random_75.size(), 2u); | |
| 56 } | |
| 57 | |
| 58 TEST(ByteVectorTest, TestHmacGeneratorDeterminism) { | |
| 59 HmacByteVectorGenerator generator(2u, "MySecret"); | |
| 60 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 61 EXPECT_EQ(random_50.size(), 2u); | |
| 62 EXPECT_EQ(random_50[0], 0x16); | |
| 63 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 64 EXPECT_EQ(random_75.size(), 2u); | |
| 65 EXPECT_EQ(random_75[0], 0xef); | |
| 66 } | |
| 67 | |
| 68 TEST(ByteVectorTest, TestStatistic50) { | |
| 69 ByteVectorGenerator generator(50u); | |
| 70 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 71 int bit_count = CountBits(random); | |
| 72 // Check bounds on bit counts that are true with 99.999% probability. | |
| 73 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
| 74 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
| 75 } | |
| 76 | |
| 77 TEST(ByteVectorTest, TestStatistic75) { | |
| 78 ByteVectorGenerator generator(50u); | |
| 79 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 80 int bit_count = CountBits(random); | |
| 81 // Check bounds on bit counts that are true with 99.999% probability. | |
| 82 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
| 83 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
| 84 } | |
| 85 | |
| 86 TEST(ByteVectorTest, TestHmacStatistic50) { | |
| 87 HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128)); | |
| 88 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
| 89 int bit_count = CountBits(random); | |
| 90 // Check bounds on bit counts that are true with 99.999% probability. | |
| 91 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
| 92 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
| 93 } | |
| 94 | |
| 95 TEST(ByteVectorTest, TestHmacStatistic75) { | |
| 96 HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128)); | |
| 97 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
| 98 int bit_count = CountBits(random); | |
| 99 // Check bounds on bit counts that are true with 99.999% probability. | |
| 100 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
| 101 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
| 102 } | |
| 103 | |
| 104 } // namespace rappor | |
| OLD | NEW |