1 /*
2  * Copyright 2018 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 <gtest/gtest.h>
18 #include <memory>
19 
20 #include "packet.h"
21 #include "packet_test_common.h"
22 #include "test_packets.h"
23 
24 using std::vector;
25 
26 namespace bluetooth {
27 
TEST(PacketBuilderTest,serializeTest)28 TEST(PacketBuilderTest, serializeTest) {
29   auto builder = TestPacketBuilder::MakeBuilder(test_l2cap_data);
30   auto packet = TestPacket::Make();
31 
32   builder->Serialize(packet);
33 
34   for (size_t i = 0; i < test_l2cap_data.size(); i++) {
35     ASSERT_EQ(test_l2cap_data[i], (*packet)[i]);
36   }
37 }
38 
TEST(PacketBuilderTest,sizeTest)39 TEST(PacketBuilderTest, sizeTest) {
40   auto builder = TestPacketBuilder::MakeBuilder(test_l2cap_data);
41 
42   ASSERT_EQ(builder->size(), test_l2cap_data.size());
43 }
44 
TEST(PacketBuilderTest,reserveSpaceTest)45 TEST(PacketBuilderTest, reserveSpaceTest) {
46   auto packet = TestPacket::Make();
47   ASSERT_EQ(packet->GetData().capacity(), 0u);
48 
49   auto builder = TestPacketBuilder::MakeBuilder(test_l2cap_data);
50   builder->ReserveSpace(packet, test_l2cap_data.size());
51 
52   ASSERT_GE(packet->GetData().capacity(), test_l2cap_data.size());
53 }
54 
TEST(PacketBuilderTest,addPayloadOctetsTest)55 TEST(PacketBuilderTest, addPayloadOctetsTest) {
56   auto builder = TestPacketBuilder::MakeBuilder(test_l2cap_data);
57   auto packet = TestPacket::Make();
58 
59   builder->AddPayloadOctets1(packet, 0x01u);
60   builder->AddPayloadOctets2(packet, 0x0302u);
61   builder->AddPayloadOctets3(packet, 0x060504u);
62   builder->AddPayloadOctets4(packet, 0x0A090807u);
63   builder->AddPayloadOctets6(packet, 0x100F0E0D0C0Bu);
64   builder->AddPayloadOctets8(packet, 0x1817161514131211u);
65 
66   for (size_t i = 0; i < 0x18; i++) {
67     ASSERT_EQ((*packet)[i], i + 1);
68   }
69 }
70 
71 }  // namespace bluetooth