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 <android-base/silent_death_test.h>
18 #include <gtest/gtest.h>
19
20 #include <memory>
21
22 #include "packet.h"
23 #include "packet_test_common.h"
24 #include "test_packets.h"
25
26 namespace bluetooth {
27
28 using pair = std::pair<size_t, size_t>;
29
30 class IteratorTest
31 : public ::testing::TestWithParam<std::pair<size_t, size_t>> {
32 public:
GetTestPacket()33 std::shared_ptr<TestPacket> GetTestPacket() {
34 auto bounds = GetParam();
35 auto lower_bound = bounds.first;
36 auto upper_bound = bounds.second;
37
38 return TestPacket::Make(test_l2cap_data, lower_bound, upper_bound);
39 }
40
GetTestPacketLength()41 size_t GetTestPacketLength() { return GetParam().second - GetParam().first; }
42
GetLowerBound()43 size_t GetLowerBound() { return GetParam().first; }
44
GetUpperBound()45 size_t GetUpperBound() { return GetParam().second; }
46 };
47
48 INSTANTIATE_TEST_CASE_P(IteratorParameterTest, IteratorTest,
49 ::testing::Values(pair(0, test_l2cap_data.size()),
50 pair(3, test_l2cap_data.size() - 2)));
51
52 using IteratorDeathTest = IteratorTest;
53
54 INSTANTIATE_TEST_CASE_P(IteratorParameterTest, IteratorDeathTest,
55 ::testing::Values(pair(0, test_l2cap_data.size()),
56 pair(3, test_l2cap_data.size() - 2)));
57
TEST_F(IteratorDeathTest,iteratorCreateDeathTest)58 TEST_F(IteratorDeathTest, iteratorCreateDeathTest) {
59 auto packet =
60 TestPacket::Make(test_l2cap_data, 3, test_l2cap_data.size() - 2);
61
62 // this will silent SIGABRT sent in ASSERT_DEATH below
63 ScopedSilentDeath _silentDeath;
64
65 ASSERT_DEATH(Iterator(packet, 0), "index_ >= packet->packet_start_index_");
66 ASSERT_DEATH(Iterator(packet, test_l2cap_data.size()),
67 "index_ <= packet->packet_end_index_");
68 }
69
TEST_F(IteratorTest,extractTest)70 TEST_F(IteratorTest, extractTest) {
71 auto packet = TestPacket::Make(test_l2cap_data);
72 Iterator general_case = packet->begin();
73
74 ASSERT_EQ(0x02u, general_case.extract<uint8_t>());
75 ASSERT_EQ(0x2edcu, general_case.extract<uint16_t>());
76 ASSERT_EQ(0x00620066u, general_case.extract<uint32_t>());
77 ASSERT_EQ(0x00010000000a0013u, general_case.extract<uint64_t>());
78 }
79
TEST_F(IteratorTest,extractBETest)80 TEST_F(IteratorTest, extractBETest) {
81 auto packet = TestPacket::Make(test_l2cap_data);
82 Iterator general_case = packet->begin();
83
84 ASSERT_EQ(0x02u, general_case.extractBE<uint8_t>());
85 ASSERT_EQ(0xdc2eu, general_case.extractBE<uint16_t>());
86 ASSERT_EQ(0x66006200u, general_case.extractBE<uint32_t>());
87 ASSERT_EQ(0x13000a0000000100u, general_case.extractBE<uint64_t>());
88 }
89
TEST_P(IteratorTest,payloadBoundsTest)90 TEST_P(IteratorTest, payloadBoundsTest) {
91 auto packet = GetTestPacket();
92 ASSERT_EQ(static_cast<size_t>(packet->end() - packet->begin()),
93 GetTestPacketLength());
94
95 auto it = packet->begin();
96 for (size_t i = 0; i < GetTestPacketLength(); i++) {
97 ASSERT_EQ(test_l2cap_data[i + GetLowerBound()], *it++);
98 }
99 }
100
TEST_P(IteratorDeathTest,extractBoundsDeathTest)101 TEST_P(IteratorDeathTest, extractBoundsDeathTest) {
102 auto packet = GetTestPacket();
103 Iterator bounds_test = packet->end();
104
105 // this will silent SIGABRT sent in ASSERT_DEATH below
106 ScopedSilentDeath _silentDeath;
107
108 ASSERT_DEATH(bounds_test.extract<uint8_t>(),
109 "index_ != packet_->packet_end_index_");
110 ASSERT_DEATH(bounds_test.extract<uint16_t>(),
111 "index_ != packet_->packet_end_index_");
112 ASSERT_DEATH(bounds_test.extract<uint32_t>(),
113 "index_ != packet_->packet_end_index_");
114 ASSERT_DEATH(bounds_test.extract<uint64_t>(),
115 "index_ != packet_->packet_end_index_");
116 }
117
TEST_P(IteratorDeathTest,extractBEBoundsDeathTest)118 TEST_P(IteratorDeathTest, extractBEBoundsDeathTest) {
119 auto packet = GetTestPacket();
120 Iterator bounds_test = packet->end();
121
122 // this will silent SIGABRT sent in ASSERT_DEATH below
123 ScopedSilentDeath _silentDeath;
124
125 ASSERT_DEATH(bounds_test.extractBE<uint8_t>(),
126 "index_ != packet_->packet_end_index_");
127 ASSERT_DEATH(bounds_test.extractBE<uint16_t>(),
128 "index_ != packet_->packet_end_index_");
129 ASSERT_DEATH(bounds_test.extractBE<uint32_t>(),
130 "index_ != packet_->packet_end_index_");
131 ASSERT_DEATH(bounds_test.extractBE<uint64_t>(),
132 "index_ != packet_->packet_end_index_");
133 }
134
TEST_P(IteratorDeathTest,dereferenceDeathTest)135 TEST_P(IteratorDeathTest, dereferenceDeathTest) {
136 auto packet = GetTestPacket();
137 Iterator dereference_test = packet->end();
138
139 ASSERT_EQ((*packet)[GetTestPacketLength() - 1],
140 *(dereference_test - static_cast<size_t>(1)));
141
142 // this will silent SIGABRT sent in ASSERT_DEATH below
143 ScopedSilentDeath _silentDeath;
144
145 ASSERT_DEATH(*dereference_test, "index_ != packet_->packet_end_index_");
146 }
147
TEST_P(IteratorTest,plusEqTest)148 TEST_P(IteratorTest, plusEqTest) {
149 auto packet = GetTestPacket();
150 Iterator plus_eq = packet->begin();
151 for (size_t i = 0; i < GetTestPacketLength(); i += 2) {
152 ASSERT_EQ(test_l2cap_data[i + GetLowerBound()], *plus_eq)
153 << "+= test: Dereferenced iterator does not equal expected at index "
154 << i;
155 plus_eq += 2;
156 }
157
158 ASSERT_EQ(plus_eq, packet->end());
159 }
160
TEST_P(IteratorTest,preIncrementTest)161 TEST_P(IteratorTest, preIncrementTest) {
162 auto packet = GetTestPacket();
163 Iterator plus_plus = packet->begin();
164 for (size_t i = 0; i < GetTestPacketLength() - 1; i++) {
165 ASSERT_EQ(test_l2cap_data[i + GetLowerBound() + 1], *(++plus_plus))
166 << "Pre-increment test: Dereferenced iterator does not equal expected "
167 << "at index " << i;
168 }
169 }
170
TEST_P(IteratorTest,postIncrementTest)171 TEST_P(IteratorTest, postIncrementTest) {
172 auto packet = GetTestPacket();
173 Iterator plus_plus = packet->begin();
174 for (size_t i = 0; i < GetTestPacketLength(); i++) {
175 ASSERT_EQ(test_l2cap_data[i + GetLowerBound()], *(plus_plus++))
176 << "Post-increment test: Dereferenced iterator does not equal expected "
177 << "at index " << i;
178 }
179 }
180
TEST_P(IteratorTest,additionTest)181 TEST_P(IteratorTest, additionTest) {
182 auto packet = GetTestPacket();
183 Iterator plus = packet->begin();
184 for (size_t i = 0; i < GetTestPacketLength(); i++) {
185 ASSERT_EQ(test_l2cap_data[i + GetLowerBound()], *plus)
186 << "+ test: Dereferenced iterator does not equal expected at index "
187 << i;
188 plus = plus + static_cast<size_t>(1);
189 }
190 }
191
TEST_P(IteratorTest,minusEqTest)192 TEST_P(IteratorTest, minusEqTest) {
193 auto packet = GetTestPacket();
194 Iterator minus_eq = packet->end();
195 minus_eq -= 1;
196 for (int i = GetTestPacketLength() - 1; i > 0; i -= 2) {
197 ASSERT_EQ(test_l2cap_data[static_cast<size_t>(i) + GetLowerBound()],
198 *minus_eq)
199 << "-= test: Dereferenced iterator does not equal expected at index "
200 << i;
201 minus_eq -= 2;
202 }
203 }
204
TEST_P(IteratorTest,preDecrementTest)205 TEST_P(IteratorTest, preDecrementTest) {
206 auto packet = GetTestPacket();
207 Iterator minus_minus = packet->end();
208 for (int i = GetTestPacketLength(); i > 0; i--) {
209 ASSERT_EQ(test_l2cap_data[static_cast<size_t>(i) + GetLowerBound() - 1],
210 *(--minus_minus))
211 << "Pre-decrement test: Dereferenced iterator does not equal expected "
212 << "at index " << i;
213 }
214 }
215
TEST_P(IteratorTest,postDecrementTest)216 TEST_P(IteratorTest, postDecrementTest) {
217 auto packet = GetTestPacket();
218 Iterator minus_minus = packet->end();
219 minus_minus--;
220 for (int i = GetTestPacketLength() - 1; i > 0; i--) {
221 ASSERT_EQ(test_l2cap_data[static_cast<size_t>(i) + GetLowerBound()],
222 *(minus_minus--))
223 << "Post-decrement test: Dereferenced iterator does not equal expected "
224 << "at index " << i;
225 }
226 }
227
TEST_P(IteratorTest,subtractionTest)228 TEST_P(IteratorTest, subtractionTest) {
229 auto packet = GetTestPacket();
230 Iterator minus = packet->end();
231 minus = minus - static_cast<size_t>(1);
232 for (int i = GetTestPacketLength() - 1; i > 0; i--) {
233 ASSERT_EQ(test_l2cap_data[static_cast<size_t>(i) + GetLowerBound()], *minus)
234 << "- test: Dereferenced iterator does not equal expected at index "
235 << i;
236 minus = minus - static_cast<size_t>(1);
237 }
238 }
239
TEST_P(IteratorTest,plusEqBoundsTest)240 TEST_P(IteratorTest, plusEqBoundsTest) {
241 auto packet = GetTestPacket();
242 Iterator plus_eq = packet->end();
243 for (size_t i = 0; i < 100; i++) {
244 plus_eq += i;
245 ASSERT_EQ(packet->end(), plus_eq)
246 << "+= test: Iterator exceeded the upper bound set by get_length()";
247 }
248 }
249
TEST_P(IteratorTest,preIncrementBoundsTest)250 TEST_P(IteratorTest, preIncrementBoundsTest) {
251 auto packet = GetTestPacket();
252 Iterator plus_plus = packet->end();
253 plus_plus--;
254 for (size_t i = 0; i < 100; i++) {
255 ASSERT_EQ(packet->end(), ++plus_plus)
256 << "Pre-increment test: Iterator exceeded the upper bound set "
257 "by get_length()";
258 }
259 }
260
TEST_P(IteratorTest,postIncrementBoundsTest)261 TEST_P(IteratorTest, postIncrementBoundsTest) {
262 auto packet = GetTestPacket();
263 Iterator plus_plus = packet->end();
264 for (size_t i = 0; i < 100; i++) {
265 ASSERT_EQ(packet->end(), plus_plus++)
266 << "Post-increment test: Iterator exceeded the upper bound set "
267 "by get_length()";
268 }
269 }
270
TEST_P(IteratorTest,additionBoundsTest)271 TEST_P(IteratorTest, additionBoundsTest) {
272 auto packet = GetTestPacket();
273 Iterator plus = packet->end();
274 for (size_t i = 0; i < 100; i++) {
275 plus = plus + static_cast<size_t>(i);
276 ASSERT_EQ(packet->end(), plus)
277 << "+ test: Iterator exceeded the upper bound set by get_length()";
278 }
279 }
280
TEST_P(IteratorTest,minusEqBoundsTest)281 TEST_P(IteratorTest, minusEqBoundsTest) {
282 auto packet = GetTestPacket();
283 Iterator minus_eq = packet->begin();
284 for (size_t i = 0; i < 100; i++) {
285 minus_eq -= i;
286 ASSERT_EQ(test_l2cap_data[GetLowerBound()], *minus_eq)
287 << "-= test: Iterator is less than the lower bound set by "
288 "packet->begin()";
289 }
290 }
291
TEST_P(IteratorTest,preDecrementBoundsTest)292 TEST_P(IteratorTest, preDecrementBoundsTest) {
293 auto packet = GetTestPacket();
294 Iterator minus_minus = packet->begin();
295 for (size_t i = 0; i < 100; i++) {
296 ASSERT_EQ(test_l2cap_data[GetLowerBound()], *(--minus_minus))
297 << "Pre-decrement test: Iterator is less than the lower bound set by "
298 "packet->begin()";
299 }
300 }
301
TEST_P(IteratorTest,postDecrementBoundsTest)302 TEST_P(IteratorTest, postDecrementBoundsTest) {
303 auto packet = GetTestPacket();
304 Iterator minus_minus = packet->begin();
305 for (size_t i = 0; i < 100; i++) {
306 ASSERT_EQ(test_l2cap_data[GetLowerBound()], *(minus_minus--))
307 << "Post-decrement test: Iterator is less than the lower bound set by "
308 "packet->begin()";
309 }
310 }
311
TEST_P(IteratorTest,subtractionBoundsTest)312 TEST_P(IteratorTest, subtractionBoundsTest) {
313 auto packet = GetTestPacket();
314 Iterator minus = packet->begin();
315 for (size_t i = 0; i < 100; i++) {
316 minus = minus - static_cast<size_t>(i);
317 ASSERT_EQ(test_l2cap_data[GetLowerBound()], *minus)
318 << "- test: Iterator is less than the lower bound set "
319 "by packet->begin()";
320 }
321 }
322
323 } // namespace bluetooth
324