| 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 <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/guid.h" | 12 #include "base/guid.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/test/test_suite.h" | 15 #include "base/test/test_suite.h" |
| 15 #include "mojo/public/cpp/bindings/binding_set.h" | 16 #include "mojo/public/cpp/bindings/binding_set.h" |
| 16 #include "services/shell/public/cpp/names.h" | 17 #include "services/shell/public/cpp/names.h" |
| 17 #include "services/shell/public/cpp/shell_test.h" | 18 #include "services/shell/public/cpp/shell_test.h" |
| 18 #include "services/shell/public/interfaces/shell.mojom.h" | 19 #include "services/shell/public/interfaces/shell.mojom.h" |
| 19 #include "services/shell/tests/connect/connect_test.mojom.h" | 20 #include "services/shell/tests/connect/connect_test.mojom.h" |
| 20 | 21 |
| 21 // Tests that multiple applications can be packaged in a single Mojo application | 22 // Tests that multiple applications can be packaged in a single Mojo application |
| 22 // implementing ShellClientFactory; that these applications can be specified by | 23 // implementing ShellClientFactory; that these applications can be specified by |
| 23 // the package's manifest and are thus registered with the PackageManager. | 24 // the package's manifest and are thus registered with the PackageManager. |
| 24 | 25 |
| 25 namespace mojo { | |
| 26 namespace shell { | 26 namespace shell { |
| 27 |
| 27 namespace { | 28 namespace { |
| 29 |
| 28 const char kTestPackageName[] = "mojo:connect_test_package"; | 30 const char kTestPackageName[] = "mojo:connect_test_package"; |
| 29 const char kTestAppName[] = "mojo:connect_test_app"; | 31 const char kTestAppName[] = "mojo:connect_test_app"; |
| 30 const char kTestAppAName[] = "mojo:connect_test_a"; | 32 const char kTestAppAName[] = "mojo:connect_test_a"; |
| 31 const char kTestAppBName[] = "mojo:connect_test_b"; | 33 const char kTestAppBName[] = "mojo:connect_test_b"; |
| 32 const char kTestClassAppName[] = "mojo:connect_test_class_app"; | 34 const char kTestClassAppName[] = "mojo:connect_test_class_app"; |
| 33 const char kTestSingletonAppName[] = "mojo:connect_test_singleton_app"; | 35 const char kTestSingletonAppName[] = "mojo:connect_test_singleton_app"; |
| 34 const char kTestDriverName[] = "exe:connect_test_driver"; | 36 const char kTestDriverName[] = "exe:connect_test_driver"; |
| 35 | 37 |
| 36 void ReceiveOneString(std::string* out_string, | 38 void ReceiveOneString(std::string* out_string, |
| 37 base::RunLoop* loop, | 39 base::RunLoop* loop, |
| 38 const String& in_string) { | 40 const mojo::String& in_string) { |
| 39 *out_string = in_string; | 41 *out_string = in_string; |
| 40 loop->Quit(); | 42 loop->Quit(); |
| 41 } | 43 } |
| 42 | 44 |
| 43 void ReceiveTwoStrings(std::string* out_string_1, std::string* out_string_2, | 45 void ReceiveTwoStrings(std::string* out_string_1, |
| 46 std::string* out_string_2, |
| 44 base::RunLoop* loop, | 47 base::RunLoop* loop, |
| 45 const String& in_string_1, const String& in_string_2) { | 48 const mojo::String& in_string_1, |
| 49 const mojo::String& in_string_2) { |
| 46 *out_string_1 = in_string_1; | 50 *out_string_1 = in_string_1; |
| 47 *out_string_2 = in_string_2; | 51 *out_string_2 = in_string_2; |
| 48 loop->Quit(); | 52 loop->Quit(); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void ReceiveConnectionResult(mojom::ConnectResult* out_result, | 55 void ReceiveConnectionResult(mojom::ConnectResult* out_result, |
| 52 Identity* out_target, | 56 Identity* out_target, |
| 53 base::RunLoop* loop, | 57 base::RunLoop* loop, |
| 54 int32_t in_result, | 58 int32_t in_result, |
| 55 mojom::IdentityPtr in_identity) { | 59 mojom::IdentityPtr in_identity) { |
| 56 *out_result = static_cast<shell::mojom::ConnectResult>(in_result); | 60 *out_result = static_cast<mojom::ConnectResult>(in_result); |
| 57 *out_target = in_identity.To<Identity>(); | 61 *out_target = in_identity.To<Identity>(); |
| 58 loop->Quit(); | 62 loop->Quit(); |
| 59 } | 63 } |
| 60 | 64 |
| 61 void QuitLoop(base::RunLoop* loop) { | 65 void QuitLoop(base::RunLoop* loop) { |
| 62 loop->Quit(); | 66 loop->Quit(); |
| 63 } | 67 } |
| 64 | 68 |
| 65 } // namespace | 69 } // namespace |
| 66 | 70 |
| 67 class ConnectTest : public mojo::test::ShellTest, | 71 class ConnectTest : public test::ShellTest, |
| 68 public InterfaceFactory<test::mojom::ExposedInterface>, | 72 public InterfaceFactory<test::mojom::ExposedInterface>, |
| 69 public test::mojom::ExposedInterface { | 73 public test::mojom::ExposedInterface { |
| 70 public: | 74 public: |
| 71 ConnectTest() : ShellTest("mojo:connect_unittests") {} | 75 ConnectTest() : ShellTest("mojo:connect_unittests") {} |
| 72 ~ConnectTest() override {} | 76 ~ConnectTest() override {} |
| 73 | 77 |
| 74 protected: | 78 protected: |
| 75 scoped_ptr<Connection> ConnectTo(Connector::ConnectParams* params) { | 79 std::unique_ptr<Connection> ConnectTo(Connector::ConnectParams* params) { |
| 76 scoped_ptr<Connection> connection = connector()->Connect(params); | 80 std::unique_ptr<Connection> connection = connector()->Connect(params); |
| 77 base::RunLoop loop; | 81 base::RunLoop loop; |
| 78 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 82 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 79 loop.Run(); | 83 loop.Run(); |
| 80 return connection; | 84 return connection; |
| 81 } | 85 } |
| 82 | 86 |
| 83 void set_ping_callback(const base::Closure& callback) { | 87 void set_ping_callback(const base::Closure& callback) { |
| 84 ping_callback_ = callback; | 88 ping_callback_ = callback; |
| 85 } | 89 } |
| 86 | 90 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 connection_state_->connection_remote_name); | 101 connection_state_->connection_remote_name); |
| 98 EXPECT_EQ(connection_remote_userid, | 102 EXPECT_EQ(connection_remote_userid, |
| 99 connection_state_->connection_remote_userid); | 103 connection_state_->connection_remote_userid); |
| 100 EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id); | 104 EXPECT_EQ(connection_remote_id, connection_state_->connection_remote_id); |
| 101 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); | 105 EXPECT_EQ(initialize_local_name, connection_state_->initialize_local_name); |
| 102 EXPECT_EQ(initialize_id, connection_state_->initialize_id); | 106 EXPECT_EQ(initialize_id, connection_state_->initialize_id); |
| 103 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); | 107 EXPECT_EQ(initialize_userid, connection_state_->initialize_userid); |
| 104 } | 108 } |
| 105 | 109 |
| 106 private: | 110 private: |
| 107 // mojo::test::ShellTest: | 111 // test::ShellTest: |
| 108 void SetUp() override { | 112 void SetUp() override { |
| 109 mojo::test::ShellTest::SetUp(); | 113 test::ShellTest::SetUp(); |
| 110 // We need to connect to the package first to force the shell to read the | 114 // We need to connect to the package first to force the shell to read the |
| 111 // package app's manifest and register aliases for the applications it | 115 // package app's manifest and register aliases for the applications it |
| 112 // provides. | 116 // provides. |
| 113 test::mojom::ConnectTestServicePtr root_service; | 117 test::mojom::ConnectTestServicePtr root_service; |
| 114 scoped_ptr<Connection> connection = connector()->Connect(kTestPackageName); | 118 std::unique_ptr<Connection> connection = |
| 119 connector()->Connect(kTestPackageName); |
| 115 connection->GetInterface(&root_service); | 120 connection->GetInterface(&root_service); |
| 116 base::RunLoop run_loop; | 121 base::RunLoop run_loop; |
| 117 std::string root_name; | 122 std::string root_name; |
| 118 root_service->GetTitle( | 123 root_service->GetTitle( |
| 119 base::Bind(&ReceiveOneString, &root_name, &run_loop)); | 124 base::Bind(&ReceiveOneString, &root_name, &run_loop)); |
| 120 run_loop.Run(); | 125 run_loop.Run(); |
| 121 } | 126 } |
| 122 | 127 |
| 123 // InterfaceFactory<test::mojom::ExposedInterface>: | 128 // InterfaceFactory<test::mojom::ExposedInterface>: |
| 124 void Create(Connection* connection, | 129 void Create(Connection* connection, |
| 125 test::mojom::ExposedInterfaceRequest request) override { | 130 test::mojom::ExposedInterfaceRequest request) override { |
| 126 bindings_.AddBinding(this, std::move(request)); | 131 bindings_.AddBinding(this, std::move(request)); |
| 127 } | 132 } |
| 133 |
| 128 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { | 134 void ConnectionAccepted(test::mojom::ConnectionStatePtr state) override { |
| 129 connection_state_ = std::move(state); | 135 connection_state_ = std::move(state); |
| 130 ping_callback_.Run(); | 136 ping_callback_.Run(); |
| 131 } | 137 } |
| 132 | 138 |
| 133 base::Closure ping_callback_; | 139 base::Closure ping_callback_; |
| 134 test::mojom::ConnectionStatePtr connection_state_; | 140 test::mojom::ConnectionStatePtr connection_state_; |
| 135 | 141 |
| 136 BindingSet<test::mojom::ExposedInterface> bindings_; | 142 mojo::BindingSet<test::mojom::ExposedInterface> bindings_; |
| 137 | 143 |
| 138 DISALLOW_COPY_AND_ASSIGN(ConnectTest); | 144 DISALLOW_COPY_AND_ASSIGN(ConnectTest); |
| 139 }; | 145 }; |
| 140 | 146 |
| 141 // Ensure the connection was properly established and that a round trip | 147 // Ensure the connection was properly established and that a round trip |
| 142 // method call/response is completed. | 148 // method call/response is completed. |
| 143 TEST_F(ConnectTest, Connect) { | 149 TEST_F(ConnectTest, Connect) { |
| 144 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 150 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 145 test::mojom::ConnectTestServicePtr service; | 151 test::mojom::ConnectTestServicePtr service; |
| 146 connection->GetInterface(&service); | 152 connection->GetInterface(&service); |
| 147 base::RunLoop run_loop; | 153 base::RunLoop run_loop; |
| 148 std::string title; | 154 std::string title; |
| 149 service->GetTitle(base::Bind(&ReceiveOneString, &title, &run_loop)); | 155 service->GetTitle(base::Bind(&ReceiveOneString, &title, &run_loop)); |
| 150 run_loop.Run(); | 156 run_loop.Run(); |
| 151 EXPECT_EQ("APP", title); | 157 EXPECT_EQ("APP", title); |
| 152 EXPECT_FALSE(connection->IsPending()); | 158 EXPECT_FALSE(connection->IsPending()); |
| 153 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 159 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
| 154 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppName); | 160 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppName); |
| 155 } | 161 } |
| 156 | 162 |
| 157 TEST_F(ConnectTest, Instances) { | 163 TEST_F(ConnectTest, Instances) { |
| 158 Connector::ConnectParams params_a( | 164 Connector::ConnectParams params_a( |
| 159 Identity(kTestAppName, mojom::kInheritUserID, "A")); | 165 Identity(kTestAppName, mojom::kInheritUserID, "A")); |
| 160 scoped_ptr<Connection> connection_a1 = ConnectTo(¶ms_a); | 166 std::unique_ptr<Connection> connection_a1 = ConnectTo(¶ms_a); |
| 161 scoped_ptr<Connection> connection_a2 = ConnectTo(¶ms_a); | 167 std::unique_ptr<Connection> connection_a2 = ConnectTo(¶ms_a); |
| 162 std::string instance_a1, instance_a2; | 168 std::string instance_a1, instance_a2; |
| 163 test::mojom::ConnectTestServicePtr service_a1; | 169 test::mojom::ConnectTestServicePtr service_a1; |
| 164 { | 170 { |
| 165 connection_a1->GetInterface(&service_a1); | 171 connection_a1->GetInterface(&service_a1); |
| 166 base::RunLoop loop; | 172 base::RunLoop loop; |
| 167 service_a1->GetInstance(base::Bind(&ReceiveOneString, &instance_a1, &loop)); | 173 service_a1->GetInstance(base::Bind(&ReceiveOneString, &instance_a1, &loop)); |
| 168 loop.Run(); | 174 loop.Run(); |
| 169 } | 175 } |
| 170 test::mojom::ConnectTestServicePtr service_a2; | 176 test::mojom::ConnectTestServicePtr service_a2; |
| 171 { | 177 { |
| 172 connection_a2->GetInterface(&service_a2); | 178 connection_a2->GetInterface(&service_a2); |
| 173 base::RunLoop loop; | 179 base::RunLoop loop; |
| 174 service_a2->GetInstance(base::Bind(&ReceiveOneString, &instance_a2, &loop)); | 180 service_a2->GetInstance(base::Bind(&ReceiveOneString, &instance_a2, &loop)); |
| 175 loop.Run(); | 181 loop.Run(); |
| 176 } | 182 } |
| 177 EXPECT_EQ(instance_a1, instance_a2); | 183 EXPECT_EQ(instance_a1, instance_a2); |
| 178 | 184 |
| 179 Connector::ConnectParams params_b( | 185 Connector::ConnectParams params_b( |
| 180 Identity(kTestAppName, mojom::kInheritUserID, "B")); | 186 Identity(kTestAppName, mojom::kInheritUserID, "B")); |
| 181 scoped_ptr<Connection> connection_b = ConnectTo(¶ms_b); | 187 std::unique_ptr<Connection> connection_b = ConnectTo(¶ms_b); |
| 182 std::string instance_b; | 188 std::string instance_b; |
| 183 test::mojom::ConnectTestServicePtr service_b; | 189 test::mojom::ConnectTestServicePtr service_b; |
| 184 { | 190 { |
| 185 connection_b->GetInterface(&service_b); | 191 connection_b->GetInterface(&service_b); |
| 186 base::RunLoop loop; | 192 base::RunLoop loop; |
| 187 service_b->GetInstance(base::Bind(&ReceiveOneString, &instance_b, &loop)); | 193 service_b->GetInstance(base::Bind(&ReceiveOneString, &instance_b, &loop)); |
| 188 loop.Run(); | 194 loop.Run(); |
| 189 } | 195 } |
| 190 | 196 |
| 191 EXPECT_NE(instance_a1, instance_b); | 197 EXPECT_NE(instance_a1, instance_b); |
| 192 } | 198 } |
| 193 | 199 |
| 194 // When both the unresolved and resolved instance names are their default | 200 // When both the unresolved and resolved instance names are their default |
| 195 // values, the instance name from the unresolved name must be used. | 201 // values, the instance name from the unresolved name must be used. |
| 196 // (The case where the instance names differ is covered by | 202 // (The case where the instance names differ is covered by |
| 197 // LifecycleTest.PackagedApp_CrashCrashesOtherProvidedApp). | 203 // LifecycleTest.PackagedApp_CrashCrashesOtherProvidedApp). |
| 198 TEST_F(ConnectTest, PreferUnresolvedDefaultInstanceName) { | 204 TEST_F(ConnectTest, PreferUnresolvedDefaultInstanceName) { |
| 199 // Connect to an app with no manifest-supplied instance name provided by a | 205 // Connect to an app with no manifest-supplied instance name provided by a |
| 200 // package, the instance name must be derived from the application instance | 206 // package, the instance name must be derived from the application instance |
| 201 // name, not the package. | 207 // name, not the package. |
| 202 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 208 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 203 { | 209 { |
| 204 base::RunLoop loop; | 210 base::RunLoop loop; |
| 205 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 211 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 206 loop.Run(); | 212 loop.Run(); |
| 207 } | 213 } |
| 208 | 214 |
| 209 std::string instance; | 215 std::string instance; |
| 210 { | 216 { |
| 211 test::mojom::ConnectTestServicePtr service; | 217 test::mojom::ConnectTestServicePtr service; |
| 212 connection->GetInterface(&service); | 218 connection->GetInterface(&service); |
| 213 base::RunLoop loop; | 219 base::RunLoop loop; |
| 214 service->GetInstance(base::Bind(&ReceiveOneString, &instance, &loop)); | 220 service->GetInstance(base::Bind(&ReceiveOneString, &instance, &loop)); |
| 215 loop.Run(); | 221 loop.Run(); |
| 216 } | 222 } |
| 217 EXPECT_EQ(GetNamePath(kTestAppName), instance); | 223 EXPECT_EQ(GetNamePath(kTestAppName), instance); |
| 218 } | 224 } |
| 219 | 225 |
| 220 // BlockedInterface should not be exposed to this application because it is not | 226 // BlockedInterface should not be exposed to this application because it is not |
| 221 // in our CapabilityFilter whitelist. | 227 // in our CapabilityFilter whitelist. |
| 222 TEST_F(ConnectTest, BlockedInterface) { | 228 TEST_F(ConnectTest, BlockedInterface) { |
| 223 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 229 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 224 base::RunLoop run_loop; | 230 base::RunLoop run_loop; |
| 225 test::mojom::BlockedInterfacePtr blocked; | 231 test::mojom::BlockedInterfacePtr blocked; |
| 226 connection->GetInterface(&blocked); | 232 connection->GetInterface(&blocked); |
| 227 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); | 233 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); |
| 228 std::string title = "unchanged"; | 234 std::string title = "unchanged"; |
| 229 blocked->GetTitleBlocked(base::Bind(&ReceiveOneString, &title, &run_loop)); | 235 blocked->GetTitleBlocked(base::Bind(&ReceiveOneString, &title, &run_loop)); |
| 230 run_loop.Run(); | 236 run_loop.Run(); |
| 231 EXPECT_EQ("unchanged", title); | 237 EXPECT_EQ("unchanged", title); |
| 232 } | 238 } |
| 233 | 239 |
| 234 // Connects to an app provided by a package. | 240 // Connects to an app provided by a package. |
| 235 TEST_F(ConnectTest, PackagedApp) { | 241 TEST_F(ConnectTest, PackagedApp) { |
| 236 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 242 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName); |
| 237 test::mojom::ConnectTestServicePtr service_a; | 243 test::mojom::ConnectTestServicePtr service_a; |
| 238 connection->GetInterface(&service_a); | 244 connection->GetInterface(&service_a); |
| 239 base::RunLoop run_loop; | 245 base::RunLoop run_loop; |
| 240 std::string a_name; | 246 std::string a_name; |
| 241 service_a->GetTitle(base::Bind(&ReceiveOneString, &a_name, &run_loop)); | 247 service_a->GetTitle(base::Bind(&ReceiveOneString, &a_name, &run_loop)); |
| 242 run_loop.Run(); | 248 run_loop.Run(); |
| 243 EXPECT_EQ("A", a_name); | 249 EXPECT_EQ("A", a_name); |
| 244 EXPECT_FALSE(connection->IsPending()); | 250 EXPECT_FALSE(connection->IsPending()); |
| 245 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 251 EXPECT_NE(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
| 246 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppAName); | 252 EXPECT_EQ(connection->GetRemoteIdentity().name(), kTestAppAName); |
| 247 } | 253 } |
| 248 | 254 |
| 249 // Ask the target application to attempt to connect to a third application | 255 // Ask the target application to attempt to connect to a third application |
| 250 // provided by a package whose id is permitted by the primary target's | 256 // provided by a package whose id is permitted by the primary target's |
| 251 // CapabilityFilter but whose package is not. The connection should be | 257 // CapabilityFilter but whose package is not. The connection should be |
| 252 // allowed regardless of the target's CapabilityFilter with respect to the | 258 // allowed regardless of the target's CapabilityFilter with respect to the |
| 253 // package. | 259 // package. |
| 254 TEST_F(ConnectTest, BlockedPackage) { | 260 TEST_F(ConnectTest, BlockedPackage) { |
| 255 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 261 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 256 test::mojom::StandaloneAppPtr standalone_app; | 262 test::mojom::StandaloneAppPtr standalone_app; |
| 257 connection->GetInterface(&standalone_app); | 263 connection->GetInterface(&standalone_app); |
| 258 base::RunLoop run_loop; | 264 base::RunLoop run_loop; |
| 259 std::string title; | 265 std::string title; |
| 260 standalone_app->ConnectToAllowedAppInBlockedPackage( | 266 standalone_app->ConnectToAllowedAppInBlockedPackage( |
| 261 base::Bind(&ReceiveOneString, &title, &run_loop)); | 267 base::Bind(&ReceiveOneString, &title, &run_loop)); |
| 262 run_loop.Run(); | 268 run_loop.Run(); |
| 263 EXPECT_EQ("A", title); | 269 EXPECT_EQ("A", title); |
| 264 } | 270 } |
| 265 | 271 |
| 266 // BlockedInterface should not be exposed to this application because it is not | 272 // BlockedInterface should not be exposed to this application because it is not |
| 267 // in our CapabilityFilter whitelist. | 273 // in our CapabilityFilter whitelist. |
| 268 TEST_F(ConnectTest, PackagedApp_BlockedInterface) { | 274 TEST_F(ConnectTest, PackagedApp_BlockedInterface) { |
| 269 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 275 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName); |
| 270 base::RunLoop run_loop; | 276 base::RunLoop run_loop; |
| 271 test::mojom::BlockedInterfacePtr blocked; | 277 test::mojom::BlockedInterfacePtr blocked; |
| 272 connection->GetInterface(&blocked); | 278 connection->GetInterface(&blocked); |
| 273 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); | 279 blocked.set_connection_error_handler(base::Bind(&QuitLoop, &run_loop)); |
| 274 run_loop.Run(); | 280 run_loop.Run(); |
| 275 } | 281 } |
| 276 | 282 |
| 277 // Connection to another application provided by the same package, blocked | 283 // Connection to another application provided by the same package, blocked |
| 278 // because it's not in the capability filter whitelist. | 284 // because it's not in the capability filter whitelist. |
| 279 TEST_F(ConnectTest, BlockedPackagedApplication) { | 285 TEST_F(ConnectTest, BlockedPackagedApplication) { |
| 280 scoped_ptr<Connection> connection = connector()->Connect(kTestAppBName); | 286 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppBName); |
| 281 test::mojom::ConnectTestServicePtr service_b; | 287 test::mojom::ConnectTestServicePtr service_b; |
| 282 connection->GetInterface(&service_b); | 288 connection->GetInterface(&service_b); |
| 283 base::RunLoop run_loop; | 289 base::RunLoop run_loop; |
| 284 connection->SetConnectionLostClosure(base::Bind(&QuitLoop, &run_loop)); | 290 connection->SetConnectionLostClosure(base::Bind(&QuitLoop, &run_loop)); |
| 285 run_loop.Run(); | 291 run_loop.Run(); |
| 286 EXPECT_FALSE(connection->IsPending()); | 292 EXPECT_FALSE(connection->IsPending()); |
| 287 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, connection->GetResult()); | 293 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, connection->GetResult()); |
| 288 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 294 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
| 289 } | 295 } |
| 290 | 296 |
| 291 TEST_F(ConnectTest, CapabilityClasses) { | 297 TEST_F(ConnectTest, CapabilityClasses) { |
| 292 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 298 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 293 test::mojom::StandaloneAppPtr standalone_app; | 299 test::mojom::StandaloneAppPtr standalone_app; |
| 294 connection->GetInterface(&standalone_app); | 300 connection->GetInterface(&standalone_app); |
| 295 std::string string1, string2; | 301 std::string string1, string2; |
| 296 base::RunLoop loop; | 302 base::RunLoop loop; |
| 297 standalone_app->ConnectToClassInterface( | 303 standalone_app->ConnectToClassInterface( |
| 298 base::Bind(&ReceiveTwoStrings, &string1, &string2, &loop)); | 304 base::Bind(&ReceiveTwoStrings, &string1, &string2, &loop)); |
| 299 loop.Run(); | 305 loop.Run(); |
| 300 EXPECT_EQ("PONG", string1); | 306 EXPECT_EQ("PONG", string1); |
| 301 EXPECT_EQ("CLASS APP", string2); | 307 EXPECT_EQ("CLASS APP", string2); |
| 302 } | 308 } |
| 303 | 309 |
| 304 TEST_F(ConnectTest, ConnectAsDifferentUser_Allowed) { | 310 TEST_F(ConnectTest, ConnectAsDifferentUser_Allowed) { |
| 305 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 311 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 306 test::mojom::UserIdTestPtr user_id_test; | 312 test::mojom::UserIdTestPtr user_id_test; |
| 307 connection->GetInterface(&user_id_test); | 313 connection->GetInterface(&user_id_test); |
| 308 shell::mojom::ConnectResult result; | 314 mojom::ConnectResult result; |
| 309 Identity target(kTestClassAppName, base::GenerateGUID()); | 315 Identity target(kTestClassAppName, base::GenerateGUID()); |
| 310 Identity result_identity; | 316 Identity result_identity; |
| 311 { | 317 { |
| 312 base::RunLoop loop; | 318 base::RunLoop loop; |
| 313 user_id_test->ConnectToClassAppAsDifferentUser( | 319 user_id_test->ConnectToClassAppAsDifferentUser( |
| 314 mojom::Identity::From(target), | 320 mojom::Identity::From(target), |
| 315 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); | 321 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); |
| 316 loop.Run(); | 322 loop.Run(); |
| 317 } | 323 } |
| 318 EXPECT_EQ(result, shell::mojom::ConnectResult::SUCCEEDED); | 324 EXPECT_EQ(result, mojom::ConnectResult::SUCCEEDED); |
| 319 EXPECT_EQ(target, result_identity); | 325 EXPECT_EQ(target, result_identity); |
| 320 } | 326 } |
| 321 | 327 |
| 322 TEST_F(ConnectTest, ConnectAsDifferentUser_Blocked) { | 328 TEST_F(ConnectTest, ConnectAsDifferentUser_Blocked) { |
| 323 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 329 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppAName); |
| 324 test::mojom::UserIdTestPtr user_id_test; | 330 test::mojom::UserIdTestPtr user_id_test; |
| 325 connection->GetInterface(&user_id_test); | 331 connection->GetInterface(&user_id_test); |
| 326 shell::mojom::ConnectResult result; | 332 mojom::ConnectResult result; |
| 327 Identity target(kTestClassAppName, base::GenerateGUID()); | 333 Identity target(kTestClassAppName, base::GenerateGUID()); |
| 328 Identity result_identity; | 334 Identity result_identity; |
| 329 { | 335 { |
| 330 base::RunLoop loop; | 336 base::RunLoop loop; |
| 331 user_id_test->ConnectToClassAppAsDifferentUser( | 337 user_id_test->ConnectToClassAppAsDifferentUser( |
| 332 mojom::Identity::From(target), | 338 mojom::Identity::From(target), |
| 333 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); | 339 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); |
| 334 loop.Run(); | 340 loop.Run(); |
| 335 } | 341 } |
| 336 EXPECT_EQ(shell::mojom::ConnectResult::ACCESS_DENIED, result); | 342 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, result); |
| 337 EXPECT_FALSE(target == result_identity); | 343 EXPECT_FALSE(target == result_identity); |
| 338 } | 344 } |
| 339 | 345 |
| 340 // There are various other tests (shell, lifecycle) that test valid client | 346 // There are various other tests (shell, lifecycle) that test valid client |
| 341 // process specifications. This is the only one for blocking. | 347 // process specifications. This is the only one for blocking. |
| 342 TEST_F(ConnectTest, ConnectToClientProcess_Blocked) { | 348 TEST_F(ConnectTest, ConnectToClientProcess_Blocked) { |
| 343 scoped_ptr<Connection> connection = connector()->Connect(kTestDriverName); | 349 std::unique_ptr<Connection> connection = |
| 350 connector()->Connect(kTestDriverName); |
| 344 test::mojom::ClientProcessTestPtr client_process_test; | 351 test::mojom::ClientProcessTestPtr client_process_test; |
| 345 connection->GetInterface(&client_process_test); | 352 connection->GetInterface(&client_process_test); |
| 346 shell::mojom::ConnectResult result; | 353 mojom::ConnectResult result; |
| 347 Identity result_identity; | 354 Identity result_identity; |
| 348 { | 355 { |
| 349 base::RunLoop loop; | 356 base::RunLoop loop; |
| 350 client_process_test->LaunchAndConnectToProcess( | 357 client_process_test->LaunchAndConnectToProcess( |
| 351 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); | 358 base::Bind(&ReceiveConnectionResult, &result, &result_identity, &loop)); |
| 352 loop.Run(); | 359 loop.Run(); |
| 353 } | 360 } |
| 354 EXPECT_EQ(shell::mojom::ConnectResult::ACCESS_DENIED, result); | 361 EXPECT_EQ(mojom::ConnectResult::ACCESS_DENIED, result); |
| 355 } | 362 } |
| 356 | 363 |
| 357 // Verifies that a client with the "all_users" capability class can receive | 364 // Verifies that a client with the "all_users" capability class can receive |
| 358 // connections from clients run as other users. | 365 // connections from clients run as other users. |
| 359 TEST_F(ConnectTest, AllUsersSingleton) { | 366 TEST_F(ConnectTest, AllUsersSingleton) { |
| 360 // Connect to an instance with an explicitly different user_id. This supplied | 367 // Connect to an instance with an explicitly different user_id. This supplied |
| 361 // user id should be ignored by the shell (which will generate its own | 368 // user id should be ignored by the shell (which will generate its own |
| 362 // synthetic user id for all-user singleton instances). | 369 // synthetic user id for all-user singleton instances). |
| 363 const std::string singleton_userid = base::GenerateGUID(); | 370 const std::string singleton_userid = base::GenerateGUID(); |
| 364 Connector::ConnectParams params( | 371 Connector::ConnectParams params( |
| 365 Identity(kTestSingletonAppName, singleton_userid)); | 372 Identity(kTestSingletonAppName, singleton_userid)); |
| 366 scoped_ptr<Connection> connection = connector()->Connect(¶ms); | 373 std::unique_ptr<Connection> connection = connector()->Connect(¶ms); |
| 367 { | 374 { |
| 368 base::RunLoop loop; | 375 base::RunLoop loop; |
| 369 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 376 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); |
| 370 loop.Run(); | 377 loop.Run(); |
| 371 EXPECT_NE(connection->GetRemoteIdentity().user_id(), singleton_userid); | 378 EXPECT_NE(connection->GetRemoteIdentity().user_id(), singleton_userid); |
| 372 } | 379 } |
| 373 // This connects using the current client's user_id. It should be bound to the | 380 // This connects using the current client's user_id. It should be bound to the |
| 374 // same service started above, with the same shell-generated user id. | 381 // same service started above, with the same shell-generated user id. |
| 375 scoped_ptr<Connection> inherit_connection = | 382 std::unique_ptr<Connection> inherit_connection = |
| 376 connector()->Connect(kTestSingletonAppName); | 383 connector()->Connect(kTestSingletonAppName); |
| 377 { | 384 { |
| 378 base::RunLoop loop; | 385 base::RunLoop loop; |
| 379 inherit_connection->AddConnectionCompletedClosure( | 386 inherit_connection->AddConnectionCompletedClosure( |
| 380 base::Bind(&QuitLoop, &loop)); | 387 base::Bind(&QuitLoop, &loop)); |
| 381 loop.Run(); | 388 loop.Run(); |
| 382 EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(), | 389 EXPECT_EQ(inherit_connection->GetRemoteIdentity().user_id(), |
| 383 connection->GetRemoteIdentity().user_id()); | 390 connection->GetRemoteIdentity().user_id()); |
| 384 } | 391 } |
| 385 } | 392 } |
| 386 | 393 |
| 387 // Tests that we can expose an interface to targets on outbound connections. | 394 // Tests that we can expose an interface to targets on outbound connections. |
| 388 TEST_F(ConnectTest, LocalInterface) { | 395 TEST_F(ConnectTest, LocalInterface) { |
| 389 // Connect to a standalone application. | 396 // Connect to a standalone application. |
| 390 { | 397 { |
| 391 test::mojom::ConnectTestServicePtr service; | 398 test::mojom::ConnectTestServicePtr service; |
| 392 scoped_ptr<Connection> connection = connector()->Connect(kTestAppName); | 399 std::unique_ptr<Connection> connection = connector()->Connect(kTestAppName); |
| 393 connection->GetInterface(&service); | 400 connection->GetInterface(&service); |
| 394 connection->AddInterface<test::mojom::ExposedInterface>(this); | 401 connection->AddInterface<test::mojom::ExposedInterface>(this); |
| 395 | 402 |
| 396 uint32_t remote_id = shell::mojom::kInvalidInstanceID; | 403 uint32_t remote_id = mojom::kInvalidInstanceID; |
| 397 { | 404 { |
| 398 base::RunLoop run_loop; | 405 base::RunLoop run_loop; |
| 399 EXPECT_TRUE(connection->IsPending()); | 406 EXPECT_TRUE(connection->IsPending()); |
| 400 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 407 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
| 401 connection->AddConnectionCompletedClosure( | 408 connection->AddConnectionCompletedClosure( |
| 402 base::Bind(&QuitLoop, &run_loop)); | 409 base::Bind(&QuitLoop, &run_loop)); |
| 403 run_loop.Run(); | 410 run_loop.Run(); |
| 404 EXPECT_FALSE(connection->IsPending()); | 411 EXPECT_FALSE(connection->IsPending()); |
| 405 remote_id = connection->GetRemoteInstanceID(); | 412 remote_id = connection->GetRemoteInstanceID(); |
| 406 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); | 413 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); |
| 407 } | 414 } |
| 408 | 415 |
| 409 { | 416 { |
| 410 base::RunLoop run_loop; | 417 base::RunLoop run_loop; |
| 411 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | 418 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); |
| 412 run_loop.Run(); | 419 run_loop.Run(); |
| 413 CompareConnectionState( | 420 CompareConnectionState( |
| 414 kTestAppName, test_name(), test_userid(), test_instance_id(), | 421 kTestAppName, test_name(), test_userid(), test_instance_id(), |
| 415 kTestAppName, connection->GetRemoteIdentity().user_id(), remote_id); | 422 kTestAppName, connection->GetRemoteIdentity().user_id(), remote_id); |
| 416 } | 423 } |
| 417 } | 424 } |
| 418 | 425 |
| 419 // Connect to an application provided by a package. | 426 // Connect to an application provided by a package. |
| 420 { | 427 { |
| 421 test::mojom::ConnectTestServicePtr service_a; | 428 test::mojom::ConnectTestServicePtr service_a; |
| 422 scoped_ptr<Connection> connection = connector()->Connect(kTestAppAName); | 429 std::unique_ptr<Connection> connection = |
| 430 connector()->Connect(kTestAppAName); |
| 423 connection->GetInterface(&service_a); | 431 connection->GetInterface(&service_a); |
| 424 connection->AddInterface<test::mojom::ExposedInterface>(this); | 432 connection->AddInterface<test::mojom::ExposedInterface>(this); |
| 425 | 433 |
| 426 uint32_t remote_id = shell::mojom::kInvalidInstanceID; | 434 uint32_t remote_id = mojom::kInvalidInstanceID; |
| 427 { | 435 { |
| 428 base::RunLoop run_loop; | 436 base::RunLoop run_loop; |
| 429 EXPECT_TRUE(connection->IsPending()); | 437 EXPECT_TRUE(connection->IsPending()); |
| 430 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); | 438 EXPECT_EQ(mojom::kInvalidInstanceID, connection->GetRemoteInstanceID()); |
| 431 connection->AddConnectionCompletedClosure( | 439 connection->AddConnectionCompletedClosure( |
| 432 base::Bind(&QuitLoop, &run_loop)); | 440 base::Bind(&QuitLoop, &run_loop)); |
| 433 run_loop.Run(); | 441 run_loop.Run(); |
| 434 EXPECT_FALSE(connection->IsPending()); | 442 EXPECT_FALSE(connection->IsPending()); |
| 435 remote_id = connection->GetRemoteInstanceID(); | 443 remote_id = connection->GetRemoteInstanceID(); |
| 436 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); | 444 EXPECT_NE(mojom::kInvalidInstanceID, remote_id); |
| 437 } | 445 } |
| 438 | 446 |
| 439 { | 447 { |
| 440 base::RunLoop run_loop; | 448 base::RunLoop run_loop; |
| 441 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); | 449 set_ping_callback(base::Bind(&QuitLoop, &run_loop)); |
| 442 run_loop.Run(); | 450 run_loop.Run(); |
| 443 CompareConnectionState( | 451 CompareConnectionState( |
| 444 kTestAppAName, test_name(), test_userid(), test_instance_id(), | 452 kTestAppAName, test_name(), test_userid(), test_instance_id(), |
| 445 kTestAppAName, connection->GetRemoteIdentity().user_id(), remote_id); | 453 kTestAppAName, connection->GetRemoteIdentity().user_id(), remote_id); |
| 446 } | 454 } |
| 447 | 455 |
| 448 } | 456 } |
| 449 } | 457 } |
| 450 | 458 |
| 451 } // namespace shell | 459 } // namespace shell |
| 452 } // namespace mojo | |
| OLD | NEW |