File: simulate_for_crosstalk_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (139 lines) | stat: -rw-r--r-- 4,382 bytes parent folder | download | duplicates (11)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstring>

#include "base/memory/scoped_refptr.h"
#include "base/metrics/field_trial.h"
#include "components/variations/entropy_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

// These tests mirror testSimulateTrialAssignments and
// testSimulateTrialAssignmentsWithForcedSalt in the internal crosstalk_test.py.
// Both places hard-code the same expected results, ensuring the chromium
// implementation matches the Python implementation.

namespace variations {
namespace {

using AssignmentCounts = std::map<std::string, std::map<std::string, int>>;

// These studies must match the ones in crosstalk_test.py.
const char* const kDnsStudyName = "DnsProbe-Attempts";
const char* const kInstantStudyName = "InstantExtended";
constexpr uint32_t kMaxLowEntropySize = 8000;

scoped_refptr<base::FieldTrial> CreateDnsStudy(double value) {
  scoped_refptr<base::FieldTrial> trial(
      base::FieldTrial::CreateSimulatedFieldTrial(kDnsStudyName, 100, "default",
                                                  value));
  trial->AppendGroup("1", 10);
  return trial;
}

scoped_refptr<base::FieldTrial> CreateInstantStudy(double value) {
  scoped_refptr<base::FieldTrial> trial(
      base::FieldTrial::CreateSimulatedFieldTrial(kInstantStudyName, 100,
                                                  "DefaultGroup", value));
  trial->AppendGroup("Group1", 5);
  trial->AppendGroup("Control1", 5);
  trial->AppendGroup("Group2", 5);
  trial->AppendGroup("Control2", 5);
  trial->AppendGroup("Group3", 5);
  trial->AppendGroup("Control3", 5);
  return trial;
}

// Simulate assigning users with every possible low entropy source to each
// study. Populate |counts| such that count[x][y] is the number of users in both
// DnsProbe-Attempts' group x and InstantExtended's group y. If |salt|, add salt
// to InstantExtended's name.
AssignmentCounts CountAssignments(bool salt) {
  AssignmentCounts counts;

  for (uint16_t source = 0; source < kMaxLowEntropySize; source++) {
    NormalizedMurmurHashEntropyProvider provider({source, kMaxLowEntropySize});

    double dns_value = provider.GetEntropyForTrial(kDnsStudyName, 0);
    std::string dns_name = CreateDnsStudy(dns_value)->group_name();

    std::string instant_study_name = kInstantStudyName;
    if (salt)
      instant_study_name += "abcdefghijklmnop";
    double instant_value = provider.GetEntropyForTrial(instant_study_name, 0);
    std::string instant_name = CreateInstantStudy(instant_value)->group_name();

    counts[dns_name][instant_name]++;
  }
  return counts;
}

}  // namespace

TEST(SimulateForCrosstalkTest, WithoutSalt) {
  // These must match crosstalk_test.py's testSimulateTrialAssignments.
  AssignmentCounts expected = {
      {
          "default",
          {
              {"Group1", 360},
              {"Control1", 365},
              {"Group2", 355},
              {"Control2", 347},
              {"Group3", 366},
              {"Control3", 354},
              {"DefaultGroup", 5053},
          },
      },
      {
          "1",
          {
              {"Group1", 40},
              {"Control1", 35},
              {"Group2", 45},
              {"Control2", 53},
              {"Group3", 34},
              {"Control3", 46},
              {"DefaultGroup", 547},
          },
      },
  };
  AssignmentCounts actual = CountAssignments(false);
  ASSERT_EQ(expected, actual);
}

TEST(SimulateForCrosstalkTest, WithSalt) {
  // These must match crosstalk_test.py's
  // testSimulateTrialAssignmentsWithForcedSalt.
  AssignmentCounts expected = {
      {
          "default",
          {
              {"Group1", 362},
              {"Control1", 372},
              {"Group2", 365},
              {"Control2", 360},
              {"Group3", 357},
              {"Control3", 355},
              {"DefaultGroup", 5029},
          },
      },
      {
          "1",
          {
              {"Group1", 38},
              {"Control1", 28},
              {"Group2", 35},
              {"Control2", 40},
              {"Group3", 43},
              {"Control3", 45},
              {"DefaultGroup", 571},
          },
      },
  };
  AssignmentCounts actual = CountAssignments(true);
  ASSERT_EQ(expected, actual);
}

}  // namespace variations