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