[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_parser_unittest.cc
blob127f7486336ebe18ab2cd7cfae678f57273a69ee
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/logging.h"
7 #include "base/path_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/search_engines/template_url.h"
10 #include "chrome/browser/search_engines/template_url_parser.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16;
16 // ParamFilterImpl ------------------------------------------------------------
18 // Filters any param which as an occurrence of name_str_ in its name or an
19 // occurrence of value_str_ in its value.
20 class ParamFilterImpl : public TemplateURLParser::ParameterFilter {
21 public:
22 ParamFilterImpl(std::string name_str, std::string value_str);
23 virtual ~ParamFilterImpl();
25 virtual bool KeepParameter(const std::string& key,
26 const std::string& value) OVERRIDE;
28 private:
29 std::string name_str_;
30 std::string value_str_;
32 DISALLOW_COPY_AND_ASSIGN(ParamFilterImpl);
35 ParamFilterImpl::ParamFilterImpl(std::string name_str, std::string value_str)
36 : name_str_(name_str),
37 value_str_(value_str) {
40 ParamFilterImpl::~ParamFilterImpl() {
43 bool ParamFilterImpl::KeepParameter(const std::string& key,
44 const std::string& value) {
45 return (name_str_.empty() || key.find(name_str_) == std::string::npos) &&
46 (value_str_.empty() || value.find(value_str_) == std::string::npos);
50 // TemplateURLParserTest ------------------------------------------------------
52 class TemplateURLParserTest : public testing::Test {
53 protected:
54 TemplateURLParserTest();
55 virtual ~TemplateURLParserTest();
57 virtual void SetUp() OVERRIDE;
59 bool is_disabled() const;
61 // Parses the OpenSearch description document at file_name (relative to the
62 // data dir). The TemplateURL is placed in |template_url_|.
63 void ParseFile(const std::string& file_name,
64 TemplateURLParser::ParameterFilter* filter);
66 // ParseFile parses the results into this template_url.
67 scoped_ptr<TemplateURL> template_url_;
69 private:
70 base::FilePath full_path_;
73 TemplateURLParserTest::TemplateURLParserTest() {
76 TemplateURLParserTest::~TemplateURLParserTest() {
79 void TemplateURLParserTest::SetUp() {
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path_));
81 full_path_ = full_path_.AppendASCII("osdd");
82 if (!base::PathExists(full_path_)) {
83 LOG(ERROR) <<
84 "This test can't be run without some non-redistributable data";
85 full_path_ = base::FilePath();
89 bool TemplateURLParserTest::is_disabled() const {
90 return full_path_.empty();
93 void TemplateURLParserTest::ParseFile(
94 const std::string& file_name,
95 TemplateURLParser::ParameterFilter* filter) {
96 base::FilePath full_path;
97 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path));
98 full_path = full_path.AppendASCII("osdd");
99 full_path = full_path.AppendASCII(file_name);
100 ASSERT_TRUE(base::PathExists(full_path));
102 std::string contents;
103 ASSERT_TRUE(base::ReadFileToString(full_path, &contents));
104 template_url_.reset(TemplateURLParser::Parse(NULL, false, contents.data(),
105 contents.length(), filter));
109 // Actual tests ---------------------------------------------------------------
111 TEST_F(TemplateURLParserTest, FailOnBogusURL) {
112 if (is_disabled())
113 return;
114 ASSERT_NO_FATAL_FAILURE(ParseFile("bogus.xml", NULL));
115 EXPECT_FALSE(template_url_.get());
118 TEST_F(TemplateURLParserTest, PassOnHTTPS) {
119 if (is_disabled())
120 return;
121 ASSERT_NO_FATAL_FAILURE(ParseFile("https.xml", NULL));
122 EXPECT_TRUE(template_url_.get());
125 TEST_F(TemplateURLParserTest, FailOnPost) {
126 if (is_disabled())
127 return;
128 ASSERT_NO_FATAL_FAILURE(ParseFile("post.xml", NULL));
129 EXPECT_FALSE(template_url_.get());
132 TEST_F(TemplateURLParserTest, TestDictionary) {
133 if (is_disabled())
134 return;
135 ASSERT_NO_FATAL_FAILURE(ParseFile("dictionary.xml", NULL));
136 ASSERT_TRUE(template_url_.get());
137 EXPECT_EQ(ASCIIToUTF16("Dictionary.com"), template_url_->short_name());
138 EXPECT_EQ(GURL("http://cache.lexico.com/g/d/favicon.ico"),
139 template_url_->favicon_url());
140 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
141 EXPECT_EQ("http://dictionary.reference.com/browse/{searchTerms}?r=75",
142 template_url_->url());
145 TEST_F(TemplateURLParserTest, TestMSDN) {
146 if (is_disabled())
147 return;
148 ASSERT_NO_FATAL_FAILURE(ParseFile("msdn.xml", NULL));
149 ASSERT_TRUE(template_url_.get());
150 EXPECT_EQ(ASCIIToUTF16("Search \" MSDN"), template_url_->short_name());
151 EXPECT_EQ(GURL("http://search.msdn.microsoft.com/search/favicon.ico"),
152 template_url_->favicon_url());
153 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
154 EXPECT_EQ("http://search.msdn.microsoft.com/search/default.aspx?"
155 "Query={searchTerms}&brand=msdn&locale=en-US",
156 template_url_->url());
159 TEST_F(TemplateURLParserTest, TestWikipedia) {
160 if (is_disabled())
161 return;
162 ASSERT_NO_FATAL_FAILURE(ParseFile("wikipedia.xml", NULL));
163 ASSERT_TRUE(template_url_.get());
164 EXPECT_EQ(ASCIIToUTF16("Wikipedia (English)"), template_url_->short_name());
165 EXPECT_EQ(GURL("http://en.wikipedia.org/favicon.ico"),
166 template_url_->favicon_url());
167 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
168 EXPECT_EQ("http://en.wikipedia.org/w/index.php?"
169 "title=Special:Search&search={searchTerms}",
170 template_url_->url());
171 EXPECT_TRUE(template_url_->suggestions_url_ref().SupportsReplacement());
172 EXPECT_EQ("http://en.wikipedia.org/w/api.php?"
173 "action=opensearch&search={searchTerms}",
174 template_url_->suggestions_url());
175 ASSERT_EQ(2U, template_url_->input_encodings().size());
176 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
177 EXPECT_EQ("Shift_JIS", template_url_->input_encodings()[1]);
180 TEST_F(TemplateURLParserTest, NoCrashOnEmptyAttributes) {
181 if (is_disabled())
182 return;
183 ASSERT_NO_FATAL_FAILURE(ParseFile("url_with_no_attributes.xml", NULL));
186 TEST_F(TemplateURLParserTest, TestFirefoxEbay) {
187 if (is_disabled())
188 return;
189 // This file uses the Parameter extension
190 // (see http://www.opensearch.org/Specifications/OpenSearch/Extensions/Parameter/1.0)
191 ParamFilterImpl filter("ebay", "ebay");
192 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_ebay.xml", &filter));
193 ASSERT_TRUE(template_url_.get());
194 EXPECT_EQ(ASCIIToUTF16("eBay"), template_url_->short_name());
195 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
196 EXPECT_EQ("http://search.ebay.com/search/search.dll?query={searchTerms}&"
197 "MfcISAPICommand=GetResult&ht=1&srchdesc=n&maxRecordsReturned=300&"
198 "maxRecordsPerPage=50&SortProperty=MetaEndSort",
199 template_url_->url());
200 ASSERT_EQ(1U, template_url_->input_encodings().size());
201 EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
202 EXPECT_EQ(GURL("http://search.ebay.com/favicon.ico"),
203 template_url_->favicon_url());
206 TEST_F(TemplateURLParserTest, TestFirefoxWebster) {
207 if (is_disabled())
208 return;
209 // This XML file uses a namespace.
210 ParamFilterImpl filter(std::string(), "Mozilla");
211 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_webster.xml", &filter));
212 ASSERT_TRUE(template_url_.get());
213 EXPECT_EQ(ASCIIToUTF16("Webster"), template_url_->short_name());
214 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
215 EXPECT_EQ("http://www.webster.com/cgi-bin/dictionary?va={searchTerms}",
216 template_url_->url());
217 ASSERT_EQ(1U, template_url_->input_encodings().size());
218 EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
219 EXPECT_EQ(GURL("http://www.webster.com/favicon.ico"),
220 template_url_->favicon_url());
223 TEST_F(TemplateURLParserTest, TestFirefoxYahoo) {
224 if (is_disabled())
225 return;
226 // This XML file uses a namespace.
227 ParamFilterImpl filter(std::string(), "Mozilla");
228 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_yahoo.xml", &filter));
229 ASSERT_TRUE(template_url_.get());
230 EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name());
231 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
232 EXPECT_EQ("http://ff.search.yahoo.com/gossip?"
233 "output=fxjson&command={searchTerms}",
234 template_url_->suggestions_url());
235 EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
236 template_url_->url());
237 ASSERT_EQ(1U, template_url_->input_encodings().size());
238 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
239 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
240 template_url_->favicon_url());
243 // Make sure we ignore POST suggestions (this is the same XML file as
244 // firefox_yahoo.xml, the suggestion method was just changed to POST).
245 TEST_F(TemplateURLParserTest, TestPostSuggestion) {
246 if (is_disabled())
247 return;
248 // This XML file uses a namespace.
249 ParamFilterImpl filter(std::string(), "Mozilla");
250 ASSERT_NO_FATAL_FAILURE(ParseFile("post_suggestion.xml", &filter));
251 ASSERT_TRUE(template_url_.get());
252 EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name());
253 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement());
254 EXPECT_TRUE(template_url_->suggestions_url().empty());
255 EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
256 template_url_->url());
257 ASSERT_EQ(1U, template_url_->input_encodings().size());
258 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
259 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
260 template_url_->favicon_url());