summaryrefslogtreecommitdiffstats
path: root/components/rappor/byte_vector_utils_unittest.cc
blob: 672fa8d6626a5b0b8d001a3a98337b47da6e6af6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/rappor/byte_vector_utils.h"

#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace rappor {

namespace {

class SecondRequest : public HmacByteVectorGenerator {
 public:
  SecondRequest(const HmacByteVectorGenerator& first_request)
      : HmacByteVectorGenerator(first_request) {}
};

std::string HexToString(const char* hex) {
  ByteVector bv;
  base::HexStringToBytes(hex, &bv);
  return std::string(bv.begin(), bv.end());
}

}  // namespace

TEST(ByteVectorTest, ByteVectorAnd) {
  ByteVector lhs(2);
  lhs[1] = 0x12;
  ByteVector rhs(2);
  rhs[1] = 0x03;

  EXPECT_EQ(0x02, (*ByteVectorAnd(lhs, &rhs))[1]);
}

TEST(ByteVectorTest, ByteVectorOr) {
  ByteVector lhs(2);
  lhs[1] = 0x12;
  ByteVector rhs(2);
  rhs[1] = 0x03;

  EXPECT_EQ(0x13, (*ByteVectorOr(lhs, &rhs))[1]);
}

TEST(ByteVectorTest, ByteVectorMerge) {
  ByteVector lhs(2);
  lhs[1] = 0x33;
  ByteVector rhs(2);
  rhs[1] = 0x55;
  ByteVector mask(2);
  mask[1] = 0x0f;

  EXPECT_EQ(0x35, (*ByteVectorMerge(mask, lhs, &rhs))[1]);
}

TEST(ByteVectorTest, ByteVectorGenerator) {
  ByteVectorGenerator generator(2u);
  ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50);
  EXPECT_EQ(random_50.size(), 2u);
  ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75);
  EXPECT_EQ(random_75.size(), 2u);
}

TEST(ByteVectorTest, HmacByteVectorGenerator) {
  HmacByteVectorGenerator generator(1u,
      std::string(HmacByteVectorGenerator::kEntropyInputSize, 0x00), "");
  ByteVector random_50 = generator.GetWeightedRandomByteVector(PROBABILITY_50);
  EXPECT_EQ(random_50.size(), 1u);
  EXPECT_EQ(random_50[0], 0x0B);
  ByteVector random_75 = generator.GetWeightedRandomByteVector(PROBABILITY_75);
  EXPECT_EQ(random_75.size(), 1u);
  EXPECT_EQ(random_75[0], 0xdf);
}

TEST(ByteVectorTest, HmacNist) {
  // Test case 0 for SHA-256 HMAC_DRBG no reseed tests from
  // http://csrc.nist.gov/groups/STM/cavp/
  const char entropy[] =
      "ca851911349384bffe89de1cbdc46e6831e44d34a4fb935ee285dd14b71a7488";
  const char nonce[] = "659ba96c601dc69fc902940805ec0ca8";
  const char expected[] =
      "e528e9abf2dece54d47c7e75e5fe302149f817ea9fb4bee6f4199697d04d5b89"
      "d54fbb978a15b5c443c9ec21036d2460b6f73ebad0dc2aba6e624abf07745bc1"
      "07694bb7547bb0995f70de25d6b29e2d3011bb19d27676c07162c8b5ccde0668"
      "961df86803482cb37ed6d5c0bb8d50cf1f50d476aa0458bdaba806f48be9dcb8";

  std::string entropy_input = HexToString(entropy) + HexToString(nonce);
  HmacByteVectorGenerator generator(1024u / 8, entropy_input, "");
  generator.GetWeightedRandomByteVector(PROBABILITY_50);
  SecondRequest generator2(generator);
  ByteVector random_50 = generator2.GetWeightedRandomByteVector(PROBABILITY_50);

  EXPECT_EQ(HexToString(expected),
      std::string(random_50.begin(), random_50.end()));
}

TEST(ByteVectorTest, WeightedRandomStatistics50) {
  ByteVectorGenerator generator(50u);
  ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50);
  int bit_count = CountBits(random);
  // Check bounds on bit counts that are true with 99.999% probability.
  EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
  EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
}

TEST(ByteVectorTest, WeightedRandomStatistics75) {
  ByteVectorGenerator generator(50u);
  ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75);
  int bit_count = CountBits(random);
  // Check bounds on bit counts that are true with 99.999% probability.
  EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
  EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
}

TEST(ByteVectorTest, HmacWeightedRandomStatistics50) {
  HmacByteVectorGenerator generator(50u,
      HmacByteVectorGenerator::GenerateEntropyInput(), "");
  ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_50);
  int bit_count = CountBits(random);
  // Check bounds on bit counts that are true with 99.999% probability.
  EXPECT_GT(bit_count, 155); // Binomial(400, .5) CDF(155) ~= 0.000004
  EXPECT_LE(bit_count, 244); // Binomial(400, .5) CDF(244) ~= 0.999996
}

TEST(ByteVectorTest, HmacWeightedRandomStatistics75) {
  HmacByteVectorGenerator generator(50u,
      HmacByteVectorGenerator::GenerateEntropyInput(), "");
  ByteVector random = generator.GetWeightedRandomByteVector(PROBABILITY_75);
  int bit_count = CountBits(random);
  // Check bounds on bit counts that are true with 99.999% probability.
  EXPECT_GT(bit_count, 259); // Binomial(400, .75) CDF(259) ~= 0.000003
  EXPECT_LE(bit_count, 337); // Binomial(400, .75) CDF(337) ~= 0.999997
}

}  // namespace rappor