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