File: flat_ruleset_indexer_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 (783 lines) | stat: -rw-r--r-- 33,217 bytes parent folder | download | duplicates (3)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/declarative_net_request/flat_ruleset_indexer.h"

#include <stdint.h>

#include <algorithm>
#include <array>
#include <map>
#include <optional>
#include <string>
#include <string_view>

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/values_test_util.h"
#include "components/url_pattern_index/flat/url_pattern_index_generated.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/browser/api/declarative_net_request/indexed_rule.h"
#include "extensions/browser/api/declarative_net_request/test_utils.h"
#include "extensions/browser/api/declarative_net_request/utils.h"
#include "extensions/common/api/declarative_net_request.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions::declarative_net_request {
namespace {

namespace flat_rule = url_pattern_index::flat;
namespace dnr_api = api::declarative_net_request;
using FlatRulesetIndexerTest = ::testing::Test;

// Helper to convert a flatbuffer string to a std::string.
std::string ToString(const flatbuffers::String* string) {
  DCHECK(string);
  return CreateString<std::string>(*string);
}

// Helper to convert a flatbuffer vector of strings to a std::vector.
std::vector<std::string> ToVector(
    const ::flatbuffers::Vector<::flatbuffers::Offset<::flatbuffers::String>>*
        vec) {
  if (!vec) {
    return std::vector<std::string>();
  }
  std::vector<std::string> result;
  result.reserve(vec->size());
  for (auto* str : *vec) {
    result.push_back(ToString(str));
  }
  return result;
}

// Helper to convert a flatbuffer vector of flat::ModifyHeaderInfo to a
// std::vector of dnr_api::ModifyHeaderInfo
std::vector<dnr_api::ModifyHeaderInfo> ToVector(
    const ::flatbuffers::Vector<::flatbuffers::Offset<flat::ModifyHeaderInfo>>*
        vec) {
  if (!vec) {
    return std::vector<dnr_api::ModifyHeaderInfo>();
  }
  std::vector<dnr_api::ModifyHeaderInfo> result;
  result.reserve(vec->size());

  for (auto* flat_header_info : *vec) {
    dnr_api::ModifyHeaderInfo header_info;

    const flat::HeaderOperation flat_operation = flat_header_info->operation();
    const flatbuffers::String* flat_value = flat_header_info->value();
    switch (flat_operation) {
      case flat::HeaderOperation_append:
        header_info.operation = dnr_api::HeaderOperation::kAppend;
        DCHECK(flat_value);
        header_info.value = ToString(flat_value);
        break;
      case flat::HeaderOperation_set:
        header_info.operation = dnr_api::HeaderOperation::kSet;
        DCHECK(flat_value);
        header_info.value = ToString(flat_value);
        break;
      case flat::HeaderOperation_remove:
        header_info.operation = dnr_api::HeaderOperation::kRemove;
        break;
    };

    const flatbuffers::String* flat_header = flat_header_info->header();
    DCHECK(flat_header);
    header_info.header = ToString(flat_header);

    const flatbuffers::String* flat_regex_filter =
        flat_header_info->regex_filter();
    if (flat_regex_filter) {
      header_info.regex_filter = ToString(flat_regex_filter);
    }

    const flatbuffers::String* flat_regex_substitution =
        flat_header_info->regex_substitution();
    if (flat_regex_substitution) {
      header_info.regex_substitution = ToString(flat_regex_substitution);
    }

    DCHECK(flat_header_info->regex_options());
    header_info.regex_options = dnr_api::HeaderRegexOptions();
    header_info.regex_options->match_all =
        flat_header_info->regex_options()->match_all();

    result.push_back(std::move(header_info));
  }

  return result;
}

// Helper to create a generic URLTransform.
dnr_api::URLTransform CreateUrlTransform() {
  const char* transform = R"(
    {
      "scheme" : "http",
      "host" : "foo.com",
      "port" : "80",
      "path" : "",
      "queryTransform" : {
        "removeParams" : ["x1", "x2"],
        "addOrReplaceParams" : [
          {"key" : "y1", "value" : "foo"},
          {"key" : "y2", "value" : "foo2", "replaceOnly": false},
          {"key" : "y3", "value" : "foo3", "replaceOnly": true}
        ]
      },
      "fragment" : "#xx",
      "username" : "user",
      "password" : "pass"
    }
  )";

