File: template_url_parser_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 (273 lines) | stat: -rw-r--r-- 11,263 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
// Copyright 2012 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/search_engines/template_url_parser.h"

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/common/chrome_paths.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;

// Filters any param which as an occurrence of |name_str| in its |key| or an
// occurrence of |value_str| in its |value|.
bool TestFilter(const std::string& name_str,
                const std::string& value_str,
                const std::string& key,
                const std::string& value) {
  return (name_str.empty() || key.find(name_str) == std::string::npos) &&
         (value_str.empty() || value.find(value_str) == std::string::npos);
}

class TemplateURLParserTest : public testing::Test {
 protected:
  TemplateURLParserTest();
  ~TemplateURLParserTest() override;

  void SetUp() override;

  // Parses the OpenSearch description document at file_name (relative to the
  // data dir). The TemplateURL is placed in |template_url_|.
  void ParseFile(const std::string& file_name,
                 const TemplateURLParser::ParameterFilter& filter);

  void ParseString(const std::string& data,
                   const TemplateURLParser::ParameterFilter& filter);

  // ParseFile parses the results into this template_url.
  std::unique_ptr<TemplateURL> template_url_;

 private:
  void OnTemplateURLParsed(base::OnceClosure quit_closure,
                           std::unique_ptr<TemplateURL> template_url) {
    template_url_ = std::move(template_url);
    std::move(quit_closure).Run();
  }

  base::FilePath osdd_dir_;
  base::test::TaskEnvironment task_environment_;
  data_decoder::test::InProcessDataDecoder data_decoder_;
};

TemplateURLParserTest::TemplateURLParserTest() = default;

TemplateURLParserTest::~TemplateURLParserTest() = default;

void TemplateURLParserTest::SetUp() {
  ASSERT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &osdd_dir_));
  osdd_dir_ = osdd_dir_.AppendASCII("osdd");
  ASSERT_TRUE(base::PathExists(osdd_dir_));
}

void TemplateURLParserTest::ParseFile(
    const std::string& file_name,
    const TemplateURLParser::ParameterFilter& filter) {
  base::FilePath full_path = osdd_dir_.AppendASCII(file_name);
  ASSERT_TRUE(base::PathExists(full_path));

  std::string contents;
  ASSERT_TRUE(base::ReadFileToString(full_path, &contents));
  ParseString(contents, filter);
}

void TemplateURLParserTest::ParseString(
    const std::string& data,
    const TemplateURLParser::ParameterFilter& filter) {
  base::RunLoop run_loop;
  SearchTermsData search_terms_data;
  TemplateURLParser::Parse(
      &search_terms_data, data, filter,
      base::BindOnce(&TemplateURLParserTest::OnTemplateURLParsed,
                     base::Unretained(this), run_loop.QuitClosure()));
  run_loop.Run();
}

// Actual tests ---------------------------------------------------------------

TEST_F(TemplateURLParserTest, FailOnBogusURL) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("bogus.xml", TemplateURLParser::ParameterFilter()));
  EXPECT_FALSE(template_url_);
}

TEST_F(TemplateURLParserTest, PassOnHTTPS) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("https.xml", TemplateURLParser::ParameterFilter()));
  EXPECT_TRUE(template_url_);
}

TEST_F(TemplateURLParserTest, FailOnPost) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("post.xml", TemplateURLParser::ParameterFilter()));
  EXPECT_FALSE(template_url_);
}

TEST_F(TemplateURLParserTest, TestDictionary) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("dictionary.xml", TemplateURLParser::ParameterFilter()));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Dictionary.com", template_url_->short_name());
  EXPECT_EQ(GURL("http://cache.lexico.com/g/d/favicon.ico"),
            template_url_->favicon_url());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://dictionary.reference.com/browse/{searchTerms}?r=75",
            template_url_->url());
}

TEST_F(TemplateURLParserTest, TestMSDN) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("msdn.xml", TemplateURLParser::ParameterFilter()));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Search \" MSDN", template_url_->short_name());
  EXPECT_EQ(GURL("http://search.msdn.microsoft.com/search/favicon.ico"),
            template_url_->favicon_url());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://search.msdn.microsoft.com/search/default.aspx?"
            "Query={searchTerms}&brand=msdn&locale=en-US",
            template_url_->url());
}

TEST_F(TemplateURLParserTest, TestWikipedia) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("wikipedia.xml", TemplateURLParser::ParameterFilter()));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Wikipedia (English)", template_url_->short_name());
  EXPECT_EQ(GURL("http://en.wikipedia.org/favicon.ico"),
            template_url_->favicon_url());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://en.wikipedia.org/w/index.php?"
            "title=Special:Search&search={searchTerms}",
            template_url_->url());
  EXPECT_TRUE(template_url_->suggestions_url_ref().SupportsReplacement(
      SearchTermsData()));
  EXPECT_EQ("http://en.wikipedia.org/w/api.php?"
            "action=opensearch&search={searchTerms}",
            template_url_->suggestions_url());
  ASSERT_EQ(2U, template_url_->input_encodings().size());
  EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
  EXPECT_EQ("Shift_JIS", template_url_->input_encodings()[1]);
}

