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/raw_builder.h"
18
19 #include <gtest/gtest.h>
20
21 #include <forward_list>
22 #include <memory>
23
24 using bluetooth::packet::BitInserter;
25 using std::vector;
26
27 namespace {
28 vector<uint8_t> count = {
29 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
30 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
31 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32 };
33
34 } // namespace
35
36 namespace bluetooth {
37 namespace packet {
38
39 class RawBuilderTest : public ::testing::Test {
40 public:
41 RawBuilderTest() = default;
42 ~RawBuilderTest() = default;
43 };
44
TEST(RawBuilderTest,buildCountTest)45 TEST(RawBuilderTest, buildCountTest) {
46 std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>();
47 ASSERT_EQ(0u, count_builder->size());
48 count_builder->AddOctets8(0x0706050403020100);
49 count_builder->AddOctets4(0x0b0a0908);
50 count_builder->AddOctets2(0x0d0c);
51 count_builder->AddOctets1(0x0e);
52 count_builder->AddOctets1(0x0f);
53 count_builder->AddOctets(std::array<uint8_t, 6>{0x10, 0x11, 0x12, 0x13, 0x14, 0x15});
54 std::vector<uint8_t> count_subset(count.begin() + 0x16, count.end());
55 count_builder->AddOctets(count_subset);
56
57 ASSERT_EQ(count.size(), count_builder->size());
58
59 std::vector<uint8_t> packet;
60 BitInserter it(packet);
61
62 count_builder->Serialize(it);
63
64 ASSERT_EQ(count, packet);
65 }
66
TEST(RawBuilderTest,buildStartingWithVector)67 TEST(RawBuilderTest, buildStartingWithVector) {
68 std::vector<uint8_t> count_first(count.begin(), count.begin() + 0x8);
69 std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(count_first);
70 count_builder->AddOctets4(0x0b0a0908);
71 count_builder->AddOctets2(0x0d0c);
72 count_builder->AddOctets1(0x0e);
73 count_builder->AddOctets1(0x0f);
74 count_builder->AddOctets8(0x1716151413121110);
75 std::vector<uint8_t> count_last(count.begin() + 0x18, count.end());
76 count_builder->AddOctets(count_last);
77
78 ASSERT_EQ(count.size(), count_builder->size());
79
80 std::vector<uint8_t> packet;
81 BitInserter it(packet);
82
83 count_builder->Serialize(it);
84
85 ASSERT_EQ(count, packet);
86 }
87
TEST(RawBuilderTest,testMaxBytes)88 TEST(RawBuilderTest, testMaxBytes) {
89 const size_t kMaxBytes = count.size();
90 std::unique_ptr<RawBuilder> count_builder = std::make_unique<RawBuilder>(kMaxBytes);
91 ASSERT_TRUE(count_builder->AddOctets(count));
92 ASSERT_FALSE(count_builder->AddOctets4(0x0b0a0908));
93 ASSERT_FALSE(count_builder->AddOctets2(0x0d0c));
94 ASSERT_FALSE(count_builder->AddOctets1(0x0e));
95 ASSERT_FALSE(count_builder->AddOctets1(0x0f));
96 ASSERT_FALSE(count_builder->AddOctets8(0x1716151413121110));
97 std::vector<uint8_t> count_last(count.begin() + 0x18, count.end());
98 ASSERT_FALSE(count_builder->AddOctets(count_last));
99
100 ASSERT_EQ(count.size(), count_builder->size());
101
102 std::vector<uint8_t> packet;
103 BitInserter it(packet);
104
105 count_builder->Serialize(it);
106
107 ASSERT_EQ(count, packet);
108 }
109
110 } // namespace packet
111 } // namespace bluetooth
112