1 // Copyright 2019 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.
5 #include "base/fuchsia/service_provider_impl.h"
7 #include <lib/zx/channel.h>
10 #include "base/fuchsia/scoped_service_binding.h"
11 #include "base/fuchsia/service_directory.h"
12 #include "base/fuchsia/test_interface_impl.h"
13 #include "base/fuchsia/testfidl/cpp/fidl.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 class ServiceProviderImplTest : public testing::Test {
23 ServiceProviderImplTest() = default;
24 ~ServiceProviderImplTest() override = default;
26 void VerifyTestInterface(fidl::InterfacePtr<testfidl::TestInterface>* stub,
27 zx_status_t expected_error) {
28 // Call the service and wait for response.
29 base::RunLoop run_loop;
30 zx_status_t actual_error = ZX_OK;
32 stub->set_error_handler([&run_loop, &actual_error](zx_status_t status) {
33 actual_error = status;
37 (*stub)->Add(2, 2, [&run_loop](int32_t result) {
44 EXPECT_EQ(expected_error, actual_error);
46 // Reset error handler because the current one captures |run_loop| and
47 // |error| references which are about to be destroyed.
48 stub->set_error_handler(nullptr);
52 MessageLoopForIO message_loop_;
53 TestInterfaceImpl test_service_;
55 DISALLOW_COPY_AND_ASSIGN(ServiceProviderImplTest);
58 // Verifies that we can connect to the service service more than once.
59 TEST_F(ServiceProviderImplTest, ConnectMulti) {
60 fidl::InterfaceHandle<::fuchsia::io::Directory> directory_channel;
61 ServiceDirectory service_directory(directory_channel.NewRequest());
62 ServiceProviderImpl provider_impl(std::move(directory_channel));
63 ScopedServiceBinding<testfidl::TestInterface> service_binding(
64 &service_directory, &test_service_);
66 ::fuchsia::sys::ServiceProviderPtr provider_client;
67 provider_impl.AddBinding(provider_client.NewRequest());
69 testfidl::TestInterfacePtr stub;
70 provider_client->ConnectToService(testfidl::TestInterface::Name_,
71 stub.NewRequest().TakeChannel());
73 testfidl::TestInterfacePtr stub2;
74 provider_client->ConnectToService(testfidl::TestInterface::Name_,
75 stub2.NewRequest().TakeChannel());
77 VerifyTestInterface(&stub, ZX_OK);
78 VerifyTestInterface(&stub2, ZX_OK);
81 // Verify that the case when the service doesn't exist is handled properly.
82 TEST_F(ServiceProviderImplTest, NoService) {
83 fidl::InterfaceHandle<::fuchsia::io::Directory> directory_channel;
84 ServiceDirectory service_directory(directory_channel.NewRequest());
85 ServiceProviderImpl provider_impl(std::move(directory_channel));
87 ::fuchsia::sys::ServiceProviderPtr provider_client;
88 provider_impl.AddBinding(provider_client.NewRequest());
90 testfidl::TestInterfacePtr stub;
91 provider_client->ConnectToService(testfidl::TestInterface::Name_,
92 stub.NewRequest().TakeChannel());
94 VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
97 } // namespace fuchsia