  auto result =
      dnr_api::URLTransform::FromValue(base::test::ParseJsonDict(transform));
  CHECK(result.has_value());
  return std::move(result).value();
}

// Helper to verify the indexed form of URlTransform created by
// |CreateUrlTransform()|.
bool VerifyUrlTransform(const flat::UrlTransform& flat_transform) {
  auto is_string_equal = [](std::string_view str,
                            const flatbuffers::String* flat_str) {
    return flat_str && ToString(flat_str) == str;
  };

  auto verify_add_or_replace_params = [&flat_transform, &is_string_equal]() {
    if (!flat_transform.add_or_replace_query_params() ||
        flat_transform.add_or_replace_query_params()->size() != 3) {
      return false;
    }

    auto does_query_key_value_match = [&flat_transform, &is_string_equal](
                                          int query_key_index,
                                          std::string_view expected_key,
                                          std::string_view expected_value,
                                          bool expected_replace_only) {
      const flat::QueryKeyValue* query_pair =
          flat_transform.add_or_replace_query_params()->Get(query_key_index);
      CHECK(query_pair);
      return is_string_equal(expected_key, query_pair->key()) &&
             is_string_equal(expected_value, query_pair->value()) &&
             query_pair->replace_only() == expected_replace_only;
    };

    return does_query_key_value_match(0, "y1", "foo", false) &&
           does_query_key_value_match(1, "y2", "foo2", false) &&
           does_query_key_value_match(2, "y3", "foo3", true);
  };

  return is_string_equal("http", flat_transform.scheme()) &&
         is_string_equal("foo.com", flat_transform.host()) &&
         !flat_transform.clear_port() &&
         is_string_equal("80", flat_transform.port()) &&
         flat_transform.clear_path() && !flat_transform.path() &&
         !flat_transform.clear_query() && !flat_transform.query() &&
         flat_transform.remove_query_params() &&
         std::vector<std::string>{"x1", "x2"} ==
             ToVector(flat_transform.remove_query_params()) &&
         verify_add_or_replace_params() && !flat_transform.clear_fragment() &&
         is_string_equal("xx", flat_transform.fragment()) &&
         is_string_equal("user", flat_transform.username()) &&
         is_string_equal("pass", flat_transform.password());
}

// Helper to create an IndexedRule.
IndexedRule CreateIndexedRule(
    uint32_t id,
    uint32_t priority,
    uint8_t options,
    uint16_t element_types,
    uint8_t activation_types,
    flat_rule::UrlPatternType url_pattern_type,
    flat_rule::AnchorType anchor_left,
    flat_rule::AnchorType anchor_right,
    std::string url_pattern,
    std::vector<std::string> domains,
    std::vector<std::string> excluded_domains,
    std::optional<std::string> redirect_url,
    dnr_api::RuleActionType action_type,
    std::optional<dnr_api::URLTransform> url_transform,
    std::optional<std::string> regex_substitution,
    std::vector<dnr_api::ModifyHeaderInfo> request_headers_to_modify,
    std::vector<dnr_api::ModifyHeaderInfo> response_headers_to_modify,
    std::vector<dnr_api::HeaderInfo> response_headers,
    std::vector<dnr_api::HeaderInfo> excluded_response_headers) {
  IndexedRule rule;
  rule.id = id;
  rule.priority = priority;
  rule.options = options;
  rule.element_types = element_types;
  rule.activation_types = activation_types;
  rule.url_pattern_type = url_pattern_type;
  rule.anchor_left = anchor_left;
  rule.anchor_right = anchor_right;
  rule.url_pattern = std::move(url_pattern);
  rule.initiator_domains = std::move(domains);
  rule.excluded_initiator_domains = std::move(excluded_domains);
  rule.redirect_url = std::move(redirect_url);
  rule.action_type = action_type;
  rule.url_transform = std::move(url_transform);
  rule.regex_substitution = std::move(regex_substitution);
  rule.request_headers_to_modify = std::move(request_headers_to_modify);
  rule.response_headers_to_modify = std::move(response_headers_to_modify);
  rule.response_headers = std::move(response_headers);
  rule.excluded_response_headers = std::move(excluded_response_headers);
  return rule;
}

