File: ieem_sitelist_parser_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (233 lines) | stat: -rw-r--r-- 10,021 bytes parent folder | download | duplicates (4)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browser_switcher/ieem_sitelist_parser.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "services/data_decoder/public/cpp/safe_xml_parser.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browser_switcher {

namespace {

void OnXmlParsed(base::RepeatingClosure quit_run_loop,
                 ParsedXml expected,
                 ParsedXml actual) {
  base::ScopedClosureRunner runner(std::move(quit_run_loop));
  EXPECT_EQ(expected.rules.sitelist, actual.rules.sitelist);
  EXPECT_EQ(expected.rules.greylist, actual.rules.greylist);
  EXPECT_EQ(expected.error.has_value(), actual.error.has_value());
  if (expected.error.has_value() && actual.error.has_value()) {
    // The actual error may have more detail at the end. Ensure it starts with
    // the expected error.
    EXPECT_THAT(*actual.error, testing::StartsWith(*expected.error));
  }
}

}  // namespace

class IeemSitelistParserTest : public testing::TestWithParam<ParsingMode> {
 public:
  ~IeemSitelistParserTest() override = default;

  ParsingMode parsing_mode() { return GetParam(); }

  void TestParseXml(const std::string& xml, ParsedXml expected) {
    base::RunLoop run_loop;
    ParseIeemXml(xml, parsing_mode(),
                 base::BindOnce(&OnXmlParsed, run_loop.QuitClosure(),
                                std::move(expected)));
    run_loop.Run();
  }

 private:
  base::test::SingleThreadTaskEnvironment task_environment_;
  data_decoder::test::InProcessDataDecoder data_decoder_;
};

TEST_P(IeemSitelistParserTest, BadXml) {
  TestParseXml("", ParsedXml({}, {}, "Invalid XML: bad content"));
  TestParseXml("thisisnotxml", ParsedXml({}, {}, "Invalid XML: bad content"));
}

TEST_P(IeemSitelistParserTest, BadXmlParsed) {
  TestParseXml("<bogus></bogus>",
               ParsedXml({}, {}, "Invalid XML root element"));
  TestParseXml("<rules version=\"424\"><unknown></unknown></rules>",
               ParsedXml({}, {}, std::nullopt));
}

TEST_P(IeemSitelistParserTest, V1OnlyBogusElements) {
  std::string xml =
      "<rules version=\"424\">"
      "<unknown><more><docMode><domain>ignore.com</domain></docMode>"
      "</more><emie><domain>ignoretoo.com<path>/ignored_path</path>"
      "</domain></emie><domain>onemoreignored.com</domain>"
      "<path>/ignore_outside_of_domain></path></unknown></rules>";
  TestParseXml(xml, ParsedXml({}, {}, std::nullopt));
}

