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_tx_engine.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
18 #include "pw_bluetooth_sapphire/internal/host/l2cap/fake_tx_channel.h"
19 #include "pw_bluetooth_sapphire/internal/host/testing/test_helpers.h"
20 #include "pw_unit_test/framework.h"
21
22 namespace bt::l2cap::internal {
23 namespace {
24
25 constexpr ChannelId kTestChannelId = 0x0001;
26
TEST(BasicModeTxEngineTest,ProcessSduTransmitsMinimalSizedSdu)27 TEST(BasicModeTxEngineTest, ProcessSduTransmitsMinimalSizedSdu) {
28 ByteBufferPtr last_pdu;
29 size_t n_pdus = 0;
30 FakeTxChannel channel;
31 channel.HandleSendFrame([&](auto pdu) {
32 ++n_pdus;
33 last_pdu = std::move(pdu);
34 });
35
36 constexpr size_t kMtu = 10;
37 const StaticByteBuffer payload(1);
38 channel.QueueSdu(std::make_unique<DynamicByteBuffer>(payload));
39 BasicModeTxEngine(kTestChannelId, kMtu, channel).NotifySduQueued();
40 EXPECT_EQ(1u, n_pdus);
41 ASSERT_TRUE(last_pdu);
42 EXPECT_TRUE(ContainersEqual(payload, *last_pdu));
43 }
44
TEST(BasicModeTxEngineTest,ProcessSduTransmitsMaximalSizedSdu)45 TEST(BasicModeTxEngineTest, ProcessSduTransmitsMaximalSizedSdu) {
46 ByteBufferPtr last_pdu;
47 size_t n_pdus = 0;
48 FakeTxChannel channel;
49 channel.HandleSendFrame([&](auto pdu) {
50 ++n_pdus;
51 last_pdu = std::move(pdu);
52 });
53
54 constexpr size_t kMtu = 1;
55 const StaticByteBuffer payload(1);
56 channel.QueueSdu(std::make_unique<DynamicByteBuffer>(payload));
57 BasicModeTxEngine(kTestChannelId, kMtu, channel).NotifySduQueued();
58 EXPECT_EQ(1u, n_pdus);
59 ASSERT_TRUE(last_pdu);
60 EXPECT_TRUE(ContainersEqual(payload, *last_pdu));
61 }
62
TEST(BasicModeTxEngineTest,ProcessSduDropsOversizedSdu)63 TEST(BasicModeTxEngineTest, ProcessSduDropsOversizedSdu) {
64 FakeTxChannel channel;
65 size_t n_pdus = 0;
66 channel.HandleSendFrame([&](auto) { ++n_pdus; });
67
68 constexpr size_t kMtu = 1;
69 channel.QueueSdu(std::make_unique<DynamicByteBuffer>(StaticByteBuffer(1, 2)));
70 BasicModeTxEngine(kTestChannelId, kMtu, channel).NotifySduQueued();
71 EXPECT_EQ(0u, n_pdus);
72 }
73
TEST(BasicModeTxEngineTest,ProcessSduSurvivesZeroByteSdu)74 TEST(BasicModeTxEngineTest, ProcessSduSurvivesZeroByteSdu) {
75 FakeTxChannel channel;
76 size_t n_pdus = 0;
77 channel.HandleSendFrame([&](auto) { ++n_pdus; });
78 constexpr size_t kMtu = 1;
79 channel.QueueSdu(std::make_unique<DynamicByteBuffer>());
80 BasicModeTxEngine(kTestChannelId, kMtu, channel).NotifySduQueued();
81 EXPECT_EQ(1u, n_pdus);
82 }
83
84 } // namespace
85 } // namespace bt::l2cap::internal
86