// Compares |indexed_rule| and |rule| for equality. Ignores the redirect url and
// the list of request and response headers since they're not stored as part of
// flat_rule::UrlRule.
bool AreRulesEqual(const IndexedRule* indexed_rule,
                   const flat_rule::UrlRule* rule) {
  CHECK(indexed_rule);
  CHECK(rule);

  return indexed_rule->id == rule->id() &&
         indexed_rule->priority == rule->priority() &&
         indexed_rule->options == rule->options() &&
         indexed_rule->element_types == rule->element_types() &&
         indexed_rule->activation_types == rule->activation_types() &&
         indexed_rule->url_pattern_type == rule->url_pattern_type() &&
         indexed_rule->anchor_left == rule->anchor_left() &&
         indexed_rule->anchor_right == rule->anchor_right() &&
         indexed_rule->url_pattern == ToString(rule->url_pattern()) &&
         indexed_rule->initiator_domains ==
             ToVector(rule->initiator_domains_included()) &&
         indexed_rule->excluded_initiator_domains ==
             ToVector(rule->initiator_domains_excluded()) &&
         indexed_rule->request_domains ==
             ToVector(rule->request_domains_included()) &&
         indexed_rule->excluded_request_domains ==
             ToVector(rule->request_domains_excluded());
}

// Returns all UrlRule(s) in the given |index|.
std::vector<const flat_rule::UrlRule*> GetAllRulesFromIndex(
    const flat_rule::UrlPatternIndex* index) {
  std::vector<const flat_rule::UrlRule*> result;

  // Iterate over all ngrams and add their corresponding rules.
  for (auto* ngram_to_rules : *index->ngram_index()) {
    if (ngram_to_rules == index->ngram_index_empty_slot()) {
      continue;
    }
    for (const auto* rule : *ngram_to_rules->rule_list()) {
      result.push_back(rule);
    }
  }

  // Add all fallback rules.
  for (const auto* rule : *index->fallback_rules()) {
    result.push_back(rule);
  }

  return result;
}

// Verifies that both |rules| and |index| correspond to the same set of rules
// (in different representations).
void VerifyIndexEquality(const std::vector<const IndexedRule*>& rules,
                         const flat_rule::UrlPatternIndex* index) {
  struct RulePair {
    raw_ptr<const IndexedRule> indexed_rule = nullptr;
    raw_ptr<const flat_rule::UrlRule> url_rule = nullptr;
  };

  // Build a map from rule IDs to RulePair(s).
  std::map<uint32_t, RulePair> map;

  for (const auto* rule : rules) {
    EXPECT_EQ(nullptr, map[rule->id].indexed_rule.get());
    map[rule->id].indexed_rule = rule;
  }

  std::vector<const flat_rule::UrlRule*> flat_rules =
      GetAllRulesFromIndex(index);
  for (const auto* rule : flat_rules) {
    EXPECT_EQ(nullptr, map[rule->id()].url_rule.get());
    map[rule->id()].url_rule = rule;
  }

  // Iterate over the map and verify equality of the two representations.
  for (const auto& elem : map) {
    EXPECT_TRUE(AreRulesEqual(elem.second.indexed_rule, elem.second.url_rule))
        << base::StringPrintf("Rule with id %u was incorrectly indexed",
                              elem.first);
  }
}