TEST_P(IeemSitelistParserTest, V1Full) {
  std::string xml =
      "<rules version=\"424\"><unknown><more><docMode><domain>ignore"
      "</domain></docMode></more><emie><domain>ignoretoo.com<path>/ignored_path"
      "</path></domain></emie><domain>onemoreingored.com</domain><path>"
      "/ignore_outside_of_domain></path></unknown><emie><other><more><docMode>"
      "<domain>ignore.com</domain></docMode></more><emie><domain>ignoretoo.com"
      "<path>/ignored_path</path></domain></emie><domain>onemoreingored.com"
      "</domain><path>/ignore_outside_of_domain></path></other><!--<domain "
      "exclude=\"false\">hotscanacc.dbch.b-source.net<path exclude=\"false\">"
      "/HotScan/</path></domain>--><domain>inside.com<more><docMode><domain>"
      "ignore.com</domain></docMode></more><emie><domain>ignoretoo.com<path>"
      "/ignored_path</path></domain></emie><domain>onemoreingored.com</domain>"
      "<path>/in_domain<more><docMode><domain>ignore.com</domain></docMode>"
      "</more><emie><domain>ignoretoo.com<path>/ignored_path</path></domain>"
      "</emie><domain>onemoreingored.com</domain><path>/ignore_nested_path>"
      "</path></path></domain><domain>   \ngoogle.com\t\t \t</domain><domain "
      "exclude=\"true\">good.com</domain><domain exclude=\"false\">more.com"
      "</domain><domain>e100.com<path>/path1</path><path exclude=\"true\">/pa2"
      "</path><path exclude=\"false\">/path3</path></domain><domain "
      "exclude=\"true\">e200.com<path>/path1</path><path exclude=\"true\">/pth2"
      "</path><path exclude=\"false\">/path3</path></domain><domain "
      "exclude=\"false\">e300.com<path>/path1</path><path exclude=\"true\">/pt2"
      "</path><path exclude=\"false\">/path3</path></domain><domain "
      "exclude=\"true\">random.com<path exclude=\"true\">/path1/</path><path "
      "exclude=\"false\" forceCompatView=\"true\">/path2<path exclude=\"true\">"
      "/TEST</path></path></domain></emie><docMode><domain docMode=\"8\">"
      "moredomains.com</domain><domain docMode=\"5\">evenmore.com<path "
      "docMode=\"5\">/r1</path><path docMode=\"5\">/r2</path></domain><domain "
      "docMode=\"5\" exclude=\"true\">domainz.com<path docMode=\"5\">/r2</path>"
      "<path docMode=\"5\" exclude=\"true\"> \n/r5\t</path><path docMode=\"5\" "
      "exclude=\"false\">/r6</path></domain><domain docMode=\"5\" "
      "exclude=\"false\">howmanydomainz.com<path docMode=\"5\">/r8</path><path "
      "docMode=\"5\" exclude=\"true\">/r9</path><path docMode=\"5\" "
      "exclude=\"false\">/r10</path></domain><domain exclude=\"true\" "
      "doNotTransition=\"true\">maybe.com<path>/yestransition</path>"
      "<path doNotTransition=\"true\">/guessnot</path></domain><domain>"
      "yes.com<path doNotTransition=\"true\">/actuallyno</path></domain>"
      "<domain doNotTransition=\"true\">no.com</domain></docMode></rules>";
  std::vector<std::string> expected_sitelist;
  std::vector<std::string> expected_greylist;
  if (parsing_mode() == ParsingMode::kIESiteListMode) {
    expected_sitelist = {
        "inside.com",
        "inside.com/in_domain",
        "google.com",
        "more.com",
        "e100.com",
        "e100.com/path1",
        "e100.com/path3",
        "e200.com/path1",
        "e200.com/path3",
        "e300.com",
        "e300.com/path1",
        "e300.com/path3",
        "random.com/path2",
        "moredomains.com",
        "evenmore.com",
        "evenmore.com/r1",
        "evenmore.com/r2",
        "domainz.com/r2",
        "domainz.com/r6",
        "howmanydomainz.com",
        "howmanydomainz.com/r8",
        "howmanydomainz.com/r10",
        "maybe.com/yestransition",
        "yes.com",
    };
    expected_greylist = {
        "maybe.com/guessnot",
        "yes.com/actuallyno",
        "no.com",
    };
  } else {
    expected_sitelist = {
        "inside.com",
        "inside.com/in_domain",
        "google.com",
        "more.com",
        "e100.com",
        "e100.com/path1",
        "e100.com/path3",
        "e200.com/path1",
        "e200.com/path3",
        "e300.com",
        "e300.com/path1",
        "e300.com/path3",
        "random.com/path2",
        "moredomains.com",
        "evenmore.com",
        "evenmore.com/r1",
        "evenmore.com/r2",
        "domainz.com/r2",
        "domainz.com/r6",
        "howmanydomainz.com",
        "howmanydomainz.com/r8",
        "howmanydomainz.com/r10",
        "maybe.com/yestransition",
        "!maybe.com/guessnot",
        "yes.com",
        "!yes.com/actuallyno",
        "!no.com",
    };
  }
  TestParseXml(xml, ParsedXml(std::move(expected_sitelist),
                              std::move(expected_greylist), std::nullopt));
}

TEST_P(IeemSitelistParserTest, V2Full) {
  // Very subtle issue in the closing element for rules.
  std::string xml =
      "<site-list version=\"205\"><!-- File creation header -->"
      "<created-by><tool>EnterpriseSitelistManager</tool><version>10240"
      "</version><date-created>20150728.135021</date-created></created-by>"
      "<!-- unknown tags --><unknown><test><mest>test</mest></test>"
      "<!-- comments --></unknown><!-- no url attrib --><site><open-in>none"
      "</open-in></site><!-- nested site list --><site-list><site "
      "url=\"ignore!\"/></site-list><!-- nested site --><site "
      "url=\"google.com\"><site url=\"nested ignore!\"></site></site><!-- "
      "unknown tags in a site on multiple levels --><site url=\"good.site\">"
      "<!-- nested comments --><somethings>klj<other some=\"none\"/>jkh"
      "</somethings></site><!-- good sites --> <site url=\"www.cpandl.com\">"
      "<compat-mode>IE8Enterprise</compat-mode><open-in>MSEdge</open-in></site>"
      "<site url=\"contoso.com\"><compat-mode>default</compat-mode><open-in>"
      "None</open-in></site><site url=\"relecloud.com\"/><site "
      "url=\"relecloud.com/about\"><compat-mode>IE8Enterprise</compat-mode>"
      "</site></site-list><!-- trailing gibberish <trailing><site "
      "url=\"ignore after site list!\">  <compat-mode>IE8Enterprise\""
      "</compat-mode></site><gibberish>Lorem ipsum sit...</gibberish>"
      "</trailing>-->";
  std::vector<std::string> expected_sitelist;
  std::vector<std::string> expected_greylist;
  if (parsing_mode() == ParsingMode::kIESiteListMode) {
    expected_sitelist = {"!www.cpandl.com"};
    expected_greylist = {
        "google.com",    "good.site",           "contoso.com",
        "relecloud.com", "relecloud.com/about",
    };
  } else {
    expected_sitelist = {
        "!google.com",  "!good.site",     "www.cpandl.com",
        "!contoso.com", "!relecloud.com", "!relecloud.com/about",
    };
  }
  TestParseXml(xml, ParsedXml(std::move(expected_sitelist),
                              std::move(expected_greylist), std::nullopt));
}

INSTANTIATE_TEST_SUITE_P(ParsingMode,
                         IeemSitelistParserTest,
                         testing::Values(ParsingMode::kDefault,
                                         ParsingMode::kIESiteListMode,
                                         // 999 should behave like kDefault.
                                         static_cast<ParsingMode>(999)));

}  // namespace browser_switcher