| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/url_matcher/url_matcher_factory.h" | 5 #include "components/url_matcher/url_matcher_factory.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "components/url_matcher/url_matcher_constants.h" | 11 #include "components/url_matcher/url_matcher_constants.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 | 14 |
| 15 namespace url_matcher { | 15 namespace url_matcher { |
| 16 | 16 |
| 17 namespace keys = url_matcher_constants; | 17 namespace keys = url_matcher_constants; |
| 18 | 18 |
| 19 TEST(URLMatcherFactoryTest, CreateFromURLFilterDictionary) { | 19 TEST(URLMatcherFactoryTest, CreateFromURLFilterDictionary) { |
| 20 URLMatcher matcher; | 20 URLMatcher matcher; |
| 21 | 21 |
| 22 std::string error; | 22 std::string error; |
| 23 scoped_refptr<URLMatcherConditionSet> result; | 23 scoped_refptr<URLMatcherConditionSet> result; |
| 24 | 24 |
| 25 // Invalid key: {"invalid": "foobar"} | 25 // Invalid key: {"invalid": "foobar"} |
| 26 DictionaryValue invalid_condition; | 26 base::DictionaryValue invalid_condition; |
| 27 invalid_condition.SetString("invalid", "foobar"); | 27 invalid_condition.SetString("invalid", "foobar"); |
| 28 | 28 |
| 29 // Invalid value type: {"hostSuffix": []} | 29 // Invalid value type: {"hostSuffix": []} |
| 30 DictionaryValue invalid_condition2; | 30 base::DictionaryValue invalid_condition2; |
| 31 invalid_condition2.Set(keys::kHostSuffixKey, new ListValue); | 31 invalid_condition2.Set(keys::kHostSuffixKey, new base::ListValue); |
| 32 | 32 |
| 33 // Invalid regex value: {"urlMatches": "*"} | 33 // Invalid regex value: {"urlMatches": "*"} |
| 34 DictionaryValue invalid_condition3; | 34 base::DictionaryValue invalid_condition3; |
| 35 invalid_condition3.SetString(keys::kURLMatchesKey, "*"); | 35 invalid_condition3.SetString(keys::kURLMatchesKey, "*"); |
| 36 | 36 |
| 37 // Invalid regex value: {"originAndPathMatches": "*"} | 37 // Invalid regex value: {"originAndPathMatches": "*"} |
| 38 DictionaryValue invalid_condition4; | 38 base::DictionaryValue invalid_condition4; |
| 39 invalid_condition4.SetString(keys::kOriginAndPathMatchesKey, "*"); | 39 invalid_condition4.SetString(keys::kOriginAndPathMatchesKey, "*"); |
| 40 | 40 |
| 41 // Valid values: | 41 // Valid values: |
| 42 // { | 42 // { |
| 43 // "port_range": [80, [1000, 1010]], | 43 // "port_range": [80, [1000, 1010]], |
| 44 // "schemes": ["http"], | 44 // "schemes": ["http"], |
| 45 // "hostSuffix": "example.com" | 45 // "hostSuffix": "example.com" |
| 46 // "hostPrefix": "www" | 46 // "hostPrefix": "www" |
| 47 // } | 47 // } |
| 48 | 48 |
| 49 // Port range: Allow 80;1000-1010. | 49 // Port range: Allow 80;1000-1010. |
| 50 ListValue* port_range = new ListValue(); | 50 base::ListValue* port_range = new base::ListValue(); |
| 51 port_range->Append(Value::CreateIntegerValue(1000)); | 51 port_range->Append(base::Value::CreateIntegerValue(1000)); |
| 52 port_range->Append(Value::CreateIntegerValue(1010)); | 52 port_range->Append(base::Value::CreateIntegerValue(1010)); |
| 53 ListValue* port_ranges = new ListValue(); | 53 base::ListValue* port_ranges = new base::ListValue(); |
| 54 port_ranges->Append(Value::CreateIntegerValue(80)); | 54 port_ranges->Append(base::Value::CreateIntegerValue(80)); |
| 55 port_ranges->Append(port_range); | 55 port_ranges->Append(port_range); |
| 56 | 56 |
| 57 ListValue* scheme_list = new ListValue(); | 57 base::ListValue* scheme_list = new base::ListValue(); |
| 58 scheme_list->Append(Value::CreateStringValue("http")); | 58 scheme_list->Append(base::Value::CreateStringValue("http")); |
| 59 | 59 |
| 60 DictionaryValue valid_condition; | 60 base::DictionaryValue valid_condition; |
| 61 valid_condition.SetString(keys::kHostSuffixKey, "example.com"); | 61 valid_condition.SetString(keys::kHostSuffixKey, "example.com"); |
| 62 valid_condition.SetString(keys::kHostPrefixKey, "www"); | 62 valid_condition.SetString(keys::kHostPrefixKey, "www"); |
| 63 valid_condition.Set(keys::kPortsKey, port_ranges); | 63 valid_condition.Set(keys::kPortsKey, port_ranges); |
| 64 valid_condition.Set(keys::kSchemesKey, scheme_list); | 64 valid_condition.Set(keys::kSchemesKey, scheme_list); |
| 65 | 65 |
| 66 // Test wrong condition name passed. | 66 // Test wrong condition name passed. |
| 67 error.clear(); | 67 error.clear(); |
| 68 result = URLMatcherFactory::CreateFromURLFilterDictionary( | 68 result = URLMatcherFactory::CreateFromURLFilterDictionary( |
| 69 matcher.condition_factory(), &invalid_condition, 1, &error); | 69 matcher.condition_factory(), &invalid_condition, 1, &error); |
| 70 EXPECT_FALSE(error.empty()); | 70 EXPECT_FALSE(error.empty()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Using upper case letters for scheme and host values is currently an error. | 115 // Using upper case letters for scheme and host values is currently an error. |
| 116 // See more context at http://crbug.com/160702#c6 . | 116 // See more context at http://crbug.com/160702#c6 . |
| 117 TEST(URLMatcherFactoryTest, UpperCase) { | 117 TEST(URLMatcherFactoryTest, UpperCase) { |
| 118 URLMatcher matcher; | 118 URLMatcher matcher; |
| 119 std::string error; | 119 std::string error; |
| 120 scoped_refptr<URLMatcherConditionSet> result; | 120 scoped_refptr<URLMatcherConditionSet> result; |
| 121 | 121 |
| 122 // {"hostContains": "exaMple"} | 122 // {"hostContains": "exaMple"} |
| 123 DictionaryValue invalid_condition1; | 123 base::DictionaryValue invalid_condition1; |
| 124 invalid_condition1.SetString(keys::kHostContainsKey, "exaMple"); | 124 invalid_condition1.SetString(keys::kHostContainsKey, "exaMple"); |
| 125 | 125 |
| 126 // {"hostSuffix": ".Com"} | 126 // {"hostSuffix": ".Com"} |
| 127 DictionaryValue invalid_condition2; | 127 base::DictionaryValue invalid_condition2; |
| 128 invalid_condition2.SetString(keys::kHostSuffixKey, ".Com"); | 128 invalid_condition2.SetString(keys::kHostSuffixKey, ".Com"); |
| 129 | 129 |
| 130 // {"hostPrefix": "WWw."} | 130 // {"hostPrefix": "WWw."} |
| 131 DictionaryValue invalid_condition3; | 131 base::DictionaryValue invalid_condition3; |
| 132 invalid_condition3.SetString(keys::kHostPrefixKey, "WWw."); | 132 invalid_condition3.SetString(keys::kHostPrefixKey, "WWw."); |
| 133 | 133 |
| 134 // {"hostEquals": "WWW.example.Com"} | 134 // {"hostEquals": "WWW.example.Com"} |
| 135 DictionaryValue invalid_condition4; | 135 base::DictionaryValue invalid_condition4; |
| 136 invalid_condition4.SetString(keys::kHostEqualsKey, "WWW.example.Com"); | 136 invalid_condition4.SetString(keys::kHostEqualsKey, "WWW.example.Com"); |
| 137 | 137 |
| 138 // {"scheme": ["HTTP"]} | 138 // {"scheme": ["HTTP"]} |
| 139 ListValue* scheme_list = new ListValue(); | 139 base::ListValue* scheme_list = new base::ListValue(); |
| 140 scheme_list->Append(Value::CreateStringValue("HTTP")); | 140 scheme_list->Append(base::Value::CreateStringValue("HTTP")); |
| 141 DictionaryValue invalid_condition5; | 141 base::DictionaryValue invalid_condition5; |
| 142 invalid_condition5.Set(keys::kSchemesKey, scheme_list); | 142 invalid_condition5.Set(keys::kSchemesKey, scheme_list); |
| 143 | 143 |
| 144 const DictionaryValue* invalid_conditions[] = { | 144 const base::DictionaryValue* invalid_conditions[] = { |
| 145 &invalid_condition1, | 145 &invalid_condition1, |
| 146 &invalid_condition2, | 146 &invalid_condition2, |
| 147 &invalid_condition3, | 147 &invalid_condition3, |
| 148 &invalid_condition4, | 148 &invalid_condition4, |
| 149 &invalid_condition5 | 149 &invalid_condition5 |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 for (size_t i = 0; i < arraysize(invalid_conditions); ++i) { | 152 for (size_t i = 0; i < arraysize(invalid_conditions); ++i) { |
| 153 error.clear(); | 153 error.clear(); |
| 154 result = URLMatcherFactory::CreateFromURLFilterDictionary( | 154 result = URLMatcherFactory::CreateFromURLFilterDictionary( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 }; | 220 }; |
| 221 | 221 |
| 222 void UrlConditionCaseTest::Test() const { | 222 void UrlConditionCaseTest::Test() const { |
| 223 CheckCondition(expected_value_, OK); | 223 CheckCondition(expected_value_, OK); |
| 224 CheckCondition(incorrect_case_value_, expected_result_for_wrong_case_); | 224 CheckCondition(incorrect_case_value_, expected_result_for_wrong_case_); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void UrlConditionCaseTest::CheckCondition( | 227 void UrlConditionCaseTest::CheckCondition( |
| 228 const std::string& value, | 228 const std::string& value, |
| 229 UrlConditionCaseTest::ResultType expected_result) const { | 229 UrlConditionCaseTest::ResultType expected_result) const { |
| 230 DictionaryValue condition; | 230 base::DictionaryValue condition; |
| 231 if (use_list_of_strings_) { | 231 if (use_list_of_strings_) { |
| 232 ListValue* list = new ListValue(); | 232 base::ListValue* list = new base::ListValue(); |
| 233 list->Append(Value::CreateStringValue(value)); | 233 list->Append(base::Value::CreateStringValue(value)); |
| 234 condition.SetWithoutPathExpansion(condition_key_, list); | 234 condition.SetWithoutPathExpansion(condition_key_, list); |
| 235 } else { | 235 } else { |
| 236 condition.SetStringWithoutPathExpansion(condition_key_, value); | 236 condition.SetStringWithoutPathExpansion(condition_key_, value); |
| 237 } | 237 } |
| 238 | 238 |
| 239 URLMatcher matcher; | 239 URLMatcher matcher; |
| 240 std::string error; | 240 std::string error; |
| 241 scoped_refptr<URLMatcherConditionSet> result; | 241 scoped_refptr<URLMatcherConditionSet> result; |
| 242 | 242 |
| 243 result = URLMatcherFactory::CreateFromURLFilterDictionary( | 243 result = URLMatcherFactory::CreateFromURLFilterDictionary( |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url), | 330 kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url), |
| 331 }; | 331 }; |
| 332 | 332 |
| 333 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(case_tests); ++i) { | 333 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(case_tests); ++i) { |
| 334 SCOPED_TRACE(base::StringPrintf("Iteration: %" PRIuS, i)); | 334 SCOPED_TRACE(base::StringPrintf("Iteration: %" PRIuS, i)); |
| 335 case_tests[i].Test(); | 335 case_tests[i].Test(); |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 | 338 |
| 339 } // namespace url_matcher | 339 } // namespace url_matcher |
| OLD | NEW |