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

Side by Side Diff: extensions/browser/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc

Issue 1902873002: Convert //extensions/browser/api from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
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 "extensions/browser/api/declarative_webrequest/webrequest_condition_att ribute.h" 5 #include "extensions/browser/api/declarative_webrequest/webrequest_condition_att ribute.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory>
10
9 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "content/public/browser/resource_request_info.h" 15 #include "content/public/browser/resource_request_info.h"
15 #include "extensions/browser/api/declarative_webrequest/webrequest_condition.h" 16 #include "extensions/browser/api/declarative_webrequest/webrequest_condition.h"
16 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" 17 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
17 #include "net/base/request_priority.h" 18 #include "net/base/request_priority.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h" 19 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "net/url_request/url_request.h" 20 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_test_util.h" 21 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 resource_types.Append(new base::StringValue("sub_frame")); 93 resource_types.Append(new base::StringValue("sub_frame"));
93 94
94 scoped_refptr<const WebRequestConditionAttribute> attribute = 95 scoped_refptr<const WebRequestConditionAttribute> attribute =
95 WebRequestConditionAttribute::Create( 96 WebRequestConditionAttribute::Create(
96 keys::kResourceTypeKey, &resource_types, &error); 97 keys::kResourceTypeKey, &resource_types, &error);
97 EXPECT_EQ("", error); 98 EXPECT_EQ("", error);
98 ASSERT_TRUE(attribute.get()); 99 ASSERT_TRUE(attribute.get());
99 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); 100 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName());
100 101
101 net::TestURLRequestContext context; 102 net::TestURLRequestContext context;
102 scoped_ptr<net::URLRequest> url_request_ok(context.CreateRequest( 103 std::unique_ptr<net::URLRequest> url_request_ok(context.CreateRequest(
103 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL)); 104 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL));
104 content::ResourceRequestInfo::AllocateForTesting( 105 content::ResourceRequestInfo::AllocateForTesting(
105 url_request_ok.get(), content::RESOURCE_TYPE_SUB_FRAME, 106 url_request_ok.get(), content::RESOURCE_TYPE_SUB_FRAME,
106 NULL, // context 107 NULL, // context
107 -1, // render_process_id 108 -1, // render_process_id
108 -1, // render_view_id 109 -1, // render_view_id
109 -1, // render_frame_id 110 -1, // render_frame_id
110 false, // is_main_frame 111 false, // is_main_frame
111 false, // parent_is_main_frame 112 false, // parent_is_main_frame
112 true, // allow_download 113 true, // allow_download
113 false, // is_async 114 false, // is_async
114 false); // is_using_lofi 115 false); // is_using_lofi
115 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(), 116 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(),
116 ON_BEFORE_REQUEST))); 117 ON_BEFORE_REQUEST)));
117 118
118 scoped_ptr<net::URLRequest> url_request_fail(context.CreateRequest( 119 std::unique_ptr<net::URLRequest> url_request_fail(context.CreateRequest(
119 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL)); 120 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL));
120 content::ResourceRequestInfo::AllocateForTesting( 121 content::ResourceRequestInfo::AllocateForTesting(
121 url_request_fail.get(), content::RESOURCE_TYPE_MAIN_FRAME, 122 url_request_fail.get(), content::RESOURCE_TYPE_MAIN_FRAME,
122 NULL, // context 123 NULL, // context
123 -1, // render_process_id 124 -1, // render_process_id
124 -1, // render_view_id 125 -1, // render_view_id
125 -1, // render_frame_id 126 -1, // render_frame_id
126 true, // is_main_frame 127 true, // is_main_frame
127 false, // parent_is_main_frame 128 false, // parent_is_main_frame
128 true, // allow_download 129 true, // allow_download
(...skipping 10 matching lines...) Expand all
139 std::string error; 140 std::string error;
140 scoped_refptr<const WebRequestConditionAttribute> result; 141 scoped_refptr<const WebRequestConditionAttribute> result;
141 142
142 net::EmbeddedTestServer test_server; 143 net::EmbeddedTestServer test_server;
143 test_server.ServeFilesFromDirectory(TestDataPath( 144 test_server.ServeFilesFromDirectory(TestDataPath(
144 "chrome/test/data/extensions/api_test/webrequest/declarative")); 145 "chrome/test/data/extensions/api_test/webrequest/declarative"));
145 ASSERT_TRUE(test_server.Start()); 146 ASSERT_TRUE(test_server.Start());
146 147
147 net::TestURLRequestContext context; 148 net::TestURLRequestContext context;
148 net::TestDelegate delegate; 149 net::TestDelegate delegate;
149 scoped_ptr<net::URLRequest> url_request(context.CreateRequest( 150 std::unique_ptr<net::URLRequest> url_request(context.CreateRequest(
150 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate)); 151 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate));
151 url_request->Start(); 152 url_request->Start();
152 base::MessageLoop::current()->Run(); 153 base::MessageLoop::current()->Run();
153 154
154 base::ListValue content_types; 155 base::ListValue content_types;
155 content_types.Append(new base::StringValue("text/plain")); 156 content_types.Append(new base::StringValue("text/plain"));
156 scoped_refptr<const WebRequestConditionAttribute> attribute_include = 157 scoped_refptr<const WebRequestConditionAttribute> attribute_include =
157 WebRequestConditionAttribute::Create( 158 WebRequestConditionAttribute::Create(
158 keys::kContentTypeKey, &content_types, &error); 159 keys::kContentTypeKey, &content_types, &error);
159 EXPECT_EQ("", error); 160 EXPECT_EQ("", error);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 ASSERT_EQ("", error); 224 ASSERT_EQ("", error);
224 ASSERT_TRUE(first_party_attribute.get()); 225 ASSERT_TRUE(first_party_attribute.get());
225 EXPECT_EQ(std::string(keys::kThirdPartyKey), 226 EXPECT_EQ(std::string(keys::kThirdPartyKey),
226 first_party_attribute->GetName()); 227 first_party_attribute->GetName());
227 228
228 const GURL url_empty; 229 const GURL url_empty;
229 const GURL url_a("http://a.com"); 230 const GURL url_a("http://a.com");
230 const GURL url_b("http://b.com"); 231 const GURL url_b("http://b.com");
231 net::TestURLRequestContext context; 232 net::TestURLRequestContext context;
232 net::TestDelegate delegate; 233 net::TestDelegate delegate;
233 scoped_ptr<net::URLRequest> url_request( 234 std::unique_ptr<net::URLRequest> url_request(
234 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate)); 235 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate));
235 236
236 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { 237 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) {
237 if (!(kActiveStages & i)) 238 if (!(kActiveStages & i))
238 continue; 239 continue;
239 const RequestStage stage = static_cast<RequestStage>(i); 240 const RequestStage stage = static_cast<RequestStage>(i);
240 url_request->set_first_party_for_cookies(url_empty); 241 url_request->set_first_party_for_cookies(url_empty);
241 EXPECT_FALSE(third_party_attribute->IsFulfilled( 242 EXPECT_FALSE(third_party_attribute->IsFulfilled(
242 WebRequestData(url_request.get(), stage))); 243 WebRequestData(url_request.get(), stage)));
243 EXPECT_TRUE(first_party_attribute->IsFulfilled( 244 EXPECT_TRUE(first_party_attribute->IsFulfilled(
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 WebRequestConditionAttribute::Create(keys::kStagesKey, 315 WebRequestConditionAttribute::Create(keys::kStagesKey,
315 &single_stage_list, 316 &single_stage_list,
316 &error)); 317 &error));
317 EXPECT_EQ("", error); 318 EXPECT_EQ("", error);
318 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); 319 ASSERT_TRUE(one_stage_attributes.back().get() != NULL);
319 } 320 }
320 321
321 const GURL url_empty; 322 const GURL url_empty;
322 net::TestURLRequestContext context; 323 net::TestURLRequestContext context;
323 net::TestDelegate delegate; 324 net::TestDelegate delegate;
324 scoped_ptr<net::URLRequest> url_request( 325 std::unique_ptr<net::URLRequest> url_request(
325 context.CreateRequest(url_empty, net::DEFAULT_PRIORITY, &delegate)); 326 context.CreateRequest(url_empty, net::DEFAULT_PRIORITY, &delegate));
326 327
327 for (size_t i = 0; i < arraysize(active_stages); ++i) { 328 for (size_t i = 0; i < arraysize(active_stages); ++i) {
328 EXPECT_FALSE(empty_attribute->IsFulfilled( 329 EXPECT_FALSE(empty_attribute->IsFulfilled(
329 WebRequestData(url_request.get(), active_stages[i].first))); 330 WebRequestData(url_request.get(), active_stages[i].first)));
330 331
331 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { 332 for (size_t j = 0; j < one_stage_attributes.size(); ++j) {
332 EXPECT_EQ(i == j, 333 EXPECT_EQ(i == j,
333 one_stage_attributes[j]->IsFulfilled( 334 one_stage_attributes[j]->IsFulfilled(
334 WebRequestData(url_request.get(), active_stages[i].first))); 335 WebRequestData(url_request.get(), active_stages[i].first)));
(...skipping 24 matching lines...) Expand all
359 out->push_back(std::vector<const std::string*>()); 360 out->push_back(std::vector<const std::string*>());
360 for (size_t j = next; j < next + sizes[i]; ++j) { 361 for (size_t j = next; j < next + sizes[i]; ++j) {
361 out->back().push_back(&(array[j])); 362 out->back().push_back(&(array[j]));
362 } 363 }
363 next += sizes[i]; 364 next += sizes[i];
364 } 365 }
365 } 366 }
366 367
367 // Builds a DictionaryValue from an array of the form {name1, value1, name2, 368 // Builds a DictionaryValue from an array of the form {name1, value1, name2,
368 // value2, ...}. Values for the same key are grouped in a ListValue. 369 // value2, ...}. Values for the same key are grouped in a ListValue.
369 scoped_ptr<base::DictionaryValue> GetDictionaryFromArray( 370 std::unique_ptr<base::DictionaryValue> GetDictionaryFromArray(
370 const std::vector<const std::string*>& array) { 371 const std::vector<const std::string*>& array) {
371 const size_t length = array.size(); 372 const size_t length = array.size();
372 CHECK(length % 2 == 0); 373 CHECK(length % 2 == 0);
373 374
374 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); 375 std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
375 for (size_t i = 0; i < length; i += 2) { 376 for (size_t i = 0; i < length; i += 2) {
376 const std::string* name = array[i]; 377 const std::string* name = array[i];
377 const std::string* value = array[i+1]; 378 const std::string* value = array[i+1];
378 if (dictionary->HasKey(*name)) { 379 if (dictionary->HasKey(*name)) {
379 base::Value* entry = NULL; 380 base::Value* entry = NULL;
380 scoped_ptr<base::Value> entry_owned; 381 std::unique_ptr<base::Value> entry_owned;
381 base::ListValue* list = NULL; 382 base::ListValue* list = NULL;
382 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) 383 if (!dictionary->GetWithoutPathExpansion(*name, &entry))
383 return scoped_ptr<base::DictionaryValue>(); 384 return std::unique_ptr<base::DictionaryValue>();
384 switch (entry->GetType()) { 385 switch (entry->GetType()) {
385 case base::Value::TYPE_STRING: 386 case base::Value::TYPE_STRING:
386 // Replace the present string with a list. 387 // Replace the present string with a list.
387 list = new base::ListValue; 388 list = new base::ListValue;
388 // Ignoring return value, we already verified the entry is there. 389 // Ignoring return value, we already verified the entry is there.
389 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); 390 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned);
390 list->Append(entry_owned.release()); 391 list->Append(entry_owned.release());
391 list->Append(new base::StringValue(*value)); 392 list->Append(new base::StringValue(*value));
392 dictionary->SetWithoutPathExpansion(*name, list); 393 dictionary->SetWithoutPathExpansion(*name, list);
393 break; 394 break;
394 case base::Value::TYPE_LIST: // Just append to the list. 395 case base::Value::TYPE_LIST: // Just append to the list.
395 CHECK(entry->GetAsList(&list)); 396 CHECK(entry->GetAsList(&list));
396 list->Append(new base::StringValue(*value)); 397 list->Append(new base::StringValue(*value));
397 break; 398 break;
398 default: 399 default:
399 NOTREACHED(); // We never put other Values here. 400 NOTREACHED(); // We never put other Values here.
400 return scoped_ptr<base::DictionaryValue>(); 401 return std::unique_ptr<base::DictionaryValue>();
401 } 402 }
402 } else { 403 } else {
403 dictionary->SetString(*name, *value); 404 dictionary->SetString(*name, *value);
404 } 405 }
405 } 406 }
406 return dictionary; 407 return dictionary;
407 } 408 }
408 409
409 // Returns whether the response headers from |url_request| satisfy the match 410 // Returns whether the response headers from |url_request| satisfy the match
410 // criteria given in |tests|. For at least one |i| all tests from |tests[i]| 411 // criteria given in |tests|. For at least one |i| all tests from |tests[i]|
411 // must pass. If |positive_test| is true, the dictionary is interpreted as the 412 // must pass. If |positive_test| is true, the dictionary is interpreted as the
412 // containsHeaders property of a RequestMatcher, otherwise as 413 // containsHeaders property of a RequestMatcher, otherwise as
413 // doesNotContainHeaders. 414 // doesNotContainHeaders.
414 void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests, 415 void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests,
415 const std::string& key, 416 const std::string& key,
416 RequestStage stage, 417 RequestStage stage,
417 net::URLRequest* url_request, 418 net::URLRequest* url_request,
418 bool* result) { 419 bool* result) {
419 base::ListValue contains_headers; 420 base::ListValue contains_headers;
420 for (size_t i = 0; i < tests.size(); ++i) { 421 for (size_t i = 0; i < tests.size(); ++i) {
421 scoped_ptr<base::DictionaryValue> temp(GetDictionaryFromArray(tests[i])); 422 std::unique_ptr<base::DictionaryValue> temp(
423 GetDictionaryFromArray(tests[i]));
422 ASSERT_TRUE(temp.get()); 424 ASSERT_TRUE(temp.get());
423 contains_headers.Append(temp.release()); 425 contains_headers.Append(temp.release());
424 } 426 }
425 427
426 std::string error; 428 std::string error;
427 scoped_refptr<const WebRequestConditionAttribute> attribute = 429 scoped_refptr<const WebRequestConditionAttribute> attribute =
428 WebRequestConditionAttribute::Create(key, &contains_headers, &error); 430 WebRequestConditionAttribute::Create(key, &contains_headers, &error);
429 ASSERT_EQ("", error); 431 ASSERT_EQ("", error);
430 ASSERT_TRUE(attribute.get()); 432 ASSERT_TRUE(attribute.get());
431 EXPECT_EQ(key, attribute->GetName()); 433 EXPECT_EQ(key, attribute->GetName());
432 434
433 *result = attribute->IsFulfilled(WebRequestData( 435 *result = attribute->IsFulfilled(WebRequestData(
434 url_request, stage, url_request->response_headers())); 436 url_request, stage, url_request->response_headers()));
435 } 437 }
436 438
437 } // namespace 439 } // namespace
438 440
439 // Here we test WebRequestConditionAttributeRequestHeaders for matching 441 // Here we test WebRequestConditionAttributeRequestHeaders for matching
440 // correctly against request headers. This test is not as extensive as 442 // correctly against request headers. This test is not as extensive as
441 // "ResponseHeaders" (below), because the header-matching code is shared 443 // "ResponseHeaders" (below), because the header-matching code is shared
442 // by both types of condition attributes, so it is enough to test it once. 444 // by both types of condition attributes, so it is enough to test it once.
443 TEST(WebRequestConditionAttributeTest, RequestHeaders) { 445 TEST(WebRequestConditionAttributeTest, RequestHeaders) {
444 // Necessary for TestURLRequest. 446 // Necessary for TestURLRequest.
445 base::MessageLoopForIO message_loop; 447 base::MessageLoopForIO message_loop;
446 448
447 net::TestURLRequestContext context; 449 net::TestURLRequestContext context;
448 net::TestDelegate delegate; 450 net::TestDelegate delegate;
449 scoped_ptr<net::URLRequest> url_request( 451 std::unique_ptr<net::URLRequest> url_request(
450 context.CreateRequest(GURL("http://example.com"), // Dummy URL. 452 context.CreateRequest(GURL("http://example.com"), // Dummy URL.
451 net::DEFAULT_PRIORITY, &delegate)); 453 net::DEFAULT_PRIORITY, &delegate));
452 url_request->SetExtraRequestHeaderByName( 454 url_request->SetExtraRequestHeaderByName(
453 "Custom-header", "custom/value", true /* overwrite */); 455 "Custom-header", "custom/value", true /* overwrite */);
454 url_request->Start(); 456 url_request->Start();
455 base::MessageLoop::current()->Run(); 457 base::MessageLoop::current()->Run();
456 458
457 std::vector<std::vector<const std::string*> > tests; 459 std::vector<std::vector<const std::string*> > tests;
458 bool result = false; 460 bool result = false;
459 461
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 // Necessary for TestURLRequest. 529 // Necessary for TestURLRequest.
528 base::MessageLoopForIO message_loop; 530 base::MessageLoopForIO message_loop;
529 531
530 net::EmbeddedTestServer test_server; 532 net::EmbeddedTestServer test_server;
531 test_server.ServeFilesFromDirectory(TestDataPath( 533 test_server.ServeFilesFromDirectory(TestDataPath(
532 "chrome/test/data/extensions/api_test/webrequest/declarative")); 534 "chrome/test/data/extensions/api_test/webrequest/declarative"));
533 ASSERT_TRUE(test_server.Start()); 535 ASSERT_TRUE(test_server.Start());
534 536
535 net::TestURLRequestContext context; 537 net::TestURLRequestContext context;
536 net::TestDelegate delegate; 538 net::TestDelegate delegate;
537 scoped_ptr<net::URLRequest> url_request(context.CreateRequest( 539 std::unique_ptr<net::URLRequest> url_request(context.CreateRequest(
538 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate)); 540 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate));
539 url_request->Start(); 541 url_request->Start();
540 base::MessageLoop::current()->Run(); 542 base::MessageLoop::current()->Run();
541 543
542 // In all the tests below we assume that the server includes the headers 544 // In all the tests below we assume that the server includes the headers
543 // Custom-Header: custom/value 545 // Custom-Header: custom/value
544 // Custom-Header-B: valueA 546 // Custom-Header-B: valueA
545 // Custom-Header-B: valueB 547 // Custom-Header-B: valueB
546 // Custom-Header-C: valueC, valueD 548 // Custom-Header-C: valueC, valueD
547 // Custom-Header-D: 549 // Custom-Header-D:
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 }; 712 };
711 const size_t kExistingSize[] = { arraysize(kExisting) }; 713 const size_t kExistingSize[] = { arraysize(kExisting) };
712 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); 714 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests);
713 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage, 715 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage,
714 url_request.get(), &result); 716 url_request.get(), &result);
715 EXPECT_FALSE(result); 717 EXPECT_FALSE(result);
716 } 718 }
717 719
718 } // namespace 720 } // namespace
719 } // namespace extensions 721 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698