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