1 /*
2  * Copyright 2019 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 "packet/packet_builder.h"
18 
19 #include <gtest/gtest.h>
20 #include <forward_list>
21 #include <memory>
22 
23 using bluetooth::packet::BasePacketBuilder;
24 using bluetooth::packet::BitInserter;
25 using bluetooth::packet::PacketBuilder;
26 using std::vector;
27 
28 namespace {
29 vector<uint8_t> count_all = {
30     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
31     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32 };
33 
34 vector<uint8_t> count_1 = {
35     0x00,
36     0x01,
37     0x02,
38 };
39 
40 vector<uint8_t> count_2 = {
41     0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
42 };
43 
44 vector<uint8_t> count_3 = {
45     0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
46 };
47 }  // namespace
48 
49 namespace bluetooth {
50 namespace packet {
51 
52 template <bool little_endian>
53 class EndianBuilder : public PacketBuilder<little_endian> {
54  public:
EndianBuilder(uint8_t byte,uint16_t two_bytes,uint32_t four_bytes,uint64_t eight_bytes)55   EndianBuilder(uint8_t byte, uint16_t two_bytes, uint32_t four_bytes, uint64_t eight_bytes)
56       : byte_(byte), two_bytes_(two_bytes), four_bytes_(four_bytes), eight_bytes_(eight_bytes) {}
57   ~EndianBuilder() = default;
58 
size() const59   virtual size_t size() const override {
60     return sizeof(signature_) + sizeof(byte_) + sizeof(two_bytes_) + sizeof(four_bytes_) + sizeof(eight_bytes_);
61   }
62 
FinalPacket()63   virtual const std::unique_ptr<std::vector<uint8_t>> FinalPacket() {
64     std::unique_ptr<std::vector<uint8_t>> packet = std::make_unique<std::vector<uint8_t>>();
65     packet->reserve(size());
66     BitInserter it(*packet);
67     Serialize(it);
68     return packet;
69   }
70 
Serialize(BitInserter & it) const71   virtual void Serialize(BitInserter& it) const override {
72     PacketBuilder<little_endian>::insert(signature_, it);
73     PacketBuilder<little_endian>::insert(byte_, it);
74     PacketBuilder<little_endian>::insert(two_bytes_, it);
75     PacketBuilder<little_endian>::insert(four_bytes_, it);
76     PacketBuilder<little_endian>::insert(eight_bytes_, it);
77   }
78 
79  private:
80   uint32_t signature_{(little_endian ? 0x03020100 : 0x00010203)};
81   uint8_t byte_;
82   uint16_t two_bytes_;
83   uint32_t four_bytes_;
84   uint64_t eight_bytes_;
85 };
86 
87 class PacketBuilderEndianTest : public ::testing::Test {
88  public:
89   PacketBuilderEndianTest() = default;
90   ~PacketBuilderEndianTest() = default;
91 };
92 
TEST(PacketBuilderEndianTest,insertTest)93 TEST(PacketBuilderEndianTest, insertTest) {
94   EndianBuilder<true> little(0x04, 0x0605, 0x0a090807, 0x1211100f0e0d0c0b);
95   EndianBuilder<false> big(0x04, 0x0506, 0x0708090a, 0x0b0c0d0e0f101112);
96   ASSERT_EQ(*big.FinalPacket(), *little.FinalPacket());
97 }
98 
99 template <typename T>
100 class VectorBuilder : public PacketBuilder<true> {
101  public:
VectorBuilder(const std::vector<uint64_t> vect)102   VectorBuilder(const std::vector<uint64_t> vect) {
103     for (uint64_t element : vect) {
104       vect_.push_back(static_cast<T>(element));
105     }
106   }
107   ~VectorBuilder() = default;
108 
size() const109   virtual size_t size() const override {
110     return vect_.size() * sizeof(T);
111   }
112 
FinalPacket()113   virtual const std::unique_ptr<std::vector<uint8_t>> FinalPacket() {
114     std::unique_ptr<std::vector<uint8_t>> packet = std::make_unique<std::vector<uint8_t>>();
115     packet->reserve(size());
116     BitInserter it(*packet);
117     Serialize(it);
118     return packet;
119   }
120 
Serialize(BitInserter & it) const121   virtual void Serialize(BitInserter& it) const override {
122     PacketBuilder<true>::insert_vector(vect_, it);
123   }
124 
125  private:
126   std::vector<T> vect_;
127 };
128 
129 template <typename T>
130 class InsertElementsBuilder : public PacketBuilder<true> {
131  public:
InsertElementsBuilder(const std::vector<uint64_t> vect)132   InsertElementsBuilder(const std::vector<uint64_t> vect) {
133     for (uint64_t element : vect) {
134       vect_.push_back(static_cast<T>(element));
135     }
136   }
137   virtual ~InsertElementsBuilder() = default;
138 
size() const139   virtual size_t size() const override {
140     return vect_.size() * sizeof(T);
141   }
142 
FinalPacket()143   virtual const std::unique_ptr<std::vector<uint8_t>> FinalPacket() {
144     std::unique_ptr<std::vector<uint8_t>> packet = std::make_unique<std::vector<uint8_t>>();
145     packet->reserve(size());
146     BitInserter it(*packet);
147     Serialize(it);
148     return packet;
149   }
150 
Serialize(BitInserter & it) const151   virtual void Serialize(BitInserter& it) const override {
152     for (T elem : vect_) {
153       PacketBuilder<true>::insert(elem, it);
154     }
155   }
156 
157  private:
158   std::vector<T> vect_;
159 };
160 
161 std::vector<uint64_t> vector_data{
162     0x7060504030201000, 0x7161514131211101, 0x7262524232221202, 0x7363534333231303, 0x7464544434241404,
163     0x7565554535251505, 0x7666564636261606, 0x7767574737271707, 0x7868584838281808,
164 };
165 
166 template <typename T>
167 class VectorBuilderTest : public ::testing::Test {
168  public:
169   VectorBuilderTest() = default;
170   ~VectorBuilderTest() = default;
171 
SetUp()172   void SetUp() {
173     packet_1_ = std::shared_ptr<VectorBuilder<T>>(new VectorBuilder<T>(vector_data));
174     packet_2_ = std::shared_ptr<InsertElementsBuilder<T>>(new InsertElementsBuilder<T>(vector_data));
175   }
176 
TearDown()177   void TearDown() {
178     packet_1_.reset();
179     packet_2_.reset();
180   }
181 
182   std::shared_ptr<VectorBuilder<T>> packet_1_;
183   std::shared_ptr<InsertElementsBuilder<T>> packet_2_;
184 };
185 
186 using VectorBaseTypes = ::testing::Types<uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t>;
187 TYPED_TEST_CASE(VectorBuilderTest, VectorBaseTypes);
188 
TYPED_TEST(VectorBuilderTest,insertVectorTest)189 TYPED_TEST(VectorBuilderTest, insertVectorTest) {
190   ASSERT_EQ(*(this->packet_1_->FinalPacket()), *(this->packet_2_->FinalPacket()));
191 }
192 
193 class NestedBuilder : public PacketBuilder<true> {
194  public:
195   ~NestedBuilder() = default;
196 
size() const197   virtual size_t size() const override {
198     size_t payload_size = (payload_ ? payload_->size() : 0);
199     return 1 + payload_size;
200   }
201 
Create(uint8_t level)202   static std::unique_ptr<NestedBuilder> Create(uint8_t level) {
203     return std::unique_ptr<NestedBuilder>(new NestedBuilder(level));
204   }
205 
CreateNested(std::unique_ptr<BasePacketBuilder> payload,uint8_t level)206   static std::unique_ptr<NestedBuilder> CreateNested(std::unique_ptr<BasePacketBuilder> payload, uint8_t level) {
207     return std::unique_ptr<NestedBuilder>(new NestedBuilder(std::move(payload), level));
208   }
209 
FinalPacket()210   virtual const std::unique_ptr<std::vector<uint8_t>> FinalPacket() {
211     std::unique_ptr<std::vector<uint8_t>> packet = std::make_unique<std::vector<uint8_t>>();
212     packet->reserve(size());
213     BitInserter it(*packet);
214     Serialize(it);
215     return packet;
216   }
217 
Serialize(BitInserter & it) const218   virtual void Serialize(BitInserter& it) const override {
219     PacketBuilder<true>::insert(level_, it);
220     if (payload_) {
221       payload_->Serialize(it);
222     }
223   }
224 
225  private:
226   std::unique_ptr<BasePacketBuilder> payload_;
227   uint8_t level_;
228 
NestedBuilder(std::unique_ptr<BasePacketBuilder> inner,uint8_t level)229   NestedBuilder(std::unique_ptr<BasePacketBuilder> inner, uint8_t level) : payload_(std::move(inner)), level_(level) {}
NestedBuilder(uint8_t level)230   NestedBuilder(uint8_t level) : level_(level) {}
231 };
232 
233 class BuilderBuilderTest : public ::testing::Test {};
234 
TEST(BuilderBuilderTest,nestingTest)235 TEST(BuilderBuilderTest, nestingTest) {
236   std::unique_ptr<BasePacketBuilder> innermost = NestedBuilder::Create(0);
237   std::unique_ptr<BasePacketBuilder> number_1 = NestedBuilder::CreateNested(std::move(innermost), 1);
238   std::unique_ptr<BasePacketBuilder> number_2 = NestedBuilder::CreateNested(std::move(number_1), 2);
239   std::unique_ptr<BasePacketBuilder> number_3 = NestedBuilder::CreateNested(std::move(number_2), 3);
240   std::unique_ptr<BasePacketBuilder> number_4 = NestedBuilder::CreateNested(std::move(number_3), 4);
241   std::unique_ptr<NestedBuilder> number_5 = NestedBuilder::CreateNested(std::move(number_4), 5);
242 
243   std::vector<uint8_t> count_down{5, 4, 3, 2, 1, 0};
244   ASSERT_EQ(*number_5->FinalPacket(), count_down);
245 }
246 }  // namespace packet
247 }  // namespace bluetooth
248