summaryrefslogtreecommitdiff
path: root/system/hci/test/packet_fragmenter_host_test.cc
blob: cc8e799c1d80ad55d2b240cdea9d84eedd4d7aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <base/logging.h>
#include <gtest/gtest.h>

#include <cstdint>
#include <queue>

#include "hci/src/packet_fragmenter.cc"
#include "osi/include/allocator.h"
#include "osi/test/AllocationTestHarness.h"
#include "stack/include/bt_hdr.h"

extern void allocation_tracker_uninit(void);

enum kPacketOrder {
  kStart = 1,
  kContinuation = 2,
};

struct AclPacketHeader {
  struct {
    uint16_t handle : 12;
    uint16_t continuation : 1;
    uint16_t start : 1;
    uint16_t reserved : 2;
  } s;
  uint16_t length;

  uint16_t GetRawHandle() const { return *(uint16_t*)(this); }

  uint16_t GetHandle() const { return s.handle; }
  uint16_t GetLength() const { return length; }
} __attribute__((packed));

struct L2capPacketHeader {
  uint16_t length;
  uint16_t cid;
} __attribute__((packed));

struct AclL2capPacketHeader {
  struct AclPacketHeader acl_header;
  struct L2capPacketHeader l2cap_header;
} __attribute__((packed));

namespace {

constexpr uint16_t kHandle = 0x123;
constexpr uint16_t kCid = 0x4567;
constexpr uint16_t kMaxPacketSize = BT_DEFAULT_BUFFER_SIZE - sizeof(BT_HDR) -
                                    L2CAP_HEADER_SIZE - HCI_ACL_PREAMBLE_SIZE;
constexpr size_t kTypicalPacketSizes[] = {
    1, 2, 3, 4, 8, 16, 32, 64, 127, 128, 129, 256, 1024, 2048, kMaxPacketSize};
constexpr size_t kNumberOfTypicalPacketSizes =
    sizeof(kTypicalPacketSizes) / sizeof(kTypicalPacketSizes[0]);

void FreeBuffer(BT_HDR* bt_hdr) { osi_free(bt_hdr); }

struct TestMutables {
  struct {
    int access_count_{0};
  } fragmented;
  struct {
    int access_count_{0};
    std::queue<std::unique_ptr<BT_HDR, decltype(&FreeBuffer)>> queue;
  } reassembled;
  struct {
    int access_count_{0};
  } transmit_finished;
};

TestMutables test_state_;

void OnFragmented(BT_HDR* packet, bool send_transmit_finished) {
  test_state_.fragmented.access_count_++;
}

void OnReassembled(BT_HDR* packet) {
  test_state_.reassembled.access_count_++;
  test_state_.reassembled.queue.push(
      std::unique_ptr<BT_HDR, decltype(&FreeBuffer)>(packet, &FreeBuffer));
}

void OnTransmitFinished(BT_HDR* packet, bool all_fragments_sent) {
  test_state_.transmit_finished.access_count_++;
}

packet_fragmenter_callbacks_t result_callbacks = {
    .fragmented = OnFragmented,
    .reassembled = OnReassembled,
    .transmit_finished = OnTransmitFinished,
};

AclPacketHeader* AclHeader(BT_HDR* packet) {
  return (AclPacketHeader*)packet->data;
}
L2capPacketHeader* L2capHeader(BT_HDR* packet) {
  return &((AclL2capPacketHeader*)packet->data)->l2cap_header;
}

uint8_t* Data(BT_HDR* packet) {
  AclPacketHeader* acl_header =
      reinterpret_cast<AclPacketHeader*>(packet->data);
  return acl_header->s.start
             ? (uint8_t*)(packet->data + sizeof(AclL2capPacketHeader))
             : (uint8_t*)(packet->data + sizeof(AclPacketHeader));
}

}  // namespace

// Needed for linkage
const controller_t* controller_get_interface() { return nullptr; }

