1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "l2cap/internal/sender.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <future>
22
23 #include "l2cap/internal/channel_impl_mock.h"
24 #include "l2cap/internal/ilink_mock.h"
25 #include "l2cap/internal/scheduler.h"
26 #include "os/handler.h"
27 #include "os/queue.h"
28 #include "os/thread.h"
29 #include "packet/raw_builder.h"
30
31 namespace bluetooth {
32 namespace l2cap {
33 namespace internal {
34 namespace {
35
36 using ::testing::Return;
37
CreateSdu(std::vector<uint8_t> payload)38 std::unique_ptr<packet::BasePacketBuilder> CreateSdu(std::vector<uint8_t> payload) {
39 auto raw_builder = std::make_unique<packet::RawBuilder>();
40 raw_builder->AddOctets(payload);
41 return raw_builder;
42 }
43
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)44 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
45 auto bytes = std::make_shared<std::vector<uint8_t>>();
46 BitInserter i(*bytes);
47 bytes->reserve(packet->size());
48 packet->Serialize(i);
49 return packet::PacketView<packet::kLittleEndian>(bytes);
50 }
51
52 class FakeScheduler : public Scheduler {
53 public:
OnPacketsReady(Cid cid,int number_packets)54 void OnPacketsReady(Cid cid, int number_packets) override {
55 on_packets_ready_(cid, number_packets);
56 }
57
SetOnPacketsReady(std::function<void (Cid cid,int number_packets)> callback)58 void SetOnPacketsReady(std::function<void(Cid cid, int number_packets)> callback) {
59 on_packets_ready_ = callback;
60 }
61 std::function<void(Cid cid, int number_packets)> on_packets_ready_;
62 };
63
64 class L2capSenderTest : public ::testing::Test {
65 public:
enqueue_callback()66 std::unique_ptr<Sender::UpperDequeue> enqueue_callback() {
67 auto packet_one = CreateSdu({'a', 'b', 'c'});
68 channel_queue_.GetUpEnd()->UnregisterEnqueue();
69 return packet_one;
70 }
71
72 protected:
SetUp()73 void SetUp() override {
74 thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
75 user_handler_ = new os::Handler(thread_);
76 queue_handler_ = new os::Handler(thread_);
77 mock_channel_ = std::make_shared<testing::MockChannelImpl>();
78 EXPECT_CALL(*mock_channel_, GetQueueDownEnd()).WillRepeatedly(Return(channel_queue_.GetDownEnd()));
79 EXPECT_CALL(*mock_channel_, GetCid()).WillRepeatedly(Return(cid_));
80 EXPECT_CALL(*mock_channel_, GetRemoteCid()).WillRepeatedly(Return(cid_));
81 sender_ = new Sender(queue_handler_, &link_, &scheduler_, mock_channel_);
82 }
83
TearDown()84 void TearDown() override {
85 queue_handler_->Clear();
86 user_handler_->Clear();
87 delete sender_;
88 delete queue_handler_;
89 delete user_handler_;
90 delete thread_;
91 }
92
93 os::Thread* thread_ = nullptr;
94 os::Handler* user_handler_ = nullptr;
95 os::Handler* queue_handler_ = nullptr;
96 common::BidiQueue<Sender::UpperEnqueue, Sender::UpperDequeue> channel_queue_{10};
97 std::shared_ptr<testing::MockChannelImpl> mock_channel_;
98 Sender* sender_ = nullptr;
99 Cid cid_ = 0x41;
100 FakeScheduler scheduler_;
101 testing::MockILink link_;
102 };
103
TEST_F(L2capSenderTest,send_packet)104 TEST_F(L2capSenderTest, send_packet) {
105 std::promise<void> promise;
106 auto future = promise.get_future();
107 scheduler_.SetOnPacketsReady([&promise](Cid cid, int number_packets) { promise.set_value(); });
108 channel_queue_.GetUpEnd()->RegisterEnqueue(
109 queue_handler_, common::Bind(&L2capSenderTest::enqueue_callback, common::Unretained(this)));
110 auto status = future.wait_for(std::chrono::milliseconds(3));
111 EXPECT_EQ(status, std::future_status::ready);
112 auto packet = sender_->GetNextPacket();
113 EXPECT_NE(packet, nullptr);
114 auto packet_view = GetPacketView(std::move(packet));
115 auto basic_frame_view = BasicFrameView::Create(packet_view);
116 EXPECT_TRUE(basic_frame_view.IsValid());
117 EXPECT_EQ(basic_frame_view.GetChannelId(), cid_);
118 auto payload = basic_frame_view.GetPayload();
119 std::string payload_string(payload.begin(), payload.end());
120 EXPECT_EQ(payload_string, "abc");
121 }
122
123 } // namespace
124 } // namespace internal
125 } // namespace l2cap
126 } // namespace bluetooth
127