File: flags_test_helpers.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 (361 lines) | stat: -rw-r--r-- 11,946 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
// Copyright 2020 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/webui/flags/flags_test_helpers.h"

#include <gtest/gtest.h>

#include <map>
#include <string>
#include <string_view>
#include <vector>

#include "base/base_paths.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "components/webui/flags/feature_entry.h"
#include "components/webui/flags/flags_state.h"

namespace {

constexpr char kMetadataFileName[] = "flag-metadata.json";
constexpr char kNeverExpireFileName[] = "flag-never-expire-list.json";

// Returns the file contents of a named file under $SRC/chrome/browser
// interpreted as a JSON value.
base::Value ReadFileContentsAsJSON(const std::string& filename) {
  base::FilePath metadata_path;
  base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &metadata_path);
  JSONFileValueDeserializer deserializer(
      metadata_path.AppendASCII("chrome").AppendASCII("browser").AppendASCII(
          filename));
  int error_code;
  std::string error_message;
  std::unique_ptr<base::Value> json =
      deserializer.Deserialize(&error_code, &error_message);
  CHECK(json) << "Failed to load " << filename << ": " << error_code << " "
              << error_message;
  return std::move(*json);
}

// Data structure capturing the metadata of the flag.
struct FlagMetadataEntry {
  std::vector<std::string> owners;
  int expiry_milestone;
};

// Lookup of metadata by flag name.
using FlagMetadataMap = std::map<std::string, FlagMetadataEntry>;

// Reads the flag metadata file.
FlagMetadataMap LoadFlagMetadata() {
  base::Value metadata_json = ReadFileContentsAsJSON(kMetadataFileName);

  FlagMetadataMap metadata;
  for (const auto& entry_val : metadata_json.GetList()) {
    const base::Value::Dict& entry = entry_val.GetDict();
    std::string name = *entry.FindString("name");
    std::vector<std::string> owners;
    if (const base::Value::List* e = entry.FindList("owners")) {
      for (const auto& owner : *e) {
        owners.push_back(owner.GetString());
      }
    }
    int expiry_milestone = entry.FindInt("expiry_milestone").value();
    metadata[name] = FlagMetadataEntry{owners, expiry_milestone};
  }

  return metadata;
}

std::vector<std::string> LoadFlagNeverExpireList() {
  base::Value list_json = ReadFileContentsAsJSON(kNeverExpireFileName);

  std::vector<std::string> result;
  for (const auto& entry : list_json.GetList()) {
    result.push_back(entry.GetString());
  }
  return result;
}

bool IsValidLookingOwner(std::string_view owner) {
  // Never allow ',' or ' ' in owner names, regardless of all other constraints.
  // It is otherwise too easy to accidentally do this:
  //   "owners": [ "foo@chromium.org,bar@chromium.org" ]
  // or this:
  //   "owners": [ "foo@chromium.org bar@chromium.org" ]
  // Apologies to those who have spaces in their email addresses or OWNERS file
  // path names :)
  if (owner.find_first_of(", ") != std::string::npos) {
    return false;
  }

  // Per the specification at the top of flag-metadata.json, an owner is one of:
  // 1) A string containing '@', which is treated as a full email address
  // 2) A string beginning with '//', which is a path to an OWNERS file

  const size_t at_pos = owner.find("@");
  if (at_pos != std::string::npos) {
    // If there's an @, check for a . after it. This catches one common error:
    // writing "foo@" in the owners list rather than bare "foo" or full
    // "foo@domain.com".
    return owner.find(".", at_pos) != std::string::npos;
  }

  if (base::StartsWith(owner, "//")) {
    // Looks like a path to a file. It would be nice to check that the file
    // actually exists here, but that's not possible because when this test
    // runs it runs in an isolated environment. To check for the presence of the
    // file the test would need a build-time declaration that it depends on that
    // file. Instead, just assume any file path ending in 'OWNERS' is valid.
    // This doesn't check that the entire filename part of the path is 'OWNERS'
    // because sometimes it is instead 'IPC_OWNERS' or similar.
    return base::EndsWith(owner, "OWNERS");
  }

  return false;
}

