File: create_trials_from_seed_fuzzer_v2.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 (145 lines) | stat: -rw-r--r-- 5,202 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial.h"
#include "base/test/bind.h"
#include "base/test/scoped_command_line.h"
#include "components/variations/client_filterable_state.h"
#include "components/variations/entropy_provider.h"
#include "components/variations/fuzzers/create_trials_from_seed_test_case.pb.h"
#include "components/variations/proto/study.pb.h"
#include "components/variations/variations_layers.h"
#include "components/variations/variations_seed_processor.h"
#include "components/variations/variations_test_utils.h"
#include "testing/libfuzzer/proto/lpm_interface.h"

namespace variations {
namespace {

struct Environment {
  Environment() { base::CommandLine::Init(0, nullptr); }

  base::AtExitManager at_exit_manager;
};

EntropyProviders CreateEntropyProviders(
    const CreateTrialsFromSeedTestCase::EntropyValues& entropy_values) {
  return EntropyProviders(
      entropy_values.client_id(), {entropy_values.low_entropy(), 8000},
      entropy_values.limited_entropy_randomization_source());
}

std::unique_ptr<ClientFilterableState> CreateClientFilterableState(
    const CreateTrialsFromSeedTestCase::ClientFilterableState& spec) {
  auto client_state = std::make_unique<ClientFilterableState>(
      base::BindOnce([] { return false; }),
      base::BindLambdaForTesting([spec]() {
        return base::flat_set<uint64_t>(spec.google_groups().begin(),
                                        spec.google_groups().end());
      }));

  if (spec.has_locale()) {
    client_state->locale = spec.locale();
  }
  if (spec.has_reference_date_seconds_since_epoch()) {
    client_state->reference_date =
        base::Time::UnixEpoch() +
        base::Seconds(spec.reference_date_seconds_since_epoch());
  }
  if (!spec.version().empty()) {
    client_state->version = base::Version(
        std::vector<uint32_t>(spec.version().begin(), spec.version().end()));
  } else {
    // Default constructed version is invalid as per base::Version so we use
    // placeholder value instead.
    client_state->version = base::Version({0, 0, 0, 0});
  }
  if (!spec.os_version().empty()) {
    client_state->os_version = base::Version(std::vector<uint32_t>(
        spec.os_version().begin(), spec.os_version().end()));
  } else {
    // Default constructed version is invalid as per base::Version so we use
    // placeholder value instead.
    client_state->os_version = base::Version(std::vector<uint32_t>{0, 0});
  }
  if (spec.has_channel()) {
    client_state->channel = spec.channel();
  }
  if (spec.has_form_factor()) {
    client_state->form_factor = spec.form_factor();
  }
  if (spec.has_cpu_architecture()) {
    client_state->cpu_architecture = spec.cpu_architecture();
  }
  if (spec.has_platform()) {
    client_state->platform = spec.platform();
  }
  if (spec.has_hardware_class()) {
    client_state->hardware_class = spec.hardware_class();
  }
  if (spec.has_is_low_end_device()) {
    client_state->is_low_end_device = spec.is_low_end_device();
  }
  if (spec.has_session_consistency_country()) {
    client_state->session_consistency_country =
        spec.session_consistency_country();
  }
  if (spec.has_permanent_consistency_country()) {
    client_state->permanent_consistency_country =
        spec.permanent_consistency_country();
  }
  if (spec.has_policy_restriction()) {
    switch (spec.policy_restriction()) {
      case CreateTrialsFromSeedTestCase_RestrictionPolicy_NO_RESTRICTIONS:
        client_state->policy_restriction = RestrictionPolicy::NO_RESTRICTIONS;
        break;
      case CreateTrialsFromSeedTestCase_RestrictionPolicy_CRITICAL_ONLY:
        client_state->policy_restriction = RestrictionPolicy::CRITICAL_ONLY;
        break;
      case CreateTrialsFromSeedTestCase_RestrictionPolicy_ALL:
        client_state->policy_restriction = RestrictionPolicy::ALL;
        break;
    }
  }
  return client_state;
}

void CreateTrialsFromSeedFuzzer(
    const variations::CreateTrialsFromSeedTestCase& test_case) {
  base::FieldTrialList field_trial_list;
  base::FeatureList feature_list;

  auto client_state = CreateClientFilterableState(
      test_case.has_client_filterable_state()
          ? test_case.client_filterable_state()
          : variations::CreateTrialsFromSeedTestCase::ClientFilterableState());
  auto entropy_providers = CreateEntropyProviders(
      test_case.has_entropy()
          ? test_case.entropy()
          : variations::CreateTrialsFromSeedTestCase::EntropyValues());

  if (!test_case.has_seed()) {
    return;
  }

  auto seed = test_case.seed();
  VariationsLayers layers(seed, entropy_providers);
  VariationsSeedProcessor().CreateTrialsFromSeed(
      seed, *client_state, base::BindRepeating(NoopUIStringOverrideCallback),
      entropy_providers, layers, &feature_list);
}

}  // namespace

DEFINE_PROTO_FUZZER(const variations::CreateTrialsFromSeedTestCase& test_case) {
  static Environment env;
  CreateTrialsFromSeedFuzzer(test_case);
}

}  // namespace variations