File: trusted_vault_synthetic_field_trial_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (408 lines) | stat: -rw-r--r-- 16,745 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/service/trusted_vault_synthetic_field_trial.h"

#include <string>

#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

namespace {

using testing::Eq;
using testing::FloatNear;
using testing::IsEmpty;

MATCHER(IsNotValid, "") {
  return !arg.is_valid();
}

MATCHER_P(IsValidWithName, expected_name, "") {
  return arg.is_valid() && arg.name() == expected_name;
}

sync_pb::TrustedVaultAutoUpgradeExperimentGroup BuildTestProto(
    int cohort,
    sync_pb::TrustedVaultAutoUpgradeExperimentGroup::Type type,
    int type_index) {
  sync_pb::TrustedVaultAutoUpgradeExperimentGroup proto;
  proto.set_cohort(cohort);
  proto.set_type(type);
  proto.set_type_index(type_index);
  return proto;
}

TEST(TrustedVaultSyntheticFieldTrialTest, ShouldBuildInvalidGroup) {
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/1,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::TYPE_UNSPECIFIED,
          /*type_index=*/0)),
      IsNotValid());
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/0,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/0)),
      IsNotValid());
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/-1,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/0)),
      IsNotValid());
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/101,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/0)),
      IsNotValid());
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/6,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/-1)),
      IsNotValid());
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/6,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/51)),
      IsNotValid());
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldBuildInvalidGroupFromProtoDefaults) {
  EXPECT_THAT(TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(
                  sync_pb::TrustedVaultAutoUpgradeExperimentGroup()),
              IsNotValid());
}

