Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: chrome/browser/extensions/api/declarative/url_matcher_unittest.cc

Issue 10052035: Implemented port filter for URLMatcher (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comment Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/extensions/api/declarative/url_matcher.h" 5 #include "chrome/browser/extensions/api/declarative/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 27 matching lines...) Expand all
38 URLMatcherSchemeFilter filter2(filter2_content); 38 URLMatcherSchemeFilter filter2(filter2_content);
39 39
40 GURL matching_url("https://www.foobar.com"); 40 GURL matching_url("https://www.foobar.com");
41 GURL non_matching_url("http://www.foobar.com"); 41 GURL non_matching_url("http://www.foobar.com");
42 EXPECT_TRUE(filter1.IsMatch(matching_url)); 42 EXPECT_TRUE(filter1.IsMatch(matching_url));
43 EXPECT_FALSE(filter1.IsMatch(non_matching_url)); 43 EXPECT_FALSE(filter1.IsMatch(non_matching_url));
44 EXPECT_TRUE(filter2.IsMatch(matching_url)); 44 EXPECT_TRUE(filter2.IsMatch(matching_url));
45 EXPECT_TRUE(filter2.IsMatch(non_matching_url)); 45 EXPECT_TRUE(filter2.IsMatch(non_matching_url));
46 } 46 }
47 47
48 TEST(URLMatcherPortFilter, TestMatching) {
49 std::vector<URLMatcherPortFilter::Range> ranges;
50 ranges.push_back(URLMatcherPortFilter::CreateRange(80, 90));
51 ranges.push_back(URLMatcherPortFilter::CreateRange(8080));
52 URLMatcherPortFilter filter(ranges);
53 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com")));
54 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80")));
55 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81")));
56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90")));
57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080")));
58 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79")));
59 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91")));
60 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com")));
61 }
62
48 TEST(URLMatcherConditionTest, IsFullURLCondition) { 63 TEST(URLMatcherConditionTest, IsFullURLCondition) {
49 SubstringPattern pattern("example.com", 1); 64 SubstringPattern pattern("example.com", 1);
50 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, 65 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
51 &pattern).IsFullURLCondition()); 66 &pattern).IsFullURLCondition());
52 67
53 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS, 68 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
54 &pattern).IsFullURLCondition()); 69 &pattern).IsFullURLCondition());
55 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS, 70 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
56 &pattern).IsFullURLCondition()); 71 &pattern).IsFullURLCondition());
57 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS, 72 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 325
311 scoped_refptr<URLMatcherConditionSet> condition_set( 326 scoped_refptr<URLMatcherConditionSet> condition_set(
312 new URLMatcherConditionSet(1, conditions)); 327 new URLMatcherConditionSet(1, conditions));
313 EXPECT_EQ(1, condition_set->id()); 328 EXPECT_EQ(1, condition_set->id());
314 EXPECT_EQ(2u, condition_set->conditions().size()); 329 EXPECT_EQ(2u, condition_set->conditions().size());
315 } 330 }
316 331
317 TEST(URLMatcherConditionSetTest, Matching) { 332 TEST(URLMatcherConditionSetTest, Matching) {
318 GURL url1("http://www.example.com/foo?bar=1"); 333 GURL url1("http://www.example.com/foo?bar=1");
319 GURL url2("http://foo.example.com/index.html"); 334 GURL url2("http://foo.example.com/index.html");
335 GURL url3("http://www.example.com:80/foo?bar=1");
336 GURL url4("http://www.example.com:8080/foo?bar=1");
320 337
321 URLMatcherConditionFactory factory; 338 URLMatcherConditionFactory factory;
322 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); 339 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
323 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); 340 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
324 341
325 std::set<URLMatcherCondition> conditions; 342 std::set<URLMatcherCondition> conditions;
326 conditions.insert(m1); 343 conditions.insert(m1);
327 conditions.insert(m2); 344 conditions.insert(m2);
328 345
329 scoped_refptr<URLMatcherConditionSet> condition_set( 346 scoped_refptr<URLMatcherConditionSet> condition_set(
330 new URLMatcherConditionSet(1, conditions)); 347 new URLMatcherConditionSet(1, conditions));
331 EXPECT_EQ(1, condition_set->id()); 348 EXPECT_EQ(1, condition_set->id());
332 EXPECT_EQ(2u, condition_set->conditions().size()); 349 EXPECT_EQ(2u, condition_set->conditions().size());
333 350
334 std::set<SubstringPattern::ID> matching_substring_patterns; 351 std::set<SubstringPattern::ID> matching_substring_patterns;
335 matching_substring_patterns.insert(m1.substring_pattern()->id()); 352 matching_substring_patterns.insert(m1.substring_pattern()->id());
336 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1)); 353 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url1));
337 354
338 matching_substring_patterns.insert(m2.substring_pattern()->id()); 355 matching_substring_patterns.insert(m2.substring_pattern()->id());
339 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1)); 356 EXPECT_TRUE(condition_set->IsMatch(matching_substring_patterns, url1));
340 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2)); 357 EXPECT_FALSE(condition_set->IsMatch(matching_substring_patterns, url2));
341 358
342
343 // Test scheme filters. 359 // Test scheme filters.
344 scoped_refptr<URLMatcherConditionSet> condition_set2( 360 scoped_refptr<URLMatcherConditionSet> condition_set2(
345 new URLMatcherConditionSet(1, conditions, 361 new URLMatcherConditionSet(1, conditions,
346 scoped_ptr<URLMatcherSchemeFilter>( 362 scoped_ptr<URLMatcherSchemeFilter>(
347 new URLMatcherSchemeFilter("https")))); 363 new URLMatcherSchemeFilter("https")),
364 scoped_ptr<URLMatcherPortFilter>(NULL)));
348 EXPECT_FALSE(condition_set2->IsMatch(matching_substring_patterns, url1)); 365 EXPECT_FALSE(condition_set2->IsMatch(matching_substring_patterns, url1));
349 scoped_refptr<URLMatcherConditionSet> condition_set3( 366 scoped_refptr<URLMatcherConditionSet> condition_set3(
350 new URLMatcherConditionSet(1, conditions, 367 new URLMatcherConditionSet(1, conditions,
351 scoped_ptr<URLMatcherSchemeFilter>( 368 scoped_ptr<URLMatcherSchemeFilter>(
352 new URLMatcherSchemeFilter("http")))); 369 new URLMatcherSchemeFilter("http")),
370 scoped_ptr<URLMatcherPortFilter>(NULL)));
353 EXPECT_TRUE(condition_set3->IsMatch(matching_substring_patterns, url1)); 371 EXPECT_TRUE(condition_set3->IsMatch(matching_substring_patterns, url1));
372
373 // Test port filters.
374 std::vector<URLMatcherPortFilter::Range> ranges;
375 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
376 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges));
377 scoped_refptr<URLMatcherConditionSet> condition_set4(
378 new URLMatcherConditionSet(1, conditions,
379 scoped_ptr<URLMatcherSchemeFilter>(NULL), filter.Pass()));
380 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url1));
381 EXPECT_TRUE(condition_set4->IsMatch(matching_substring_patterns, url3));
382 EXPECT_FALSE(condition_set4->IsMatch(matching_substring_patterns, url4));
354 } 383 }
355 384
356 385
357 // 386 //
358 // URLMatcher 387 // URLMatcher
359 // 388 //
360 389
361 TEST(URLMatcherTest, FullTest) { 390 TEST(URLMatcherTest, FullTest) {
362 GURL url1("http://www.example.com/foo?bar=1"); 391 GURL url1("http://www.example.com/foo?bar=1");
363 GURL url2("http://foo.example.com/index.html"); 392 GURL url2("http://foo.example.com/index.html");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 // The cached singleton in matcher.condition_factory_ should be destroyed to 442 // The cached singleton in matcher.condition_factory_ should be destroyed to
414 // free memory. 443 // free memory.
415 int patternId2 = factory->CreateHostSuffixCondition( 444 int patternId2 = factory->CreateHostSuffixCondition(
416 "example.com").substring_pattern()->id(); 445 "example.com").substring_pattern()->id();
417 // If patternId1 and patternId2 are different that indicates that 446 // If patternId1 and patternId2 are different that indicates that
418 // matcher.condition_factory_ does not leak memory. 447 // matcher.condition_factory_ does not leak memory.
419 EXPECT_NE(patternId1, patternId2); 448 EXPECT_NE(patternId1, patternId2);
420 } 449 }
421 450
422 } // namespace extensions 451 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698