// Verifies that |extension_metadata| is sorted by ID and corresponds to rules
// in |rules|.
void VerifyExtensionMetadata(
    const std::vector<const IndexedRule*>& rules,
    const ::flatbuffers::Vector<flatbuffers::Offset<flat::UrlRuleMetadata>>*
        extension_metdata) {
  struct MetadataPair {
    raw_ptr<const IndexedRule> indexed_rule = nullptr;
    raw_ptr<const flat::UrlRuleMetadata> metadata = nullptr;
  };

  // Build a map from IDs to MetadataPair(s).
  std::map<uint32_t, MetadataPair> map;

  for (const auto* rule : rules) {
    // It is possible for a rule to be present in multiple indices, such as a
    // remove headers rule that removes more than one header.
    EXPECT_TRUE(map[rule->id].indexed_rule == nullptr ||
                map[rule->id].indexed_rule == rule);
    map[rule->id].indexed_rule = rule;
  }

  int previous_id = kMinValidID - 1;
  for (const auto* metadata : *extension_metdata) {
    EXPECT_EQ(nullptr, map[metadata->id()].metadata.get());
    map[metadata->id()].metadata = metadata;

    // Also verify that the metadata vector is sorted by ID.
    int current_id = static_cast<int>(metadata->id());
    EXPECT_LT(previous_id, current_id)
        << "|extension_metdata| is not sorted by ID";
    previous_id = current_id;
  }

  // Returns whether the metadata for the given rule was correctly indexed.
  auto is_metadata_correct = [](const MetadataPair& pair) {
    EXPECT_TRUE(pair.indexed_rule);

    if (ConvertToFlatActionType(pair.indexed_rule->action_type) !=
        pair.metadata->action()) {
      return false;
    }

    EXPECT_FALSE(pair.indexed_rule->redirect_url &&
                 pair.indexed_rule->url_transform);

    if (pair.indexed_rule->redirect_url) {
      if (!pair.metadata->redirect_url()) {
        return false;
      }
      return pair.indexed_rule->redirect_url ==
             ToString(pair.metadata->redirect_url());
    }

    if (pair.indexed_rule->url_transform) {
      if (!pair.metadata->transform()) {
        return false;
      }
      return VerifyUrlTransform(*pair.metadata->transform());
    }

    auto are_header_modifications_equal =
        [](const ::flatbuffers::Vector<
               ::flatbuffers::Offset<flat::ModifyHeaderInfo>>* metadata_headers,
           const std::vector<dnr_api::ModifyHeaderInfo>& indexed_headers) {
          return std::ranges::equal(indexed_headers, ToVector(metadata_headers),
                                    EqualsForTesting);
        };

    EXPECT_TRUE(are_header_modifications_equal(
        pair.metadata->request_headers(),
        pair.indexed_rule->request_headers_to_modify));
    EXPECT_TRUE(are_header_modifications_equal(
        pair.metadata->response_headers(),
        pair.indexed_rule->response_headers_to_modify));

    return true;
  };

  // Iterate over the map and verify correctness of the metadata.
  for (const auto& elem : map) {
    EXPECT_TRUE(is_metadata_correct(elem.second)) << base::StringPrintf(
        "Redirect rule with id %u was incorrectly indexed", elem.first);
  }
}

const flat::ExtensionIndexedRuleset* AddRuleAndGetRuleset(
    const std::vector<IndexedRule>& rules_to_index,
    flatbuffers::DetachedBuffer* buffer) {
  FlatRulesetIndexer indexer;
  for (const auto& rule : rules_to_index) {
    indexer.AddUrlRule(rule);
  }
  *buffer = indexer.FinishAndReleaseBuffer();

  EXPECT_EQ(rules_to_index.size(), indexer.indexed_rules_count());
  flatbuffers::Verifier verifier(buffer->data(), buffer->size());
  if (!flat::VerifyExtensionIndexedRulesetBuffer(verifier)) {
    return nullptr;
  }

  return flat::GetExtensionIndexedRuleset(buffer->data());
}

// Helper which:
//    - Constructs an ExtensionIndexedRuleset flatbuffer from the passed
//      IndexedRule(s) using FlatRulesetIndexer.
//    - Verifies that the ExtensionIndexedRuleset created is valid.
// Note: this does not test regex rules which are part of the
// ExtensionIndexedRuleset.
void AddRulesAndVerifyIndex(const std::vector<IndexedRule>& rules_to_index,
                            base::span<const std::vector<const IndexedRule*>>
                                before_request_expected_index_lists,
                            base::span<const std::vector<const IndexedRule*>>
                                headers_received_expected_index_lists) {
  flatbuffers::DetachedBuffer buffer;
  const flat::ExtensionIndexedRuleset* ruleset =
      AddRuleAndGetRuleset(rules_to_index, &buffer);
  ASSERT_TRUE(ruleset);

  for (size_t i = 0; i < flat::IndexType_count; ++i) {
    SCOPED_TRACE(base::StringPrintf("Testing index %zu", i));
    VerifyIndexEquality(before_request_expected_index_lists[i],
                        ruleset->before_request_index_list()->Get(i));
    VerifyIndexEquality(headers_received_expected_index_lists[i],
                        ruleset->headers_received_index_list()->Get(i));
  }

  {
    SCOPED_TRACE("Testing extension metadata");
    std::vector<const IndexedRule*> all_rules;
    for (size_t i = 0; i < flat::IndexType_count; i++) {
      all_rules.insert(all_rules.end(),
                       before_request_expected_index_lists[i].begin(),
                       before_request_expected_index_lists[i].end());
      all_rules.insert(all_rules.end(),
                       headers_received_expected_index_lists[i].begin(),
                       headers_received_expected_index_lists[i].end());
    }
    VerifyExtensionMetadata(all_rules, ruleset->extension_metadata());
  }
}

