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) { | |
Alexei Svitkine (slow)
2014/02/12 18:13:05
Nit: No need for the actual test case names to sta
Steven Holte
2014/02/12 22:28:50
Done.
| |
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(1u, | |
42 std::string(HmacByteVectorGenerator::kEntropyInputSize, 0x00), ""); | |
43 ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
44 EXPECT_EQ(random_50.size(), 1u); | |
45 EXPECT_EQ(random_50[0], 0x26); | |
46 ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
47 EXPECT_EQ(random_75.size(), 1u); | |
48 EXPECT_EQ(random_75[0], 0xdf); | |
49 } | |
50 | |
51 TEST(ByteVectorTest, TestStatistic50) { | |
52 ByteVectorGenerator generator(50u); | |
53 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
54 int bit_count = CountBits(random); | |
55 // Check bounds on bit counts that are true with 99.999% probability. | |
56 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
57 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
58 } | |
59 | |
60 TEST(ByteVectorTest, TestStatistic75) { | |
61 ByteVectorGenerator generator(50u); | |
62 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
63 int bit_count = CountBits(random); | |
64 // Check bounds on bit counts that are true with 99.999% probability. | |
65 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
66 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
67 } | |
68 | |
69 TEST(ByteVectorTest, TestHmacStatistic50) { | |
70 HmacByteVectorGenerator generator(50u, | |
71 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | |
72 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50); | |
73 int bit_count = CountBits(random); | |
74 // Check bounds on bit counts that are true with 99.999% probability. | |
75 EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004 | |
76 EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996 | |
77 } | |
78 | |
79 TEST(ByteVectorTest, TestHmacStatistic75) { | |
80 HmacByteVectorGenerator generator(50u, | |
81 HmacByteVectorGenerator::GenerateEntropyInput(), ""); | |
82 ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75); | |
83 int bit_count = CountBits(random); | |
84 // Check bounds on bit counts that are true with 99.999% probability. | |
85 EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003 | |
86 EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997 | |
87 } | |
88 | |
89 } // namespace rappor | |
OLD | NEW |