xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/fuchsia/bt_host/host_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "host.h"
16 
17 #include <lib/fidl/cpp/binding.h>
18 
19 #include <string>
20 
21 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/fake_hci_transport_server.h"
22 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/fake_vendor_server.h"
23 #include "pw_bluetooth_sapphire/internal/host/testing/loop_fixture.h"
24 
25 namespace bthost::testing {
26 
27 using TestingBase = ::bt::testing::TestLoopFixture;
28 
29 const std::string DEFAULT_DEV_PATH = "/dev/class/bt-hci/000";
30 class HostComponentTest : public TestingBase {
31  public:
32   HostComponentTest() = default;
33   ~HostComponentTest() override = default;
34 
SetUp()35   void SetUp() override {
36     host_ = BtHostComponent::CreateForTesting(dispatcher(), DEFAULT_DEV_PATH);
37 
38     auto [vendor_client_end, vendor_server_end] =
39         fidl::Endpoints<fuchsia_hardware_bluetooth::Vendor>::Create();
40 
41     auto [hci_client_end, hci_server_end] =
42         fidl::Endpoints<fuchsia_hardware_bluetooth::HciTransport>::Create();
43 
44     vendor_ = std::move(vendor_client_end);
45     hci_ = std::move(hci_client_end);
46 
47     fake_hci_server_.emplace(std::move(hci_server_end), dispatcher());
48     fake_vendor_server_.emplace(std::move(vendor_server_end), dispatcher());
49   }
50 
TearDown()51   void TearDown() override {
52     if (host_) {
53       host_->ShutDown();
54     }
55     host_ = nullptr;
56     TestingBase::TearDown();
57   }
58 
hci()59   fidl::ClientEnd<fuchsia_hardware_bluetooth::HciTransport> hci() {
60     return std::move(hci_);
61   }
62 
vendor()63   fidl::ClientEnd<fuchsia_hardware_bluetooth::Vendor> vendor() {
64     return std::move(vendor_);
65   }
66 
67  protected:
host() const68   BtHostComponent* host() const { return host_.get(); }
69 
DestroyHost()70   void DestroyHost() { host_ = nullptr; }
71 
72  private:
73   std::unique_ptr<BtHostComponent> host_;
74 
75   fidl::ClientEnd<fuchsia_hardware_bluetooth::HciTransport> hci_;
76 
77   fidl::ClientEnd<fuchsia_hardware_bluetooth::Vendor> vendor_;
78 
79   std::optional<bt::fidl::testing::FakeHciTransportServer> fake_hci_server_;
80 
81   std::optional<bt::fidl::testing::FakeVendorServer> fake_vendor_server_;
82 
83   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(HostComponentTest);
84 };
85 
TEST_F(HostComponentTest,InitializeFailsWhenCommandTimesOut)86 TEST_F(HostComponentTest, InitializeFailsWhenCommandTimesOut) {
87   std::optional<bool> init_cb_result;
88   bool error_cb_called = false;
89   bool init_result = host()->Initialize(
90       vendor(),
91       [&](bool success) {
92         init_cb_result = success;
93         if (!success) {
94           host()->ShutDown();
95         }
96       },
97       [&]() { error_cb_called = true; },
98       /*legacy_pairing_enabled=*/false);
99   EXPECT_EQ(init_result, true);
100 
101   constexpr zx::duration kCommandTimeout = zx::sec(15);
102   RunLoopFor(kCommandTimeout);
103   ASSERT_TRUE(init_cb_result.has_value());
104   EXPECT_FALSE(*init_cb_result);
105   EXPECT_FALSE(error_cb_called);
106 }
107 
108 }  // namespace bthost::testing
109