/**
 * Test class to test selected functionality in hci/src/hci_layer.cc
 */
class HciPacketFragmenterTest : public AllocationTestHarness {
 protected:
  void SetUp() override {
    AllocationTestHarness::SetUp();
    // Disable our allocation tracker to allow ASAN full range
    allocation_tracker_uninit();
    packet_fragmenter_ = packet_fragmenter_get_interface();
    packet_fragmenter_->init(&result_callbacks);
    test_state_ = TestMutables();
  }

  void TearDown() override {
    FlushPartialPackets();
    while (!test_state_.reassembled.queue.empty()) {
      test_state_.reassembled.queue.pop();
    }
    packet_fragmenter_->cleanup();
    AllocationTestHarness::TearDown();
  }
  const packet_fragmenter_t* packet_fragmenter_;

  // Start acl packet
  BT_HDR* AllocateL2capPacket(size_t l2cap_length,
                              const std::vector<uint8_t> data) const {
    auto packet =
        AllocateAclPacket(data.size() + sizeof(L2capPacketHeader), kStart);
    L2capHeader(packet)->length = l2cap_length;
    L2capHeader(packet)->cid = kCid;
    std::copy(data.cbegin(), data.cend(), Data(packet));
    return packet;
  }

  // Continuation acl packet
  BT_HDR* AllocateL2capPacket(const std::vector<uint8_t> data) const {
    auto packet = AllocateAclPacket(data.size(), kContinuation);
    std::copy(data.cbegin(), data.cend(), Data(packet));
    return packet;
  }

  const std::vector<uint8_t> CreateData(size_t size) const {
    CHECK(size > 0);
    std::vector<uint8_t> v(size);
    uint8_t sum = 0;
    for (size_t s = 0; s < size; s++) {
      sum += v[s] = s;
    }
    v[0] = (~sum + 1);  // First byte has sum complement
    return v;
  }

  // Verify packet integrity
  bool VerifyData(const uint8_t* data, size_t size) const {
    CHECK(size > 0);
    uint8_t sum = 0;
    for (size_t s = 0; s < size; s++) {
      sum += data[s];
    }
    return sum == 0;
  }

 private:
  BT_HDR* AllocateAclPacket(size_t acl_length,
                            kPacketOrder packet_order) const {
    BT_HDR* packet = AllocatePacket(sizeof(AclPacketHeader) + acl_length,
                                    MSG_HC_TO_STACK_HCI_ACL);
    AclHeader(packet)->s.handle = kHandle;
    AclHeader(packet)->length = acl_length;
    switch (packet_order) {
      case kStart:
        AclHeader(packet)->s.start = 1;
        break;
      case kContinuation:
        AclHeader(packet)->s.continuation = 1;
        break;
    }
    return packet;
  }

  BT_HDR* AllocatePacket(size_t packet_length, uint16_t event_mask) const {
    BT_HDR* packet =
        static_cast<BT_HDR*>(osi_calloc(sizeof(BT_HDR) + packet_length));
    packet->event = event_mask;
    packet->len = static_cast<uint16_t>(packet_length);
    return packet;
  }

  void FlushPartialPackets() const {
    while (!partial_packets.empty()) {
      BT_HDR* partial_packet = partial_packets.at(kHandle);
      partial_packets.erase(kHandle);
      osi_free(partial_packet);
    }
  }
};

TEST_F(HciPacketFragmenterTest, TestStruct_Handle) {
  AclPacketHeader acl_header;
  memset(&acl_header, 0, sizeof(acl_header));

  for (uint16_t h = 0; h < UINT16_MAX; h++) {
    acl_header.s.handle = h;
    CHECK(acl_header.GetHandle() == (h & HANDLE_MASK));
    CHECK(acl_header.s.continuation == 0);
    CHECK(acl_header.s.start == 0);
    CHECK(acl_header.s.reserved == 0);

    CHECK((acl_header.GetRawHandle() & HANDLE_MASK) == (h & HANDLE_MASK));
    GET_BOUNDARY_FLAG(acl_header.GetRawHandle() == 0);
  }
}

