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 "pw_bluetooth_sapphire/fuchsia/host/socket/socket_factory.h"
16
17 #include <memory>
18
19 #include "pw_bluetooth_sapphire/internal/host/l2cap/channel.h"
20 #include "pw_bluetooth_sapphire/internal/host/l2cap/fake_channel.h"
21 #include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h"
22 #include "pw_bluetooth_sapphire/internal/host/testing/loop_fixture.h"
23 #include "pw_unit_test/framework.h"
24
25 namespace bt::socket {
26 namespace {
27
28 // We'll test the template just for L2CAP channels.
29 using FactoryT = SocketFactory<l2cap::Channel>;
30
31 constexpr l2cap::ChannelId kDynamicChannelIdMin = 0x0040;
32 constexpr l2cap::ChannelId kRemoteChannelId = 0x0050;
33 constexpr hci_spec::ConnectionHandle kDefaultConnectionHandle = 0x0001;
34 constexpr hci_spec::ConnectionHandle kAnotherConnectionHandle = 0x0002;
35
36 class SocketFactoryTest : public testing::TestLoopFixture {
37 public:
SocketFactoryTest()38 SocketFactoryTest() {
39 channel_ =
40 std::make_unique<l2cap::testing::FakeChannel>(kDynamicChannelIdMin,
41 kRemoteChannelId,
42 kDefaultConnectionHandle,
43 bt::LinkType::kACL);
44 }
45
TearDown()46 void TearDown() override {
47 // Process any pending events, to tickle any use-after-free bugs.
48 RunLoopUntilIdle();
49 }
50
51 protected:
channel()52 l2cap::Channel::WeakPtr channel() { return channel_->GetWeakPtr(); }
fake_channel()53 l2cap::testing::FakeChannel::WeakPtr fake_channel() {
54 return channel_->AsWeakPtr();
55 }
56
57 private:
58 std::unique_ptr<l2cap::testing::FakeChannel> channel_;
59 };
60
TEST_F(SocketFactoryTest,TemplatesCompile)61 TEST_F(SocketFactoryTest, TemplatesCompile) {
62 socket::SocketFactory<l2cap::Channel> l2cap_factory;
63 }
64
TEST_F(SocketFactoryTest,CanCreateSocket)65 TEST_F(SocketFactoryTest, CanCreateSocket) {
66 FactoryT socket_factory;
67 EXPECT_TRUE(socket_factory.MakeSocketForChannel(channel()));
68 }
69
TEST_F(SocketFactoryTest,SocketCreationFailsIfChannelIsNullptr)70 TEST_F(SocketFactoryTest, SocketCreationFailsIfChannelIsNullptr) {
71 FactoryT socket_factory;
72 EXPECT_FALSE(socket_factory.MakeSocketForChannel(l2cap::Channel::WeakPtr()));
73 }
74
TEST_F(SocketFactoryTest,SocketCreationFailsIfChannelAlreadyHasASocket)75 TEST_F(SocketFactoryTest, SocketCreationFailsIfChannelAlreadyHasASocket) {
76 FactoryT socket_factory;
77 zx::socket socket = socket_factory.MakeSocketForChannel(channel());
78 ASSERT_TRUE(socket);
79
80 EXPECT_FALSE(socket_factory.MakeSocketForChannel(channel()));
81 }
82
TEST_F(SocketFactoryTest,SocketCreationFailsIfChannelActivationFails)83 TEST_F(SocketFactoryTest, SocketCreationFailsIfChannelActivationFails) {
84 fake_channel()->set_activate_fails(true);
85 EXPECT_FALSE(FactoryT().MakeSocketForChannel(channel()));
86 }
87
TEST_F(SocketFactoryTest,CanCreateSocketForNewChannelWithRecycledId)88 TEST_F(SocketFactoryTest, CanCreateSocketForNewChannelWithRecycledId) {
89 FactoryT socket_factory;
90 auto original_channel =
91 std::make_unique<l2cap::testing::FakeChannel>(kDynamicChannelIdMin + 1,
92 kRemoteChannelId,
93 kDefaultConnectionHandle,
94 bt::LinkType::kACL);
95 zx::socket socket =
96 socket_factory.MakeSocketForChannel(original_channel->GetWeakPtr());
97 ASSERT_TRUE(socket);
98 original_channel->Close();
99 original_channel.reset();
100 RunLoopUntilIdle(); // Process any events related to channel closure.
101
102 auto new_channel =
103 std::make_unique<l2cap::testing::FakeChannel>(kDynamicChannelIdMin + 1,
104 kRemoteChannelId,
105 kDefaultConnectionHandle,
106 bt::LinkType::kACL);
107 EXPECT_TRUE(socket_factory.MakeSocketForChannel(new_channel->GetWeakPtr()));
108 new_channel->Close();
109 original_channel.reset();
110 }
111
TEST_F(SocketFactoryTest,DestructionWithActiveRelayDoesNotCrash)112 TEST_F(SocketFactoryTest, DestructionWithActiveRelayDoesNotCrash) {
113 FactoryT socket_factory;
114 zx::socket socket = socket_factory.MakeSocketForChannel(channel());
115 ASSERT_TRUE(socket);
116 // |socket_factory| is destroyed implicitly.
117 }
118
TEST_F(SocketFactoryTest,DestructionAfterDeactivatingRelayDoesNotCrash)119 TEST_F(SocketFactoryTest, DestructionAfterDeactivatingRelayDoesNotCrash) {
120 FactoryT socket_factory;
121 zx::socket socket = socket_factory.MakeSocketForChannel(channel());
122 ASSERT_TRUE(socket);
123 fake_channel()->Close();
124 RunLoopUntilIdle(); // Process any events related to channel closure.
125 // |socket_factory| is destroyed implicitly.
126 }
127
TEST_F(SocketFactoryTest,SameChannelIdDifferentHandles)128 TEST_F(SocketFactoryTest, SameChannelIdDifferentHandles) {
129 FactoryT socket_factory;
130 EXPECT_TRUE(socket_factory.MakeSocketForChannel(channel()));
131 auto another_channel =
132 std::make_unique<l2cap::testing::FakeChannel>(kDynamicChannelIdMin,
133 kRemoteChannelId,
134 kAnotherConnectionHandle,
135 bt::LinkType::kACL);
136 EXPECT_TRUE(
137 socket_factory.MakeSocketForChannel(another_channel->GetWeakPtr()));
138 another_channel->Close();
139 }
140
TEST_F(SocketFactoryTest,ClosedCallbackCalledOnChannelClosure)141 TEST_F(SocketFactoryTest, ClosedCallbackCalledOnChannelClosure) {
142 FactoryT socket_factory;
143 int closed_cb_count = 0;
144 auto closed_cb = [&]() { closed_cb_count++; };
145 zx::socket sock =
146 socket_factory.MakeSocketForChannel(channel(), std::move(closed_cb));
147 EXPECT_TRUE(sock);
148 fake_channel()->Close();
149 EXPECT_EQ(closed_cb_count, 1);
150 }
151
TEST_F(SocketFactoryTest,ClosedCallbackCalledOnSocketClosure)152 TEST_F(SocketFactoryTest, ClosedCallbackCalledOnSocketClosure) {
153 FactoryT socket_factory;
154 int closed_cb_count = 0;
155 auto closed_cb = [&]() { closed_cb_count++; };
156 zx::socket sock =
157 socket_factory.MakeSocketForChannel(channel(), std::move(closed_cb));
158 EXPECT_TRUE(sock);
159 sock.reset();
160 RunLoopUntilIdle();
161 EXPECT_EQ(closed_cb_count, 1);
162 }
163
164 } // namespace
165 } // namespace bt::socket
166