std::string NormalizeName(const std::string& name) {
  std::string normalized_name = base::ToLowerASCII(name);
  std::replace(normalized_name.begin(), normalized_name.end(), '_', '-');

  return normalized_name;
}

constexpr char kStartSentinel[] = "(start of file)";

using NameNameMap = std::map<std::string, std::string>;
using NameVector = std::vector<std::string>;

// Given a NameVector, returns a map from each name n to the name preceding n in
// the NameVector. The returned map maps the first name to kStartSentinel.
// Preconditions:
//   * There are no duplicates in |strings|
//   * No entry in |strings| equals kStartSentinel
// Postconditions:
//   * Every entry in |strings| appears as a key in the result map
//   * Every entry in |strings| maps to another entry in |strings| or to
//     kStartSentinel in the result map
NameNameMap BuildAfterMap(const NameVector& strings) {
  NameNameMap after_map;
  CHECK_NE(strings[0], kStartSentinel);
  after_map[strings[0]] = kStartSentinel;
  for (size_t i = 1; i < strings.size(); ++i) {
    CHECK_NE(strings[i], kStartSentinel);
    CHECK(!after_map.contains(strings[i]));
    after_map[strings[i]] = strings[i - 1];
  }

  // Postconditions:
  for (const auto& entry : strings) {
    CHECK(after_map.contains(entry));
  }

  return after_map;
}

// Given a vector of names, returns a vector of normalized names, and an inverse
// mapping from normalized name to previous name. The inverse mapping is
// populated only for names which were altered when normalized.
// Preconditions: none
// Postconditions:
//   * Every (key, value) pair in |denormalized| have key != value
//   * Every (key, value) pair in |denormalized| have key = NormalizeName(value)
std::pair<NameVector, NameNameMap> NormalizeNames(const NameVector& names) {
  NameNameMap denormalized;
  NameVector normalized;
  for (const auto& name : names) {
    std::string n = NormalizeName(name);
    normalized.push_back(n);
    if (n != name) {
      denormalized[n] = name;
    }
  }

  // Postconditions:
  for (const auto& pair : denormalized) {
    CHECK_NE(pair.first, pair.second);
    CHECK_EQ(pair.first, NormalizeName(pair.second));
  }

  return std::tie(normalized, denormalized);
}

// Given a list of flag names, adds test failures for any that do not appear in
// alphabetical order. This is more complex than simply sorting the list and
// checking whether the order changed - this function is supposed to emit error
// messages which tell the user specifically which flags need to be moved and to
// where in the file.
void EnsureNamesAreAlphabetical(const NameVector& names,
                                const std::string& filename) {
  auto [normalized, denormalized] = NormalizeNames(names);
  auto was_after = BuildAfterMap(normalized);

  std::sort(normalized.begin(), normalized.end());
  auto goes_after = BuildAfterMap(normalized);

  auto denormalize = [&](const std::string& name) {
    return denormalized.contains(name) ? denormalized[name] : name;
  };

  for (const auto& n : normalized) {
    if (was_after[n] != goes_after[n]) {
      ADD_FAILURE() << "In '" << filename << "': flag '" << denormalize(n)
                    << "' should be right after '" << denormalize(goes_after[n])
                    << "'";
    }
  }
}

bool IsUnexpireFlagFor(const flags_ui::FeatureEntry& entry, int milestone) {
  std::string expected_flag =
      base::StringPrintf("temporary-unexpire-flags-m%d", milestone);
  if (entry.internal_name != expected_flag) {
    return false;
  }
  if (!(entry.supported_platforms & flags_ui::kFlagInfrastructure)) {
    return false;
  }
  if (entry.type != flags_ui::FeatureEntry::FEATURE_VALUE) {
    return false;
  }
  std::string expected_feature =
      base::StringPrintf("UnexpireFlagsM%d", milestone);
  const auto* feature = entry.feature.feature;
  if (!feature || feature->name != expected_feature) {
    return false;
  }
  return true;
}

}  // namespace

