summaryrefslogtreecommitdiffstats
path: root/toolkit/components/resistfingerprinting/tests/gtest/test_reduceprecision.cpp
blob: 5d8b598ff09d58cf7edec4861ee518b7b5e20e37 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <math.h>

#include "gtest/gtest.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsServiceManagerUtils.h"
#include "nsRFPService.h"

using namespace mozilla;

// clang-format off
/*
   Hello! Are you looking at this file because you got an error you don't understand?
   Perhaps something that looks like the following?

    toolkit/components/resistfingerprinting/tests/test_reduceprecision.cpp:15: Failure
      Expected: reduced1
        Which is: 2064.83
      To be equal to: reduced2
        Which is: 2064.83

   "Gosh," you might say, "They sure look equal to me. What the heck is going on here?"

   The answer lies beyond what you can see, in that which you cannot see. One must
   journey into the depths, the hidden, that which the world fights its hardest to
   conceal from us.

   Specifically: you need to look at more decimal places. Run the test with:
       MOZ_LOG="nsResistFingerprinting:5"

   And look for two successive lines similar to the below (the format will probably
   be different by the time you read this comment):
      V/nsResistFingerprinting Given: 2064.83384599999999, Reciprocal Rounding with 50000.00000000000000, Intermediate: 103241692.00000000000000, Got: 2064.83383999999978
      V/nsResistFingerprinting Given: 2064.83383999999978, Reciprocal Rounding with 50000.00000000000000, Intermediate: 103241691.00000000000000, Got: 2064.83381999999983

   Look at the last two values:
      Got: 2064.83383999999978
      Got: 2064.83381999999983

   They're supposed to be equal. They're not. But they both round to 2064.83.
   Good luck and godspeed.
*/
// clang-format on

bool setupJitter(bool enabled) {
  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);

  bool jitterEnabled = false;
  if (prefs) {
    prefs->GetBoolPref(
        "privacy.resistFingerprinting.reduceTimerPrecision.jitter",
        &jitterEnabled);
    prefs->SetBoolPref(
        "privacy.resistFingerprinting.reduceTimerPrecision.jitter", enabled);
  }

  return jitterEnabled;
}

void cleanupJitter(bool jitterWasEnabled) {
  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
  if (prefs) {
    prefs->SetBoolPref(
        "privacy.resistFingerprinting.reduceTimerPrecision.jitter",
        jitterWasEnabled);
  }
}

void process(double clock, nsRFPService::TimeScale clockUnits,
             double precision) {
  double reduced1 = nsRFPService::ReduceTimePrecisionImpl(
      clock, clockUnits, precision, -1, TimerPrecisionType::Normal);
  double reduced2 = nsRFPService::ReduceTimePrecisionImpl(
      reduced1, clockUnits, precision, -1, TimerPrecisionType::Normal);
  ASSERT_EQ(reduced1, reduced2);
}

TEST(ResistFingerprinting, ReducePrecision_Assumptions)
{
  ASSERT_EQ(FLT_RADIX, 2);
  ASSERT_EQ(DBL_MANT_DIG, 53);
}

