1 /* 2 * Copyright 2018 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 "hci/hci_packets.h" 18 19 #include <gtest/gtest.h> 20 #include <memory> 21 22 #include "os/log.h" 23 #include "packet/bit_inserter.h" 24 #include "packet/raw_builder.h" 25 26 using bluetooth::packet::BitInserter; 27 using bluetooth::packet::RawBuilder; 28 using std::vector; 29 30 namespace { 31 vector<uint8_t> information_request = { 32 0xfe, 0x2e, 0x0a, 0x00, 0x06, 0x00, 0x01, 0x00, 0x0a, 0x02, 0x02, 0x00, 0x02, 0x00, 33 }; 34 // 0x00, 0x01, 0x02, 0x03, ... 35 vector<uint8_t> counting_bytes; 36 // 0xFF, 0xFE, 0xFD, 0xFC, ... 37 vector<uint8_t> counting_down_bytes; 38 const size_t count_size = 0x8; 39 40 } // namespace 41 42 namespace bluetooth { 43 namespace hci { 44 45 class AclBuilderTest : public ::testing::Test { 46 public: 47 AclBuilderTest() { 48 counting_bytes.reserve(count_size); 49 counting_down_bytes.reserve(count_size); 50 for (size_t i = 0; i < count_size; i++) { 51 counting_bytes.push_back(i); 52 counting_down_bytes.push_back(~i); 53 } 54 } 55 ~AclBuilderTest() = default; 56 }; 57 58 TEST(AclBuilderTest, buildAclCount) { 59 uint16_t handle = 0x0314; 60 PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE; 61 BroadcastFlag broadcast_flag = BroadcastFlag::ACTIVE_SLAVE_BROADCAST; 62 63 std::unique_ptr<RawBuilder> count_payload = std::make_unique<RawBuilder>(); 64 count_payload->AddOctets(counting_bytes); 65 ASSERT_EQ(counting_bytes.size(), count_payload->size()); 66 67 std::unique_ptr<AclPacketBuilder> count_packet = 68 AclPacketBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(count_payload)); 69 70 ASSERT_EQ(counting_bytes.size() + 4, count_packet->size()); 71 72 std::shared_ptr<std::vector<uint8_t>> count_packet_bytes = std::make_shared<std::vector<uint8_t>>(); 73 BitInserter it(*count_packet_bytes); 74 count_packet->Serialize(it); 75 76 PacketView<true> count_packet_bytes_view(count_packet_bytes); 77 AclPacketView count_packet_view = AclPacketView::Create(count_packet_bytes_view); 78 ASSERT_TRUE(count_packet_view.IsValid()); 79 80 ASSERT_EQ(handle, count_packet_view.GetHandle()); 81 ASSERT_EQ(packet_boundary_flag, count_packet_view.GetPacketBoundaryFlag()); 82 ASSERT_EQ(broadcast_flag, count_packet_view.GetBroadcastFlag()); 83 PacketView<true> count_view = count_packet_view.GetPayload(); 84 85 ASSERT_EQ(count_view.size(), counting_bytes.size()); 86 for (size_t i = 0; i < count_view.size(); i++) { 87 ASSERT_EQ(count_view[i], counting_bytes[i]); 88 } 89 } 90 91 TEST(AclBuilderTest, buildAclCountInverted) { 92 uint16_t handle = 0x0304; 93 PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::CONTINUING_FRAGMENT; 94 BroadcastFlag broadcast_flag = BroadcastFlag::POINT_TO_POINT; 95 96 std::unique_ptr<RawBuilder> counting_down_bytes_payload = std::make_unique<RawBuilder>(); 97 counting_down_bytes_payload->AddOctets(counting_down_bytes); 98 ASSERT_EQ(counting_down_bytes.size(), counting_down_bytes_payload->size()); 99 100 std::unique_ptr<AclPacketBuilder> counting_down_bytes_packet = 101 AclPacketBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(counting_down_bytes_payload)); 102 103 ASSERT_EQ(counting_down_bytes.size() + 4, counting_down_bytes_packet->size()); 104 105 std::shared_ptr<std::vector<uint8_t>> counting_down_bytes_packet_bytes = std::make_shared<std::vector<uint8_t>>(); 106 BitInserter it(*counting_down_bytes_packet_bytes); 107 counting_down_bytes_packet->Serialize(it); 108 PacketView<true> counting_down_bytes_packet_bytes_view(counting_down_bytes_packet_bytes); 109 AclPacketView counting_down_bytes_packet_view = AclPacketView::Create(counting_down_bytes_packet_bytes_view); 110 ASSERT_TRUE(counting_down_bytes_packet_view.IsValid()); 111 112 ASSERT_EQ(handle, counting_down_bytes_packet_view.GetHandle()); 113 ASSERT_EQ(packet_boundary_flag, counting_down_bytes_packet_view.GetPacketBoundaryFlag()); 114 ASSERT_EQ(broadcast_flag, counting_down_bytes_packet_view.GetBroadcastFlag()); 115 PacketView<true> counting_down_bytes_view = counting_down_bytes_packet_view.GetPayload(); 116 117 ASSERT_EQ(counting_down_bytes_view.size(), counting_down_bytes.size()); 118 for (size_t i = 0; i < counting_down_bytes_view.size(); i++) { 119 ASSERT_EQ(counting_down_bytes_view[i], counting_down_bytes[i]); 120 } 121 } 122 123 TEST(AclBuilderTest, buildInformationRequest) { 124 uint16_t handle = 0x0efe; 125 PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE; 126 BroadcastFlag broadcast_flag = BroadcastFlag::POINT_TO_POINT; 127 128 std::vector<uint8_t> payload_bytes(information_request.begin() + 4, information_request.end()); 129 std::unique_ptr<RawBuilder> payload = std::make_unique<RawBuilder>(); 130 payload->AddOctets(payload_bytes); 131 ASSERT_EQ(payload_bytes.size(), payload->size()); 132 133 std::unique_ptr<AclPacketBuilder> packet = 134 AclPacketBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(payload)); 135 136 ASSERT_EQ(information_request.size(), packet->size()); 137 138 std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>(); 139 BitInserter it(*packet_bytes); 140 packet->Serialize(it); 141 PacketView<true> packet_bytes_view(packet_bytes); 142 AclPacketView packet_view = AclPacketView::Create(packet_bytes_view); 143 ASSERT_TRUE(packet_view.IsValid()); 144 145 ASSERT_EQ(packet_bytes->size(), information_request.size()); 146 for (size_t i = 0; i < packet_bytes->size(); i++) { 147 ASSERT_EQ((*packet_bytes)[i], information_request[i]); 148 } 149 150 ASSERT_EQ(handle, packet_view.GetHandle()); 151 ASSERT_EQ(packet_boundary_flag, packet_view.GetPacketBoundaryFlag()); 152 ASSERT_EQ(broadcast_flag, packet_view.GetBroadcastFlag()); 153 PacketView<true> payload_view = packet_view.GetPayload(); 154 155 ASSERT_EQ(payload_view.size(), payload_bytes.size()); 156 for (size_t i = 0; i < payload_view.size(); i++) { 157 ASSERT_EQ(payload_view[i], payload_bytes[i]); 158 } 159 160 ASSERT_EQ(packet_view.size(), information_request.size()); 161 for (size_t i = 0; i < packet_view.size(); i++) { 162 ASSERT_EQ(packet_view[i], information_request[i]); 163 } 164 } 165 166 } // namespace hci 167 } // namespace bluetooth 168