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/basictypes.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace extensions { | 29 namespace extensions { |
30 | 30 |
31 namespace keys = declarative_webrequest_constants; | 31 namespace keys = declarative_webrequest_constants; |
32 | 32 |
33 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 33 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
34 // Necessary for TestURLRequest. | 34 // Necessary for TestURLRequest. |
35 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 35 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
36 | 36 |
37 std::string error; | 37 std::string error; |
38 scoped_refptr<const WebRequestConditionAttribute> result; | 38 scoped_refptr<const WebRequestConditionAttribute> result; |
39 StringValue string_value("main_frame"); | 39 base::StringValue string_value("main_frame"); |
40 ListValue resource_types; | 40 base::ListValue resource_types; |
41 resource_types.Append(new base::StringValue("main_frame")); | 41 resource_types.Append(new base::StringValue("main_frame")); |
42 | 42 |
43 // Test wrong condition name passed. | 43 // Test wrong condition name passed. |
44 error.clear(); | 44 error.clear(); |
45 result = WebRequestConditionAttribute::Create( | 45 result = WebRequestConditionAttribute::Create( |
46 kUnknownConditionName, &resource_types, &error); | 46 kUnknownConditionName, &resource_types, &error); |
47 EXPECT_FALSE(error.empty()); | 47 EXPECT_FALSE(error.empty()); |
48 EXPECT_FALSE(result.get()); | 48 EXPECT_FALSE(result.get()); |
49 | 49 |
50 // Test wrong data type passed | 50 // Test wrong data type passed |
(...skipping 18 matching lines...) Expand all Loading... |
69 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, | 69 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, |
70 result->GetType()); | 70 result->GetType()); |
71 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); | 71 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); |
72 } | 72 } |
73 | 73 |
74 TEST(WebRequestConditionAttributeTest, ResourceType) { | 74 TEST(WebRequestConditionAttributeTest, ResourceType) { |
75 // Necessary for TestURLRequest. | 75 // Necessary for TestURLRequest. |
76 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 76 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
77 | 77 |
78 std::string error; | 78 std::string error; |
79 ListValue resource_types; | 79 base::ListValue resource_types; |
80 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding | 80 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding |
81 // ResourceType::Type is not 0, the default value. | 81 // ResourceType::Type is not 0, the default value. |
82 resource_types.Append(new base::StringValue("sub_frame")); | 82 resource_types.Append(new base::StringValue("sub_frame")); |
83 | 83 |
84 scoped_refptr<const WebRequestConditionAttribute> attribute = | 84 scoped_refptr<const WebRequestConditionAttribute> attribute = |
85 WebRequestConditionAttribute::Create( | 85 WebRequestConditionAttribute::Create( |
86 keys::kResourceTypeKey, &resource_types, &error); | 86 keys::kResourceTypeKey, &resource_types, &error); |
87 EXPECT_EQ("", error); | 87 EXPECT_EQ("", error); |
88 ASSERT_TRUE(attribute.get()); | 88 ASSERT_TRUE(attribute.get()); |
89 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 89 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
(...skipping 30 matching lines...) Expand all Loading... |
120 | 120 |
121 net::TestURLRequestContext context; | 121 net::TestURLRequestContext context; |
122 net::TestDelegate delegate; | 122 net::TestDelegate delegate; |
123 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 123 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
124 net::DEFAULT_PRIORITY, | 124 net::DEFAULT_PRIORITY, |
125 &delegate, | 125 &delegate, |
126 &context); | 126 &context); |
127 url_request.Start(); | 127 url_request.Start(); |
128 base::MessageLoop::current()->Run(); | 128 base::MessageLoop::current()->Run(); |
129 | 129 |
130 ListValue content_types; | 130 base::ListValue content_types; |
131 content_types.Append(new base::StringValue("text/plain")); | 131 content_types.Append(new base::StringValue("text/plain")); |
132 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 132 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
133 WebRequestConditionAttribute::Create( | 133 WebRequestConditionAttribute::Create( |
134 keys::kContentTypeKey, &content_types, &error); | 134 keys::kContentTypeKey, &content_types, &error); |
135 EXPECT_EQ("", error); | 135 EXPECT_EQ("", error); |
136 ASSERT_TRUE(attribute_include.get()); | 136 ASSERT_TRUE(attribute_include.get()); |
137 EXPECT_FALSE(attribute_include->IsFulfilled( | 137 EXPECT_FALSE(attribute_include->IsFulfilled( |
138 WebRequestData(&url_request, ON_BEFORE_REQUEST, | 138 WebRequestData(&url_request, ON_BEFORE_REQUEST, |
139 url_request.response_headers()))); | 139 url_request.response_headers()))); |
140 EXPECT_TRUE(attribute_include->IsFulfilled( | 140 EXPECT_TRUE(attribute_include->IsFulfilled( |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 251 |
252 // Check that exactly all active stages are considered in this test. | 252 // Check that exactly all active stages are considered in this test. |
253 unsigned int covered_stages = 0; | 253 unsigned int covered_stages = 0; |
254 for (size_t i = 0; i < arraysize(active_stages); ++i) | 254 for (size_t i = 0; i < arraysize(active_stages); ++i) |
255 covered_stages |= active_stages[i].first; | 255 covered_stages |= active_stages[i].first; |
256 EXPECT_EQ(kActiveStages, covered_stages); | 256 EXPECT_EQ(kActiveStages, covered_stages); |
257 | 257 |
258 std::string error; | 258 std::string error; |
259 | 259 |
260 // Create an attribute with an empty set of applicable stages. | 260 // Create an attribute with an empty set of applicable stages. |
261 ListValue empty_list; | 261 base::ListValue empty_list; |
262 scoped_refptr<const WebRequestConditionAttribute> empty_attribute = | 262 scoped_refptr<const WebRequestConditionAttribute> empty_attribute = |
263 WebRequestConditionAttribute::Create(keys::kStagesKey, | 263 WebRequestConditionAttribute::Create(keys::kStagesKey, |
264 &empty_list, | 264 &empty_list, |
265 &error); | 265 &error); |
266 EXPECT_EQ("", error); | 266 EXPECT_EQ("", error); |
267 ASSERT_TRUE(empty_attribute.get()); | 267 ASSERT_TRUE(empty_attribute.get()); |
268 EXPECT_EQ(std::string(keys::kStagesKey), empty_attribute->GetName()); | 268 EXPECT_EQ(std::string(keys::kStagesKey), empty_attribute->GetName()); |
269 | 269 |
270 // Create an attribute with all possible applicable stages. | 270 // Create an attribute with all possible applicable stages. |
271 ListValue all_stages; | 271 base::ListValue all_stages; |
272 for (size_t i = 0; i < arraysize(active_stages); ++i) | 272 for (size_t i = 0; i < arraysize(active_stages); ++i) |
273 all_stages.AppendString(active_stages[i].second); | 273 all_stages.AppendString(active_stages[i].second); |
274 scoped_refptr<const WebRequestConditionAttribute> attribute_with_all = | 274 scoped_refptr<const WebRequestConditionAttribute> attribute_with_all = |
275 WebRequestConditionAttribute::Create(keys::kStagesKey, | 275 WebRequestConditionAttribute::Create(keys::kStagesKey, |
276 &all_stages, | 276 &all_stages, |
277 &error); | 277 &error); |
278 EXPECT_EQ("", error); | 278 EXPECT_EQ("", error); |
279 ASSERT_TRUE(attribute_with_all.get()); | 279 ASSERT_TRUE(attribute_with_all.get()); |
280 EXPECT_EQ(std::string(keys::kStagesKey), attribute_with_all->GetName()); | 280 EXPECT_EQ(std::string(keys::kStagesKey), attribute_with_all->GetName()); |
281 | 281 |
282 // Create one attribute for each single stage, to be applicable in that stage. | 282 // Create one attribute for each single stage, to be applicable in that stage. |
283 std::vector<scoped_refptr<const WebRequestConditionAttribute> > | 283 std::vector<scoped_refptr<const WebRequestConditionAttribute> > |
284 one_stage_attributes; | 284 one_stage_attributes; |
285 | 285 |
286 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 286 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
287 ListValue single_stage_list; | 287 base::ListValue single_stage_list; |
288 single_stage_list.AppendString(active_stages[i].second); | 288 single_stage_list.AppendString(active_stages[i].second); |
289 one_stage_attributes.push_back( | 289 one_stage_attributes.push_back( |
290 WebRequestConditionAttribute::Create(keys::kStagesKey, | 290 WebRequestConditionAttribute::Create(keys::kStagesKey, |
291 &single_stage_list, | 291 &single_stage_list, |
292 &error)); | 292 &error)); |
293 EXPECT_EQ("", error); | 293 EXPECT_EQ("", error); |
294 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); | 294 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); |
295 } | 295 } |
296 | 296 |
297 const GURL url_empty; | 297 const GURL url_empty; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 out->push_back(std::vector<const std::string*>()); | 335 out->push_back(std::vector<const std::string*>()); |
336 for (size_t j = next; j < next + sizes[i]; ++j) { | 336 for (size_t j = next; j < next + sizes[i]; ++j) { |
337 out->back().push_back(&(array[j])); | 337 out->back().push_back(&(array[j])); |
338 } | 338 } |
339 next += sizes[i]; | 339 next += sizes[i]; |
340 } | 340 } |
341 } | 341 } |
342 | 342 |
343 // Builds a DictionaryValue from an array of the form {name1, value1, name2, | 343 // Builds a DictionaryValue from an array of the form {name1, value1, name2, |
344 // value2, ...}. Values for the same key are grouped in a ListValue. | 344 // value2, ...}. Values for the same key are grouped in a ListValue. |
345 scoped_ptr<DictionaryValue> GetDictionaryFromArray( | 345 scoped_ptr<base::DictionaryValue> GetDictionaryFromArray( |
346 const std::vector<const std::string*>& array) { | 346 const std::vector<const std::string*>& array) { |
347 const size_t length = array.size(); | 347 const size_t length = array.size(); |
348 CHECK(length % 2 == 0); | 348 CHECK(length % 2 == 0); |
349 | 349 |
350 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue); | 350 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); |
351 for (size_t i = 0; i < length; i += 2) { | 351 for (size_t i = 0; i < length; i += 2) { |
352 const std::string* name = array[i]; | 352 const std::string* name = array[i]; |
353 const std::string* value = array[i+1]; | 353 const std::string* value = array[i+1]; |
354 if (dictionary->HasKey(*name)) { | 354 if (dictionary->HasKey(*name)) { |
355 Value* entry = NULL; | 355 base::Value* entry = NULL; |
356 scoped_ptr<Value> entry_owned; | 356 scoped_ptr<base::Value> entry_owned; |
357 ListValue* list = NULL; | 357 base::ListValue* list = NULL; |
358 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) | 358 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) |
359 return scoped_ptr<DictionaryValue>(); | 359 return scoped_ptr<base::DictionaryValue>(); |
360 switch (entry->GetType()) { | 360 switch (entry->GetType()) { |
361 case Value::TYPE_STRING: // Replace the present string with a list. | 361 case base::Value::TYPE_STRING: |
362 list = new ListValue; | 362 // Replace the present string with a list. |
| 363 list = new base::ListValue; |
363 // Ignoring return value, we already verified the entry is there. | 364 // Ignoring return value, we already verified the entry is there. |
364 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); | 365 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); |
365 list->Append(entry_owned.release()); | 366 list->Append(entry_owned.release()); |
366 list->Append(new base::StringValue(*value)); | 367 list->Append(new base::StringValue(*value)); |
367 dictionary->SetWithoutPathExpansion(*name, list); | 368 dictionary->SetWithoutPathExpansion(*name, list); |
368 break; | 369 break; |
369 case Value::TYPE_LIST: // Just append to the list. | 370 case base::Value::TYPE_LIST: // Just append to the list. |
370 CHECK(entry->GetAsList(&list)); | 371 CHECK(entry->GetAsList(&list)); |
371 list->Append(new base::StringValue(*value)); | 372 list->Append(new base::StringValue(*value)); |
372 break; | 373 break; |
373 default: | 374 default: |
374 NOTREACHED(); // We never put other Values here. | 375 NOTREACHED(); // We never put other Values here. |
375 return scoped_ptr<DictionaryValue>(); | 376 return scoped_ptr<base::DictionaryValue>(); |
376 } | 377 } |
377 } else { | 378 } else { |
378 dictionary->SetString(*name, *value); | 379 dictionary->SetString(*name, *value); |
379 } | 380 } |
380 } | 381 } |
381 return dictionary.Pass(); | 382 return dictionary.Pass(); |
382 } | 383 } |
383 | 384 |
384 // Returns whether the response headers from |url_request| satisfy the match | 385 // Returns whether the response headers from |url_request| satisfy the match |
385 // criteria given in |tests|. For at least one |i| all tests from |tests[i]| | 386 // criteria given in |tests|. For at least one |i| all tests from |tests[i]| |
386 // must pass. If |positive_test| is true, the dictionary is interpreted as the | 387 // must pass. If |positive_test| is true, the dictionary is interpreted as the |
387 // containsHeaders property of a RequestMatcher, otherwise as | 388 // containsHeaders property of a RequestMatcher, otherwise as |
388 // doesNotContainHeaders. | 389 // doesNotContainHeaders. |
389 void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests, | 390 void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests, |
390 const std::string& key, | 391 const std::string& key, |
391 RequestStage stage, | 392 RequestStage stage, |
392 net::URLRequest* url_request, | 393 net::URLRequest* url_request, |
393 bool* result) { | 394 bool* result) { |
394 ListValue contains_headers; | 395 base::ListValue contains_headers; |
395 for (size_t i = 0; i < tests.size(); ++i) { | 396 for (size_t i = 0; i < tests.size(); ++i) { |
396 scoped_ptr<DictionaryValue> temp(GetDictionaryFromArray(tests[i])); | 397 scoped_ptr<base::DictionaryValue> temp(GetDictionaryFromArray(tests[i])); |
397 ASSERT_TRUE(temp.get()); | 398 ASSERT_TRUE(temp.get()); |
398 contains_headers.Append(temp.release()); | 399 contains_headers.Append(temp.release()); |
399 } | 400 } |
400 | 401 |
401 std::string error; | 402 std::string error; |
402 scoped_refptr<const WebRequestConditionAttribute> attribute = | 403 scoped_refptr<const WebRequestConditionAttribute> attribute = |
403 WebRequestConditionAttribute::Create(key, &contains_headers, &error); | 404 WebRequestConditionAttribute::Create(key, &contains_headers, &error); |
404 ASSERT_EQ("", error); | 405 ASSERT_EQ("", error); |
405 ASSERT_TRUE(attribute.get()); | 406 ASSERT_TRUE(attribute.get()); |
406 EXPECT_EQ(key, attribute->GetName()); | 407 EXPECT_EQ(key, attribute->GetName()); |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 keys::kValueEqualsKey, "valueB" | 674 keys::kValueEqualsKey, "valueB" |
674 }; | 675 }; |
675 const size_t kExistingSize[] = { arraysize(kExisting) }; | 676 const size_t kExistingSize[] = { arraysize(kExisting) }; |
676 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 677 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
677 MatchAndCheck( | 678 MatchAndCheck( |
678 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 679 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
679 EXPECT_FALSE(result); | 680 EXPECT_FALSE(result); |
680 } | 681 } |
681 | 682 |
682 } // namespace extensions | 683 } // namespace extensions |
OLD | NEW |