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