TEST_F(FlatRulesetIndexerTest, TestEmptyIndex) {
  std::vector<const IndexedRule*>
      expected_before_request_index_lists[flat::IndexType_count];
  std::vector<const IndexedRule*>
      expected_headers_received_index_lists[flat::IndexType_count];
  AddRulesAndVerifyIndex({}, expected_before_request_index_lists,
                         expected_headers_received_index_lists);
}

TEST_F(FlatRulesetIndexerTest, MultipleRules) {
  std::vector<IndexedRule> rules_to_index;

  // Explicitly push the elements instead of using the initializer list
  // constructor, because it does not support move-only types.

  // Blocking rules.
  rules_to_index.push_back(CreateIndexedRule(
      7, kMinValidPriority, flat_rule::OptionFlag_NONE,
      flat_rule::ElementType_OBJECT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_BOUNDARY, "google.com", {"a.com"}, {"x.a.com"},
      std::nullopt, dnr_api::RuleActionType::kBlock, std::nullopt, std::nullopt,
      {}, {}, {}, {}));
  rules_to_index.push_back(CreateIndexedRule(
      2, kMinValidPriority, flat_rule::OptionFlag_APPLIES_TO_THIRD_PARTY,
      flat_rule::ElementType_IMAGE | flat_rule::ElementType_WEBSOCKET,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_WILDCARDED,
      flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "*google*",
      {"a.com"}, {}, std::nullopt, dnr_api::RuleActionType::kBlock,
      std::nullopt, std::nullopt, {}, {}, {}, {}));

  // Redirect rules.
  rules_to_index.push_back(CreateIndexedRule(
      15, 2, flat_rule::OptionFlag_APPLIES_TO_FIRST_PARTY,
      flat_rule::ElementType_IMAGE, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_SUBDOMAIN,
      flat_rule::AnchorType_BOUNDARY, "google.com", {}, {},
      "http://example1.com", dnr_api::RuleActionType::kRedirect, std::nullopt,
      std::nullopt, {}, {}, {}, {}));
  rules_to_index.push_back(CreateIndexedRule(
      10, 2, flat_rule::OptionFlag_NONE,
      flat_rule::ElementType_SUBDOCUMENT | flat_rule::ElementType_SCRIPT,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_SUBSTRING,
      flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "example1", {},
      {"a.com"}, "http://example2.com", dnr_api::RuleActionType::kRedirect,
      std::nullopt, std::nullopt, {}, {}, {}, {}));
  rules_to_index.push_back(CreateIndexedRule(
      9, 3, flat_rule::OptionFlag_NONE, flat_rule::ElementType_NONE,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_WILDCARDED,
      flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "*", {}, {},
      "http://example2.com", dnr_api::RuleActionType::kRedirect, std::nullopt,
      std::nullopt, {}, {}, {}, {}));
  rules_to_index.push_back(CreateIndexedRule(
      100, 3, flat_rule::OptionFlag_NONE, flat_rule::ElementType_NONE,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_WILDCARDED,
      flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "*", {}, {},
      std::nullopt, dnr_api::RuleActionType::kRedirect, CreateUrlTransform(),
      std::nullopt, {}, {}, {}, {}));

  // Allow rules.
  rules_to_index.push_back(CreateIndexedRule(
      17, kMinValidPriority, flat_rule::OptionFlag_IS_ALLOWLIST,
      flat_rule::ElementType_PING | flat_rule::ElementType_SCRIPT,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_SUBSTRING,
      flat_rule::AnchorType_SUBDOMAIN, flat_rule::AnchorType_NONE,
      "example1.com", {"xyz.com"}, {}, std::nullopt,
      dnr_api::RuleActionType::kAllow, std::nullopt, std::nullopt, {}, {}, {},
      {}));
  rules_to_index.push_back(CreateIndexedRule(
      16, kMinValidPriority,
      flat_rule::OptionFlag_IS_ALLOWLIST | flat_rule::OptionFlag_IS_MATCH_CASE,
      flat_rule::ElementType_IMAGE, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, "example3", {}, {}, std::nullopt,
      dnr_api::RuleActionType::kAllow, std::nullopt, std::nullopt, {}, {}, {},
      {}));

  // Allow all requests rule.
  rules_to_index.push_back(CreateIndexedRule(
      22, 3, flat_rule::OptionFlag_NONE, flat_rule::ElementType_SUBDOCUMENT,
      flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_SUBSTRING,
      flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "example.com", {},
      {}, std::nullopt, dnr_api::RuleActionType::kAllowAllRequests,
      std::nullopt, std::nullopt, {}, {}, {}, {}));

  // Modify headers rules.
  std::vector<dnr_api::ModifyHeaderInfo> request_headers_1;
  request_headers_1.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kSet, "cookie", "sample-cookie"));

  std::vector<dnr_api::ModifyHeaderInfo> response_headers_1;
  response_headers_1.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kRemove, "set-cookie", std::nullopt));

  response_headers_1.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kAppend, "custom-1", "value-1"));

  response_headers_1.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kSet, "custom-2", "value-2"));

  rules_to_index.push_back(CreateIndexedRule(
      23, kMinValidPriority, flat_rule::OptionFlag_IS_MATCH_CASE,
      flat_rule::ElementType_SUBDOCUMENT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_SUBDOMAIN,
      flat_rule::AnchorType_NONE, "example.com", {}, {}, std::nullopt,
      dnr_api::RuleActionType::kModifyHeaders, std::nullopt, std::nullopt,
      std::move(request_headers_1), std::move(response_headers_1), {}, {}));

  std::vector<dnr_api::ModifyHeaderInfo> request_headers_2;
  request_headers_2.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kRemove, "referer", std::nullopt));

  request_headers_2.push_back(
      CreateModifyHeaderInfo(dnr_api::HeaderOperation::kRemove, "cookie",
                             std::nullopt, "bad-cookie", std::nullopt));

  {
    std::optional<dnr_api::HeaderRegexOptions> regex_options =
        std::make_optional(dnr_api::HeaderRegexOptions());
    regex_options->match_all = true;

    // TODO(crbug.com/352093575): Make the header operation null when feature
    // launches to avoid the documentation labelling it as optional when all
    // current ModifyHeader actions require an operation to be specified.
    request_headers_2.push_back(CreateModifyHeaderInfo(
        dnr_api::HeaderOperation::kRemove, "cookie", std::nullopt,
        "worst-cookie", "best-cookie=phew", std::move(regex_options)));
  }

  rules_to_index.push_back(CreateIndexedRule(
      24, kMinValidPriority, flat_rule::OptionFlag_IS_MATCH_CASE,
      flat_rule::ElementType_SUBDOCUMENT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_SUBDOMAIN,
      flat_rule::AnchorType_NONE, "example.com", {}, {}, std::nullopt,
      dnr_api::RuleActionType::kModifyHeaders, std::nullopt, std::nullopt,
      std::move(request_headers_2), {}, {}, {}));

  {
    // Blocking rule matching on response headers.
    std::vector<dnr_api::HeaderInfo> response_headers;
    response_headers.push_back(CreateHeaderInfo(
        "header", std::vector<std::string>({"value"}), std::nullopt));
    rules_to_index.push_back(CreateIndexedRule(
        137, kMinValidPriority, flat_rule::OptionFlag_NONE,
        flat_rule::ElementType_OBJECT, flat_rule::ActivationType_NONE,
        flat_rule::UrlPatternType_SUBSTRING, flat_rule::AnchorType_NONE,
        flat_rule::AnchorType_BOUNDARY, "google.com", {"a.com"}, {"x.a.com"},
        std::nullopt, dnr_api::RuleActionType::kBlock, std::nullopt,
        std::nullopt, {}, {}, std::move(response_headers), {}));

    // Allow all requests rule matching on excluded response headers.
    std::vector<dnr_api::HeaderInfo> excluded_response_headers;
    excluded_response_headers.push_back(CreateHeaderInfo(
        "excluded-header", std::vector<std::string>({"value"}), std::nullopt));
    rules_to_index.push_back(CreateIndexedRule(
        122, 3, flat_rule::OptionFlag_NONE, flat_rule::ElementType_SUBDOCUMENT,
        flat_rule::ActivationType_NONE, flat_rule::UrlPatternType_SUBSTRING,
        flat_rule::AnchorType_NONE, flat_rule::AnchorType_NONE, "example.com",
        {}, {}, std::nullopt, dnr_api::RuleActionType::kAllowAllRequests,
        std::nullopt, std::nullopt, {}, {}, {},
        std::move(excluded_response_headers)));
  }

  // Note: It's unsafe to store/return pointers to a mutable vector since the
  // vector can resize/reallocate invalidating the existing pointers/iterators.
  // Hence we build `expected_before_request_index_lists` and
  // `expected_headers_received_index_lists` once the vector `rules_to_index` is
  // finalized.
  std::array<std::vector<const IndexedRule*>, flat::IndexType_count>
      expected_before_request_index_lists;
  expected_before_request_index_lists
      [flat::IndexType_before_request_except_allow_all_requests] = {
          &rules_to_index[0], &rules_to_index[1], &rules_to_index[2],
          &rules_to_index[3], &rules_to_index[4], &rules_to_index[5],
          &rules_to_index[6], &rules_to_index[7]};

  expected_before_request_index_lists[flat::IndexType_allow_all_requests] = {
      &rules_to_index[8]};

  expected_before_request_index_lists[flat::IndexType_modify_headers] = {
      &rules_to_index[9], &rules_to_index[10]};

  std::array<std::vector<const IndexedRule*>, flat::IndexType_count>
      expected_headers_received_index_lists;
  expected_headers_received_index_lists
      [flat::IndexType_before_request_except_allow_all_requests] = {
          &rules_to_index[11]};
  expected_headers_received_index_lists[flat::IndexType_allow_all_requests] = {
      &rules_to_index[12]};
  expected_headers_received_index_lists[flat::IndexType_modify_headers] = {};

  AddRulesAndVerifyIndex(rules_to_index, expected_before_request_index_lists,
                         expected_headers_received_index_lists);
}

