aboutsummaryrefslogtreecommitdiff
path: root/client/cpp/encoder_unittest.cc
blob: 0f48604614061f6338d518250a8b578cc8f5a2ec (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <gtest/gtest.h>
#include <stdexcept>

#include "encoder.h"
#include "openssl_hash_impl.h"
#include "unix_kernel_rand_impl.h"

   // We need the same "random" inputs to the IRR
   // each time to have reproducible tests.
FILE* mock_urandom(void) {
 int i;
 FILE *fp;
 fp = tmpfile();
 for (i = 0; i < 1024; i++) {
   fputc((i * 17) % 256, fp);
 }
 fflush(fp);
 fp = freopen(NULL, "r", fp);
 return fp;
}

class EncoderTest : public ::testing::Test {
  protected:
   EncoderTest() {
      encoder_id = std::string("metric-name").c_str();
      fp = mock_urandom();
      irr_rand = new rappor::UnixKernelRand(fp);
   }

   virtual ~EncoderTest() {
     fclose(fp);
     delete irr_rand;
     delete deps;
     delete params;
     delete encoder;
   }

FILE* fp; const char* encoder_id;
   rappor::UnixKernelRand *irr_rand;
   rappor::Deps *deps;
   rappor::Params *params;
   rappor::Encoder *encoder;
   rappor::Bits bits_out;
   std::vector<uint8_t> bits_vector;
};

// Uses HmacSha256 and 32-bit outputs.
class EncoderUint32Test : public EncoderTest {
  protected:
   EncoderUint32Test() {
     deps = new rappor::Deps(rappor::Md5, "client-secret", rappor::HmacSha256,
                             *irr_rand);
     params = new rappor::Params(32,    // num_bits (k)
                                 2,     // num_hashes (h)
                                 128,   // num_cohorts (m)
                                 0.25,  // probability f for PRR
                                 0.75,  // probability p for IRR
                                 0.5);  // probability q for IRR
     encoder = new rappor::Encoder(encoder_id, *params, *deps);
   }
};

// Uses HmacDrbg and variable-size vector outputs.
class EncoderUnlimTest : public EncoderTest {
 protected:
  EncoderUnlimTest() {
    deps = new rappor::Deps(rappor::Md5, "client-secret", rappor::HmacDrbg,
                            *irr_rand);
    params = new rappor::Params(64,    // num_bits (k)
                                2,     // num_hashes (h)
                                128,   // num_cohorts (m)
                                0.25,  // probability f for PRR
                                0.75,  // probability p for IRR
                                0.5);  // probability q for IRR
    encoder = new rappor::Encoder(encoder_id, *params, *deps);
  }
};


///// EncoderUint32Test
TEST_F(EncoderUint32Test, EncodeStringUint32) {
  ASSERT_TRUE(encoder->EncodeString("foo", &bits_out));
  ASSERT_EQ(2281639167, bits_out);
  ASSERT_EQ(3, encoder->cohort());
}

TEST_F(EncoderUint32Test, EncodeStringUint32Cohort) {
  encoder->set_cohort(4);  // Set pre-selected cohort.
  ASSERT_TRUE(encoder->EncodeString("foo", &bits_out));
  ASSERT_EQ(2281637247, bits_out);
  ASSERT_EQ(4, encoder->cohort());
}

TEST_F(EncoderUint32Test, EncodeBitsUint32) {
  ASSERT_TRUE(encoder->EncodeBits(0x123, &bits_out));
  ASSERT_EQ(2784956095, bits_out);
  ASSERT_EQ(3, encoder->cohort());
}

// Negative tests
// num_bits is negative.
TEST_F(EncoderUint32Test, NumBitsMustBePositiveDeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(-1,    // num_bits (k) [BAD]
                              2,     // num_hashes (h)
                              128,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

// num_hashes is negative.
TEST_F(EncoderUint32Test, NumHashesMustBePositiveDeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              -1,    // num_hashes (h) [BAD]
                              128,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

// num_cohorts is negative.
TEST_F(EncoderUint32Test, NumCohortsMustBePositiveDeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              -1,   // num_cohorts (m)  [BAD]
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Encoder.*Assertion.*failed");
}

// Invalid probabilities.
TEST_F(EncoderUint32Test, InvalidProbabilitiesDeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  // prob_f negative.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              -0.1,  // probability f for PRR [BAD]
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
  // prob_f > 1.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              1.1,  // probability f for PRR [BAD]
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
  // prob_p < 0.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              -0.1,  // probability p for IRR [BAD]
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
  // prob_p > 1.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              1.1,  // probability p for IRR [BAD]
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
  // prob_q < 0.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              -0.1);  // probability q for IRR [BAD]
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
  // prob_q > 1.
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              2,     // num_hashes (h)
                              1,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              1.1);  // probability q for IRR [BAD]
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

// num_bits 64 when only 32 bits are possible.
TEST_F(EncoderUint32Test, Sha256NoMoreThan32BitsDeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(64,    // num_bits (k)
                              2,     // num_hashes (h)
                              128,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

// num_hashes too high.
TEST_F(EncoderUint32Test, NumHashesNoMoreThan16DeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(32,    // num_bits (k)
                              17,     // num_hashes (h)
                              128,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

// EncoderString with 4-byte vector and HMACSHA256 and
// EncoderString with Uint32 and HMACSHA256 should match.
TEST_F(EncoderUint32Test, StringUint32AndStringVectorMatch) {
  ASSERT_TRUE(encoder->EncodeString("foo", &bits_out));
  ASSERT_EQ(2281639167, bits_out);
  std::vector<uint8_t> expected_out(4);
  expected_out[0] = (bits_out & 0xFF000000) >> 24;
  expected_out[1] = (bits_out & 0x00FF0000) >> 16;
  expected_out[2] = (bits_out & 0x0000FF00) >> 8;
  expected_out[3] = bits_out  & 0x000000FF;

  // Reset the mock randomizer.
  delete irr_rand;
  delete deps;
  delete encoder;
  fclose(fp);
  fp = mock_urandom();
  irr_rand = new rappor::UnixKernelRand(fp);
  deps = new rappor::Deps(rappor::Md5, "client-secret", rappor::HmacSha256,
                          *irr_rand);
  encoder = new rappor::Encoder(encoder_id, *params, *deps);
  ASSERT_TRUE(encoder->EncodeString("foo", &bits_vector));
  ASSERT_EQ(expected_out, bits_vector);
}

///// EncoderUnlimTest

TEST_F(EncoderUnlimTest, EncodeStringUint64) {
  static const uint8_t ex[] = { 134, 255, 11, 255, 252, 119, 240, 223 };
  std::vector<uint8_t> expected_vector(ex, ex + sizeof(ex));

  ASSERT_TRUE(encoder->EncodeString("foo", &bits_vector));
  ASSERT_EQ(expected_vector, bits_vector);
  ASSERT_EQ(93, encoder->cohort());
}

// Negative tests.
TEST_F(EncoderUnlimTest, NumBitsNotMultipleOf8DeathTest) {
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
  delete params;
  params = new rappor::Params(63,    // num_bits (k) [BAD]
                              17,     // num_hashes (h)
                              128,   // num_cohorts (m)
                              0.25,  // probability f for PRR
                              0.75,  // probability p for IRR
                              0.5);  // probability q for IRR
  EXPECT_DEATH(rappor::Encoder(encoder_id, *params, *deps),
               "Assertion.*failed");
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}