OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/byte_vector_utils.h" | 5 #include "components/rappor/byte_vector_utils.h" |
6 | 6 |
7 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace rappor { | 11 namespace rappor { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class SecondRequest : public HmacByteVectorGenerator { | 15 class SecondRequest : public HmacByteVectorGenerator { |
16 public: | 16 public: |
17 SecondRequest(const HmacByteVectorGenerator& first_request) | 17 SecondRequest(const HmacByteVectorGenerator& first_request) |
18 : HmacByteVectorGenerator(first_request) {} | 18 : HmacByteVectorGenerator(first_request) {} |
19 }; | 19 }; |
20 | 20 |
21 std::string HexToString(const char* hex) { | 21 std::string HexToString(const char* hex) { |
22 ByteVector bv; | 22 ByteVector bv; |
23 base::HexStringToBytes(hex, &bv); | 23 base::HexStringToBytes(hex, &bv); |
24 return std::string(bv.begin(), bv.end()); | 24 return std::string(bv.begin(), bv.end()); |
25 } | 25 } |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
| 29 TEST(ByteVectorTest, ByteVectorAnd) { |
| 30 ByteVector lhs(2); |
| 31 lhs[1] = 0x12; |
| 32 ByteVector rhs(2); |
| 33 rhs[1] = 0x03; |
| 34 |
| 35 EXPECT_EQ(0x02, (*ByteVectorAnd(lhs, &rhs))[1]); |
| 36 } |
| 37 |
29 TEST(ByteVectorTest, ByteVectorOr) { | 38 TEST(ByteVectorTest, ByteVectorOr) { |
30 ByteVector lhs(2); | 39 ByteVector lhs(2); |
31 lhs[1] = 0x12; | 40 lhs[1] = 0x12; |
32 ByteVector rhs(2); | 41 ByteVector rhs(2); |
33 rhs[1] = 0x03; | 42 rhs[1] = 0x03; |
34 | 43 |
35 EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]); | 44 EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]); |
36 } | 45 } |
37 | 46 |
38 TEST(ByteVectorTest, ByteVectorMerge) { | 47 TEST(ByteVectorTest, ByteVectorMerge) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 HmacByteVectorGenerator generator(50u, | 128 HmacByteVectorGenerator generator(50u, |
120 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | 129 HmacByteVectorGenerator::GenerateEntropyInput(), ""); |
121 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | 130 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); |
122 int bit_count = CountBits(random); | 131 int bit_count = CountBits(random); |
123 // Check bounds on bit counts that are true with 99.999% probability. | 132 // 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 | 133 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 | 134 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 |
126 } | 135 } |
127 | 136 |
128 } // namespace rappor | 137 } // namespace rappor |
OLD | NEW |