Chromium Code Reviews| 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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
| 12 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 14 #include "net/test/test_server.h" | 15 #include "net/test/test_server.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 18 using base::DictionaryValue; | |
| 19 using base::ListValue; | |
| 20 using base::StringValue; | |
| 21 using base::Value; | |
| 22 | |
| 17 namespace { | 23 namespace { |
| 18 const char kUnknownConditionName[] = "unknownType"; | 24 const char kUnknownConditionName[] = "unknownType"; |
| 19 } // namespace | 25 } // namespace |
| 20 | 26 |
| 21 namespace extensions { | 27 namespace extensions { |
| 22 | 28 |
| 23 namespace keys = declarative_webrequest_constants; | 29 namespace keys = declarative_webrequest_constants; |
| 24 | 30 |
| 25 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 31 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
| 26 // Necessary for TestURLRequest. | 32 // Necessary for TestURLRequest. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 scoped_ptr<WebRequestConditionAttribute> attribute_unexcluded = | 155 scoped_ptr<WebRequestConditionAttribute> attribute_unexcluded = |
| 150 WebRequestConditionAttribute::Create( | 156 WebRequestConditionAttribute::Create( |
| 151 keys::kExcludeContentTypeKey, &content_types, &error); | 157 keys::kExcludeContentTypeKey, &content_types, &error); |
| 152 EXPECT_EQ("", error); | 158 EXPECT_EQ("", error); |
| 153 ASSERT_TRUE(attribute_unexcluded.get()); | 159 ASSERT_TRUE(attribute_unexcluded.get()); |
| 154 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( | 160 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( |
| 155 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, | 161 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, |
| 156 url_request.response_headers()))); | 162 url_request.response_headers()))); |
| 157 } | 163 } |
| 158 | 164 |
| 165 namespace { | |
| 166 | |
| 167 // Builds a vector of vectors of string pointers from an array of strings. | |
| 168 // |array| is in fact a sequence of arrays. The array |sizes| captures the sizes | |
| 169 // of all parts of |array|, and |size| is the length of |sizes| itself. | |
| 170 // Example (this is pseudo-code, not C++): | |
| 171 // array = { "a", "b", "c", "d", "e", "f" } | |
| 172 // sizes = { 2, 0, 4 } | |
| 173 // size = 3 | |
| 174 // results in out == { {&"a", &"b"}, {}, {&"c", &"d", &"e", &"f"} } | |
| 175 void GetArrayAsVector(const std::string array[], | |
| 176 const size_t sizes[], | |
| 177 const size_t size, | |
| 178 std::vector< std::vector<const std::string*> >* out) { | |
| 179 out->clear(); | |
| 180 size_t next = 0; | |
| 181 for (size_t i = 0; i < size; ++i) { | |
| 182 out->push_back(std::vector<const std::string*>()); | |
| 183 for (size_t j = next; j < next + sizes[i]; ++j) { | |
| 184 out->back().push_back(&(array[j])); | |
| 185 } | |
| 186 next += sizes[i]; | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 // Builds a DictionaryValue from an array of the form {name1, value1, name2, | |
| 191 // value2, ...}. Values for the same key are grouped in a ListValue. | |
| 192 scoped_ptr<DictionaryValue> GetDictionaryFromArray( | |
| 193 const std::vector<const std::string*>& array) { | |
| 194 const size_t length = array.size(); | |
| 195 if (length % 2 == 1) | |
|
battre
2012/08/23 12:32:11
I think you can just CHECK() this.
vabr (Chromium)
2012/08/24 14:48:59
Done.
| |
| 196 return scoped_ptr<DictionaryValue>(NULL); | |
| 197 | |
| 198 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue); | |
| 199 for (size_t i = 0; i < length; i += 2) { | |
| 200 const std::string* name = array[i]; | |
| 201 const std::string* value = array[i+1]; | |
| 202 if (dictionary->HasKey(*name)) { | |
| 203 Value* entry = NULL; | |
| 204 ListValue* list = NULL; | |
| 205 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) | |
| 206 return scoped_ptr<DictionaryValue>(NULL); | |
| 207 switch (entry->GetType()) { | |
| 208 case Value::TYPE_STRING: // Replace the present string with a list. | |
| 209 list = new ListValue; | |
| 210 // Ignoring return value, we already verified the entry is there. | |
| 211 dictionary->RemoveWithoutPathExpansion(*name, &entry); | |
| 212 list->Append(entry); | |
| 213 list->Append(Value::CreateStringValue(*value)); | |
| 214 dictionary->SetWithoutPathExpansion(*name, list); | |
| 215 break; | |
| 216 case Value::TYPE_LIST: // Just append to the list. | |
| 217 if (!entry->GetAsList(&list)) | |
|
battre
2012/08/23 12:32:11
CHECK?
vabr (Chromium)
2012/08/24 14:48:59
Done.
| |
| 218 return scoped_ptr<DictionaryValue>(NULL); | |
| 219 list->Append(Value::CreateStringValue(*value)); | |
| 220 break; | |
| 221 default: | |
| 222 NOTREACHED(); // We never put other Values here. | |
| 223 return scoped_ptr<DictionaryValue>(NULL); | |
| 224 } | |
| 225 } else { | |
| 226 dictionary->SetString(*name, *value); | |
| 227 } | |
| 228 } | |
| 229 return dictionary.Pass(); | |
| 230 } | |
| 231 | |
| 232 // Returns how the response headers from |url_request| satisfy the match | |
| 233 // criteria given in |tests|. For at least one |i| all tests from |tests[i]| | |
| 234 // must pass. If |positive_test| is true, the dictionary is interpreted as the | |
| 235 // containsHeaders property of a RequestMatcher, otherwise as | |
| 236 // doesNotContainHeaders. | |
| 237 void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests, | |
| 238 net::URLRequest* url_request, | |
| 239 const bool positive_test, | |
|
battre
2012/08/23 12:32:11
nit: we usually don't pass atomic types as const p
vabr (Chromium)
2012/08/24 14:48:59
Actually I got rid of the intermediate boolean an
| |
| 240 bool* result) { | |
|
battre
2012/08/23 12:32:11
return the result instead?
vabr (Chromium)
2012/08/24 14:48:59
Can't do that, using ASSERT_* requires void return
| |
| 241 ListValue contains_headers; | |
| 242 for (size_t i = 0; i < tests.size(); ++i) { | |
| 243 scoped_ptr<DictionaryValue> temp(GetDictionaryFromArray(tests[i])); | |
| 244 ASSERT_TRUE(temp.get() != NULL); | |
|
battre
2012/08/23 12:32:11
is the != NULL necessary?
vabr (Chromium)
2012/08/24 14:48:59
It is not necessary :).
I tend to write it because
| |
| 245 contains_headers.Append(temp.release()); | |
| 246 } | |
| 247 | |
| 248 std::string error; | |
| 249 const std::string key(positive_test ? | |
| 250 keys::kContainsHeadersKey : | |
| 251 keys::kDoesNotContainHeadersKey); | |
| 252 scoped_ptr<WebRequestConditionAttribute> attribute = | |
| 253 WebRequestConditionAttribute::Create(key, &contains_headers, &error); | |
| 254 ASSERT_EQ("", error); | |
| 255 ASSERT_TRUE(attribute.get() != NULL); | |
| 256 | |
| 257 *result = attribute->IsFulfilled(WebRequestRule::RequestData( | |
| 258 url_request, ON_HEADERS_RECEIVED, url_request->response_headers())); | |
| 259 } | |
| 260 | |
| 261 } // namespace | |
| 262 | |
| 263 // Here we test WebRequestConditionAttributeContainsHeaders for: | |
| 264 // 1. Correct implementation of prefix/suffix/contains/equals matching. | |
| 265 // 2. Performing logical disjunction (||) between multiple specifications. | |
| 266 // 3. Negating the match in case of 'doesNotContainHeaders'. | |
| 267 TEST(WebRequestConditionAttributeTest, Headers) { | |
| 268 // Necessary for TestURLRequest. | |
| 269 MessageLoop message_loop(MessageLoop::TYPE_IO); | |
| 270 | |
| 271 net::TestServer test_server( | |
| 272 net::TestServer::TYPE_HTTP, | |
| 273 net::TestServer::kLocalhost, | |
| 274 FilePath(FILE_PATH_LITERAL( | |
| 275 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | |
| 276 ASSERT_TRUE(test_server.Start()); | |
| 277 | |
| 278 TestURLRequestContext context; | |
| 279 TestDelegate delegate; | |
| 280 TestURLRequest url_request(test_server.GetURL("files/headers.html"), | |
| 281 &delegate, &context); | |
| 282 url_request.Start(); | |
| 283 MessageLoop::current()->Run(); | |
| 284 | |
| 285 // In all the tests below we assume that the server includes the header | |
| 286 // "Custom-Header: custom/value" and "Custom-Header-B: valueA, valueB" in the | |
| 287 // response, but does not include "Non-existing: void". | |
| 288 | |
| 289 std::vector< std::vector<const std::string*> > tests; | |
| 290 bool result; | |
| 291 | |
| 292 // 1.a. -- All these tests should pass. | |
| 293 const std::string kPassingCondition[] = { | |
| 294 keys::kNamePrefixKey, "Custom", | |
| 295 keys::kNameSuffixKey, "m-header", // Header names are case insensitive. | |
| 296 keys::kValueContainsKey, "alu", | |
| 297 keys::kValueEqualsKey, "custom/value" | |
| 298 }; | |
| 299 const size_t kPassingConditionSizes[] = { arraysize(kPassingCondition) }; | |
| 300 GetArrayAsVector(kPassingCondition, kPassingConditionSizes, 1u, &tests); | |
| 301 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 302 EXPECT_TRUE(result); | |
| 303 | |
| 304 // 1.b. -- None of the following tests in the discjunction should pass. | |
| 305 const std::string kFailCondition[] = { | |
| 306 keys::kNamePrefixKey, " Custom", // Test 1. | |
| 307 keys::kNameContainsKey, " -", // Test 2. | |
| 308 keys::kValueSuffixKey, "alu", // Test 3. | |
| 309 keys::kValueEqualsKey, "custom" // Test 4. | |
|
battre
2012/08/23 12:32:11
how about a test for case sensitivity in the value
vabr (Chromium)
2012/08/24 14:48:59
Good idea! Done as 1.h.
| |
| 310 }; | |
| 311 const size_t kFailConditionSizes[] = { 2u, 2u, 2u, 2u }; | |
| 312 GetArrayAsVector(kFailCondition, kFailConditionSizes, 4u, &tests); | |
| 313 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 314 EXPECT_FALSE(result); | |
| 315 | |
| 316 // 1.c. -- This should fail (mixing name and value from different headers) | |
| 317 const std::string kMixingCondition[] = { | |
| 318 keys::kNameSuffixKey, "Header-B", | |
| 319 keys::kValueEqualsKey, "custom/value" | |
| 320 }; | |
| 321 const size_t kMixingConditionSizes[] = { arraysize(kMixingCondition) }; | |
| 322 GetArrayAsVector(kMixingCondition, kMixingConditionSizes, 1u, &tests); | |
| 323 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 324 EXPECT_FALSE(result); | |
| 325 | |
| 326 // 1.d. -- Test handling multiple values for one header (both should pass). | |
| 327 const std::string kMoreValues1[] = { | |
| 328 keys::kNameEqualsKey, "Custom-header-b", | |
| 329 keys::kValueEqualsKey, "valueA" | |
|
battre
2012/08/23 12:32:11
Please see my comment in headers.html.mock-http-he
vabr (Chromium)
2012/08/24 14:48:59
Done.
| |
| 330 }; | |
| 331 const size_t kMoreValues1Sizes[] = { arraysize(kMoreValues1) }; | |
| 332 GetArrayAsVector(kMoreValues1, kMoreValues1Sizes, 1u, &tests); | |
| 333 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 334 EXPECT_TRUE(result); | |
| 335 const std::string kMoreValues2[] = { | |
| 336 keys::kNameEqualsKey, "Custom-header-b", | |
| 337 keys::kValueEqualsKey, "valueB" | |
| 338 }; | |
| 339 const size_t kMoreValues2Sizes[] = { arraysize(kMoreValues2) }; | |
| 340 GetArrayAsVector(kMoreValues2, kMoreValues2Sizes, 1u, &tests); | |
| 341 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 342 EXPECT_TRUE(result); | |
| 343 | |
| 344 // 1.e. -- This should fail as conjunction but pass as disjunction. | |
| 345 const std::string kConflict[] = { | |
| 346 keys::kNameSuffixKey, "Header", // True for some header. | |
| 347 keys::kNameSuffixKey, "Header-B" // True for a different header. | |
| 348 }; | |
| 349 // First disjunction, no conflict. | |
| 350 const size_t kNoConflictSizes[] = { 2u, 2u }; | |
| 351 GetArrayAsVector(kConflict, kNoConflictSizes, 2u, &tests); | |
| 352 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 353 EXPECT_TRUE(result); | |
| 354 // Then conjunction, conflict. | |
| 355 const size_t kConflictSizes[] = { arraysize(kConflict) }; | |
| 356 GetArrayAsVector(kConflict, kConflictSizes, 1u, &tests); | |
| 357 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 358 EXPECT_FALSE(result); | |
| 359 | |
| 360 // 2.a. -- This should pass as disjunction, because one of the tests passes. | |
| 361 const std::string kDisjunction[] = { | |
| 362 keys::kNamePrefixKey, "Non-existing", // This one fails. | |
| 363 keys::kNameSuffixKey, "Non-existing", // This one fails. | |
| 364 keys::kValueEqualsKey, "void", // This one fails. | |
| 365 keys::kValueContainsKey, "alu" // This passes. | |
| 366 }; | |
| 367 const size_t kDisjunctionSizes[] = { 2u, 2u, 2u, 2u }; | |
| 368 GetArrayAsVector(kDisjunction, kDisjunctionSizes, 4u, &tests); | |
| 369 MatchAndCheck(tests, &url_request, true /*positive_test*/, &result); | |
| 370 EXPECT_TRUE(result); | |
| 371 | |
| 372 // 3.a. -- This should pass. | |
| 373 const std::string kNonExistent[] = { | |
| 374 keys::kNameEqualsKey, "Non-existing", | |
| 375 keys::kValueEqualsKey, "void" | |
| 376 }; | |
| 377 const size_t kNonExistentSizes[] = { arraysize(kNonExistent) }; | |
| 378 GetArrayAsVector(kNonExistent, kNonExistentSizes, 1u, &tests); | |
| 379 MatchAndCheck(tests, &url_request, false /*positive_test*/, &result); | |
| 380 EXPECT_TRUE(result); | |
| 381 | |
| 382 // 3.b. -- This should fail. | |
| 383 const std::string kExisting[] = { | |
| 384 keys::kNameEqualsKey, "custom-header-b", | |
| 385 keys::kValueEqualsKey, "valueB" | |
| 386 }; | |
| 387 const size_t kExistingSize[] = { arraysize(kExisting) }; | |
| 388 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | |
| 389 MatchAndCheck(tests, &url_request, false /*positive_test*/, &result); | |
| 390 EXPECT_FALSE(result); | |
| 391 } | |
| 392 | |
| 159 } // namespace extensions | 393 } // namespace extensions |
| OLD | NEW |