| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/shell/capability_filter_test.h" | 5 #include "mojo/shell/capability_filter_test.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "mojo/common/weak_binding_set.h" | 12 #include "mojo/common/weak_binding_set.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 #include "mojo/shell/application_loader.h" | 14 #include "mojo/shell/application_loader.h" |
| 15 #include "mojo/shell/package_manager.h" | 15 #include "mojo/shell/package_manager.h" |
| 16 #include "mojo/shell/public/cpp/connection.h" | 16 #include "mojo/shell/public/cpp/connection.h" |
| 17 #include "mojo/shell/public/cpp/interface_factory.h" | 17 #include "mojo/shell/public/cpp/interface_factory.h" |
| 18 #include "mojo/shell/public/cpp/shell_connection.h" | 18 #include "mojo/shell/public/cpp/shell_connection.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace shell { | 21 namespace shell { |
| 22 namespace test { | 22 namespace test { |
| 23 | 23 |
| 24 // Lives on the main thread of the test. | 24 // Lives on the main thread of the test. |
| 25 // Listens for services exposed/blocked and for application connections being | 25 // Listens for interfaces exposed/blocked and for application connections being |
| 26 // closed. Quits |loop| when all expectations are met. | 26 // closed. Quits |loop| when all expectations are met. |
| 27 class ConnectionValidator : public ApplicationLoader, | 27 class ConnectionValidator : public ApplicationLoader, |
| 28 public ShellClient, | 28 public ShellClient, |
| 29 public InterfaceFactory<Validator>, | 29 public InterfaceFactory<Validator>, |
| 30 public Validator { | 30 public Validator { |
| 31 public: | 31 public: |
| 32 ConnectionValidator(const std::set<std::string>& expectations, | 32 ConnectionValidator(const std::set<std::string>& expectations, |
| 33 base::MessageLoop* loop) | 33 base::MessageLoop* loop) |
| 34 : app_(nullptr), | 34 : app_(nullptr), |
| 35 expectations_(expectations), | 35 expectations_(expectations), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Overridden from InterfaceFactory<Validator>: | 63 // Overridden from InterfaceFactory<Validator>: |
| 64 void Create(Connection* connection, | 64 void Create(Connection* connection, |
| 65 InterfaceRequest<Validator> request) override { | 65 InterfaceRequest<Validator> request) override { |
| 66 validator_bindings_.AddBinding(this, std::move(request)); | 66 validator_bindings_.AddBinding(this, std::move(request)); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Overridden from Validator: | 69 // Overridden from Validator: |
| 70 void AddServiceCalled(const String& app_url, | 70 void AddInterfaceCalled(const String& app_url, |
| 71 const String& service_url, | 71 const String& service_url, |
| 72 const String& name, | 72 const String& name, |
| 73 bool blocked) override { | 73 bool blocked) override { |
| 74 Validate(base::StringPrintf("%s %s %s %s", | 74 Validate(base::StringPrintf("%s %s %s %s", |
| 75 blocked ? "B" : "E", app_url.data(), service_url.data(), name.data())); | 75 blocked ? "B" : "E", app_url.data(), service_url.data(), name.data())); |
| 76 } | 76 } |
| 77 void ConnectionClosed(const String& app_url, | 77 void ConnectionClosed(const String& app_url, |
| 78 const String& service_url) override { | 78 const String& service_url) override { |
| 79 Validate(base::StringPrintf("C %s %s", app_url.data(), service_url.data())); | 79 Validate(base::StringPrintf("C %s %s", app_url.data(), service_url.data())); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void Validate(const std::string& result) { | 82 void Validate(const std::string& result) { |
| 83 DVLOG(1) << "Validate: " << result; | 83 DVLOG(1) << "Validate: " << result; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 // Overridden from ShellClient: | 118 // Overridden from ShellClient: |
| 119 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { | 119 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { |
| 120 shell_ = shell; | 120 shell_ = shell; |
| 121 // ServiceApplications have no capability filter and can thus connect | 121 // ServiceApplications have no capability filter and can thus connect |
| 122 // directly to the validator application. | 122 // directly to the validator application. |
| 123 shell_->ConnectToService("test:validator", &validator_); | 123 shell_->ConnectToService("test:validator", &validator_); |
| 124 } | 124 } |
| 125 bool AcceptConnection(Connection* connection) override { | 125 bool AcceptConnection(Connection* connection) override { |
| 126 AddService<Safe>(connection); | 126 AddInterface<Safe>(connection); |
| 127 AddService<Unsafe>(connection); | 127 AddInterface<Unsafe>(connection); |
| 128 return true; | 128 return true; |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Overridden from InterfaceFactory<Safe>: | 131 // Overridden from InterfaceFactory<Safe>: |
| 132 void Create(Connection* connection, | 132 void Create(Connection* connection, |
| 133 InterfaceRequest<Safe> request) override { | 133 InterfaceRequest<Safe> request) override { |
| 134 safe_bindings_.AddBinding(this, std::move(request)); | 134 safe_bindings_.AddBinding(this, std::move(request)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Overridden from InterfaceFactory<Unsafe>: | 137 // Overridden from InterfaceFactory<Unsafe>: |
| 138 void Create(Connection* connection, | 138 void Create(Connection* connection, |
| 139 InterfaceRequest<Unsafe> request) override { | 139 InterfaceRequest<Unsafe> request) override { |
| 140 unsafe_bindings_.AddBinding(this, std::move(request)); | 140 unsafe_bindings_.AddBinding(this, std::move(request)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 template <typename Interface> | 143 template <typename Interface> |
| 144 void AddService(Connection* connection) { | 144 void AddInterface(Connection* connection) { |
| 145 validator_->AddServiceCalled(connection->GetRemoteApplicationURL(), | 145 validator_->AddInterfaceCalled(connection->GetRemoteApplicationURL(), |
| 146 connection->GetConnectionURL(), | 146 connection->GetConnectionURL(), |
| 147 Interface::Name_, | 147 Interface::Name_, |
| 148 !connection->AddInterface<Interface>(this)); | 148 !connection->AddInterface<Interface>(this)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 Shell* shell_; | 151 Shell* shell_; |
| 152 ValidatorPtr validator_; | 152 ValidatorPtr validator_; |
| 153 WeakBindingSet<Safe> safe_bindings_; | 153 WeakBindingSet<Safe> safe_bindings_; |
| 154 WeakBindingSet<Unsafe> unsafe_bindings_; | 154 WeakBindingSet<Unsafe> unsafe_bindings_; |
| 155 | 155 |
| 156 DISALLOW_COPY_AND_ASSIGN(ServiceApplication); | 156 DISALLOW_COPY_AND_ASSIGN(ServiceApplication); |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 //////////////////////////////////////////////////////////////////////////////// | 159 //////////////////////////////////////////////////////////////////////////////// |
| 160 // TestApplication: | 160 // TestApplication: |
| 161 | 161 |
| 162 TestApplication::TestApplication() : shell_(nullptr) {} | 162 TestApplication::TestApplication() : shell_(nullptr) {} |
| 163 TestApplication::~TestApplication() {} | 163 TestApplication::~TestApplication() {} |
| 164 | 164 |
| 165 void TestApplication::Initialize(Shell* shell, const std::string& url, | 165 void TestApplication::Initialize(Shell* shell, const std::string& url, |
| 166 uint32_t id) { | 166 uint32_t id) { |
| 167 shell_ = shell; | 167 shell_ = shell; |
| 168 url_ = url; | 168 url_ = url; |
| 169 } | 169 } |
| 170 bool TestApplication::AcceptConnection(Connection* connection) { | 170 bool TestApplication::AcceptConnection(Connection* connection) { |
| 171 // TestApplications receive their Validator via the inbound connection. | 171 // TestApplications receive their Validator via the inbound connection. |
| 172 connection->GetInterface(&validator_); | 172 connection->GetInterface(&validator_); |
| 173 | 173 |
| 174 connection1_ = shell_->Connect("test:service"); | 174 connection1_ = shell_->Connect("test:service"); |
| 175 connection1_->SetRemoteServiceProviderConnectionErrorHandler( | 175 connection1_->SetRemoteInterfaceProviderConnectionErrorHandler( |
| 176 base::Bind(&TestApplication::ConnectionClosed, | 176 base::Bind(&TestApplication::ConnectionClosed, |
| 177 base::Unretained(this), "test:service")); | 177 base::Unretained(this), "test:service")); |
| 178 | 178 |
| 179 connection2_ = shell_->Connect("test:service2"); | 179 connection2_ = shell_->Connect("test:service2"); |
| 180 connection2_->SetRemoteServiceProviderConnectionErrorHandler( | 180 connection2_->SetRemoteInterfaceProviderConnectionErrorHandler( |
| 181 base::Bind(&TestApplication::ConnectionClosed, | 181 base::Bind(&TestApplication::ConnectionClosed, |
| 182 base::Unretained(this), "test:service2")); | 182 base::Unretained(this), "test:service2")); |
| 183 return true; | 183 return true; |
| 184 } | 184 } |
| 185 | 185 |
| 186 void TestApplication::ConnectionClosed(const std::string& service_url) { | 186 void TestApplication::ConnectionClosed(const std::string& service_url) { |
| 187 validator_->ConnectionClosed(url_, service_url); | 187 validator_->ConnectionClosed(url_, service_url); |
| 188 } | 188 } |
| 189 | 189 |
| 190 //////////////////////////////////////////////////////////////////////////////// | 190 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 application_manager_.reset( | 284 application_manager_.reset( |
| 285 new ApplicationManager(make_scoped_ptr(CreatePackageManager()))); | 285 new ApplicationManager(make_scoped_ptr(CreatePackageManager()))); |
| 286 CreateLoader<ServiceApplication>("test:service"); | 286 CreateLoader<ServiceApplication>("test:service"); |
| 287 CreateLoader<ServiceApplication>("test:service2"); | 287 CreateLoader<ServiceApplication>("test:service2"); |
| 288 } | 288 } |
| 289 | 289 |
| 290 void CapabilityFilterTest::TearDown() { | 290 void CapabilityFilterTest::TearDown() { |
| 291 application_manager_.reset(); | 291 application_manager_.reset(); |
| 292 } | 292 } |
| 293 | 293 |
| 294 class ServiceProviderImpl : public ServiceProvider { | 294 class InterfaceProviderImpl : public InterfaceProvider { |
| 295 public: | 295 public: |
| 296 explicit ServiceProviderImpl( | 296 explicit InterfaceProviderImpl( |
| 297 InterfaceRequest<ServiceProvider> service_provider, | 297 InterfaceRequest<InterfaceProvider> interfaces, |
| 298 InterfaceFactory<Validator>* factory) | 298 InterfaceFactory<Validator>* factory) |
| 299 : binding_(this, std::move(service_provider)), | 299 : binding_(this, std::move(interfaces)), |
| 300 factory_(factory) {} | 300 factory_(factory) {} |
| 301 ~ServiceProviderImpl() override {} | 301 ~InterfaceProviderImpl() override {} |
| 302 | 302 |
| 303 private: | 303 private: |
| 304 // ServiceProvider method. | 304 // InterfaceProvider method. |
| 305 void ConnectToService(const mojo::String& service_name, | 305 void GetInterface(const mojo::String& interface_name, |
| 306 ScopedMessagePipeHandle client_handle) override { | 306 ScopedMessagePipeHandle client_handle) override { |
| 307 if (service_name == Validator::Name_) { | 307 if (interface_name == Validator::Name_) { |
| 308 factory_->Create(nullptr, | 308 factory_->Create(nullptr, |
| 309 MakeRequest<Validator>(std::move(client_handle))); | 309 MakeRequest<Validator>(std::move(client_handle))); |
| 310 } | 310 } |
| 311 } | 311 } |
| 312 | 312 |
| 313 Binding<ServiceProvider> binding_; | 313 Binding<InterfaceProvider> binding_; |
| 314 InterfaceFactory<Validator>* factory_; | 314 InterfaceFactory<Validator>* factory_; |
| 315 | 315 |
| 316 DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl); | 316 DISALLOW_COPY_AND_ASSIGN(InterfaceProviderImpl); |
| 317 }; | 317 }; |
| 318 | 318 |
| 319 void CapabilityFilterTest::RunApplication(const std::string& url, | 319 void CapabilityFilterTest::RunApplication(const std::string& url, |
| 320 const CapabilityFilter& filter) { | 320 const CapabilityFilter& filter) { |
| 321 ServiceProviderPtr services; | 321 InterfaceProviderPtr remote_interfaces; |
| 322 | 322 |
| 323 // We expose Validator to the test application via ConnectToApplication | 323 // We expose Validator to the test application via ConnectToApplication |
| 324 // because we don't allow the test application to connect to test:validator. | 324 // because we don't allow the test application to connect to test:validator. |
| 325 // Adding it to the CapabilityFilter would interfere with the test. | 325 // Adding it to the CapabilityFilter would interfere with the test. |
| 326 ServiceProviderPtr exposed_services; | 326 InterfaceProviderPtr local_interfaces; |
| 327 new ServiceProviderImpl(GetProxy(&exposed_services), validator_); | 327 new InterfaceProviderImpl(GetProxy(&local_interfaces), validator_); |
| 328 scoped_ptr<ConnectToApplicationParams> params( | 328 scoped_ptr<ConnectToApplicationParams> params( |
| 329 new ConnectToApplicationParams); | 329 new ConnectToApplicationParams); |
| 330 params->SetTarget(Identity(GURL(url), std::string(), filter)); | 330 params->SetTarget(Identity(GURL(url), std::string(), filter)); |
| 331 params->set_services(GetProxy(&services)); | 331 params->set_remote_interfaces(GetProxy(&remote_interfaces)); |
| 332 params->set_exposed_services(std::move(exposed_services)); | 332 params->set_local_interfaces(std::move(local_interfaces)); |
| 333 params->set_on_application_end(base::MessageLoop::QuitWhenIdleClosure()); | 333 params->set_on_application_end(base::MessageLoop::QuitWhenIdleClosure()); |
| 334 application_manager_->ConnectToApplication(std::move(params)); | 334 application_manager_->ConnectToApplication(std::move(params)); |
| 335 } | 335 } |
| 336 | 336 |
| 337 void CapabilityFilterTest::InitValidator( | 337 void CapabilityFilterTest::InitValidator( |
| 338 const std::set<std::string>& expectations) { | 338 const std::set<std::string>& expectations) { |
| 339 validator_ = new ConnectionValidator(expectations, &loop_); | 339 validator_ = new ConnectionValidator(expectations, &loop_); |
| 340 application_manager()->SetLoaderForURL(make_scoped_ptr(validator_), | 340 application_manager()->SetLoaderForURL(make_scoped_ptr(validator_), |
| 341 GURL("test:validator")); | 341 GURL("test:validator")); |
| 342 } | 342 } |
| 343 | 343 |
| 344 void CapabilityFilterTest::RunTest() { | 344 void CapabilityFilterTest::RunTest() { |
| 345 loop()->Run(); | 345 loop()->Run(); |
| 346 EXPECT_TRUE(validator_->expectations_met()); | 346 EXPECT_TRUE(validator_->expectations_met()); |
| 347 if (!validator_->expectations_met()) | 347 if (!validator_->expectations_met()) |
| 348 validator_->PrintUnmetExpectations(); | 348 validator_->PrintUnmetExpectations(); |
| 349 } | 349 } |
| 350 | 350 |
| 351 } // namespace test | 351 } // namespace test |
| 352 } // namespace shell | 352 } // namespace shell |
| 353 } // namespace mojo | 353 } // namespace mojo |
| OLD | NEW |