TEST(ResistFingerprinting, ReducePrecision_Reciprocal)
{
  bool jitterEnabled = setupJitter(false);
  // This one has a rounding error in the Reciprocal case:
  process(2064.8338460, nsRFPService::TimeScale::MicroSeconds, 20);
  // These are just big values
  process(1516305819, nsRFPService::TimeScale::MicroSeconds, 20);
  process(69053.12, nsRFPService::TimeScale::MicroSeconds, 20);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_KnownGood)
{
  bool jitterEnabled = setupJitter(false);
  process(2064.8338460, nsRFPService::TimeScale::MilliSeconds, 20);
  process(69027.62, nsRFPService::TimeScale::MilliSeconds, 20);
  process(69053.12, nsRFPService::TimeScale::MilliSeconds, 20);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_KnownBad)
{
  bool jitterEnabled = setupJitter(false);
  process(1054.842405, nsRFPService::TimeScale::MilliSeconds, 20);
  process(273.53038600000002, nsRFPService::TimeScale::MilliSeconds, 20);
  process(628.66686500000003, nsRFPService::TimeScale::MilliSeconds, 20);
  process(521.28919100000007, nsRFPService::TimeScale::MilliSeconds, 20);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_Edge)
{
  bool jitterEnabled = setupJitter(false);
  process(2611.14, nsRFPService::TimeScale::MilliSeconds, 20);
  process(2611.16, nsRFPService::TimeScale::MilliSeconds, 20);
  process(2612.16, nsRFPService::TimeScale::MilliSeconds, 20);
  process(2601.64, nsRFPService::TimeScale::MilliSeconds, 20);
  process(2595.16, nsRFPService::TimeScale::MilliSeconds, 20);
  process(2578.66, nsRFPService::TimeScale::MilliSeconds, 20);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_Expectations)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.14, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.145, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.141, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15999, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.13, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 2611.12);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_Expectations_HighRes)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.14, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.145, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.141, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15999, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.13, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 2611.12);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_Expectations_RFP)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.14, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.145, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.141, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15999, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.13, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 2611.12);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_Expectations_DangerouslyNone)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.14, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.14);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.145, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.145);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.141, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.141);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15999, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.15999);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.15, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.15);
  result = nsRFPService::ReduceTimePrecisionImpl(
      2611.13, nsRFPService::TimeScale::MilliSeconds, 20, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 2611.13);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_ExpectedLossOfPrecision)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  // We lose integer precision at 9007199254740992 - let's confirm that.
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740992.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 9007199254740990.0);
  // 9007199254740995 is approximated to 9007199254740996
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740995.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 9007199254740996);
  // 9007199254740999 is approximated as 9007199254741000
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740999.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 9007199254741000.0);
  // 9007199254743568 can be represented exactly, but will be clamped to
  // 9007199254743564
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254743568.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 9007199254743564.0);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_ExpectedLossOfPrecision_HighRes)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  // We lose integer precision at 9007199254740992 - let's confirm that.
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740992.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 9007199254740980.0);
  // 9007199254740995 is approximated to 9007199254740980
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740995.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 9007199254740980);
  // 9007199254740999 is approximated as 9007199254741000
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740999.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 9007199254741000.0);
  // 9007199254743568 can be represented exactly, but will be clamped to
  // 9007199254743560
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254743568.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::UnconditionalAKAHighRes);
  ASSERT_EQ(result, 9007199254743560.0);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_ExpectedLossOfPrecision_RFP)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  // We lose integer precision at 9007199254740992 - let's confirm that.
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740992.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 9007199254740990.0);
  // 9007199254740995 is approximated to 9007199254740980
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740995.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 9007199254740995);
  // 9007199254740999 is approximated as 9007199254741000
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740999.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 9007199254741000.0);
  // 9007199254743568 can be represented exactly, but will be clamped to
  // 9007199254743560
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254743568.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::RFP);
  ASSERT_EQ(result, 9007199254743565.0);
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting,
     ReducePrecision_ExpectedLossOfPrecision_DangerouslyNone)
{
  bool jitterEnabled = setupJitter(false);
  double result;
  // We lose integer precision at 9007199254740992 - let's confirm that.
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740992.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 9007199254740992.0);
  // 9007199254740995 is approximated to 9007199254740980
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740995.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 9007199254740995);
  // 9007199254740999 is approximated as 9007199254741000
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254740999.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 9007199254740999.0);
  // 9007199254743568 can be represented exactly, but will be clamped to
  // 9007199254743560
  result = nsRFPService::ReduceTimePrecisionImpl(
      9007199254743568.0, nsRFPService::TimeScale::MicroSeconds, 5, -1,
      TimerPrecisionType::DangerouslyNone);
  ASSERT_EQ(result, 9007199254743568.0);
  cleanupJitter(jitterEnabled);
}

// Use an ugly but simple hack to turn an integer-based rand()
// function to a double-based one.
#define RAND_DOUBLE (rand() * (rand() / (double)rand()))

// If you're doing logging, you really don't want to run this test.
#define RUN_AGGRESSIVE false

