1 // Copyright 2023 The Chromium Authors
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/scoped_service_publisher.h"
6
7 #include <fidl/base.testfidl/cpp/fidl.h>
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <lib/async/default.h>
10 #include <lib/fidl/cpp/interface_handle.h>
11 #include <lib/fidl/cpp/interface_ptr.h>
12 #include <lib/sys/cpp/component_context.h>
13 #include <lib/sys/cpp/service_directory.h>
14 #include <lib/vfs/cpp/pseudo_dir.h>
15
16 #include "base/fuchsia/process_context.h"
17 #include "base/fuchsia/test_component_context_for_process.h"
18 #include "base/fuchsia/test_interface_natural_impl.h"
19 #include "base/test/task_environment.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace base {
23
24 class ScopedNaturalServicePublisherTest : public testing::Test {
25 protected:
26 ScopedNaturalServicePublisherTest() = default;
27 ~ScopedNaturalServicePublisherTest() override = default;
28
29 const base::test::SingleThreadTaskEnvironment task_environment_{
30 base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
31
32 TestComponentContextForProcess test_context_;
33 TestInterfaceNaturalImpl test_service_;
34 };
35
TEST_F(ScopedNaturalServicePublisherTest,OutgoingDirectory)36 TEST_F(ScopedNaturalServicePublisherTest, OutgoingDirectory) {
37 fidl::Client<base_testfidl::TestInterface> client_a;
38
39 {
40 ScopedNaturalServicePublisher<base_testfidl::TestInterface> publisher(
41 base::ComponentContextForProcess()->outgoing().get(),
42 test_service_.bindings().CreateHandler(&test_service_,
43 async_get_default_dispatcher(),
44 [](fidl::UnbindInfo info) {}));
45 client_a = base::CreateTestInterfaceClient(
46 test_context_.published_services_natural());
47 EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
48 }
49
50 // Existing channels remain valid after the publisher goes out of scope.
51 EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
52
53 // New connections attempts will be dropped.
54 auto client_b = base::CreateTestInterfaceClient(
55 test_context_.published_services_natural());
56 EXPECT_EQ(VerifyTestInterface(client_b), ZX_ERR_PEER_CLOSED);
57 }
58
TEST_F(ScopedNaturalServicePublisherTest,PseudoDir)59 TEST_F(ScopedNaturalServicePublisherTest, PseudoDir) {
60 vfs::PseudoDir directory;
61 auto pseudodir_endpoints = fidl::CreateEndpoints<fuchsia_io::Directory>();
62 ASSERT_TRUE(pseudodir_endpoints.is_ok())
63 << pseudodir_endpoints.status_string();
64 directory.Serve(fuchsia::io::OpenFlags::RIGHT_READABLE |
65 fuchsia::io::OpenFlags::RIGHT_WRITABLE,
66 pseudodir_endpoints->server.TakeChannel());
67
68 fidl::Client<base_testfidl::TestInterface> client_a;
69
70 {
71 ScopedNaturalServicePublisher<base_testfidl::TestInterface> publisher(
72 &directory, test_service_.bindings().CreateHandler(
73 &test_service_, async_get_default_dispatcher(),
74 [](fidl::UnbindInfo info) {}));
75 client_a =
76 base::CreateTestInterfaceClient(pseudodir_endpoints->client.borrow());
77 EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
78 }
79
80 // Existing channels remain valid after the publisher goes out of scope.
81 EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
82
83 // New connection attempts will be dropped.
84 auto client_b =
85 base::CreateTestInterfaceClient(pseudodir_endpoints->client.borrow());
86 EXPECT_EQ(VerifyTestInterface(client_b), ZX_ERR_PEER_CLOSED);
87 }
88
89 } // namespace base
90