1 // Copyright 2018 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_directory.h"
7 #include <lib/fdio/fdio.h>
8 #include <lib/zx/channel.h>
11 #include "base/bind.h"
12 #include "base/fuchsia/service_directory_test_base.h"
13 #include "base/location.h"
14 #include "base/run_loop.h"
15 #include "base/task_runner.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 class ServiceDirectoryTest : public ServiceDirectoryTestBase {};
25 // Verifies that ServiceDirectoryClient can consume a public service in
26 // ServiceDirectory and that connection is disconnected when the client stub is
28 TEST_F(ServiceDirectoryTest, ConnectDisconnect) {
29 auto stub = public_service_directory_client_
30 ->ConnectToService<testfidl::TestInterface>();
31 VerifyTestInterface(&stub, ZX_OK);
33 base::RunLoop run_loop;
34 service_binding_->SetOnLastClientCallback(run_loop.QuitClosure());
36 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
39 [](base::RunLoop* run_loop) {
44 TestTimeouts::action_timeout());
50 // Verify that we can connect to a service through both "public" and "svc".
51 TEST_F(ServiceDirectoryTest, ConnectNewAndLegacyServices) {
52 auto stub = public_service_directory_client_
53 ->ConnectToService<testfidl::TestInterface>();
54 auto stub2 = legacy_public_service_directory_client_
55 ->ConnectToService<testfidl::TestInterface>();
56 VerifyTestInterface(&stub, ZX_OK);
57 VerifyTestInterface(&stub2, ZX_OK);
60 // Verify that we can connect to the same service more than once.
61 TEST_F(ServiceDirectoryTest, ConnectMulti) {
62 auto stub = public_service_directory_client_
63 ->ConnectToService<testfidl::TestInterface>();
64 auto stub2 = public_service_directory_client_
65 ->ConnectToService<testfidl::TestInterface>();
66 VerifyTestInterface(&stub, ZX_OK);
67 VerifyTestInterface(&stub2, ZX_OK);
70 // Verify that services are also exported to the legacy flat service namespace.
71 TEST_F(ServiceDirectoryTest, ConnectLegacy) {
72 auto stub = root_service_directory_client_
73 ->ConnectToService<testfidl::TestInterface>();
74 VerifyTestInterface(&stub, ZX_OK);
77 // Verify that ServiceDirectoryClient can handle the case when the service
78 // directory connection is disconnected.
79 TEST_F(ServiceDirectoryTest, DirectoryGone) {
80 service_binding_.reset();
81 service_directory_.reset();
83 fidl::InterfacePtr<testfidl::TestInterface> stub;
85 public_service_directory_client_->ConnectToService(stub.NewRequest());
86 EXPECT_EQ(status, ZX_ERR_PEER_CLOSED);
88 VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
91 // Verify that the case when the service doesn't exist is handled properly.
92 TEST_F(ServiceDirectoryTest, NoService) {
93 service_binding_.reset();
94 auto stub = public_service_directory_client_
95 ->ConnectToService<testfidl::TestInterface>();
96 VerifyTestInterface(&stub, ZX_ERR_PEER_CLOSED);
99 // Verify that we can connect to a debug service.
100 TEST_F(ServiceDirectoryTest, ConnectDebugService) {
101 // Remove the public service binding.
102 service_binding_.reset();
104 // Publish the test service to the "debug" directory.
105 ScopedServiceBinding<testfidl::TestInterface> debug_service_binding(
106 service_directory_->debug(), &test_service_);
108 auto debug_stub = debug_service_directory_client_
109 ->ConnectToService<testfidl::TestInterface>();
110 VerifyTestInterface(&debug_stub, ZX_OK);
112 auto release_stub = public_service_directory_client_
113 ->ConnectToService<testfidl::TestInterface>();
114 VerifyTestInterface(&release_stub, ZX_ERR_PEER_CLOSED);
117 } // namespace fuchsia