OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/shell/capability_filter_test.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "mojo/application/public/cpp/application_connection.h" |
| 10 #include "mojo/application/public/cpp/application_impl.h" |
| 11 #include "mojo/application/public/cpp/connect.h" |
| 12 #include "mojo/application/public/cpp/interface_factory.h" |
| 13 #include "mojo/application/public/cpp/service_provider_impl.h" |
| 14 #include "mojo/common/weak_binding_set.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 16 #include "mojo/shell/application_loader.h" |
| 17 #include "mojo/shell/package_manager.h" |
| 18 |
| 19 namespace mojo { |
| 20 namespace shell { |
| 21 namespace test { |
| 22 |
| 23 // Lives on the main thread of the test. |
| 24 // Listens for services exposed/blocked and for application connections being |
| 25 // closed. Quits |loop| when all expectations are met. |
| 26 class ConnectionValidator : public ApplicationLoader, |
| 27 public ApplicationDelegate, |
| 28 public InterfaceFactory<Validator>, |
| 29 public Validator { |
| 30 public: |
| 31 ConnectionValidator(const std::set<std::string>& expectations, |
| 32 base::MessageLoop* loop) |
| 33 : app_(nullptr), |
| 34 expectations_(expectations), |
| 35 loop_(loop) {} |
| 36 ~ConnectionValidator() override {} |
| 37 |
| 38 bool expectations_met() { |
| 39 return unexpected_.empty() && expectations_.empty(); |
| 40 } |
| 41 |
| 42 void PrintUnmetExpectations() { |
| 43 for (auto expectation : expectations_) |
| 44 ADD_FAILURE() << "Unmet: " << expectation; |
| 45 for (auto unexpected : unexpected_) |
| 46 ADD_FAILURE() << "Unexpected: " << unexpected; |
| 47 } |
| 48 |
| 49 private: |
| 50 // Overridden from ApplicationLoader: |
| 51 void Load(const GURL& url, InterfaceRequest<Application> request) override { |
| 52 app_.reset(new ApplicationImpl(this, request.Pass())); |
| 53 } |
| 54 |
| 55 // Overridden from ApplicationDelegate: |
| 56 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 57 connection->AddService<Validator>(this); |
| 58 return true; |
| 59 } |
| 60 |
| 61 // Overridden from InterfaceFactory<Validator>: |
| 62 void Create(ApplicationConnection* connection, |
| 63 InterfaceRequest<Validator> request) override { |
| 64 validator_bindings_.AddBinding(this, request.Pass()); |
| 65 } |
| 66 |
| 67 // Overridden from Validator: |
| 68 void AddServiceCalled(const String& app_url, |
| 69 const String& service_url, |
| 70 const String& name, |
| 71 bool blocked) override { |
| 72 Validate(base::StringPrintf("%s %s %s %s", |
| 73 blocked ? "B" : "E", app_url.data(), service_url.data(), name.data())); |
| 74 } |
| 75 void ConnectionClosed(const String& app_url, |
| 76 const String& service_url) override { |
| 77 Validate(base::StringPrintf("C %s %s", app_url.data(), service_url.data())); |
| 78 } |
| 79 |
| 80 void Validate(const std::string& result) { |
| 81 DVLOG(1) << "Validate: " << result; |
| 82 auto i = expectations_.find(result); |
| 83 if (i != expectations_.end()) { |
| 84 expectations_.erase(i); |
| 85 if (expectations_.empty()) |
| 86 loop_->Quit(); |
| 87 } else { |
| 88 // This is a test failure, and will result in PrintUnexpectedExpecations() |
| 89 // being called. |
| 90 unexpected_.insert(result); |
| 91 loop_->Quit(); |
| 92 } |
| 93 } |
| 94 |
| 95 scoped_ptr<ApplicationImpl> app_; |
| 96 std::set<std::string> expectations_; |
| 97 std::set<std::string> unexpected_; |
| 98 base::MessageLoop* loop_; |
| 99 WeakBindingSet<Validator> validator_bindings_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(ConnectionValidator); |
| 102 }; |
| 103 |
| 104 // This class models a system service that exposes two interfaces, Safe and |
| 105 // Unsafe. The interface Unsafe is not to be exposed to untrusted applications. |
| 106 class ServiceApplication : public ApplicationDelegate, |
| 107 public InterfaceFactory<Safe>, |
| 108 public InterfaceFactory<Unsafe>, |
| 109 public Safe, |
| 110 public Unsafe { |
| 111 public: |
| 112 ServiceApplication() : app_(nullptr) {} |
| 113 ~ServiceApplication() override {} |
| 114 |
| 115 private: |
| 116 // Overridden from ApplicationDelegate: |
| 117 void Initialize(ApplicationImpl* app) override { |
| 118 app_ = app; |
| 119 // ServiceApplications have no capability filter and can thus connect |
| 120 // directly to the validator application. |
| 121 URLRequestPtr request(URLRequest::New()); |
| 122 request->url = String::From("test:validator"); |
| 123 app_->ConnectToService(request.Pass(), &validator_); |
| 124 } |
| 125 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 126 AddService<Safe>(connection); |
| 127 AddService<Unsafe>(connection); |
| 128 return true; |
| 129 } |
| 130 |
| 131 // Overridden from InterfaceFactory<Safe>: |
| 132 void Create(ApplicationConnection* connection, |
| 133 InterfaceRequest<Safe> request) override { |
| 134 safe_bindings_.AddBinding(this, request.Pass()); |
| 135 } |
| 136 |
| 137 // Overridden from InterfaceFactory<Unsafe>: |
| 138 void Create(ApplicationConnection* connection, |
| 139 InterfaceRequest<Unsafe> request) override { |
| 140 unsafe_bindings_.AddBinding(this, request.Pass()); |
| 141 } |
| 142 |
| 143 template <typename Interface> |
| 144 void AddService(ApplicationConnection* connection) { |
| 145 validator_->AddServiceCalled(connection->GetRemoteApplicationURL(), |
| 146 connection->GetConnectionURL(), |
| 147 Interface::Name_, |
| 148 !connection->AddService<Interface>(this)); |
| 149 } |
| 150 |
| 151 ApplicationImpl* app_; |
| 152 ValidatorPtr validator_; |
| 153 WeakBindingSet<Safe> safe_bindings_; |
| 154 WeakBindingSet<Unsafe> unsafe_bindings_; |
| 155 |
| 156 DISALLOW_COPY_AND_ASSIGN(ServiceApplication); |
| 157 }; |
| 158 |
| 159 //////////////////////////////////////////////////////////////////////////////// |
| 160 // TestApplication: |
| 161 |
| 162 TestApplication::TestApplication() : app_(nullptr) {} |
| 163 TestApplication::~TestApplication() {} |
| 164 |
| 165 void TestApplication::Initialize(ApplicationImpl* app) { |
| 166 app_ = app; |
| 167 } |
| 168 bool TestApplication::ConfigureIncomingConnection( |
| 169 ApplicationConnection* connection) { |
| 170 // TestApplications receive their Validator via the inbound connection. |
| 171 connection->ConnectToService(&validator_); |
| 172 |
| 173 URLRequestPtr request(URLRequest::New()); |
| 174 request->url = String::From("test:service"); |
| 175 connection1_ = app_->ConnectToApplication(request.Pass()); |
| 176 connection1_->SetRemoteServiceProviderConnectionErrorHandler( |
| 177 base::Bind(&TestApplication::ConnectionClosed, |
| 178 base::Unretained(this), "test:service")); |
| 179 |
| 180 URLRequestPtr request2(URLRequest::New()); |
| 181 request2->url = String::From("test:service2"); |
| 182 connection2_ = app_->ConnectToApplication(request2.Pass()); |
| 183 connection2_->SetRemoteServiceProviderConnectionErrorHandler( |
| 184 base::Bind(&TestApplication::ConnectionClosed, |
| 185 base::Unretained(this), "test:service2")); |
| 186 return true; |
| 187 } |
| 188 |
| 189 void TestApplication::ConnectionClosed(const std::string& service_url) { |
| 190 validator_->ConnectionClosed(app_->url(), service_url); |
| 191 } |
| 192 |
| 193 //////////////////////////////////////////////////////////////////////////////// |
| 194 // TestLoader: |
| 195 |
| 196 TestLoader::TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {} |
| 197 TestLoader::~TestLoader() {} |
| 198 |
| 199 void TestLoader::Load(const GURL& url, |
| 200 InterfaceRequest<Application> request) { |
| 201 app_.reset(new ApplicationImpl(delegate_.get(), request.Pass())); |
| 202 } |
| 203 |
| 204 //////////////////////////////////////////////////////////////////////////////// |
| 205 // CapabilityFilterTest: |
| 206 |
| 207 CapabilityFilterTest::CapabilityFilterTest() : validator_(nullptr) {} |
| 208 CapabilityFilterTest::~CapabilityFilterTest() {} |
| 209 |
| 210 void CapabilityFilterTest::RunBlockingTest() { |
| 211 std::set<std::string> expectations; |
| 212 expectations.insert("E test:trusted test:service mojo::shell::Safe"); |
| 213 expectations.insert("E test:trusted test:service mojo::shell::Unsafe"); |
| 214 expectations.insert("E test:trusted test:service2 mojo::shell::Safe"); |
| 215 expectations.insert("E test:trusted test:service2 mojo::shell::Unsafe"); |
| 216 expectations.insert("E test:untrusted test:service mojo::shell::Safe"); |
| 217 expectations.insert("B test:untrusted test:service mojo::shell::Unsafe"); |
| 218 expectations.insert("C test:untrusted test:service2"); |
| 219 InitValidator(expectations); |
| 220 |
| 221 // This first application can only connect to test:service. Connections to |
| 222 // test:service2 will be blocked. It also will only be able to see the |
| 223 // "Safe" interface exposed by test:service. It will be blocked from seeing |
| 224 // "Unsafe". |
| 225 AllowedInterfaces interfaces; |
| 226 interfaces.insert(Safe::Name_); |
| 227 CapabilityFilter filter; |
| 228 filter["test:service"] = interfaces; |
| 229 RunApplication("test:untrusted", filter); |
| 230 |
| 231 // This second application can connect to both test:service and |
| 232 // test:service2. It can connect to both "Safe" and "Unsafe" interfaces. |
| 233 RunApplication("test:trusted", GetPermissiveCapabilityFilter()); |
| 234 |
| 235 RunTest(); |
| 236 } |
| 237 |
| 238 void CapabilityFilterTest::RunWildcardTest() { |
| 239 std::set<std::string> expectations; |
| 240 expectations.insert("E test:wildcard test:service mojo::shell::Safe"); |
| 241 expectations.insert("E test:wildcard test:service mojo::shell::Unsafe"); |
| 242 expectations.insert("E test:wildcard test:service2 mojo::shell::Safe"); |
| 243 expectations.insert("E test:wildcard test:service2 mojo::shell::Unsafe"); |
| 244 expectations.insert("C test:blocked test:service"); |
| 245 expectations.insert("C test:blocked test:service2"); |
| 246 expectations.insert("B test:wildcard2 test:service mojo::shell::Safe"); |
| 247 expectations.insert("B test:wildcard2 test:service mojo::shell::Unsafe"); |
| 248 expectations.insert("B test:wildcard2 test:service2 mojo::shell::Safe"); |
| 249 expectations.insert("B test:wildcard2 test:service2 mojo::shell::Unsafe"); |
| 250 expectations.insert("E test:wildcard3 test:service mojo::shell::Safe"); |
| 251 expectations.insert("E test:wildcard3 test:service mojo::shell::Unsafe"); |
| 252 expectations.insert("E test:wildcard3 test:service2 mojo::shell::Safe"); |
| 253 expectations.insert("B test:wildcard3 test:service2 mojo::shell::Unsafe"); |
| 254 InitValidator(expectations); |
| 255 |
| 256 // This application is allowed to connect to any application because of a |
| 257 // wildcard rule, and any interface exposed because of a wildcard rule in |
| 258 // the interface array. |
| 259 RunApplication("test:wildcard", GetPermissiveCapabilityFilter()); |
| 260 |
| 261 // This application is allowed to connect to no other applications because |
| 262 // of an empty capability filter. |
| 263 RunApplication("test:blocked", CapabilityFilter()); |
| 264 |
| 265 // This application is allowed to connect to any application because of a |
| 266 // wildcard rule but may not connect to any interfaces because of an empty |
| 267 // interface array. |
| 268 CapabilityFilter filter1; |
| 269 filter1["*"] = AllowedInterfaces(); |
| 270 RunApplication("test:wildcard2", filter1); |
| 271 |
| 272 // This application is allowed to connect to both test:service and |
| 273 // test:service2, and may see any interface exposed by test:service but only |
| 274 // the Safe interface exposed by test:service2. |
| 275 AllowedInterfaces interfaces2; |
| 276 interfaces2.insert("*"); |
| 277 CapabilityFilter filter2; |
| 278 filter2["test:service"] = interfaces2; |
| 279 AllowedInterfaces interfaces3; |
| 280 interfaces3.insert(Safe::Name_); |
| 281 filter2["test:service2"] = interfaces3; |
| 282 RunApplication("test:wildcard3", filter2); |
| 283 } |
| 284 |
| 285 |
| 286 void CapabilityFilterTest::SetUp() { |
| 287 application_manager_.reset( |
| 288 new ApplicationManager(make_scoped_ptr(CreatePackageManager()))); |
| 289 CreateLoader<ServiceApplication>("test:service"); |
| 290 CreateLoader<ServiceApplication>("test:service2"); |
| 291 } |
| 292 |
| 293 void CapabilityFilterTest::TearDown() { |
| 294 application_manager_.reset(); |
| 295 } |
| 296 |
| 297 void CapabilityFilterTest::RunApplication(const std::string& url, |
| 298 const CapabilityFilter& filter) { |
| 299 ServiceProviderPtr services; |
| 300 |
| 301 // We expose Validator to the test application via ConnectToApplication |
| 302 // because we don't allow the test application to connect to test:validator. |
| 303 // Adding it to the CapabilityFilter would interfere with the test. |
| 304 ServiceProviderPtr exposed_services; |
| 305 (new ServiceProviderImpl(GetProxy(&exposed_services)))-> |
| 306 AddService<Validator>(validator_); |
| 307 scoped_ptr<ConnectToApplicationParams> params( |
| 308 new ConnectToApplicationParams); |
| 309 params->SetTarget(Identity(GURL(url), std::string(), filter)); |
| 310 params->set_services(GetProxy(&services)); |
| 311 params->set_exposed_services(exposed_services.Pass()); |
| 312 params->set_on_application_end(base::MessageLoop::QuitWhenIdleClosure()); |
| 313 application_manager_->ConnectToApplication(params.Pass()); |
| 314 } |
| 315 |
| 316 void CapabilityFilterTest::InitValidator( |
| 317 const std::set<std::string>& expectations) { |
| 318 validator_ = new ConnectionValidator(expectations, &loop_); |
| 319 application_manager()->SetLoaderForURL(make_scoped_ptr(validator_), |
| 320 GURL("test:validator")); |
| 321 } |
| 322 |
| 323 void CapabilityFilterTest::RunTest() { |
| 324 loop()->Run(); |
| 325 EXPECT_TRUE(validator_->expectations_met()); |
| 326 if (!validator_->expectations_met()) |
| 327 validator_->PrintUnmetExpectations(); |
| 328 } |
| 329 |
| 330 } // namespace test |
| 331 } // namespace shell |
| 332 } // namespace mojo |
OLD | NEW |