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