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

Side by Side Diff: extensions/renderer/api_binding_unittest.cc

Issue 2821793003: [Extensions Bindings] Apply per-context restrictions to events (Closed)
Patch Set: . Created 3 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/ptr_util.h" 6 #include "base/memory/ptr_util.h"
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "extensions/renderer/api_binding.h" 10 #include "extensions/renderer/api_binding.h"
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 : type_refs_(APITypeReferenceMap::InitializeTypeCallback()) {} 140 : type_refs_(APITypeReferenceMap::InitializeTypeCallback()) {}
141 void SetUp() override { 141 void SetUp() override {
142 APIBindingTest::SetUp(); 142 APIBindingTest::SetUp();
143 request_handler_ = base::MakeUnique<APIRequestHandler>( 143 request_handler_ = base::MakeUnique<APIRequestHandler>(
144 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), 144 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)),
145 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), 145 base::Bind(&RunFunctionOnGlobalAndIgnoreResult),
146 APILastError(APILastError::GetParent())); 146 APILastError(APILastError::GetParent()));
147 } 147 }
148 148
149 void TearDown() override { 149 void TearDown() override {
150 DisposeAllContexts();
150 request_handler_.reset(); 151 request_handler_.reset();
151 event_handler_.reset(); 152 event_handler_.reset();
152 binding_.reset(); 153 binding_.reset();
153 APIBindingTest::TearDown(); 154 APIBindingTest::TearDown();
154 } 155 }
155 156
157 void OnWillDisposeContext(v8::Local<v8::Context> context) override {
158 event_handler_->InvalidateContext(context);
159 request_handler_->InvalidateContext(context);
160 }
161
156 void SetFunctions(const char* functions) { 162 void SetFunctions(const char* functions) {
157 binding_functions_ = ListValueFromString(functions); 163 binding_functions_ = ListValueFromString(functions);
158 ASSERT_TRUE(binding_functions_); 164 ASSERT_TRUE(binding_functions_);
159 } 165 }
160 166
161 void SetEvents(const char* events) { 167 void SetEvents(const char* events) {
162 binding_events_ = ListValueFromString(events); 168 binding_events_ = ListValueFromString(events);
163 ASSERT_TRUE(binding_events_); 169 ASSERT_TRUE(binding_events_);
164 } 170 }
165 171
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", 495 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})",
490 kError); 496 kError);
491 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']", false); 497 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']", false);
492 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']", false); 498 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']", false);
493 ExpectPass(binding_object, "obj.takesRefEnum(obj.refEnum.BETA)", "['beta']", 499 ExpectPass(binding_object, "obj.takesRefEnum(obj.refEnum.BETA)", "['beta']",
494 false); 500 false);
495 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); 501 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError);
496 } 502 }
497 503
498 TEST_F(APIBindingUnittest, RestrictedAPIs) { 504 TEST_F(APIBindingUnittest, RestrictedAPIs) {
499 const char kRestrictedFunctions[] = 505 const char kFunctions[] =
500 "[{" 506 "[{"
501 " 'name': 'allowedOne'," 507 " 'name': 'allowedOne',"
502 " 'parameters': []" 508 " 'parameters': []"
503 "}, {" 509 "}, {"
504 " 'name': 'allowedTwo'," 510 " 'name': 'allowedTwo',"
505 " 'parameters': []" 511 " 'parameters': []"
506 "}, {" 512 "}, {"
507 " 'name': 'restrictedOne'," 513 " 'name': 'restrictedOne',"
508 " 'parameters': []" 514 " 'parameters': []"
509 "}, {" 515 "}, {"
510 " 'name': 'restrictedTwo'," 516 " 'name': 'restrictedTwo',"
511 " 'parameters': []" 517 " 'parameters': []"
512 "}]"; 518 "}]";
513 SetFunctions(kRestrictedFunctions); 519 SetFunctions(kFunctions);
520 const char kEvents[] =
521 "[{'name': 'allowedEvent'}, {'name': 'restrictedEvent'}]";
522 SetEvents(kEvents);
514 InitializeBinding(); 523 InitializeBinding();
515 524
516 v8::HandleScope handle_scope(isolate()); 525 v8::HandleScope handle_scope(isolate());
517 v8::Local<v8::Context> context = MainContext(); 526 v8::Local<v8::Context> context = MainContext();
518 527
519 auto is_available = [](const std::string& name) { 528 auto is_available = [](const std::string& name) {
520 std::set<std::string> functions = {"test.allowedOne", "test.allowedTwo", 529 std::set<std::string> allowed = {"test.allowedOne", "test.allowedTwo",
521 "test.restrictedOne", 530 "test.allowedEvent"};
522 "test.restrictedTwo"}; 531 std::set<std::string> restricted = {
523 EXPECT_TRUE(functions.count(name)); 532 "test.restrictedOne", "test.restrictedTwo", "test.restrictedEvent"};
524 return name == "test.allowedOne" || name == "test.allowedTwo"; 533 EXPECT_TRUE(allowed.count(name) || restricted.count(name)) << name;
534 return allowed.count(name) != 0;
525 }; 535 };
526 536
527 v8::Local<v8::Object> binding_object = 537 v8::Local<v8::Object> binding_object =
528 binding()->CreateInstance(context, isolate(), base::Bind(is_available)); 538 binding()->CreateInstance(context, isolate(), base::Bind(is_available));
529 539
530 auto is_defined = [&binding_object, context](const std::string& name) { 540 auto is_defined = [&binding_object, context](const std::string& name) {
531 v8::Local<v8::Value> val = 541 v8::Local<v8::Value> val =
532 GetPropertyFromObject(binding_object, context, name); 542 GetPropertyFromObject(binding_object, context, name);
533 EXPECT_FALSE(val.IsEmpty()); 543 EXPECT_FALSE(val.IsEmpty());
534 return !val->IsUndefined() && !val->IsNull(); 544 return !val->IsUndefined() && !val->IsNull();
535 }; 545 };
536 546
537 EXPECT_TRUE(is_defined("allowedOne")); 547 EXPECT_TRUE(is_defined("allowedOne"));
538 EXPECT_TRUE(is_defined("allowedTwo")); 548 EXPECT_TRUE(is_defined("allowedTwo"));
549 EXPECT_TRUE(is_defined("allowedEvent"));
539 EXPECT_FALSE(is_defined("restrictedOne")); 550 EXPECT_FALSE(is_defined("restrictedOne"));
540 EXPECT_FALSE(is_defined("restrictedTwo")); 551 EXPECT_FALSE(is_defined("restrictedTwo"));
552 EXPECT_FALSE(is_defined("restrictedEvent"));
541 } 553 }
542 554
543 // Tests that events specified in the API are created as properties of the API 555 // Tests that events specified in the API are created as properties of the API
544 // object. 556 // object.
545 TEST_F(APIBindingUnittest, TestEventCreation) { 557 TEST_F(APIBindingUnittest, TestEventCreation) {
546 SetEvents("[{'name': 'onFoo'}, {'name': 'onBar'}]"); 558 SetEvents("[{'name': 'onFoo'}, {'name': 'onBar'}]");
547 SetFunctions(kFunctions); 559 SetFunctions(kFunctions);
548 InitializeBinding(); 560 InitializeBinding();
549 561
550 v8::HandleScope handle_scope(isolate()); 562 v8::HandleScope handle_scope(isolate());
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 1168
1157 blink::WebScopedUserGesture user_gesture(nullptr); 1169 blink::WebScopedUserGesture user_gesture(nullptr);
1158 RunFunction(function, context, arraysize(argv), argv); 1170 RunFunction(function, context, arraysize(argv), argv);
1159 ASSERT_TRUE(last_request()); 1171 ASSERT_TRUE(last_request());
1160 EXPECT_TRUE(last_request()->has_user_gesture); 1172 EXPECT_TRUE(last_request()->has_user_gesture);
1161 1173
1162 reset_last_request(); 1174 reset_last_request();
1163 } 1175 }
1164 1176
1165 } // namespace extensions 1177 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding.cc ('k') | extensions/renderer/native_extension_bindings_system_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698