xref: /aosp_15_r20/external/cronet/base/fuchsia/scoped_service_publisher_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 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 <fuchsia/io/cpp/fidl.h>
8 #include <lib/fidl/cpp/interface_handle.h>
9 #include <lib/fidl/cpp/interface_ptr.h>
10 #include <lib/sys/cpp/component_context.h>
11 #include <lib/sys/cpp/service_directory.h>
12 #include <lib/vfs/cpp/pseudo_dir.h>
13 
14 #include "base/fuchsia/process_context.h"
15 #include "base/fuchsia/test_component_context_for_process.h"
16 #include "base/fuchsia/test_interface_impl.h"
17 #include "base/test/task_environment.h"
18 #include "base/testfidl/cpp/fidl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 namespace base {
22 
23 class ScopedServicePublisherTest : public testing::Test {
24  protected:
25   ScopedServicePublisherTest() = default;
26   ~ScopedServicePublisherTest() override = default;
27 
28   const base::test::SingleThreadTaskEnvironment task_environment_{
29       base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
30 
31   TestComponentContextForProcess test_context_;
32   TestInterfaceImpl test_service_;
33 };
34 
TEST_F(ScopedServicePublisherTest,OutgoingDirectory)35 TEST_F(ScopedServicePublisherTest, OutgoingDirectory) {
36   fidl::InterfacePtr<testfidl::TestInterface> client_a;
37 
38   {
39     ScopedServicePublisher<testfidl::TestInterface> publisher(
40         base::ComponentContextForProcess()->outgoing().get(),
41         test_service_.bindings().GetHandler(&test_service_));
42     client_a =
43         test_context_.published_services()->Connect<testfidl::TestInterface>();
44     EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
45   }
46 
47   // Existing channels remain valid after the publisher goes out of scope.
48   EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
49 
50   // New connections attempts will be dropped.
51   auto client_b =
52       test_context_.published_services()->Connect<testfidl::TestInterface>();
53   EXPECT_EQ(VerifyTestInterface(client_b), ZX_ERR_PEER_CLOSED);
54 }
55 
TEST_F(ScopedServicePublisherTest,PseudoDir)56 TEST_F(ScopedServicePublisherTest, PseudoDir) {
57   vfs::PseudoDir directory;
58   fidl::InterfaceHandle<fuchsia::io::Directory> directory_handle;
59   directory.Serve(fuchsia::io::OpenFlags::RIGHT_READABLE |
60                       fuchsia::io::OpenFlags::RIGHT_WRITABLE,
61                   directory_handle.NewRequest().TakeChannel());
62   sys::ServiceDirectory services(std::move(directory_handle));
63 
64   fidl::InterfacePtr<testfidl::TestInterface> client_a;
65 
66   {
67     ScopedServicePublisher<testfidl::TestInterface> publisher(
68         &directory, test_service_.bindings().GetHandler(&test_service_));
69     client_a = services.Connect<testfidl::TestInterface>();
70     EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
71   }
72 
73   // Existing channels remain valid after the publisher goes out of scope.
74   EXPECT_EQ(VerifyTestInterface(client_a), ZX_OK);
75 
76   // New connection attempts will be dropped.
77   auto client_b = services.Connect<testfidl::TestInterface>();
78   EXPECT_EQ(VerifyTestInterface(client_b), ZX_ERR_PEER_CLOSED);
79 }
80 
81 }  // namespace base
82