TEST_F(HciPacketFragmenterTest, TestStruct_Continuation) {
  AclPacketHeader acl_header;
  memset(&acl_header, 0, sizeof(acl_header));

  for (uint16_t h = 0; h < UINT16_MAX; h++) {
    acl_header.s.continuation = h;
    CHECK(acl_header.GetHandle() == 0);
    CHECK(acl_header.s.continuation == (h & 0x1));
    CHECK(acl_header.s.start == 0);
    CHECK(acl_header.s.reserved == 0);

    CHECK((acl_header.GetRawHandle() & HANDLE_MASK) == 0);
    GET_BOUNDARY_FLAG(acl_header.GetRawHandle() == (h & 0x3));
  }
}

TEST_F(HciPacketFragmenterTest, TestStruct_Start) {
  AclPacketHeader acl_header;
  memset(&acl_header, 0, sizeof(acl_header));

  for (uint16_t h = 0; h < UINT16_MAX; h++) {
    acl_header.s.start = h;
    CHECK(acl_header.GetHandle() == 0);
    CHECK(acl_header.s.continuation == 0);
    CHECK(acl_header.s.start == (h & 0x1));
    CHECK(acl_header.s.reserved == 0);

    CHECK((acl_header.GetRawHandle() & HANDLE_MASK) == 0);
    GET_BOUNDARY_FLAG(acl_header.GetRawHandle() == (h & 0x3));
  }
}

TEST_F(HciPacketFragmenterTest, TestStruct_Reserved) {
  AclPacketHeader acl_header;
  memset(&acl_header, 0, sizeof(acl_header));

  for (uint16_t h = 0; h < UINT16_MAX; h++) {
    acl_header.s.reserved = h;
    CHECK(acl_header.GetHandle() == 0);
    CHECK(acl_header.s.continuation == 0);
    CHECK(acl_header.s.start == 0);
    CHECK(acl_header.s.reserved == (h & 0x3));
  }
}
TEST_F(HciPacketFragmenterTest, CreateAndVerifyPackets) {
  const size_t size_check[] = {1,  2,   3,   4,   8,   16,   32,
                               64, 127, 128, 129, 256, 1024, 0xfff0};
  const std::vector<size_t> sizes(
      size_check, size_check + sizeof(size_check) / sizeof(size_check[0]));

  for (const auto packet_size : sizes) {
    const std::vector<uint8_t> data = CreateData(packet_size);
    uint8_t buf[packet_size];
    std::copy(data.cbegin(), data.cend(), buf);
    CHECK(VerifyData(buf, packet_size));
  }
}

TEST_F(HciPacketFragmenterTest, OnePacket_Immediate) {
  const std::vector<size_t> sizes(
      kTypicalPacketSizes, kTypicalPacketSizes + kNumberOfTypicalPacketSizes);

  int reassembled_access_count = 0;
  for (const auto packet_size : sizes) {
    const std::vector<uint8_t> data = CreateData(packet_size);
    reassemble_and_dispatch(AllocateL2capPacket(data.size(), data));

    CHECK(partial_packets.size() == 0);
    CHECK(test_state_.reassembled.access_count_ == ++reassembled_access_count);
    auto packet = std::move(test_state_.reassembled.queue.front());
    test_state_.reassembled.queue.pop();
    CHECK(VerifyData(Data(packet.get()), packet_size));
  }
}

TEST_F(HciPacketFragmenterTest, OnePacket_ImmediateTooBig) {
  const size_t packet_size = kMaxPacketSize + 1;
  const std::vector<uint8_t> data = CreateData(packet_size);
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), data));

  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 0);
}

