| 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/application_connection.h" | |
| 17 #include "mojo/shell/public/cpp/application_impl.h" | 16 #include "mojo/shell/public/cpp/application_impl.h" |
| 18 #include "mojo/shell/public/cpp/connect.h" | 17 #include "mojo/shell/public/cpp/connection.h" |
| 19 #include "mojo/shell/public/cpp/interface_factory.h" | 18 #include "mojo/shell/public/cpp/interface_factory.h" |
| 20 #include "mojo/shell/public/cpp/service_provider_impl.h" | 19 #include "mojo/shell/public/cpp/service_provider_impl.h" |
| 21 | 20 |
| 22 namespace mojo { | 21 namespace mojo { |
| 23 namespace shell { | 22 namespace shell { |
| 24 namespace test { | 23 namespace test { |
| 25 | 24 |
| 26 // Lives on the main thread of the test. | 25 // Lives on the main thread of the test. |
| 27 // Listens for services exposed/blocked and for application connections being | 26 // Listens for services exposed/blocked and for application connections being |
| 28 // closed. Quits |loop| when all expectations are met. | 27 // closed. Quits |loop| when all expectations are met. |
| 29 class ConnectionValidator : public ApplicationLoader, | 28 class ConnectionValidator : public ApplicationLoader, |
| 30 public ApplicationDelegate, | 29 public ShellClient, |
| 31 public InterfaceFactory<Validator>, | 30 public InterfaceFactory<Validator>, |
| 32 public Validator { | 31 public Validator { |
| 33 public: | 32 public: |
| 34 ConnectionValidator(const std::set<std::string>& expectations, | 33 ConnectionValidator(const std::set<std::string>& expectations, |
| 35 base::MessageLoop* loop) | 34 base::MessageLoop* loop) |
| 36 : app_(nullptr), | 35 : app_(nullptr), |
| 37 expectations_(expectations), | 36 expectations_(expectations), |
| 38 loop_(loop) {} | 37 loop_(loop) {} |
| 39 ~ConnectionValidator() override {} | 38 ~ConnectionValidator() override {} |
| 40 | 39 |
| 41 bool expectations_met() { | 40 bool expectations_met() { |
| 42 return unexpected_.empty() && expectations_.empty(); | 41 return unexpected_.empty() && expectations_.empty(); |
| 43 } | 42 } |
| 44 | 43 |
| 45 void PrintUnmetExpectations() { | 44 void PrintUnmetExpectations() { |
| 46 for (auto expectation : expectations_) | 45 for (auto expectation : expectations_) |
| 47 ADD_FAILURE() << "Unmet: " << expectation; | 46 ADD_FAILURE() << "Unmet: " << expectation; |
| 48 for (auto unexpected : unexpected_) | 47 for (auto unexpected : unexpected_) |
| 49 ADD_FAILURE() << "Unexpected: " << unexpected; | 48 ADD_FAILURE() << "Unexpected: " << unexpected; |
| 50 } | 49 } |
| 51 | 50 |
| 52 private: | 51 private: |
| 53 // Overridden from ApplicationLoader: | 52 // Overridden from ApplicationLoader: |
| 54 void Load(const GURL& url, | 53 void Load(const GURL& url, |
| 55 InterfaceRequest<mojom::Application> request) override { | 54 InterfaceRequest<mojom::Application> request) override { |
| 56 app_.reset(new ApplicationImpl(this, std::move(request))); | 55 app_.reset(new ApplicationImpl(this, std::move(request))); |
| 57 } | 56 } |
| 58 | 57 |
| 59 // Overridden from ApplicationDelegate: | 58 // Overridden from ShellClient: |
| 60 bool AcceptConnection(ApplicationConnection* connection) override { | 59 bool AcceptConnection(Connection* connection) override { |
| 61 connection->AddService<Validator>(this); | 60 connection->AddService<Validator>(this); |
| 62 return true; | 61 return true; |
| 63 } | 62 } |
| 64 | 63 |
| 65 // Overridden from InterfaceFactory<Validator>: | 64 // Overridden from InterfaceFactory<Validator>: |
| 66 void Create(ApplicationConnection* connection, | 65 void Create(Connection* connection, |
| 67 InterfaceRequest<Validator> request) override { | 66 InterfaceRequest<Validator> request) override { |
| 68 validator_bindings_.AddBinding(this, std::move(request)); | 67 validator_bindings_.AddBinding(this, std::move(request)); |
| 69 } | 68 } |
| 70 | 69 |
| 71 // Overridden from Validator: | 70 // Overridden from Validator: |
| 72 void AddServiceCalled(const String& app_url, | 71 void AddServiceCalled(const String& app_url, |
| 73 const String& service_url, | 72 const String& service_url, |
| 74 const String& name, | 73 const String& name, |
| 75 bool blocked) override { | 74 bool blocked) override { |
| 76 Validate(base::StringPrintf("%s %s %s %s", | 75 Validate(base::StringPrintf("%s %s %s %s", |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 std::set<std::string> expectations_; | 99 std::set<std::string> expectations_; |
| 101 std::set<std::string> unexpected_; | 100 std::set<std::string> unexpected_; |
| 102 base::MessageLoop* loop_; | 101 base::MessageLoop* loop_; |
| 103 WeakBindingSet<Validator> validator_bindings_; | 102 WeakBindingSet<Validator> validator_bindings_; |
| 104 | 103 |
| 105 DISALLOW_COPY_AND_ASSIGN(ConnectionValidator); | 104 DISALLOW_COPY_AND_ASSIGN(ConnectionValidator); |
| 106 }; | 105 }; |
| 107 | 106 |
| 108 // This class models a system service that exposes two interfaces, Safe and | 107 // This class models a system service that exposes two interfaces, Safe and |
| 109 // Unsafe. The interface Unsafe is not to be exposed to untrusted applications. | 108 // Unsafe. The interface Unsafe is not to be exposed to untrusted applications. |
| 110 class ServiceApplication : public ApplicationDelegate, | 109 class ServiceApplication : public ShellClient, |
| 111 public InterfaceFactory<Safe>, | 110 public InterfaceFactory<Safe>, |
| 112 public InterfaceFactory<Unsafe>, | 111 public InterfaceFactory<Unsafe>, |
| 113 public Safe, | 112 public Safe, |
| 114 public Unsafe { | 113 public Unsafe { |
| 115 public: | 114 public: |
| 116 ServiceApplication() : shell_(nullptr) {} | 115 ServiceApplication() : shell_(nullptr) {} |
| 117 ~ServiceApplication() override {} | 116 ~ServiceApplication() override {} |
| 118 | 117 |
| 119 private: | 118 private: |
| 120 // Overridden from ApplicationDelegate: | 119 // Overridden from ShellClient: |
| 121 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { | 120 void Initialize(Shell* shell, const std::string& url, uint32_t id) override { |
| 122 shell_ = shell; | 121 shell_ = shell; |
| 123 // ServiceApplications have no capability filter and can thus connect | 122 // ServiceApplications have no capability filter and can thus connect |
| 124 // directly to the validator application. | 123 // directly to the validator application. |
| 125 shell_->ConnectToService("test:validator", &validator_); | 124 shell_->ConnectToService("test:validator", &validator_); |
| 126 } | 125 } |
| 127 bool AcceptConnection(ApplicationConnection* connection) override { | 126 bool AcceptConnection(Connection* connection) override { |
| 128 AddService<Safe>(connection); | 127 AddService<Safe>(connection); |
| 129 AddService<Unsafe>(connection); | 128 AddService<Unsafe>(connection); |
| 130 return true; | 129 return true; |
| 131 } | 130 } |
| 132 | 131 |
| 133 // Overridden from InterfaceFactory<Safe>: | 132 // Overridden from InterfaceFactory<Safe>: |
| 134 void Create(ApplicationConnection* connection, | 133 void Create(Connection* connection, |
| 135 InterfaceRequest<Safe> request) override { | 134 InterfaceRequest<Safe> request) override { |
| 136 safe_bindings_.AddBinding(this, std::move(request)); | 135 safe_bindings_.AddBinding(this, std::move(request)); |
| 137 } | 136 } |
| 138 | 137 |
| 139 // Overridden from InterfaceFactory<Unsafe>: | 138 // Overridden from InterfaceFactory<Unsafe>: |
| 140 void Create(ApplicationConnection* connection, | 139 void Create(Connection* connection, |
| 141 InterfaceRequest<Unsafe> request) override { | 140 InterfaceRequest<Unsafe> request) override { |
| 142 unsafe_bindings_.AddBinding(this, std::move(request)); | 141 unsafe_bindings_.AddBinding(this, std::move(request)); |
| 143 } | 142 } |
| 144 | 143 |
| 145 template <typename Interface> | 144 template <typename Interface> |
| 146 void AddService(ApplicationConnection* connection) { | 145 void AddService(Connection* connection) { |
| 147 validator_->AddServiceCalled(connection->GetRemoteApplicationURL(), | 146 validator_->AddServiceCalled(connection->GetRemoteApplicationURL(), |
| 148 connection->GetConnectionURL(), | 147 connection->GetConnectionURL(), |
| 149 Interface::Name_, | 148 Interface::Name_, |
| 150 !connection->AddService<Interface>(this)); | 149 !connection->AddService<Interface>(this)); |
| 151 } | 150 } |
| 152 | 151 |
| 153 Shell* shell_; | 152 Shell* shell_; |
| 154 ValidatorPtr validator_; | 153 ValidatorPtr validator_; |
| 155 WeakBindingSet<Safe> safe_bindings_; | 154 WeakBindingSet<Safe> safe_bindings_; |
| 156 WeakBindingSet<Unsafe> unsafe_bindings_; | 155 WeakBindingSet<Unsafe> unsafe_bindings_; |
| 157 | 156 |
| 158 DISALLOW_COPY_AND_ASSIGN(ServiceApplication); | 157 DISALLOW_COPY_AND_ASSIGN(ServiceApplication); |
| 159 }; | 158 }; |
| 160 | 159 |
| 161 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
| 162 // TestApplication: | 161 // TestApplication: |
| 163 | 162 |
| 164 TestApplication::TestApplication() : shell_(nullptr) {} | 163 TestApplication::TestApplication() : shell_(nullptr) {} |
| 165 TestApplication::~TestApplication() {} | 164 TestApplication::~TestApplication() {} |
| 166 | 165 |
| 167 void TestApplication::Initialize(Shell* shell, const std::string& url, | 166 void TestApplication::Initialize(Shell* shell, const std::string& url, |
| 168 uint32_t id) { | 167 uint32_t id) { |
| 169 shell_ = shell; | 168 shell_ = shell; |
| 170 url_ = url; | 169 url_ = url; |
| 171 } | 170 } |
| 172 bool TestApplication::AcceptConnection( | 171 bool TestApplication::AcceptConnection(Connection* connection) { |
| 173 ApplicationConnection* connection) { | |
| 174 // TestApplications receive their Validator via the inbound connection. | 172 // TestApplications receive their Validator via the inbound connection. |
| 175 connection->ConnectToService(&validator_); | 173 connection->ConnectToService(&validator_); |
| 176 | 174 |
| 177 connection1_ = shell_->ConnectToApplication("test:service"); | 175 connection1_ = shell_->ConnectToApplication("test:service"); |
| 178 connection1_->SetRemoteServiceProviderConnectionErrorHandler( | 176 connection1_->SetRemoteServiceProviderConnectionErrorHandler( |
| 179 base::Bind(&TestApplication::ConnectionClosed, | 177 base::Bind(&TestApplication::ConnectionClosed, |
| 180 base::Unretained(this), "test:service")); | 178 base::Unretained(this), "test:service")); |
| 181 | 179 |
| 182 connection2_ = shell_->ConnectToApplication("test:service2"); | 180 connection2_ = shell_->ConnectToApplication("test:service2"); |
| 183 connection2_->SetRemoteServiceProviderConnectionErrorHandler( | 181 connection2_->SetRemoteServiceProviderConnectionErrorHandler( |
| 184 base::Bind(&TestApplication::ConnectionClosed, | 182 base::Bind(&TestApplication::ConnectionClosed, |
| 185 base::Unretained(this), "test:service2")); | 183 base::Unretained(this), "test:service2")); |
| 186 return true; | 184 return true; |
| 187 } | 185 } |
| 188 | 186 |
| 189 void TestApplication::ConnectionClosed(const std::string& service_url) { | 187 void TestApplication::ConnectionClosed(const std::string& service_url) { |
| 190 validator_->ConnectionClosed(url_, service_url); | 188 validator_->ConnectionClosed(url_, service_url); |
| 191 } | 189 } |
| 192 | 190 |
| 193 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
| 194 // TestLoader: | 192 // TestLoader: |
| 195 | 193 |
| 196 TestLoader::TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {} | 194 TestLoader::TestLoader(ShellClient* delegate) : delegate_(delegate) {} |
| 197 TestLoader::~TestLoader() {} | 195 TestLoader::~TestLoader() {} |
| 198 | 196 |
| 199 void TestLoader::Load(const GURL& url, | 197 void TestLoader::Load(const GURL& url, |
| 200 InterfaceRequest<mojom::Application> request) { | 198 InterfaceRequest<mojom::Application> request) { |
| 201 app_.reset(new ApplicationImpl(delegate_.get(), std::move(request))); | 199 app_.reset(new ApplicationImpl(delegate_.get(), std::move(request))); |
| 202 } | 200 } |
| 203 | 201 |
| 204 //////////////////////////////////////////////////////////////////////////////// | 202 //////////////////////////////////////////////////////////////////////////////// |
| 205 // CapabilityFilterTest: | 203 // CapabilityFilterTest: |
| 206 | 204 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 void CapabilityFilterTest::RunTest() { | 321 void CapabilityFilterTest::RunTest() { |
| 324 loop()->Run(); | 322 loop()->Run(); |
| 325 EXPECT_TRUE(validator_->expectations_met()); | 323 EXPECT_TRUE(validator_->expectations_met()); |
| 326 if (!validator_->expectations_met()) | 324 if (!validator_->expectations_met()) |
| 327 validator_->PrintUnmetExpectations(); | 325 validator_->PrintUnmetExpectations(); |
| 328 } | 326 } |
| 329 | 327 |
| 330 } // namespace test | 328 } // namespace test |
| 331 } // namespace shell | 329 } // namespace shell |
| 332 } // namespace mojo | 330 } // namespace mojo |
| OLD | NEW |