// Verify that the serialized flatbuffer data is valid for regex rules.
TEST_F(FlatRulesetIndexerTest, RegexRules) {
  std::vector<IndexedRule> rules_to_index;

  // Blocking rule.
  rules_to_index.push_back(CreateIndexedRule(
      7, kMinValidPriority, flat_rule::OptionFlag_NONE,
      flat_rule::ElementType_OBJECT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_REGEXP, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, R"(^https://(abc|def))", {"a.com"},
      {"x.a.com"}, std::nullopt, dnr_api::RuleActionType::kBlock, std::nullopt,
      std::nullopt, {}, {}, {}, {}));
  // Redirect rule.
  rules_to_index.push_back(CreateIndexedRule(
      15, 2, flat_rule::OptionFlag_APPLIES_TO_FIRST_PARTY,
      flat_rule::ElementType_IMAGE, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_REGEXP, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, R"(^(http|https))", {}, {},
      "http://example1.com", dnr_api::RuleActionType::kRedirect, std::nullopt,
      std::nullopt, {}, {}, {}, {}));
  // Regex substitution rule.
  rules_to_index.push_back(CreateIndexedRule(
      10, 29, flat_rule::OptionFlag_APPLIES_TO_FIRST_PARTY,
      flat_rule::ElementType_SCRIPT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_REGEXP, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, R"((\d+\).google.com)", {}, {}, std::nullopt,
      dnr_api::RuleActionType::kRedirect, std::nullopt,
      R"(http://redirect.com?num=\1)", {}, {}, {}, {}));

  // Modify headers rule.
  std::vector<dnr_api::ModifyHeaderInfo> request_headers;
  request_headers.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kRemove, "referer", std::nullopt));
  request_headers.push_back(CreateModifyHeaderInfo(
      dnr_api::HeaderOperation::kSet, "cookie", "sample-cookie"));
  rules_to_index.push_back(CreateIndexedRule(
      21, kMinValidPriority, flat_rule::OptionFlag_IS_MATCH_CASE,
      flat_rule::ElementType_SUBDOCUMENT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_REGEXP, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, "*", {}, {}, std::nullopt,
      dnr_api::RuleActionType::kModifyHeaders, std::nullopt, std::nullopt,
      std::move(request_headers), {}, {}, {}));

  // Blocking rule that matches on response headers.
  std::vector<dnr_api::HeaderInfo> excluded_response_headers;
  excluded_response_headers.push_back(
      CreateHeaderInfo("excluded-header", std::nullopt, std::nullopt));
  rules_to_index.push_back(CreateIndexedRule(
      117, kMinValidPriority, flat_rule::OptionFlag_NONE,
      flat_rule::ElementType_OBJECT, flat_rule::ActivationType_NONE,
      flat_rule::UrlPatternType_REGEXP, flat_rule::AnchorType_NONE,
      flat_rule::AnchorType_NONE, R"(^https://(abc|def))", {"a.com"},
      {"x.a.com"}, std::nullopt, dnr_api::RuleActionType::kBlock, std::nullopt,
      std::nullopt, {}, {}, {}, std::move(excluded_response_headers)));

  flatbuffers::DetachedBuffer buffer;
  const flat::ExtensionIndexedRuleset* ruleset =
      AddRuleAndGetRuleset(rules_to_index, &buffer);
  ASSERT_TRUE(ruleset);

  // All the indices should be empty, since we only have regex rules.
  for (size_t i = 0; i < flat::IndexType_count; ++i) {
    SCOPED_TRACE(base::StringPrintf("Testing index %zu", i));
    VerifyIndexEquality({}, ruleset->before_request_index_list()->Get(i));
    VerifyIndexEquality({}, ruleset->headers_received_index_list()->Get(i));
  }

  {
    SCOPED_TRACE("Testing extension metadata");
    std::vector<const IndexedRule*> all_rules;
    for (const IndexedRule& rule : rules_to_index) {
      all_rules.push_back(&rule);
    }
    VerifyExtensionMetadata(all_rules, ruleset->extension_metadata());
  }

  ASSERT_TRUE(ruleset->before_request_regex_rules());
  ASSERT_EQ(4u, ruleset->before_request_regex_rules()->size());

  const flat::RegexRule* blocking_rule = nullptr;
  const flat::RegexRule* redirect_rule = nullptr;
  const flat::RegexRule* regex_substitution_rule = nullptr;
  const flat::RegexRule* modify_header_rule = nullptr;
  for (const auto* regex_rule : *ruleset->before_request_regex_rules()) {
    if (regex_rule->action_type() == flat::ActionType_block) {
      blocking_rule = regex_rule;
    } else if (regex_rule->action_type() == flat::ActionType_redirect) {
      if (regex_rule->regex_substitution()) {
        regex_substitution_rule = regex_rule;
      } else {
        redirect_rule = regex_rule;
      }
    } else if (regex_rule->action_type() == flat::ActionType_modify_headers) {
      modify_header_rule = regex_rule;
    }
  }

  ASSERT_TRUE(blocking_rule);
  EXPECT_TRUE(AreRulesEqual(&rules_to_index[0], blocking_rule->url_rule()));
  EXPECT_FALSE(blocking_rule->regex_substitution());

  ASSERT_TRUE(redirect_rule);
  EXPECT_TRUE(AreRulesEqual(&rules_to_index[1], redirect_rule->url_rule()));
  EXPECT_FALSE(redirect_rule->regex_substitution());

  ASSERT_TRUE(regex_substitution_rule);
  EXPECT_TRUE(
      AreRulesEqual(&rules_to_index[2], regex_substitution_rule->url_rule()));
  EXPECT_EQ(R"(http://redirect.com?num=\1)",
            ToString(regex_substitution_rule->regex_substitution()));

  ASSERT_TRUE(modify_header_rule);
  EXPECT_TRUE(
      AreRulesEqual(&rules_to_index[3], modify_header_rule->url_rule()));
  EXPECT_FALSE(modify_header_rule->regex_substitution());

  ASSERT_TRUE(ruleset->headers_received_regex_rules());
  ASSERT_EQ(1u, ruleset->headers_received_regex_rules()->size());
  const flat::RegexRule* header_matching_rule =
      ruleset->headers_received_regex_rules()->Get(0);

  ASSERT_TRUE(header_matching_rule);
  EXPECT_TRUE(
      AreRulesEqual(&rules_to_index[4], header_matching_rule->url_rule()));
  EXPECT_FALSE(header_matching_rule->regex_substitution());
}

}  // namespace
}  // namespace extensions::declarative_net_request