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.
4 
5 #include "base/fuchsia/service_directory_test_base.h"
6 
7 #include <lib/fdio/directory.h>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/fuchsia/fuchsia_logging.h"
12 #include "base/location.h"
13 #include "base/test/test_timeouts.h"
14 
15 namespace base {
16 namespace fuchsia {
17 
ServiceDirectoryTestBase()18 ServiceDirectoryTestBase::ServiceDirectoryTestBase()
19     : run_timeout_(FROM_HERE, TestTimeouts::action_timeout()) {
20   // Mount service dir and publish the service.
21   outgoing_directory_ = std::make_unique<sys::OutgoingDirectory>();
22   fidl::InterfaceHandle<::fuchsia::io::Directory> directory;
23   zx_status_t status =
24       outgoing_directory_->Serve(directory.NewRequest().TakeChannel());
25   ZX_CHECK(status == ZX_OK, status);
26   service_binding_ =
27       std::make_unique<ScopedServiceBinding<testfidl::TestInterface>>(
28           outgoing_directory_.get(), &test_service_);
29 
30   // Create the sys::ServiceDirectory, connected to the "svc" sub-directory.
31   fidl::InterfaceHandle<::fuchsia::io::Directory> svc_directory;
32   CHECK_EQ(fdio_service_connect_at(
33                directory.channel().get(), "svc",
34                svc_directory.NewRequest().TakeChannel().release()),
35            ZX_OK);
36   public_service_directory_ =
37       std::make_unique<sys::ServiceDirectory>(std::move(svc_directory));
38 
39   // Create the sys::ServiceDirectory, connected to the "debug" sub-directory.
40   fidl::InterfaceHandle<::fuchsia::io::Directory> debug_directory;
41   CHECK_EQ(fdio_service_connect_at(
42                directory.channel().get(), "debug",
43                debug_directory.NewRequest().TakeChannel().release()),
44            ZX_OK);
45   debug_service_directory_ =
46       std::make_unique<sys::ServiceDirectory>(std::move(debug_directory));
47 
48   // Create a sys::ServiceDirectory for the "private" part of the directory.
49   root_service_directory_ =
50       std::make_unique<sys::ServiceDirectory>(std::move(directory));
51 }
52 
53 ServiceDirectoryTestBase::~ServiceDirectoryTestBase() = default;
54 
VerifyTestInterface(fidl::InterfacePtr<testfidl::TestInterface> * stub,zx_status_t expected_error)55 void ServiceDirectoryTestBase::VerifyTestInterface(
56     fidl::InterfacePtr<testfidl::TestInterface>* stub,
57     zx_status_t expected_error) {
58   // Call the service and wait for response.
59   RunLoop run_loop;
60   zx_status_t actual_error = ZX_OK;
61 
62   stub->set_error_handler([&run_loop, &actual_error](zx_status_t status) {
63     actual_error = status;
64     run_loop.Quit();
65   });
66 
67   (*stub)->Add(2, 2, [&run_loop](int32_t result) {
68     EXPECT_EQ(result, 4);
69     run_loop.Quit();
70   });
71 
72   run_loop.Run();
73 
74   EXPECT_EQ(expected_error, actual_error);
75 
76   // Reset error handler because the current one captures |run_loop| and
77   // |error| references which are about to be destroyed.
78   stub->set_error_handler(nullptr);
79 }
80 
81 }  // namespace fuchsia
82 }  // namespace base
83