1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172

base / fuchsia / scoped_natural_service_binding_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <lib/async/default.h>
#include <lib/sys/cpp/component_context.h>

#include "base/fuchsia/process_context.h"
#include "base/fuchsia/scoped_service_binding.h"
#include "base/fuchsia/test_component_context_for_process.h"
#include "base/fuchsia/test_interface_natural_impl.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

class ScopedNaturalServiceBindingTest : public testing::Test {
 protected:
  ScopedNaturalServiceBindingTest() = default;
  ~ScopedNaturalServiceBindingTest() override = default;

  const base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO};

  TestComponentContextForProcess test_context_;
  TestInterfaceNaturalImpl test_service_;
};

// Verifies that ScopedNaturalServiceBinding allows more than one simultaneous
// client.
TEST_F(ScopedNaturalServiceBindingTest, ConnectTwice) {
  ScopedNaturalServiceBinding<base_testfidl::TestInterface> binding(
      ComponentContextForProcess()->outgoing().get(), &test_service_);

  auto stub =
      CreateTestInterfaceClient(test_context_.published_services_natural());
  auto stub2 =
      CreateTestInterfaceClient(test_context_.published_services_natural());
  EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
  EXPECT_EQ(VerifyTestInterface(stub2), ZX_OK);
}

// Verifies that ScopedNaturalServiceBinding allows more than one simultaneous
// client with a non-default discovery name.
TEST_F(ScopedNaturalServiceBindingTest, ConnectTwiceNameOverride) {
  const char kInterfaceName[] = "fuchsia.TestInterface2";

  ScopedNaturalServiceBinding<base_testfidl::TestInterface> new_service_binding(
      ComponentContextForProcess()->outgoing().get(), &test_service_,
      kInterfaceName);

  auto stub = CreateTestInterfaceClient(
      test_context_.published_services_natural(), kInterfaceName);
  auto stub2 = CreateTestInterfaceClient(
      test_context_.published_services_natural(), kInterfaceName);
  EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
  EXPECT_EQ(VerifyTestInterface(stub2), ZX_OK);
}

// Verify that we can publish a debug `TestInterface` service.
TEST_F(ScopedNaturalServiceBindingTest, ConnectDebugService) {
  vfs::PseudoDir* const debug_dir =
      ComponentContextForProcess()->outgoing()->debug_dir();

  // Publish the test service to the "debug" directory.
  ScopedNaturalServiceBinding<base_testfidl::TestInterface>
      debug_service_binding(debug_dir, &test_service_);

  // Connect a `ClientEnd` to the "debug" subdirectory.
  auto debug_directory_endpoints =
      fidl::CreateEndpoints<fuchsia_io::Directory>();
  ASSERT_TRUE(debug_directory_endpoints.is_ok())
      << debug_directory_endpoints.status_string();
  debug_dir->Serve(fuchsia_io::wire::kPermReadable,
                   fidl::ServerEnd<fuchsia_io::Directory>(
                       debug_directory_endpoints->server.TakeChannel()));

  // Attempt to connect via the "debug" directory.
  auto debug_stub =
      CreateTestInterfaceClient(std::move(debug_directory_endpoints->client));
  EXPECT_EQ(VerifyTestInterface(debug_stub), ZX_OK);

  // Verify that the `TestInterface` service does not appear in the outgoing
  // service directory.
  auto release_stub =
      CreateTestInterfaceClient(test_context_.published_services_natural());
  EXPECT_EQ(VerifyTestInterface(release_stub), ZX_ERR_NOT_FOUND);
}

// Test the last client callback is called every time the number of active
// clients reaches 0.
TEST_F(ScopedNaturalServiceBindingTest, MultipleLastClientCallback) {
  ScopedNaturalServiceBinding<base_testfidl::TestInterface> binding(
      ComponentContextForProcess()->outgoing().get(), &test_service_);
  int disconnect_count = 0;
  binding.SetOnLastClientCallback(
      BindLambdaForTesting([&disconnect_count] { ++disconnect_count; }));

  // Connect a client, verify it is functional.
  {
    auto stub =
        CreateTestInterfaceClient(test_context_.published_services_natural());
    EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
  }

  // Client disconnected on going out of scope, the callback should have been
  // called once.
  RunLoop().RunUntilIdle();
  EXPECT_EQ(disconnect_count, 1);

  // Connect another client, verify it is functional.
  {
    auto stub =
        CreateTestInterfaceClient(test_context_.published_services_natural());
    EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
  }

  // Client disconnected on going out of scope, the callback should have been
  // called a second time.
  RunLoop().RunUntilIdle();
  EXPECT_EQ(disconnect_count, 2);
}

// Test the last client callback is called every time the number of active
// clients reaches 0.
TEST_F(ScopedNaturalServiceBindingTest, LastClientCallbackOnlyForLastClient) {
  ScopedNaturalServiceBinding<base_testfidl::TestInterface> binding(
      ComponentContextForProcess()->outgoing().get(), &test_service_);
  int disconnect_count = 0;
  binding.SetOnLastClientCallback(
      BindLambdaForTesting([&disconnect_count] { ++disconnect_count; }));

  {
    // Connect a long lived client, verify it is functional.
    auto long_lived_stub =
        CreateTestInterfaceClient(test_context_.published_services_natural());
    EXPECT_EQ(VerifyTestInterface(long_lived_stub), ZX_OK);

    // Connect a client, verify it is functional.
    {
      auto stub =
          CreateTestInterfaceClient(test_context_.published_services_natural());
      EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
    }

    // Client disconnected on going out of scope, the callback should not have
    // been called because the long-lived client is still connected.
    RunLoop().RunUntilIdle();
    EXPECT_EQ(disconnect_count, 0);

    // Connect another client, verify it is functional.
    {
      auto stub =
          CreateTestInterfaceClient(test_context_.published_services_natural());
      EXPECT_EQ(VerifyTestInterface(stub), ZX_OK);
    }

    // Client disconnected on going out of scope, the callback should not have
    // been called because the long-lived client is still connected.
    RunLoop().RunUntilIdle();
    EXPECT_EQ(disconnect_count, 0);
  }

  // Long lived client disconnected on going out of scope, the callback should
  // have been called a third time.
  RunLoop().RunUntilIdle();
  EXPECT_EQ(disconnect_count, 1);
}

}  // namespace base