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

Side by Side Diff: chrome/common/extensions/url_pattern_unittest.cc

Issue 7229012: Use extension match pattern syntax in content settings extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unit test Created 9 years, 6 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "chrome/common/extensions/url_pattern.h" 6 #include "chrome/common/extensions/url_pattern.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "googleurl/src/gurl.h" 8 #include "googleurl/src/gurl.h"
9 9
10 // See url_pattern.h for examples of valid and invalid patterns. 10 // See url_pattern.h for examples of valid and invalid patterns.
(...skipping 25 matching lines...) Expand all
36 36
37 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) { 37 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) {
38 URLPattern pattern(URLPattern::SCHEME_ALL); 38 URLPattern pattern(URLPattern::SCHEME_ALL);
39 EXPECT_EQ(kInvalidPatterns[i].expected_result, 39 EXPECT_EQ(kInvalidPatterns[i].expected_result,
40 pattern.Parse(kInvalidPatterns[i].pattern, 40 pattern.Parse(kInvalidPatterns[i].pattern,
41 URLPattern::PARSE_LENIENT)) 41 URLPattern::PARSE_LENIENT))
42 << kInvalidPatterns[i].pattern; 42 << kInvalidPatterns[i].pattern;
43 } 43 }
44 }; 44 };
45 45
46 TEST(ExtensionURLPatternTest, Colons) { 46 TEST(ExtensionURLPatternTest, Ports) {
47 const struct { 47 const struct {
48 const char* pattern; 48 const char* pattern;
49 URLPattern::ParseResult expected_result; 49 URLPattern::ParseResult expected_result_strict;
50 URLPattern::ParseResult expected_result_lenient;
51 const char* expected_port;
50 } kTestPatterns[] = { 52 } kTestPatterns[] = {
51 { "http://foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, 53 { "http://foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON,
52 { "http://foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON }, 54 URLPattern::PARSE_SUCCESS, "1234" },
53 { "http://*.foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, 55 { "http://foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON,
54 { "http://*.foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON }, 56 URLPattern::PARSE_SUCCESS, "1234" },
55 { "http://:1234/", URLPattern::PARSE_ERROR_HAS_COLON }, 57 { "http://*.foo:1234/", URLPattern::PARSE_ERROR_HAS_COLON,
56 { "http://foo:/", URLPattern::PARSE_ERROR_HAS_COLON }, 58 URLPattern::PARSE_SUCCESS, "1234" },
57 { "http://*.foo:/", URLPattern::PARSE_ERROR_HAS_COLON }, 59 { "http://*.foo:1234/bar", URLPattern::PARSE_ERROR_HAS_COLON,
58 { "http://foo:com/", URLPattern::PARSE_ERROR_HAS_COLON }, 60 URLPattern::PARSE_SUCCESS, "1234" },
61 { "http://:1234/", URLPattern::PARSE_ERROR_HAS_COLON,
62 URLPattern::PARSE_SUCCESS, "1234" },
63 { "http://foo:/", URLPattern::PARSE_ERROR_HAS_COLON,
64 URLPattern::PARSE_SUCCESS, "" },
65 { "http://*.foo:/", URLPattern::PARSE_ERROR_HAS_COLON,
66 URLPattern::PARSE_SUCCESS, "" },
67 { "http://foo:com/", URLPattern::PARSE_ERROR_HAS_COLON,
68 URLPattern::PARSE_ERROR_INVALID_PORT, "" },
69 { "http://foo:123456/", URLPattern::PARSE_ERROR_HAS_COLON,
70 URLPattern::PARSE_ERROR_INVALID_PORT, "" },
59 71
60 // Port-like strings in the path should not trigger a warning. 72 // Port-like strings in the path should not trigger a warning.
61 { "http://*/:1234", URLPattern::PARSE_SUCCESS }, 73 { "http://*/:1234", URLPattern::PARSE_SUCCESS,
62 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS }, 74 URLPattern::PARSE_SUCCESS, "" },
63 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS }, 75 { "http://*.foo/bar:1234", URLPattern::PARSE_SUCCESS,
76 URLPattern::PARSE_SUCCESS, "" },
77 { "http://foo/bar:1234/path", URLPattern::PARSE_SUCCESS,
78 URLPattern::PARSE_SUCCESS, "" },
64 }; 79 };
65 80
66 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) { 81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) {
67 URLPattern pattern(URLPattern::SCHEME_ALL); 82 URLPattern pattern(URLPattern::SCHEME_ALL);
68 83
69 // Without |strict_error_checks|, expect success. 84 // Check results without |strict_error_checks|.
70 EXPECT_EQ(URLPattern::PARSE_SUCCESS, 85 EXPECT_EQ(kTestPatterns[i].expected_result_lenient,
71 pattern.Parse(kTestPatterns[i].pattern, 86 pattern.Parse(kTestPatterns[i].pattern,
72 URLPattern::PARSE_LENIENT)) 87 URLPattern::PARSE_LENIENT))
73 << "Got unexpected error for URL pattern: " 88 << "Got unexpected result for URL pattern: "
89 << kTestPatterns[i].pattern;
90 EXPECT_EQ(kTestPatterns[i].expected_port, pattern.port())
91 << "Got unexpected port for URL pattern: "
74 << kTestPatterns[i].pattern; 92 << kTestPatterns[i].pattern;
75 93
76 EXPECT_EQ(kTestPatterns[i].expected_result, 94 // Check results with |strict_error_checks|.
95 EXPECT_EQ(kTestPatterns[i].expected_result_strict,
77 pattern.Parse(kTestPatterns[i].pattern, 96 pattern.Parse(kTestPatterns[i].pattern,
78 URLPattern::PARSE_STRICT)) 97 URLPattern::PARSE_STRICT))
79 << "Got unexpected result for URL pattern: " 98 << "Got unexpected result for URL pattern: "
80 << kTestPatterns[i].pattern; 99 << kTestPatterns[i].pattern;
81 } 100 }
82 }; 101 };
83 102
84 // all pages for a given scheme 103 // all pages for a given scheme
85 TEST(ExtensionURLPatternTest, Match1) { 104 TEST(ExtensionURLPatternTest, Match1) {
86 URLPattern pattern(kAllSchemes); 105 URLPattern pattern(kAllSchemes);
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 { "chrome://*/*" }, 383 { "chrome://*/*" },
365 { "chrome://newtab/" }, 384 { "chrome://newtab/" },
366 { "about:*" }, 385 { "about:*" },
367 { "about:blank" }, 386 { "about:blank" },
368 { "chrome-extension://*/*" }, 387 { "chrome-extension://*/*" },
369 { "chrome-extension://FTW/" }, 388 { "chrome-extension://FTW/" },
370 { "data:*" }, 389 { "data:*" },
371 { "data:monkey" }, 390 { "data:monkey" },
372 { "javascript:*" }, 391 { "javascript:*" },
373 { "javascript:atemyhomework" }, 392 { "javascript:atemyhomework" },
393 { "http://www.example.com:8080/foo" },
374 }; 394 };
375 395
376 TEST(ExtensionURLPatternTest, GetAsString) { 396 TEST(ExtensionURLPatternTest, GetAsString) {
377 for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) { 397 for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) {
378 URLPattern pattern(URLPattern::SCHEME_ALL); 398 URLPattern pattern(URLPattern::SCHEME_ALL);
379 EXPECT_EQ(URLPattern::PARSE_SUCCESS, 399 EXPECT_EQ(URLPattern::PARSE_SUCCESS,
380 pattern.Parse(kGetAsStringTestCases[i].pattern, 400 pattern.Parse(kGetAsStringTestCases[i].pattern,
381 URLPattern::PARSE_STRICT)); 401 URLPattern::PARSE_LENIENT));
382 EXPECT_STREQ(kGetAsStringTestCases[i].pattern, 402 EXPECT_STREQ(kGetAsStringTestCases[i].pattern,
383 pattern.GetAsString().c_str()); 403 pattern.GetAsString().c_str());
384 } 404 }
385 } 405 }
386 406
387 void TestPatternOverlap(const URLPattern& pattern1, const URLPattern& pattern2, 407 void TestPatternOverlap(const URLPattern& pattern1, const URLPattern& pattern2,
388 bool expect_overlap) { 408 bool expect_overlap) {
389 EXPECT_EQ(expect_overlap, pattern1.OverlapsWith(pattern2)) 409 EXPECT_EQ(expect_overlap, pattern1.OverlapsWith(pattern2))
390 << pattern1.GetAsString() << ", " << pattern2.GetAsString(); 410 << pattern1.GetAsString() << ", " << pattern2.GetAsString();
391 EXPECT_EQ(expect_overlap, pattern2.OverlapsWith(pattern1)) 411 EXPECT_EQ(expect_overlap, pattern2.OverlapsWith(pattern1))
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 EXPECT_EQ("https://*/*", all_urls[1].GetAsString()); 480 EXPECT_EQ("https://*/*", all_urls[1].GetAsString());
461 EXPECT_EQ("file:///*", all_urls[2].GetAsString()); 481 EXPECT_EQ("file:///*", all_urls[2].GetAsString());
462 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString()); 482 EXPECT_EQ("ftp://*/*", all_urls[3].GetAsString());
463 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString()); 483 EXPECT_EQ("chrome://*/*", all_urls[4].GetAsString());
464 484
465 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString()); 485 EXPECT_EQ("http://google.com/foo", all_schemes[0].GetAsString());
466 EXPECT_EQ("https://google.com/foo", all_schemes[1].GetAsString()); 486 EXPECT_EQ("https://google.com/foo", all_schemes[1].GetAsString());
467 487
468 EXPECT_EQ("http://google.com/monkey", monkey[0].GetAsString()); 488 EXPECT_EQ("http://google.com/monkey", monkey[0].GetAsString());
469 } 489 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698