| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 14 #include "mojo/shell/public/cpp/application_test_base.h" | |
| 15 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | |
| 16 #include "mojo/shell/tests/connect/connect_test.mojom.h" | |
| 17 | |
| 18 // Tests that multiple applications can be packaged in a single Mojo application | |
| 19 // implementing ShellClientFactory; that these applications can be specified by | |
| 20 // the package's manifest and are thus registered with the PackageManager. | |
| 21 | |
| 22 namespace mojo { | |
| 23 namespace shell { | |
| 24 namespace { | |
| 25 const char kTestPackageName[] = "mojo:connect_test_package"; | |
| 26 const char kTestAppName[] = "mojo:connect_test_app"; | |
| 27 const char kTestAppAName[] = "mojo:connect_test_a"; | |
| 28 const char kTestAppBName[] = "mojo:connect_test_b"; | |
| 29 | |
| 30 void ReceiveTitle(std::string* out_name, | |
| 31 base::RunLoop* loop, | |
| 32 const String& name) { | |
| 33 *out_name = name; | |
| 34 loop->Quit(); | |
| 35 } | |
| 36 | |
| 37 void QuitLoop(base::RunLoop* loop) { | |
| 38 loop->Quit(); | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 class ConnectApptest : public mojo::test::ApplicationTestBase, | |
| 44 public InterfaceFactory<test::mojom::ExposedInterface>, | |
| 45 public test::mojom::ExposedInterface { | |
| 46 public: | |
| 47 ConnectApptest() {} | |
| 48 ~ConnectApptest() override {} | |
| 49 | |
| 50 protected: | |
| 51 void set_ping_callback(const base::Closure& callback) { | |
| 52 ping_callback_ = callback; | |
| 53 } | |
| 54 | |
| 55 void CompareConnectionState( | |
| 56 const std::string& connection_local_name, | |
| 57 const std::string& connection_remote_name, | |
| 58 uint32_t connection_remote_userid, | |
| 59 uint32_t connection_remote_id, | |
| 60 const std::string& initialize_local_name, | |
| 61 uint32_t initialize_id, | |
| 62 uint32_t initialize_userid) { | |
| 63 EXPECT_EQ(connection_local_name, connection_state_->connection_local_name); | |
| 64 EXPECT_EQ(connection_remote_name, | |
| 65 connection_state_->connection_remote_name); | |
| 66 EXPECT_EQ(connection_remote_userid, | |
| 67 connection_state_->connection_remote_userid); | |
| 68 EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id); | |
| 69 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); | |
| 70 EXPECT_EQ(initialize_id, connection_state_->initialize_id); | |
| 71 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 // mojo::test::ApplicationTestBase: | |
| 76 void SetUp() override { | |
| 77 mojo::test::ApplicationTestBase::SetUp(); | |
| 78 // We need to connect to the package first to force the shell to read the | |
| 79 // package app's manifest and register aliases for the applications it | |
| 80 // provides. | |
| 81 test::mojom::ConnectTestServicePtr root_service; | |
| 82 scoped_ptr<Connection> connection = connector()->Connect(kTestPackageName); | |
| 83 connection->GetInterface(&root_service); | |
| 84 base::RunLoop run_loop; | |
| 85 std::string root_name; | |
| 86 root_service->GetTitle(base::Bind(&ReceiveTitle, &root_name, &run_loop)); | |
| 87 run_loop.Run(); | |
| 88 } | |
| 89 | |
| 90 // InterfaceFactory<test::mojom::ExposedInterface>: | |
| 91 void Create(Connection* connection, | |
| 92 test::mojom::ExposedInterfaceRequest request) override { | |
| 93 bindings_.AddBinding(this, std::move(request)); | |
| 94 } | |
| 95 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { | |
| 96 connection_state_ = std::move(state); | |
| 97 ping_callback_.Run(); | |
| 98 } | |
| 99 | |
| 100 base::Closure ping_callback_; | |
| 101 test::mojom::ConnectionStatePtr connection_state_; | |
| 102 | |
| 103 BindingSet<test::mojom::ExposedInterface> bindings_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(ConnectApptest); | |
| 106 }; | |
| 107 | |
| 108 // Ensure the connection was properly established and that a round trip | |
| 109 // method call/response is completed. | |
| 110 TEST_F(ConnectApptest, Connect) { | |
| 111 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | |
| 112 test::mojom::ConnectTestServicePtr service; | |
| 113 connection->GetInterface(&service); | |
| 114 base::RunLoop run_loop; | |
| 115 std::string title; | |
| 116 service->GetTitle(base::Bind(&ReceiveTitle, &title, &run_loop)); | |
| 117 run_loop.Run(); | |
| 118 EXPECT_EQ("APP", title); | |
| 119 uint32_t id = mojom::Connector::kInvalidApplicationID; | |
| 120 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); | |
| 121 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID); | |
| 122 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppName); | |
| 123 } | |
| 124 | |
| 125 // BlockedInterface should not be exposed to this application because it is not | |
| 126 // in our CapabilityFilter whitelist. | |
| 127 TEST_F(ConnectApptest, BlockedInterface) { | |
| 128 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | |
| 129 base::RunLoop run_loop; | |
| 130 test::mojom::BlockedInterfacePtr blocked; | |
| 131 connection->GetInterface(&blocked); | |
| 132 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); | |
| 133 std::string title = "unchanged"; | |
| 134 blocked->GetTitleBlocked(base::Bind(&ReceiveTitle, &title, &run_loop)); | |
| 135 run_loop.Run(); | |
| 136 EXPECT_EQ("unchanged", title); | |
| 137 } | |
| 138 | |
| 139 // Connects to an app provided by a package. | |
| 140 TEST_F(ConnectApptest, PackagedApp) { | |
| 141 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | |
| 142 test::mojom::ConnectTestServicePtr service_a; | |
| 143 connection->GetInterface(&service_a); | |
| 144 base::RunLoop run_loop; | |
| 145 std::string a_name; | |
| 146 service_a->GetTitle(base::Bind(&ReceiveTitle, &a_name, &run_loop)); | |
| 147 run_loop.Run(); | |
| 148 EXPECT_EQ("A", a_name); | |
| 149 uint32_t id = mojom::Connector::kInvalidApplicationID; | |
| 150 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); | |
| 151 EXPECT_NE(id, mojom::Connector::kInvalidApplicationID); | |
| 152 EXPECT_EQ(connection->GetRemoteApplicationName(), kTestAppAName); | |
| 153 } | |
| 154 | |
| 155 // Ask the target application to attempt to connect to a third application | |
| 156 // provided by a package whose id is permitted by the primary target's | |
| 157 // CapabilityFilter but whose package is not. The connection should be | |
| 158 // blocked and the returned title should be "uninitialized". | |
| 159 TEST_F(ConnectApptest, BlockedPackage) { | |
| 160 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | |
| 161 test::mojom::StandaloneAppPtr standalone_app; | |
| 162 connection->GetInterface(&standalone_app); | |
| 163 base::RunLoop run_loop; | |
| 164 std::string title; | |
| 165 standalone_app->ConnectToAllowedAppInBlockedPackage( | |
| 166 base::Bind(&ReceiveTitle, &title, &run_loop)); | |
| 167 run_loop.Run(); | |
| 168 EXPECT_EQ("uninitialized", title); | |
| 169 } | |
| 170 | |
| 171 // BlockedInterface should not be exposed to this application because it is not | |
| 172 // in our CapabilityFilter whitelist. | |
| 173 TEST_F(ConnectApptest, PackagedApp_BlockedInterface) { | |
| 174 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | |
| 175 base::RunLoop run_loop; | |
| 176 test::mojom::BlockedInterfacePtr blocked; | |
| 177 connection->GetInterface(&blocked); | |
| 178 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); | |
| 179 run_loop.Run(); | |
| 180 } | |
| 181 | |
| 182 // Connection to another application provided by the same package, blocked | |
| 183 // because it's not in the capability filter whitelist. | |
| 184 TEST_F(ConnectApptest, BlockedPackagedApplication) { | |
| 185 scoped_ptr<Connection> connection = connector()->Connect(kTestAppBName); | |
| 186 test::mojom::ConnectTestServicePtr service_b; | |
| 187 connection->GetInterface(&service_b); | |
| 188 base::RunLoop run_loop; | |
| 189 connection->SetRemoteInterfaceProviderConnectionErrorHandler( | |
| 190 base::Bind(&QuitLoop, &run_loop)); | |
| 191 run_loop.Run(); | |
| 192 uint32_t id = mojom::Connector::kInvalidApplicationID; | |
| 193 EXPECT_TRUE(connection->GetRemoteApplicationID(&id)); | |
| 194 EXPECT_EQ(id, mojom::Connector::kInvalidApplicationID); | |
| 195 } | |
| 196 | |
| 197 // Tests that we can expose an interface to targets on outbound connections. | |
| 198 // TODO(beng): Currently all interfaces on outbound connections are exposed. | |
| 199 // See ConnectorImpl::Connect(). | |
| 200 TEST_F(ConnectApptest, LocalInterface) { | |
| 201 // Connect to a standalone application. | |
| 202 { | |
| 203 test::mojom::ConnectTestServicePtr service; | |
| 204 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | |
| 205 connection->GetInterface(&service); | |
| 206 connection->AddInterface<test::mojom::ExposedInterface>(this); | |
| 207 | |
| 208 uint32_t remote_id = mojom::Connector::kInvalidApplicationID; | |
| 209 { | |
| 210 base::RunLoop run_loop; | |
| 211 EXPECT_FALSE(connection->GetRemoteApplicationID(&remote_id)); | |
| 212 EXPECT_EQ(mojom::Connector::kInvalidApplicationID, remote_id); | |
| 213 connection->AddRemoteIDCallback(base::Bind(&QuitLoop, &run_loop)); | |
| 214 run_loop.Run(); | |
| 215 EXPECT_TRUE(connection->GetRemoteApplicationID(&remote_id)); | |
| 216 EXPECT_NE(mojom::Connector::kInvalidApplicationID, remote_id); | |
| 217 } | |
| 218 | |
| 219 { | |
| 220 base::RunLoop run_loop; | |
| 221 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | |
| 222 run_loop.Run(); | |
| 223 CompareConnectionState( | |
| 224 kTestAppName, test_name(), test_userid(), test_instance_id(), | |
| 225 kTestAppName, remote_id, connection->GetRemoteUserID()); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 // Connect to an application provided by a package. | |
| 230 { | |
| 231 test::mojom::ConnectTestServicePtr service_a; | |
| 232 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | |
| 233 connection->GetInterface(&service_a); | |
| 234 connection->AddInterface<test::mojom::ExposedInterface>(this); | |
| 235 | |
| 236 uint32_t remote_id = mojom::Connector::kInvalidApplicationID; | |
| 237 { | |
| 238 base::RunLoop run_loop; | |
| 239 EXPECT_FALSE(connection->GetRemoteApplicationID(&remote_id)); | |
| 240 EXPECT_EQ(mojom::Connector::kInvalidApplicationID, remote_id); | |
| 241 connection->AddRemoteIDCallback(base::Bind(&QuitLoop, &run_loop)); | |
| 242 run_loop.Run(); | |
| 243 EXPECT_TRUE(connection->GetRemoteApplicationID(&remote_id)); | |
| 244 EXPECT_NE(mojom::Connector::kInvalidApplicationID, remote_id); | |
| 245 } | |
| 246 | |
| 247 { | |
| 248 base::RunLoop run_loop; | |
| 249 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | |
| 250 run_loop.Run(); | |
| 251 CompareConnectionState( | |
| 252 kTestAppAName, test_name(), test_userid(), test_instance_id(), | |
| 253 kTestAppAName, remote_id, connection->GetRemoteUserID()); | |
| 254 } | |
| 255 | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 } // namespace shell | |
| 260 } // namespace mojo | |
| OLD | NEW |