TEST_F(HciPacketFragmenterTest, ThreePackets_Immediate) {
  const size_t packet_size = 512;
  const std::vector<uint8_t> data = CreateData(packet_size);
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), data));
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), data));
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), data));
  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 3);
}

TEST_F(HciPacketFragmenterTest, OnePacket_SplitTwo) {
  const std::vector<size_t> sizes(
      kTypicalPacketSizes, kTypicalPacketSizes + kNumberOfTypicalPacketSizes);

  int reassembled_access_count = 0;
  for (auto packet_size : sizes) {
    const std::vector<uint8_t> data = CreateData(packet_size);
    const std::vector<uint8_t> part1(data.cbegin(),
                                     data.cbegin() + packet_size / 2);
    reassemble_and_dispatch(AllocateL2capPacket(data.size(), part1));

    CHECK(partial_packets.size() == 1);
    CHECK(test_state_.reassembled.access_count_ == reassembled_access_count);

    const std::vector<uint8_t> part2(data.cbegin() + packet_size / 2,
                                     data.cend());
    reassemble_and_dispatch(AllocateL2capPacket(part2));

    CHECK(partial_packets.size() == 0);
    CHECK(test_state_.reassembled.access_count_ == ++reassembled_access_count);

    auto packet = std::move(test_state_.reassembled.queue.front());
    test_state_.reassembled.queue.pop();
    CHECK(VerifyData(Data(packet.get()), packet_size));
  }
}

TEST_F(HciPacketFragmenterTest, OnePacket_SplitALot) {
  const size_t packet_size = 512;
  const size_t stride = 2;

  const std::vector<uint8_t> data = CreateData(packet_size);
  const std::vector<uint8_t> first_part(data.cbegin(), data.cbegin() + stride);
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), first_part));
  CHECK(partial_packets.size() == 1);

  for (size_t i = 2; i < packet_size - stride; i += stride) {
    const std::vector<uint8_t> middle_part(data.cbegin() + i,
                                           data.cbegin() + i + stride);
    reassemble_and_dispatch(AllocateL2capPacket(middle_part));
  }
  CHECK(partial_packets.size() == 1);
  CHECK(test_state_.reassembled.access_count_ == 0);

  const std::vector<uint8_t> last_part(data.cbegin() + packet_size - stride,
                                       data.cend());
  reassemble_and_dispatch(AllocateL2capPacket(last_part));

  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 1);
  auto packet = std::move(test_state_.reassembled.queue.front());
  CHECK(VerifyData(Data(packet.get()), packet_size));
}

TEST_F(HciPacketFragmenterTest, TwoPacket_InvalidLength) {
  const size_t packet_size = UINT16_MAX;
  const std::vector<uint8_t> data = CreateData(packet_size);
  const std::vector<uint8_t> first_part(data.cbegin(),
                                        data.cbegin() + packet_size / 2);
  reassemble_and_dispatch(AllocateL2capPacket(data.size(), first_part));

  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 0);

  const std::vector<uint8_t> second_part(data.cbegin() + packet_size / 2,
                                         data.cend());
  reassemble_and_dispatch(AllocateL2capPacket(second_part));

  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 0);
}

TEST_F(HciPacketFragmenterTest, TwoPacket_HugeBogusSecond) {
  const size_t packet_size = kMaxPacketSize;
  const std::vector<uint8_t> data = CreateData(UINT16_MAX);
  const std::vector<uint8_t> first_part(data.cbegin(),
                                        data.cbegin() + packet_size - 1);
  reassemble_and_dispatch(AllocateL2capPacket(packet_size, first_part));

  CHECK(partial_packets.size() == 1);
  CHECK(test_state_.reassembled.access_count_ == 0);

  const std::vector<uint8_t> second_part(data.cbegin() + packet_size - 1,
                                         data.cend());
  reassemble_and_dispatch(AllocateL2capPacket(second_part));

  CHECK(partial_packets.size() == 0);
  CHECK(test_state_.reassembled.access_count_ == 1);
}