service_directory_test_base.cc 2.8 KB

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