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/scheduler.h" 25 #include "os/handler.h" 26 #include "os/queue.h" 27 #include "os/thread.h" 28 #include "packet/raw_builder.h" 29 30 namespace bluetooth { 31 namespace l2cap { 32 namespace internal { 33 namespace { 34 35 using ::testing::Return; 36 37 std::unique_ptr<packet::BasePacketBuilder> CreateSdu(std::vector<uint8_t> payload) { 38 auto raw_builder = std::make_unique<packet::RawBuilder>(); 39 raw_builder->AddOctets(payload); 40 return raw_builder; 41 } 42 43 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) { 44 auto bytes = std::make_shared<std::vector<uint8_t>>(); 45 BitInserter i(*bytes); 46 bytes->reserve(packet->size()); 47 packet->Serialize(i); 48 return packet::PacketView<packet::kLittleEndian>(bytes); 49 } 50 51 class FakeScheduler : public Scheduler { 52 public: 53 void OnPacketsReady(Cid cid, int number_packets) override { 54 on_packets_ready_(cid, number_packets); 55 } 56 57 void SetOnPacketsReady(std::function<void(Cid cid, int number_packets)> callback) { 58 on_packets_ready_ = callback; 59 } 60 std::function<void(Cid cid, int number_packets)> on_packets_ready_; 61 }; 62 63 class L2capSenderTest : public ::testing::Test { 64 public: 65 std::unique_ptr<Sender::UpperDequeue> enqueue_callback() { 66 auto packet_one = CreateSdu({'a', 'b', 'c'}); 67 channel_queue_.GetUpEnd()->UnregisterEnqueue(); 68 return packet_one; 69 } 70 71 protected: 72 void SetUp() override { 73 thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL); 74 user_handler_ = new os::Handler(thread_); 75 queue_handler_ = new os::Handler(thread_); 76 mock_channel_ = std::make_shared<testing::MockChannelImpl>(); 77 EXPECT_CALL(*mock_channel_, GetQueueDownEnd()).WillRepeatedly(Return(channel_queue_.GetDownEnd())); 78 EXPECT_CALL(*mock_channel_, GetCid()).WillRepeatedly(Return(cid_)); 79 EXPECT_CALL(*mock_channel_, GetRemoteCid()).WillRepeatedly(Return(cid_)); 80 sender_ = new Sender(queue_handler_, nullptr, &scheduler_, mock_channel_); 81 } 82 83 void TearDown() override { 84 queue_handler_->Clear(); 85 user_handler_->Clear(); 86 delete sender_; 87 delete queue_handler_; 88 delete user_handler_; 89 delete thread_; 90 } 91 92 os::Thread* thread_ = nullptr; 93 os::Handler* user_handler_ = nullptr; 94 os::Handler* queue_handler_ = nullptr; 95 common::BidiQueue<Sender::UpperEnqueue, Sender::UpperDequeue> channel_queue_{10}; 96 std::shared_ptr<testing::MockChannelImpl> mock_channel_; 97 Sender* sender_ = nullptr; 98 Cid cid_ = 0x41; 99 FakeScheduler scheduler_; 100 }; 101 102 TEST_F(L2capSenderTest, send_packet) { 103 std::promise<void> promise; 104 auto future = promise.get_future(); 105 scheduler_.SetOnPacketsReady([&promise](Cid cid, int number_packets) { promise.set_value(); }); 106 channel_queue_.GetUpEnd()->RegisterEnqueue( 107 queue_handler_, common::Bind(&L2capSenderTest::enqueue_callback, common::Unretained(this))); 108 auto status = future.wait_for(std::chrono::milliseconds(3)); 109 EXPECT_EQ(status, std::future_status::ready); 110 auto packet = sender_->GetNextPacket(); 111 EXPECT_NE(packet, nullptr); 112 auto packet_view = GetPacketView(std::move(packet)); 113 auto basic_frame_view = BasicFrameView::Create(packet_view); 114 EXPECT_TRUE(basic_frame_view.IsValid()); 115 EXPECT_EQ(basic_frame_view.GetChannelId(), cid_); 116 auto payload = basic_frame_view.GetPayload(); 117 std::string payload_string(payload.begin(), payload.end()); 118 EXPECT_EQ(payload_string, "abc"); 119 } 120 121 } // namespace 122 } // namespace internal 123 } // namespace l2cap 124 } // namespace bluetooth 125