OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/matcher/url_matcher.h" | 5 #include "extensions/common/matcher/url_matcher.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_NE(old_id_4, c7.string_pattern()->id()); | 240 EXPECT_NE(old_id_4, c7.string_pattern()->id()); |
241 } | 241 } |
242 | 242 |
243 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { | 243 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { |
244 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" | 244 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8" |
245 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | 245 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
246 URLMatcherConditionFactory factory; | 246 URLMatcherConditionFactory factory; |
247 std::string url = factory.CanonicalizeURLForComponentSearches(gurl); | 247 std::string url = factory.CanonicalizeURLForComponentSearches(gurl); |
248 | 248 |
249 // Test host component. | 249 // Test host component. |
250 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(""), url)); | 250 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url)); |
251 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url)); | 251 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url)); |
252 EXPECT_TRUE( | 252 EXPECT_TRUE( |
253 Matches(factory.CreateHostPrefixCondition("www.google.com"), url)); | 253 Matches(factory.CreateHostPrefixCondition("www.google.com"), url)); |
254 EXPECT_TRUE( | 254 EXPECT_TRUE( |
255 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url)); | 255 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url)); |
256 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url)); | 256 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url)); |
257 EXPECT_FALSE( | 257 EXPECT_FALSE( |
258 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url)); | 258 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url)); |
259 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url)); | 259 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url)); |
260 | 260 |
261 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(""), url)); | 261 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url)); |
262 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url)); | 262 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url)); |
263 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url)); | 263 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url)); |
264 EXPECT_TRUE( | 264 EXPECT_TRUE( |
265 Matches(factory.CreateHostSuffixCondition("www.google.com"), url)); | 265 Matches(factory.CreateHostSuffixCondition("www.google.com"), url)); |
266 EXPECT_TRUE( | 266 EXPECT_TRUE( |
267 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url)); | 267 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url)); |
268 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url)); | 268 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url)); |
269 EXPECT_FALSE( | 269 EXPECT_FALSE( |
270 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url)); | 270 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url)); |
271 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url)); | 271 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url)); |
272 | 272 |
273 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(""), url)); | 273 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(std::string()), url)); |
274 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url)); | 274 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url)); |
275 EXPECT_TRUE( | 275 EXPECT_TRUE( |
276 Matches(factory.CreateHostEqualsCondition("www.google.com"), url)); | 276 Matches(factory.CreateHostEqualsCondition("www.google.com"), url)); |
277 EXPECT_FALSE( | 277 EXPECT_FALSE( |
278 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url)); | 278 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url)); |
279 | 279 |
280 | 280 |
281 // Test path component. | 281 // Test path component. |
282 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(""), url)); | 282 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url)); |
283 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url)); | 283 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url)); |
284 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url)); | 284 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url)); |
285 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url)); | 285 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url)); |
286 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url)); | 286 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url)); |
287 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url)); | 287 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url)); |
288 | 288 |
289 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(""), url)); | 289 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(std::string()), url)); |
290 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url)); | 290 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url)); |
291 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url)); | 291 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url)); |
292 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url)); | 292 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url)); |
293 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url)); | 293 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url)); |
294 | 294 |
295 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url)); | 295 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url)); |
296 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url)); | 296 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url)); |
297 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url)); | 297 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url)); |
298 EXPECT_FALSE( | 298 EXPECT_FALSE( |
299 Matches(factory.CreatePathEqualsCondition("www.google.com"), url)); | 299 Matches(factory.CreatePathEqualsCondition("www.google.com"), url)); |
300 | 300 |
301 | 301 |
302 // Test query component. | 302 // Test query component. |
303 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(""), url)); | 303 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(std::string()), url)); |
304 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url)); | 304 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url)); |
305 // The '?' at the beginning is just ignored. | 305 // The '?' at the beginning is just ignored. |
306 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url)); | 306 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url)); |
307 | 307 |
308 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(""), url)); | 308 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(std::string()), url)); |
309 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url)); | 309 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url)); |
310 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url)); | 310 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url)); |
311 // "Suffix" condition + pattern starting with '?' = "equals" condition. | 311 // "Suffix" condition + pattern starting with '?' = "equals" condition. |
312 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition( | 312 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition( |
313 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); | 313 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
314 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition( | 314 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition( |
315 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | 315 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
316 | 316 |
317 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( | 317 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( |
318 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); | 318 "?sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
319 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( | 319 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition( |
320 "sourceid=chrome-instant&ie=UTF-8&ion="), url)); | 320 "sourceid=chrome-instant&ie=UTF-8&ion="), url)); |
321 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( | 321 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( |
322 "sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | 322 "sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
323 // The '?' at the beginning is just ignored. | 323 // The '?' at the beginning is just ignored. |
324 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( | 324 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition( |
325 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); | 325 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); |
326 EXPECT_FALSE( | 326 EXPECT_FALSE( |
327 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url)); | 327 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url)); |
328 | 328 |
329 | 329 |
330 // Test adjacent components | 330 // Test adjacent components |
331 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | 331 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( |
332 "google.com", "/webhp"), url)); | 332 "google.com", "/webhp"), url)); |
333 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | 333 EXPECT_TRUE(Matches( |
334 "", "/webhp"), url)); | 334 factory.CreateHostSuffixPathPrefixCondition(std::string(), "/webhp"), |
335 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition( | 335 url)); |
336 "google.com", ""), url)); | 336 EXPECT_TRUE(Matches( |
337 EXPECT_FALSE(Matches(factory.CreateHostSuffixPathPrefixCondition( | 337 factory.CreateHostSuffixPathPrefixCondition("google.com", std::string()), |
338 "www", ""), url)); | 338 url)); |
| 339 EXPECT_FALSE(Matches( |
| 340 factory.CreateHostSuffixPathPrefixCondition("www", std::string()), url)); |
339 | 341 |
340 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( | 342 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
341 "www.google.com", "/webhp"), url)); | 343 "www.google.com", "/webhp"), url)); |
342 EXPECT_FALSE(Matches(factory.CreateHostEqualsPathPrefixCondition( | 344 EXPECT_FALSE(Matches( |
343 "", "/webhp"), url)); | 345 factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"), |
| 346 url)); |
344 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( | 347 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition( |
345 "www.google.com", ""), url)); | 348 "www.google.com", std::string()), |
346 EXPECT_FALSE(Matches(factory.CreateHostEqualsPathPrefixCondition( | 349 url)); |
347 "google.com", ""), url)); | 350 EXPECT_FALSE(Matches( |
| 351 factory.CreateHostEqualsPathPrefixCondition("google.com", std::string()), |
| 352 url)); |
348 } | 353 } |
349 | 354 |
350 TEST(URLMatcherConditionFactoryTest, TestFullSearches) { | 355 TEST(URLMatcherConditionFactoryTest, TestFullSearches) { |
351 // The Port 443 is stripped because it is the default port for https. | 356 // The Port 443 is stripped because it is the default port for https. |
352 GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8" | 357 GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8" |
353 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); | 358 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); |
354 URLMatcherConditionFactory factory; | 359 URLMatcherConditionFactory factory; |
355 std::string url = factory.CanonicalizeURLForFullSearches(gurl); | 360 std::string url = factory.CanonicalizeURLForFullSearches(gurl); |
356 | 361 |
357 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url)); | 362 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(std::string()), url)); |
358 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | 363 EXPECT_TRUE( |
359 "https://www.goog"), url)); | 364 Matches(factory.CreateURLPrefixCondition("https://www.goog"), url)); |
360 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | 365 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( |
361 "https://www.google.com"), url)); | 366 "https://www.google.com"), url)); |
362 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( | 367 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition( |
363 "https://www.google.com/webhp?"), url)); | 368 "https://www.google.com/webhp?"), url)); |
364 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition( | 369 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition( |
365 "http://www.google.com"), url)); | 370 "http://www.google.com"), url)); |
366 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url)); | 371 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url)); |
367 | 372 |
368 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url)); | 373 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(std::string()), url)); |
369 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url)); | 374 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url)); |
370 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url)); | 375 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url)); |
371 | 376 |
372 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(""), url)); | 377 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(std::string()), url)); |
373 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url)); | 378 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url)); |
374 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url)); | 379 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url)); |
375 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url)); | 380 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url)); |
376 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url)); | 381 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url)); |
377 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url)); | 382 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url)); |
378 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url)); | 383 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url)); |
379 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url)); | 384 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url)); |
380 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url)); | 385 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url)); |
381 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url)); | 386 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url)); |
382 | 387 |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 | 625 |
621 const int kConditionSetId = 1; | 626 const int kConditionSetId = 1; |
622 URLMatcherConditionSet::Vector insert; | 627 URLMatcherConditionSet::Vector insert; |
623 insert.push_back(make_scoped_refptr( | 628 insert.push_back(make_scoped_refptr( |
624 new URLMatcherConditionSet(kConditionSetId, conditions))); | 629 new URLMatcherConditionSet(kConditionSetId, conditions))); |
625 matcher.AddConditionSets(insert); | 630 matcher.AddConditionSets(insert); |
626 EXPECT_EQ(1u, matcher.MatchURL(url).size()); | 631 EXPECT_EQ(1u, matcher.MatchURL(url).size()); |
627 } | 632 } |
628 | 633 |
629 } // namespace extensions | 634 } // namespace extensions |
OLD | NEW |