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, TestOr) { |
| 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, TestMerge) { |
| 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, TestGenerator) { |
| 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, TestHmacGeneratorDeterminism) { |
| 41 HmacByteVectorGenerator generator(2u, "MySecret"); |
| 42 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); |
| 43 EXPECT_EQ(random_50.size(), 2u); |
| 44 EXPECT_EQ(random_50[0], 0x92); |
| 45 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); |
| 46 EXPECT_EQ(random_75.size(), 2u); |
| 47 EXPECT_EQ(random_75[0], 0x7f); |
| 48 } |
| 49 |
| 50 TEST(ByteVectorTest, TestStatistic50) { |
| 51 ByteVectorGenerator generator(50u); |
| 52 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); |
| 53 int bit_count = CountBits(random); |
| 54 // Check bounds on bit counts that are true with 99.999% probability. |
| 55 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 |
| 56 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 |
| 57 } |
| 58 |
| 59 TEST(ByteVectorTest, TestStatistic75) { |
| 60 ByteVectorGenerator generator(50u); |
| 61 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); |
| 62 int bit_count = CountBits(random); |
| 63 // Check bounds on bit counts that are true with 99.999% probability. |
| 64 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 |
| 65 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 |
| 66 } |
| 67 |
| 68 TEST(ByteVectorTest, TestHmacStatistic50) { |
| 69 HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128)); |
| 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, TestHmacStatistic75) { |
| 78 HmacByteVectorGenerator generator(50u, base::RandBytesAsString(128)); |
| 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 } // namespace rappor |
OLD | NEW |