TEST(TrustedVaultSyntheticFieldTrialTest, ShouldGetValidGroupName) {
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/5,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::TREATMENT,
          /*type_index=*/0)),
      IsValidWithName("Cohort5_Treatment"));
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/6,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/0)),
      IsValidWithName("Cohort6_Control"));
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/7,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION,
          /*type_index=*/0)),
      IsValidWithName("Cohort7_Validation"));
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldGetValidGroupNameWithTypeIndex) {
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/5,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::TREATMENT,
          /*type_index=*/1)),
      IsValidWithName("Cohort5_Treatment1"));
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/6,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          /*type_index=*/2)),
      IsValidWithName("Cohort6_Control2"));
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/7,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION,
          /*type_index=*/3)),
      IsValidWithName("Cohort7_Validation3"));
  EXPECT_THAT(
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          /*cohort=*/8,
          sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION,
          /*type_index=*/50)),
      IsValidWithName("Cohort8_Validation50"));
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldProduceDeterministicFloatBetweenZeroAndOneFromGaiaId) {
  // Tolerance used in case floating point operations may differ slightly across
  // platforms.
  const float kEpsilon = 1E-6f;

  // Expected values have been computed empirically. They are all between zero
  // and one as expected and appear to be uniformly distributed.
  struct {
    std::string gaia_id_str;
    std::string salt;
    float expected_value;
  } test_cases[] = {
      {"gaia_id_1", "salt1", 0.18181f},
      {"gaia_id_2", "salt1", 0.46258f},
      {"gaia_id_3", "salt1", 0.08043f},
      {"gaia_id_3", "salt2", 0.82853f},
      {"gaia_id_3", "salt3", 0.10936f},
      {"gaia_id_3", "", 0.76956f},
      // Specific value used in other tests (found empirically).
      {"gaia_id_49", "UserSampling", 0.00543f},
      {"gaia_id_49", "OnProfileLoadSampled", 0.05258f},
  };

  for (const auto& test_case : test_cases) {
    EXPECT_THAT(TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
                    DeterministicFloatBetweenZeroAndOneFromGaiaIdForTest(
                        GaiaId(test_case.gaia_id_str), test_case.salt),
                FloatNear(test_case.expected_value, kEpsilon))
        << " for " << test_case.gaia_id_str << " and salt " << test_case.salt;
  }
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldSampleGaiaIdsWithTenPercentProbability) {
  const int kNumGaiaIdsToTest = 50;

  int num_sampled_gaia_ids = 0;
  for (int i = 0; i < kNumGaiaIdsToTest; i++) {
    if (TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
            ShouldSampleGaiaIdWithTenPercentProbabilityForTest(
                GaiaId(base::StringPrintf("gaia_id_%d", i)))) {
      num_sampled_gaia_ids++;
    }
  }

  // About 10% of the gaia IDs should have returned true (i.e. should be
  // sampled). In this case, as empirically observed, it is off by one.
  EXPECT_THAT(num_sampled_gaia_ids, Eq(kNumGaiaIdsToTest / 10 + 1));
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldLogValidationMetricsUponOnProfileLoad) {
  const int kCohort = 6;     // Not relevant in this test.
  const int kTypeIndex = 7;  // Not relevant in this test.
  const auto control_group =
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          kCohort, sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          kTypeIndex));
  const auto validation_group =
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          kCohort, sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION,
          kTypeIndex));

  // String chosen from previous tests, given the convenient float value
  // computed from it.
  const GaiaId kGaiaId("gaia_id_49");
  ASSERT_TRUE(TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
                  ShouldSampleGaiaIdWithTenPercentProbabilityForTest(kGaiaId));
  ASSERT_THAT(TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
                  DeterministicFloatBetweenZeroAndOneFromGaiaIdForTest(
                      kGaiaId, /*salt=*/"OnProfileLoadSampled"),
              FloatNear(0.05258f, 1E-6));

  // Control group.
  {
    base::HistogramTester tester;
    control_group.LogValidationMetricsUponOnProfileLoad(kGaiaId);

    // Metrics with account consistency record a deterministic bucket.
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V01",
        false, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V04",
        false, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V06",
        false, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V09",
        false, /*expected_bucket_count=*/1);
    // Thresholds larger than 0.05258f record `true`.
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V20",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V23",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V25",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V28",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V50",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V53",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V55",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V58",
        true, /*expected_bucket_count=*/1);

    // Metrics without account consistency are known to record one value, but
    // the value is random.
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C01_V01",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C01_V04",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C01_V06",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C01_V09",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C20_V20",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C20_V23",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C20_V25",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C20_V28",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C50_V50",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C50_V53",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C50_V55",
        1);
    tester.ExpectTotalCount(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithoutAccountConsistency.Binary_C50_V58",
        1);
  }

  // Validation group.
  {
    base::HistogramTester tester;
    validation_group.LogValidationMetricsUponOnProfileLoad(kGaiaId);

    // Metrics with account consistency record a deterministic bucket.
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V01",
        false, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V04",
        false, /*expected_bucket_count=*/1);
    // Thresholds larger than 0.05258f record `true`. Note that this includes
    // two additional metrics compared to control group tested earlier.
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V06",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C01_V09",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V20",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V23",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V25",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C20_V28",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V50",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V53",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V55",
        true, /*expected_bucket_count=*/1);
    tester.ExpectUniqueSample(
        "Sync.TrustedVaultAutoUpgrade.Validation.OnProfileLoadSampled."
        "WithAccountConsistency.Binary_C50_V58",
        true, /*expected_bucket_count=*/1);
  }
}

TEST(TrustedVaultSyntheticFieldTrialTest,
     ShouldNotLogValidationMetricsUponOnProfileLoadDueToSampling) {
  const int kCohort = 6;     // Not relevant in this test.
  const int kTypeIndex = 7;  // Not relevant in this test.
  const auto control_group =
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          kCohort, sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL,
          kTypeIndex));
  const auto validation_group =
      TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(BuildTestProto(
          kCohort, sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION,
          kTypeIndex));

  // String chosen from previous tests.
  const GaiaId kGaiaId("gaia_id_1");
  ASSERT_FALSE(TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
                   ShouldSampleGaiaIdWithTenPercentProbabilityForTest(kGaiaId));

  base::HistogramTester tester;
  control_group.LogValidationMetricsUponOnProfileLoad(kGaiaId);
  validation_group.LogValidationMetricsUponOnProfileLoad(kGaiaId);

  // As per ASSERT_FALSE above, `kGaiaId` should be filtered out during sampling
  // and therefore no histograms should be recorded.
  EXPECT_THAT(tester.GetTotalCountsForPrefix("Sync."), IsEmpty());
}

}  // namespace

}  // namespace syncer