summaryrefslogtreecommitdiff
path: root/system/gd/packet/raw_builder_unittest.cc
blob: 64ca0ed24a454a89d19a3d37d2913dc8b0a3e305 (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
/*
 * Copyright 2019 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 "packet/raw_builder.h"

#include <gtest/gtest.h>
#include <forward_list>
#include <memory>

#include "hci/address.h"

using bluetooth::hci::Address;
using bluetooth::packet::BitInserter;
using std::vector;

namespace {
vector<uint8_t> count = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};

}  // namespace

namespace bluetooth {
namespace packet {

class RawBuilderTest : public ::testing::Test {
 public:
  RawBuilderTest() = default;
  ~RawBuilderTest() = default;
};

TEST(RawBuilderTest, buildCountTest) {
  std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>();
  ASSERT_EQ(0u, count_builder->size());
  count_builder->AddOctets8(0x0706050403020100);
  count_builder->AddOctets4(0x0b0a0908);
  count_builder->AddOctets2(0x0d0c);
  count_builder->AddOctets1(0x0e);
  count_builder->AddOctets1(0x0f);
  count_builder->AddAddress(Address({0x10, 0x11, 0x12, 0x13, 0x14, 0x15}));
  std::vector<uint8_t> count_subset(count.begin() + 0x16, count.end());
  count_builder->AddOctets(count_subset);

  ASSERT_EQ(count.size(), count_builder->size());

  std::vector<uint8_t> packet;
  BitInserter it(packet);

  count_builder->Serialize(it);

  ASSERT_EQ(count, packet);
}

TEST(RawBuilderTest, buildStartingWithVector) {
  std::vector<uint8_t> count_first(count.begin(), count.begin() + 0x8);
  std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(count_first);
  count_builder->AddOctets4(0x0b0a0908);
  count_builder->AddOctets2(0x0d0c);
  count_builder->AddOctets1(0x0e);
  count_builder->AddOctets1(0x0f);
  count_builder->AddOctets8(0x1716151413121110);
  std::vector<uint8_t> count_last(count.begin() + 0x18, count.end());
  count_builder->AddOctets(count_last);

  ASSERT_EQ(count.size(), count_builder->size());

  std::vector<uint8_t> packet;
  BitInserter it(packet);

  count_builder->Serialize(it);

  ASSERT_EQ(count, packet);
}

TEST(RawBuilderTest, testMaxBytes) {
  const size_t kMaxBytes = count.size();
  std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(kMaxBytes);
  ASSERT_TRUE(count_builder->AddOctets(count));
  ASSERT_FALSE(count_builder->AddOctets4(0x0b0a0908));
  ASSERT_FALSE(count_builder->AddOctets2(0x0d0c));
  ASSERT_FALSE(count_builder->AddOctets1(0x0e));
  ASSERT_FALSE(count_builder->AddOctets1(0x0f));
  ASSERT_FALSE(count_builder->AddOctets8(0x1716151413121110));
  std::vector<uint8_t> count_last(count.begin() + 0x18, count.end());
  ASSERT_FALSE(count_builder->AddOctets(count_last));

  ASSERT_EQ(count.size(), count_builder->size());

  std::vector<uint8_t> packet;
  BitInserter it(packet);

  count_builder->Serialize(it);

  ASSERT_EQ(count, packet);
}

}  // namespace packet
}  // namespace bluetooth