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.
5 #include "components/rappor/byte_vector_utils.h"
7 #include "base/rand_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 class SecondRequest
: public HmacByteVectorGenerator
{
17 SecondRequest(const HmacByteVectorGenerator
& first_request
)
18 : HmacByteVectorGenerator(first_request
) {}
21 std::string
HexToString(const char* hex
) {
23 base::HexStringToBytes(hex
, &bv
);
24 return std::string(bv
.begin(), bv
.end());
29 TEST(ByteVectorTest
, ByteVectorOr
) {
35 EXPECT_EQ(0x13, (*ByteVectorOr(lhs
, &rhs
))[1]);
38 TEST(ByteVectorTest
, ByteVectorMerge
) {
46 EXPECT_EQ(0x35, (*ByteVectorMerge(mask
, lhs
, &rhs
))[1]);
49 TEST(ByteVectorTest
, ByteVectorGenerator
) {
50 ByteVectorGenerator
generator(2u);
51 ByteVector random_50
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
52 EXPECT_EQ(random_50
.size(), 2u);
53 ByteVector random_75
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
54 EXPECT_EQ(random_75
.size(), 2u);
57 TEST(ByteVectorTest
, HmacByteVectorGenerator
) {
58 HmacByteVectorGenerator
generator(1u,
59 std::string(HmacByteVectorGenerator::kEntropyInputSize
, 0x00), "");
60 ByteVector random_50
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
61 EXPECT_EQ(random_50
.size(), 1u);
62 EXPECT_EQ(random_50
[0], 0x0B);
63 ByteVector random_75
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
64 EXPECT_EQ(random_75
.size(), 1u);
65 EXPECT_EQ(random_75
[0], 0xdf);
68 TEST(ByteVectorTest
, HmacNist
) {
69 // Test case 0 for SHA-256 HMAC_DRBG no reseed tests from
70 // http://csrc.nist.gov/groups/STM/cavp/
71 const char entropy
[] =
72 "ca851911349384bffe89de1cbdc46e6831e44d34a4fb935ee285dd14b71a7488";
73 const char nonce
[] = "659ba96c601dc69fc902940805ec0ca8";
74 const char expected
[] =
75 "e528e9abf2dece54d47c7e75e5fe302149f817ea9fb4bee6f4199697d04d5b89"
76 "d54fbb978a15b5c443c9ec21036d2460b6f73ebad0dc2aba6e624abf07745bc1"
77 "07694bb7547bb0995f70de25d6b29e2d3011bb19d27676c07162c8b5ccde0668"
78 "961df86803482cb37ed6d5c0bb8d50cf1f50d476aa0458bdaba806f48be9dcb8";
80 std::string entropy_input
= HexToString(entropy
) + HexToString(nonce
);
81 HmacByteVectorGenerator
generator(1024u / 8, entropy_input
, "");
82 generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
83 SecondRequest
generator2(generator
);
84 ByteVector random_50
= generator2
.GetWeightedRandomByteVector(PROBABILITY_50
);
86 EXPECT_EQ(HexToString(expected
),
87 std::string(random_50
.begin(), random_50
.end()));
90 TEST(ByteVectorTest
, WeightedRandomStatistics50
) {
91 ByteVectorGenerator
generator(50u);
92 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
93 int bit_count
= CountBits(random
);
94 // Check bounds on bit counts that are true with 99.999% probability.
95 EXPECT_GT(bit_count
, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
96 EXPECT_LE(bit_count
, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
99 TEST(ByteVectorTest
, WeightedRandomStatistics75
) {
100 ByteVectorGenerator
generator(50u);
101 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
102 int bit_count
= CountBits(random
);
103 // Check bounds on bit counts that are true with 99.999% probability.
104 EXPECT_GT(bit_count
, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
105 EXPECT_LE(bit_count
, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
108 TEST(ByteVectorTest
, HmacWeightedRandomStatistics50
) {
109 HmacByteVectorGenerator
generator(50u,
110 HmacByteVectorGenerator::GenerateEntropyInput(), "");
111 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
112 int bit_count
= CountBits(random
);
113 // Check bounds on bit counts that are true with 99.999% probability.
114 EXPECT_GT(bit_count
, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
115 EXPECT_LE(bit_count
, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
118 TEST(ByteVectorTest
, HmacWeightedRandomStatistics75
) {
119 HmacByteVectorGenerator
generator(50u,
120 HmacByteVectorGenerator::GenerateEntropyInput(), "");
121 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
122 int bit_count
= CountBits(random
);
123 // Check bounds on bit counts that are true with 99.999% probability.
124 EXPECT_GT(bit_count
, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
125 EXPECT_LE(bit_count
, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
128 } // namespace rappor