1 // Copyright 2023 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/internal/host/testing/fake_signaling_server.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/l2cap/test_packets.h"
18 #include "pw_bluetooth_sapphire/internal/host/testing/fake_l2cap.h"
19 #include "pw_bluetooth_sapphire/internal/host/testing/test_helpers.h"
20
21 namespace bt::testing {
22
23 class FakeSignalingServerTest : public ::testing::Test {
24 public:
25 FakeSignalingServerTest() = default;
26 ~FakeSignalingServerTest() override = default;
27
28 // Each test sets up its own FakeL2cap and FakeSignalingServer, so only
29 // instantiate constants here.
30 hci_spec::ConnectionHandle kConnectionHandle = 0x01;
31 l2cap::CommandId kCommandId = 0x02;
32
33 private:
34 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(FakeSignalingServerTest);
35 };
36
TEST_F(FakeSignalingServerTest,ExtendedFeaturesInformationRequest)37 TEST_F(FakeSignalingServerTest, ExtendedFeaturesInformationRequest) {
38 // Copy the received packet to a local variable.
39 std::unique_ptr<ByteBuffer> received_packet;
40 auto send_cb = [&received_packet](auto conn, auto cid, auto& buffer) {
41 received_packet = std::make_unique<DynamicByteBuffer>(buffer);
42 };
43 auto fake_l2cap = FakeL2cap(send_cb);
44 auto server = std::make_unique<FakeSignalingServer>();
45 server->RegisterWithL2cap(&fake_l2cap);
46
47 // Assemble and send the information request.
48 auto sent_acl_packet =
49 l2cap::testing::AclExtFeaturesInfoReq(kCommandId, kConnectionHandle);
50 const auto& send_header = sent_acl_packet.To<hci_spec::ACLDataHeader>();
51 auto send_header_len = sizeof(send_header);
52 auto send_payload_len = le16toh(send_header.data_total_length);
53 auto sent_packet = DynamicByteBuffer(send_payload_len);
54 sent_acl_packet.Copy(&sent_packet, send_header_len, send_payload_len);
55 fake_l2cap.HandlePdu(0x001, sent_packet);
56
57 // Assemble the expected packet and confirm that it matches the received
58 // packet.
59 l2cap::ExtendedFeatures extended_features =
60 l2cap::kExtendedFeaturesBitFixedChannels |
61 l2cap::kExtendedFeaturesBitEnhancedRetransmission;
62 auto expected_acl_response = l2cap::testing::AclExtFeaturesInfoRsp(
63 kCommandId, kConnectionHandle, extended_features);
64 auto expected_response = expected_acl_response.view(
65 sizeof(hci_spec::ACLDataHeader) + sizeof(l2cap::CommandHeader));
66 EXPECT_TRUE(ContainersEqual(expected_response, *received_packet));
67 }
68
TEST_F(FakeSignalingServerTest,FixedChannelInformationRequest)69 TEST_F(FakeSignalingServerTest, FixedChannelInformationRequest) {
70 // Copy the received packet to a local variable.
71 std::unique_ptr<ByteBuffer> received_packet;
72 auto send_cb = [&received_packet](auto conn, auto cid, auto& buffer) {
73 received_packet = std::make_unique<DynamicByteBuffer>(buffer);
74 };
75 auto fake_l2cap = FakeL2cap(send_cb);
76 auto server = std::make_unique<FakeSignalingServer>();
77 server->RegisterWithL2cap(&fake_l2cap);
78
79 // Assemble and send the information request.
80 auto sent_acl_packet = l2cap::testing::AclFixedChannelsSupportedInfoReq(
81 kCommandId, kConnectionHandle);
82 const auto& send_header = sent_acl_packet.To<hci_spec::ACLDataHeader>();
83 auto send_header_len = sizeof(send_header);
84 auto send_payload_len = le16toh(send_header.data_total_length);
85 auto sent_packet = DynamicByteBuffer(send_payload_len);
86 sent_acl_packet.Copy(&sent_packet, send_header_len, send_payload_len);
87 fake_l2cap.HandlePdu(0x001, sent_packet);
88
89 // Assemble the expected packet and confirm that it matches the received
90 // packet.
91 l2cap::FixedChannelsSupported fixed_channels =
92 l2cap::kFixedChannelsSupportedBitSignaling;
93 auto expected_acl_response = l2cap::testing::AclFixedChannelsSupportedInfoRsp(
94 kCommandId, kConnectionHandle, fixed_channels);
95 auto expected_response = expected_acl_response.view(
96 sizeof(hci_spec::ACLDataHeader) + sizeof(l2cap::CommandHeader));
97 EXPECT_TRUE(ContainersEqual(expected_response, *received_packet));
98 }
99
TEST_F(FakeSignalingServerTest,RejectInvalidInformationRequest)100 TEST_F(FakeSignalingServerTest, RejectInvalidInformationRequest) {
101 std::unique_ptr<ByteBuffer> received_packet;
102 auto send_cb = [&received_packet](auto conn, auto cid, auto& buffer) {
103 received_packet = std::make_unique<DynamicByteBuffer>(buffer);
104 };
105 auto fake_l2cap = FakeL2cap(send_cb);
106 auto server = std::make_unique<FakeSignalingServer>();
107 server->RegisterWithL2cap(&fake_l2cap);
108
109 // Construct and send a custom invalid packet here.
110 StaticByteBuffer sent_packet(
111 // Length = 0x06 (4 byte header + 2 byte information type)
112 0x06,
113 0x00,
114 // Channel Id: 0x0001
115 LowerBits(l2cap::kSignalingChannelId),
116 UpperBits(l2cap::kSignalingChannelId),
117 // Command code for information request = 0x0A, CommandID = 0x02
118 l2cap::kInformationRequest,
119 kCommandId,
120 // Payload length = 0x02
121 0x02,
122 0x00,
123 // Information type = 0x0004
124 LowerBits(0x0004),
125 UpperBits(0x0004));
126 fake_l2cap.HandlePdu(0x001, sent_packet);
127
128 // Assemble the expected packet and confirm that it matches the received
129 // packet.
130 l2cap::ChannelId cid = l2cap::kSignalingChannelId;
131 auto expected_acl_response = l2cap::testing::AclCommandRejectNotUnderstoodRsp(
132 kCommandId, kConnectionHandle, cid);
133 auto expected_response = expected_acl_response.view(
134 sizeof(hci_spec::ACLDataHeader) + sizeof(l2cap::CommandHeader));
135 EXPECT_TRUE(ContainersEqual(expected_response, *received_packet));
136 }
137
138 } // namespace bt::testing
139