TEST_F(TemplateURLParserTest, NoCrashOnEmptyAttributes) {
  ASSERT_NO_FATAL_FAILURE(ParseFile("url_with_no_attributes.xml",
                                    TemplateURLParser::ParameterFilter()));
}

TEST_F(TemplateURLParserTest, TestFirefoxEbay) {
  // This file uses the Parameter extension
  // (see http://www.opensearch.org/Specifications/OpenSearch/Extensions/Parameter/1.0)
  TemplateURLParser::ParameterFilter filter =
      base::BindRepeating(&TestFilter, "ebay", "ebay");
  ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_ebay.xml", filter));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"eBay", template_url_->short_name());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://search.ebay.com/search/search.dll?query={searchTerms}&"
            "MfcISAPICommand=GetResult&ht=1&srchdesc=n&maxRecordsReturned=300&"
            "maxRecordsPerPage=50&SortProperty=MetaEndSort",
            template_url_->url());
  ASSERT_EQ(1U, template_url_->input_encodings().size());
  EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
  EXPECT_EQ(GURL("http://search.ebay.com/favicon.ico"),
            template_url_->favicon_url());
}

TEST_F(TemplateURLParserTest, TestFirefoxWebster) {
  // This XML file uses a namespace.
  TemplateURLParser::ParameterFilter filter =
      base::BindRepeating(&TestFilter, std::string(), "Mozilla");
  ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_webster.xml", filter));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Webster", template_url_->short_name());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://www.webster.com/cgi-bin/dictionary?va={searchTerms}",
            template_url_->url());
  ASSERT_EQ(1U, template_url_->input_encodings().size());
  EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
  EXPECT_EQ(GURL("http://www.webster.com/favicon.ico"),
            template_url_->favicon_url());
}

TEST_F(TemplateURLParserTest, TestFirefoxYahoo) {
  // This XML file uses a namespace.
  TemplateURLParser::ParameterFilter filter =
      base::BindRepeating(&TestFilter, std::string(), "Mozilla");
  ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_yahoo.xml", filter));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Yahoo", template_url_->short_name());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_EQ("http://ff.search.yahoo.com/gossip?"
            "output=fxjson&command={searchTerms}",
            template_url_->suggestions_url());
  EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
            template_url_->url());
  ASSERT_EQ(1U, template_url_->input_encodings().size());
  EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
  EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
            template_url_->favicon_url());
}

// Make sure we ignore POST suggestions (this is the same XML file as
// firefox_yahoo.xml, the suggestion method was just changed to POST).
TEST_F(TemplateURLParserTest, TestPostSuggestion) {
  // This XML file uses a namespace.
  TemplateURLParser::ParameterFilter filter =
      base::BindRepeating(&TestFilter, std::string(), "Mozilla");
  ASSERT_NO_FATAL_FAILURE(ParseFile("post_suggestion.xml", filter));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Yahoo", template_url_->short_name());
  EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
  EXPECT_TRUE(template_url_->suggestions_url().empty());
  EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
            template_url_->url());
  ASSERT_EQ(1U, template_url_->input_encodings().size());
  EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
  EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
            template_url_->favicon_url());
}

// <Alias> tags are parsed and used as keyword for the template URL.
TEST_F(TemplateURLParserTest, TestKeyword) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("keyword.xml", TemplateURLParser::ParameterFilter()));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Example", template_url_->short_name());
  EXPECT_EQ("https://www.example.com/search?q={searchTerms}",
            template_url_->url());
  EXPECT_EQ(u"moose", template_url_->keyword());
}

// Empty <Alias> tags are ignored and the default keyword is used instead
// (because empty keywords are not allowed).
TEST_F(TemplateURLParserTest, TestEmptyKeyword) {
  ASSERT_NO_FATAL_FAILURE(
      ParseFile("empty_keyword.xml", TemplateURLParser::ParameterFilter()));
  ASSERT_TRUE(template_url_);
  EXPECT_EQ(u"Example", template_url_->short_name());
  EXPECT_EQ("https://www.example.com/search?q={searchTerms}",
            template_url_->url());
  EXPECT_EQ(u"example.com", template_url_->keyword());
}

// An invalid template URL should not crash the parser.
// crbug.com/770734
TEST_F(TemplateURLParserTest, InvalidInput) {
  TemplateURLParser::ParameterFilter filter = base::BindRepeating(
      [](const std::string&, const std::string&) -> bool { return true; });
  constexpr char char_data[] = R"(
    <OpenSearchDescription>
    <Url template=")R:RRR?>RRR0" type="application/x-suggestions+json">
      <Param name="name" value="value"/>
    </Url>
    </OpenSearchDescription>
  )";
  ParseString(char_data, filter);
}