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_test_base.h"
7 #include <lib/fdio/directory.h>
13 ServiceDirectoryTestBase::ServiceDirectoryTestBase() {
14 // TODO(https://crbug.com/920920): Remove the ServiceDirectory's implicit
15 // "public" sub-directory and update this setup logic.
17 // Mount service dir and publish the service.
18 fidl::InterfaceHandle<::fuchsia::io::Directory> directory;
20 std::make_unique<ServiceDirectory>(directory.NewRequest());
22 std::make_unique<ScopedServiceBinding<testfidl::TestInterface>>(
23 service_directory_.get(), &test_service_);
25 // Create the ServiceDirectoryClient, connected to the "svc" sub-directory.
26 fidl::InterfaceHandle<::fuchsia::io::Directory> svc_directory;
27 CHECK_EQ(fdio_service_connect_at(
28 directory.channel().get(), "/svc/.",
29 svc_directory.NewRequest().TakeChannel().release()),
31 public_service_directory_client_ =
32 std::make_unique<ServiceDirectoryClient>(std::move(svc_directory));
34 // Create the ServiceDirectoryClient, connected to the "debug" sub-directory.
35 fidl::InterfaceHandle<::fuchsia::io::Directory> debug_directory;
36 CHECK_EQ(fdio_service_connect_at(
37 directory.channel().get(), "/debug/.",
38 debug_directory.NewRequest().TakeChannel().release()),
40 debug_service_directory_client_ =
41 std::make_unique<ServiceDirectoryClient>(std::move(debug_directory));
43 // Create the ServiceDirectoryClient, connected to the "public" sub-directory
44 // (same contents as "svc", provided for compatibility).
45 fidl::InterfaceHandle<::fuchsia::io::Directory> public_directory;
46 CHECK_EQ(fdio_service_connect_at(
47 directory.channel().get(), "/public/.",
48 public_directory.NewRequest().TakeChannel().release()),
50 legacy_public_service_directory_client_ =
51 std::make_unique<ServiceDirectoryClient>(std::move(public_directory));
53 // Create a ServiceDirectoryClient for the "private" part of the directory.
54 root_service_directory_client_ =
55 std::make_unique<ServiceDirectoryClient>(std::move(directory));
58 ServiceDirectoryTestBase::~ServiceDirectoryTestBase() = default;
60 void ServiceDirectoryTestBase::VerifyTestInterface(
61 fidl::InterfacePtr<testfidl::TestInterface>* stub,
62 zx_status_t expected_error) {
63 // Call the service and wait for response.
64 base::RunLoop run_loop;
65 zx_status_t actual_error = ZX_OK;
67 stub->set_error_handler([&run_loop, &actual_error](zx_status_t status) {
68 actual_error = status;
72 (*stub)->Add(2, 2, [&run_loop](int32_t result) {
79 EXPECT_EQ(expected_error, actual_error);
81 // Reset error handler because the current one captures |run_loop| and
82 // |error| references which are about to be destroyed.
83 stub->set_error_handler(nullptr);
86 } // namespace fuchsia