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