TEST(ResistFingerprinting, ReducePrecision_Aggressive)
{
  if (!RUN_AGGRESSIVE) {
    return;
  }

  bool jitterEnabled = setupJitter(false);

  for (int i = 0; i < 10000; i++) {
    // Test three different time magnitudes, with decimals.
    // Note that we need separate variables for the different units, as scaling
    // them after calculating them will erase effects of approximation.
    // A magnitude in the seconds since epoch range.
    double time1_s = fmod(RAND_DOUBLE, 1516305819.0);
    double time1_ms = fmod(RAND_DOUBLE, 1516305819000.0);
    double time1_us = fmod(RAND_DOUBLE, 1516305819000000.0);
    // A magnitude in the 'couple of minutes worth of milliseconds' range.
    double time2_s = fmod(RAND_DOUBLE, (60.0 * 60 * 5));
    double time2_ms = fmod(RAND_DOUBLE, (1000.0 * 60 * 60 * 5));
    double time2_us = fmod(RAND_DOUBLE, (1000000.0 * 60 * 60 * 5));
    // A magnitude in the small range
    double time3_s = fmod(RAND_DOUBLE, 10);
    double time3_ms = fmod(RAND_DOUBLE, 10000);
    double time3_us = fmod(RAND_DOUBLE, 10000000);

    // Test two precision magnitudes, no decimals.
    // A magnitude in the high milliseconds.
    double precision1 = rand() % 250000;
    // a magnitude in the low microseconds.
    double precision2 = rand() % 200;

    process(time1_s, nsRFPService::TimeScale::Seconds, precision1);
    process(time1_s, nsRFPService::TimeScale::Seconds, precision2);
    process(time2_s, nsRFPService::TimeScale::Seconds, precision1);
    process(time2_s, nsRFPService::TimeScale::Seconds, precision2);
    process(time3_s, nsRFPService::TimeScale::Seconds, precision1);
    process(time3_s, nsRFPService::TimeScale::Seconds, precision2);

    process(time1_ms, nsRFPService::TimeScale::MilliSeconds, precision1);
    process(time1_ms, nsRFPService::TimeScale::MilliSeconds, precision2);
    process(time2_ms, nsRFPService::TimeScale::MilliSeconds, precision1);
    process(time2_ms, nsRFPService::TimeScale::MilliSeconds, precision2);
    process(time3_ms, nsRFPService::TimeScale::MilliSeconds, precision1);
    process(time3_ms, nsRFPService::TimeScale::MilliSeconds, precision2);

    process(time1_us, nsRFPService::TimeScale::MicroSeconds, precision1);
    process(time1_us, nsRFPService::TimeScale::MicroSeconds, precision2);
    process(time2_us, nsRFPService::TimeScale::MicroSeconds, precision1);
    process(time2_us, nsRFPService::TimeScale::MicroSeconds, precision2);
    process(time3_us, nsRFPService::TimeScale::MicroSeconds, precision1);
    process(time3_us, nsRFPService::TimeScale::MicroSeconds, precision2);
  }
  cleanupJitter(jitterEnabled);
}

TEST(ResistFingerprinting, ReducePrecision_JitterTestVectors)
{
  bool jitterEnabled = setupJitter(true);

  // clang-format off
  /*
   * Here's our test vector. We set the secret to the 16 byte value
   * 0x0001020304050607 0x1011121314151617
   *
   * We then use a stand alone XOrShift128+ generator:
   *
   * The midpoints are:
   *   0    = 328
   *   500  = 25
   *   1000 = 73
   *   1500 = 89
   *   2000 = 73
   *   2500 = 153
   *   3000 = 9
   *   3500 = 89
   *   4000 = 73
   *   4500 = 205
   *   5000 = 253
   *   5500 = 141
   */
  // clang-format on

  // Set the secret
  long long throwAway;
  uint8_t hardcodedSecret[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
                                 0x06, 0x07, 0x10, 0x11, 0x12, 0x13,
                                 0x14, 0x15, 0x16, 0x17};

  nsRFPService::RandomMidpoint(0, 500, -1, &throwAway, hardcodedSecret);

  // Run the test vectors
  double result;

  result = nsRFPService::ReduceTimePrecisionImpl(
      1, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 0);
  result = nsRFPService::ReduceTimePrecisionImpl(
      184, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 0);
  result = nsRFPService::ReduceTimePrecisionImpl(
      185, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      329, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      499, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);

  result = nsRFPService::ReduceTimePrecisionImpl(
      500, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      520, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      524, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      525, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 1000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      930, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 1000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      1072, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 1000);

  result = nsRFPService::ReduceTimePrecisionImpl(
      4000, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4050, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4072, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4073, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4340, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4499, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);

  result = nsRFPService::ReduceTimePrecisionImpl(
      4500, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4536, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4704, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 4500);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4705, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 5000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      4726, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 5000);
  result = nsRFPService::ReduceTimePrecisionImpl(
      5106, nsRFPService::TimeScale::MicroSeconds, 500, 4000,
      TimerPrecisionType::Normal);
  ASSERT_EQ(result, 5000);

  cleanupJitter(jitterEnabled);
}