| 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, Uint64ToByteVectorSmall) { |
| 30 ByteVector bytes(1); |
| 31 Uint64ToByteVector(0x10, 1, &bytes); |
| 32 EXPECT_EQ(1u, bytes.size()); |
| 33 EXPECT_EQ(0x10, bytes[0]); |
| 34 } |
| 35 |
| 36 TEST(ByteVectorTest, Uint64ToByteVectorLarge) { |
| 37 ByteVector bytes(8); |
| 38 Uint64ToByteVector(0xfedcba9876543210, 8, &bytes); |
| 39 EXPECT_EQ(8u, bytes.size()); |
| 40 EXPECT_EQ(0x10, bytes[0]); |
| 41 EXPECT_EQ(0xfe, bytes[7]); |
| 42 } |
| 43 |
| 29 TEST(ByteVectorTest, ByteVectorAnd) { | 44 TEST(ByteVectorTest, ByteVectorAnd) { |
| 30 ByteVector lhs(2); | 45 ByteVector lhs(2); |
| 31 lhs[1] = 0x12; | 46 lhs[1] = 0x12; |
| 32 ByteVector rhs(2); | 47 ByteVector rhs(2); |
| 33 rhs[1] = 0x03; | 48 rhs[1] = 0x03; |
| 34 | 49 |
| 35 EXPECT_EQ(0x02, (*ByteVectorAnd(lhs, &rhs))[1]); | 50 EXPECT_EQ(0x02, (*ByteVectorAnd(lhs, &rhs))[1]); |
| 36 } | 51 } |
| 37 | 52 |
| 38 TEST(ByteVectorTest, ByteVectorOr) { | 53 TEST(ByteVectorTest, ByteVectorOr) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 HmacByteVectorGenerator generator(50u, | 143 HmacByteVectorGenerator generator(50u, |
| 129 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | 144 HmacByteVectorGenerator::GenerateEntropyInput(), ""); |
| 130 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | 145 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); |
| 131 int bit_count = CountBits(random); | 146 int bit_count = CountBits(random); |
| 132 // Check bounds on bit counts that are true with 99.999% probability. | 147 // Check bounds on bit counts that are true with 99.999% probability. |
| 133 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | 148 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 |
| 134 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | 149 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 |
| 135 } | 150 } |
| 136 | 151 |
| 137 } // namespace rappor | 152 } // namespace rappor |
| OLD | NEW |