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/basic_mode_channel_data_controller.h"
18
19 #include <gtest/gtest.h>
20
21 #include "l2cap/internal/scheduler_mock.h"
22 #include "packet/raw_builder.h"
23
24 namespace bluetooth {
25 namespace l2cap {
26 namespace internal {
27 namespace {
28
CreateSdu(std::vector<uint8_t> payload)29 std::unique_ptr<packet::BasePacketBuilder> CreateSdu(std::vector<uint8_t> payload) {
30 auto raw_builder = std::make_unique<packet::RawBuilder>();
31 raw_builder->AddOctets(payload);
32 return raw_builder;
33 }
34
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)35 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
36 auto bytes = std::make_shared<std::vector<uint8_t>>();
37 BitInserter i(*bytes);
38 bytes->reserve(packet->size());
39 packet->Serialize(i);
40 return packet::PacketView<packet::kLittleEndian>(bytes);
41 }
42
sync_handler(os::Handler * handler)43 void sync_handler(os::Handler* handler) {
44 std::promise<void> promise;
45 auto future = promise.get_future();
46 handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
47 auto status = future.wait_for(std::chrono::milliseconds(300));
48 EXPECT_EQ(status, std::future_status::ready);
49 }
50
51 class BasicModeDataControllerTest : public ::testing::Test {
52 protected:
SetUp()53 void SetUp() override {
54 thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
55 user_handler_ = new os::Handler(thread_);
56 queue_handler_ = new os::Handler(thread_);
57 }
58
TearDown()59 void TearDown() override {
60 queue_handler_->Clear();
61 user_handler_->Clear();
62 delete queue_handler_;
63 delete user_handler_;
64 delete thread_;
65 }
66
67 os::Thread* thread_ = nullptr;
68 os::Handler* user_handler_ = nullptr;
69 os::Handler* queue_handler_ = nullptr;
70 };
71
TEST_F(BasicModeDataControllerTest,transmit)72 TEST_F(BasicModeDataControllerTest, transmit) {
73 common::BidiQueue<Scheduler::UpperEnqueue, Scheduler::UpperDequeue> channel_queue{10};
74 testing::MockScheduler scheduler;
75 BasicModeDataController controller{1, 1, channel_queue.GetDownEnd(), queue_handler_, &scheduler};
76 EXPECT_CALL(scheduler, OnPacketsReady(1, 1));
77 controller.OnSdu(CreateSdu({'a', 'b', 'c', 'd'}));
78 auto next_packet = controller.GetNextPacket();
79 EXPECT_NE(next_packet, nullptr);
80 auto view = GetPacketView(std::move(next_packet));
81 auto pdu_view = BasicFrameView::Create(view);
82 EXPECT_TRUE(pdu_view.IsValid());
83 auto payload = pdu_view.GetPayload();
84 std::string data = std::string(payload.begin(), payload.end());
85 EXPECT_EQ(data, "abcd");
86 }
87
TEST_F(BasicModeDataControllerTest,receive)88 TEST_F(BasicModeDataControllerTest, receive) {
89 common::BidiQueue<Scheduler::UpperEnqueue, Scheduler::UpperDequeue> channel_queue{10};
90 testing::MockScheduler scheduler;
91 BasicModeDataController controller{1, 1, channel_queue.GetDownEnd(), queue_handler_, &scheduler};
92 auto base_view = GetPacketView(BasicFrameBuilder::Create(1, CreateSdu({'a', 'b', 'c', 'd'})));
93 controller.OnPdu(base_view);
94 sync_handler(queue_handler_);
95 auto packet_view = channel_queue.GetUpEnd()->TryDequeue();
96 EXPECT_NE(packet_view, nullptr);
97 std::string data = std::string(packet_view->begin(), packet_view->end());
98 EXPECT_EQ(data, "abcd");
99 }
100
101 } // namespace
102 } // namespace internal
103 } // namespace l2cap
104 } // namespace bluetooth
105