namespace flags_ui {

namespace testing {

void EnsureEveryFlagHasMetadata(
    const base::span<const flags_ui::FeatureEntry>& entries) {
  FlagMetadataMap metadata = LoadFlagMetadata();
  std::vector<std::string> missing_flags;

  for (const auto& entry : entries) {
    // Flags that are part of the flags system itself (like unexpiry meta-flags)
    // don't have metadata, so skip them here.
    if (entry.supported_platforms & flags_ui::kFlagInfrastructure) {
      continue;
    }

    if (metadata.count(entry.internal_name) == 0) {
      missing_flags.push_back(entry.internal_name);
    }
  }

  std::sort(missing_flags.begin(), missing_flags.end());

  EXPECT_EQ(0u, missing_flags.size())
      << "Missing flags: " << base::JoinString(missing_flags, "\n  ");
}

void EnsureOnlyPermittedFlagsNeverExpire() {
  FlagMetadataMap metadata = LoadFlagMetadata();
  std::vector<std::string> listed_flags = LoadFlagNeverExpireList();
  std::vector<std::string> missing_flags;

  for (const auto& entry : metadata) {
    if (entry.second.expiry_milestone == -1 &&
        !base::Contains(listed_flags, entry.first)) {
      missing_flags.push_back(entry.first);
    }
  }

  std::sort(missing_flags.begin(), missing_flags.end());

  EXPECT_EQ(0u, missing_flags.size())
      << "Flags not listed for no-expire: "
      << base::JoinString(missing_flags, "\n  ");
}

void EnsureEveryFlagHasNonEmptyOwners() {
  FlagMetadataMap metadata = LoadFlagMetadata();
  std::vector<std::string> sad_flags;

  for (const auto& it : metadata) {
    if (it.second.owners.empty()) {
      sad_flags.push_back(it.first);
    }
  }

  std::sort(sad_flags.begin(), sad_flags.end());

  EXPECT_EQ(0u, sad_flags.size())
      << "Flags missing owners: " << base::JoinString(sad_flags, "\n  ");
}

void EnsureOwnersLookValid() {
  FlagMetadataMap metadata = LoadFlagMetadata();
  std::vector<std::string> sad_flags;

  for (const auto& flag : metadata) {
    for (const auto& owner : flag.second.owners) {
      if (!IsValidLookingOwner(owner)) {
        sad_flags.push_back(flag.first);
      }
    }
  }

  EXPECT_EQ(0u, sad_flags.size()) << "Flags with invalid-looking owners: "
                                  << base::JoinString(sad_flags, "\n");
}

void EnsureFlagsAreListedInAlphabeticalOrder() {
  {
    auto json = ReadFileContentsAsJSON(kMetadataFileName);
    std::vector<std::string> names;
    for (const auto& entry : json.GetList()) {
      names.push_back(*entry.GetDict().FindString("name"));
    }
    EnsureNamesAreAlphabetical(names, kMetadataFileName);
  }

  {
    auto json = ReadFileContentsAsJSON(kNeverExpireFileName);
    std::vector<std::string> names;
    for (const auto& entry : json.GetList()) {
      names.push_back(entry.GetString());
    }

    EnsureNamesAreAlphabetical(names, kNeverExpireFileName);
  }
}

// TODO(crbug.com/40785799): Call this from the iOS flags unittests once
// flag expiration is supported there.
void EnsureRecentUnexpireFlagsArePresent(
    const base::span<const flags_ui::FeatureEntry>& entries,
    int current_milestone) {
  auto contains_unexpire_for = [&](int mstone) {
    for (const auto& entry : entries) {
      if (IsUnexpireFlagFor(entry, mstone)) {
        return true;
      }
    }
    return false;
  };

  EXPECT_FALSE(contains_unexpire_for(current_milestone));
  EXPECT_TRUE(contains_unexpire_for(current_milestone - 1));
  EXPECT_TRUE(contains_unexpire_for(current_milestone - 2));
  EXPECT_FALSE(contains_unexpire_for(current_milestone - 3));
}

}  // namespace testing

}  // namespace flags_ui