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
, Uint64ToByteVectorSmall
) {
31 Uint64ToByteVector(0x10, 1, &bytes
);
32 EXPECT_EQ(1u, bytes
.size());
33 EXPECT_EQ(0x10, bytes
[0]);
36 TEST(ByteVectorTest
, Uint64ToByteVectorLarge
) {
38 Uint64ToByteVector(0xfedcba9876543210, 8, &bytes
);
39 EXPECT_EQ(8u, bytes
.size());
40 EXPECT_EQ(0x10, bytes
[0]);
41 EXPECT_EQ(0xfe, bytes
[7]);
44 TEST(ByteVectorTest
, ByteVectorAnd
) {
50 EXPECT_EQ(0x02, (*ByteVectorAnd(lhs
, &rhs
))[1]);
53 TEST(ByteVectorTest
, ByteVectorOr
) {
59 EXPECT_EQ(0x13, (*ByteVectorOr(lhs
, &rhs
))[1]);
62 TEST(ByteVectorTest
, ByteVectorMerge
) {
70 EXPECT_EQ(0x35, (*ByteVectorMerge(mask
, lhs
, &rhs
))[1]);
73 TEST(ByteVectorTest
, ByteVectorGenerator
) {
74 ByteVectorGenerator
generator(2u);
75 ByteVector random_50
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
76 EXPECT_EQ(random_50
.size(), 2u);
77 ByteVector random_75
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
78 EXPECT_EQ(random_75
.size(), 2u);
81 TEST(ByteVectorTest
, HmacByteVectorGenerator
) {
82 HmacByteVectorGenerator
generator(1u,
83 std::string(HmacByteVectorGenerator::kEntropyInputSize
, 0x00), "");
84 ByteVector random_50
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
85 EXPECT_EQ(random_50
.size(), 1u);
86 EXPECT_EQ(random_50
[0], 0x0B);
87 ByteVector random_75
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
88 EXPECT_EQ(random_75
.size(), 1u);
89 EXPECT_EQ(random_75
[0], 0xdf);
92 TEST(ByteVectorTest
, HmacNist
) {
93 // Test case 0 for SHA-256 HMAC_DRBG no reseed tests from
94 // http://csrc.nist.gov/groups/STM/cavp/
95 const char entropy
[] =
96 "ca851911349384bffe89de1cbdc46e6831e44d34a4fb935ee285dd14b71a7488";
97 const char nonce
[] = "659ba96c601dc69fc902940805ec0ca8";
98 const char expected
[] =
99 "e528e9abf2dece54d47c7e75e5fe302149f817ea9fb4bee6f4199697d04d5b89"
100 "d54fbb978a15b5c443c9ec21036d2460b6f73ebad0dc2aba6e624abf07745bc1"
101 "07694bb7547bb0995f70de25d6b29e2d3011bb19d27676c07162c8b5ccde0668"
102 "961df86803482cb37ed6d5c0bb8d50cf1f50d476aa0458bdaba806f48be9dcb8";
104 std::string entropy_input
= HexToString(entropy
) + HexToString(nonce
);
105 HmacByteVectorGenerator
generator(1024u / 8, entropy_input
, "");
106 generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
107 SecondRequest
generator2(generator
);
108 ByteVector random_50
= generator2
.GetWeightedRandomByteVector(PROBABILITY_50
);
110 EXPECT_EQ(HexToString(expected
),
111 std::string(random_50
.begin(), random_50
.end()));
114 TEST(ByteVectorTest
, WeightedRandomStatistics50
) {
115 ByteVectorGenerator
generator(50u);
116 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
117 int bit_count
= CountBits(random
);
118 // Check bounds on bit counts that are true with 99.999% probability.
119 EXPECT_GT(bit_count
, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
120 EXPECT_LE(bit_count
, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
123 TEST(ByteVectorTest
, WeightedRandomStatistics75
) {
124 ByteVectorGenerator
generator(50u);
125 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
126 int bit_count
= CountBits(random
);
127 // Check bounds on bit counts that are true with 99.999% probability.
128 EXPECT_GT(bit_count
, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
129 EXPECT_LE(bit_count
, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
132 TEST(ByteVectorTest
, HmacWeightedRandomStatistics50
) {
133 HmacByteVectorGenerator
generator(50u,
134 HmacByteVectorGenerator::GenerateEntropyInput(), "");
135 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_50
);
136 int bit_count
= CountBits(random
);
137 // Check bounds on bit counts that are true with 99.999% probability.
138 EXPECT_GT(bit_count
, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
139 EXPECT_LE(bit_count
, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
142 TEST(ByteVectorTest
, HmacWeightedRandomStatistics75
) {
143 HmacByteVectorGenerator
generator(50u,
144 HmacByteVectorGenerator::GenerateEntropyInput(), "");
145 ByteVector random
= generator
.GetWeightedRandomByteVector(PROBABILITY_75
);
146 int bit_count
= CountBits(random
);
147 // Check bounds on bit counts that are true with 99.999% probability.
148 EXPECT_GT(bit_count
, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
149 EXPECT_LE(bit_count
, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
152 } // namespace rappor