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/l2cap/basic_mode_rx_engine.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
18 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
19 #include "pw_bluetooth_sapphire/internal/host/l2cap/fragmenter.h"
20 #include "pw_bluetooth_sapphire/internal/host/l2cap/recombiner.h"
21 #include "pw_bluetooth_sapphire/internal/host/testing/test_helpers.h"
22 #include "pw_unit_test/framework.h"
23
24 namespace bt::l2cap::internal {
25 namespace {
26
27 constexpr hci_spec::ConnectionHandle kTestHandle = 0x0001;
28 constexpr ChannelId kTestChannelId = 0x0001;
29
TEST(BasicModeRxEngineTest,ProcessPduReturnsSdu)30 TEST(BasicModeRxEngineTest, ProcessPduReturnsSdu) {
31 const StaticByteBuffer payload('h', 'e', 'l', 'l', 'o');
32 const auto sdu = BasicModeRxEngine().ProcessPdu(
33 Fragmenter(kTestHandle)
34 .BuildFrame(
35 kTestChannelId, payload, FrameCheckSequenceOption::kNoFcs));
36 ASSERT_TRUE(sdu);
37 EXPECT_TRUE(ContainersEqual(payload, *sdu));
38 }
39
TEST(BasicModeRxEngineTest,ProcessPduCanHandleZeroBytePayload)40 TEST(BasicModeRxEngineTest, ProcessPduCanHandleZeroBytePayload) {
41 const StaticByteBuffer byte_buf(0x01,
42 0x00,
43 0x04,
44 0x00, // ACL data header
45 0x00,
46 0x00,
47 0xFF,
48 0xFF // Basic L2CAP header
49 );
50 auto hci_packet = hci::ACLDataPacket::New(
51 static_cast<uint16_t>(byte_buf.size() - sizeof(hci_spec::ACLDataHeader)));
52 hci_packet->mutable_view()->mutable_data().Write(byte_buf);
53 hci_packet->InitializeFromBuffer();
54
55 Recombiner recombiner(kTestHandle);
56 auto result = recombiner.ConsumeFragment(std::move(hci_packet));
57 EXPECT_FALSE(result.frames_dropped);
58 ASSERT_TRUE(result.pdu);
59
60 ASSERT_TRUE(result.pdu->is_valid());
61 ASSERT_EQ(1u, result.pdu->fragment_count());
62 ASSERT_EQ(0u, result.pdu->length());
63
64 const ByteBufferPtr sdu =
65 BasicModeRxEngine().ProcessPdu(std::move(*result.pdu));
66 ASSERT_TRUE(sdu);
67 EXPECT_EQ(0u, sdu->size());
68 }
69
70 } // namespace
71 } // namespace bt::l2cap::internal
72