1 /******************************************************************************
2  *
3  *  Copyright 2018 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "stack_rfcomm_test_utils.h"
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include <string>
25 
26 #include "stack/include/rfcdefs.h"
27 #include "stack_test_packet_utils.h"
28 
29 namespace {
30 
31 using testing::ElementsAreArray;
32 
33 using bluetooth::AllocateWrappedIncomingL2capAclPacket;
34 using bluetooth::CreateAclPacket;
35 using bluetooth::CreateL2capDataPacket;
36 
37 using bluetooth::rfcomm::CreateMccMscFrame;
38 using bluetooth::rfcomm::CreateMccPnFrame;
39 using bluetooth::rfcomm::CreateMultiplexerControlFrame;
40 using bluetooth::rfcomm::CreateQuickDataPacket;
41 using bluetooth::rfcomm::CreateQuickMscPacket;
42 using bluetooth::rfcomm::CreateQuickPnPacket;
43 using bluetooth::rfcomm::CreateQuickSabmPacket;
44 using bluetooth::rfcomm::CreateQuickUaPacket;
45 using bluetooth::rfcomm::CreateRfcommPacket;
46 using bluetooth::rfcomm::GetAddressField;
47 using bluetooth::rfcomm::GetControlField;
48 using bluetooth::rfcomm::GetDlci;
49 
50 const uint8_t kIncomingSabmChannel0[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
51                                          0x5c, 0x00, 0x03, 0x3f, 0x01, 0x1c};
52 
TEST(RfcommTestPacketGeneratorTest,TestGenerateSabmChannel0Packet)53 TEST(RfcommTestPacketGeneratorTest, TestGenerateSabmChannel0Packet) {
54   uint8_t dlci = GetDlci(false, 0);
55   EXPECT_EQ(dlci, 0);
56   uint8_t address_field = GetAddressField(true, true, dlci);
57   EXPECT_EQ(address_field, 0x03);
58   uint8_t control_field = GetControlField(true, RFCOMM_SABME);
59   EXPECT_EQ(control_field, 0x3F);
60   std::vector<uint8_t> rfcomm_packet = CreateRfcommPacket(address_field, control_field, -1, {});
61   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
62   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
63   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingSabmChannel0));
64 }
65 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateSabmChannel0Packet)66 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateSabmChannel0Packet) {
67   EXPECT_THAT(CreateQuickSabmPacket(RFCOMM_MX_DLCI, 0x005c, 0x0008),
68               ElementsAreArray(kIncomingSabmChannel0));
69 }
70 
71 const uint8_t kOutgoingUaChannel0[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
72                                        0x00, 0x17, 0x03, 0x73, 0x01, 0xd7};
73 
TEST(RfcommTestPacketGeneratorTest,TestGenerateUaPacket)74 TEST(RfcommTestPacketGeneratorTest, TestGenerateUaPacket) {
75   uint8_t dlci = GetDlci(false, 0);
76   EXPECT_EQ(dlci, 0);
77   uint8_t address_field = GetAddressField(true, true, dlci);
78   EXPECT_EQ(address_field, 0x03);
79   uint8_t control_field = GetControlField(true, RFCOMM_UA);
80   EXPECT_EQ(control_field, 0x73);
81   std::vector<uint8_t> rfcomm_packet = CreateRfcommPacket(address_field, control_field, -1, {});
82   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x1700, rfcomm_packet);
83   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
84   EXPECT_THAT(acl_packet, ElementsAreArray(kOutgoingUaChannel0));
85 }
86 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUaPacket)87 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUaPacket) {
88   EXPECT_THAT(CreateQuickUaPacket(RFCOMM_MX_DLCI, 0x1700, 0x0008),
89               ElementsAreArray(kOutgoingUaChannel0));
90 }
91 
92 const uint8_t kIncomingUihPnSetChannelTo3[] = {0x08, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x5c, 0x00,
93                                                0x03, 0xef, 0x15, 0x83, 0x11, 0x06, 0xf0, 0x00,
94                                                0x00, 0x74, 0x03, 0x00, 0x01, 0x70};
95 
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihPnSetChannel3Packet)96 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihPnSetChannel3Packet) {
97   uint8_t dlci = GetDlci(false, 0);
98   EXPECT_EQ(dlci, 0);
99   uint8_t address_field = GetAddressField(true, true, dlci);
100   EXPECT_EQ(address_field, 0x03);
101   uint8_t control_field = GetControlField(false, RFCOMM_UIH);
102   EXPECT_EQ(control_field, 0xEF);
103   uint8_t new_dlci = GetDlci(false, 3);
104   EXPECT_EQ(new_dlci, 6);
105   std::vector<uint8_t> mcc_pn_data = CreateMccPnFrame(new_dlci, 0x0, 0xF, 0, 0, 884, 0, 1);
106   std::vector<uint8_t> mcc_payload = CreateMultiplexerControlFrame(0x20, true, mcc_pn_data);
107   std::vector<uint8_t> rfcomm_packet =
108           CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
109   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
110   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
111   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihPnSetChannelTo3));
112 }
113 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihPnSetChannel3Packet)114 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihPnSetChannel3Packet) {
115   EXPECT_THAT(CreateQuickPnPacket(true, GetDlci(false, 3), true, 884, 0xF, 0, 1, 0x005c, 0x0008),
116               ElementsAreArray(kIncomingUihPnSetChannelTo3));
117 }
118 
119 const uint8_t kIncomingSabmChannel3[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
120                                          0x5c, 0x00, 0x1b, 0x3f, 0x01, 0xd3};
121 
TEST(RfcommTestPacketGeneratorTest,TestGenerateSabmChannel3Packet)122 TEST(RfcommTestPacketGeneratorTest, TestGenerateSabmChannel3Packet) {
123   uint8_t dlci = GetDlci(false, 3);
124   EXPECT_EQ(dlci, 6);
125   uint8_t address_field = GetAddressField(true, true, dlci);
126   EXPECT_EQ(address_field, 0x1b);
127   uint8_t control_field = GetControlField(true, RFCOMM_SABME);
128   EXPECT_EQ(control_field, 0x3F);
129   std::vector<uint8_t> rfcomm_packet = CreateRfcommPacket(address_field, control_field, -1, {});
130   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
131   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
132   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingSabmChannel3));
133 }
134 
TEST(RfcommTestPacketGeneratorTest,TestGenerateQuickSabmChannel3Packet)135 TEST(RfcommTestPacketGeneratorTest, TestGenerateQuickSabmChannel3Packet) {
136   EXPECT_THAT(CreateQuickSabmPacket(GetDlci(false, 3), 0x005c, 0x0008),
137               ElementsAreArray(kIncomingSabmChannel3));
138 }
139 
140 const uint8_t kIncomingUihMscCmdFrame[] = {0x08, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x5c, 0x00,
141                                            0x03, 0xef, 0x09, 0xe3, 0x05, 0x1b, 0x8d, 0x70};
142 
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihMscCmdPacket)143 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihMscCmdPacket) {
144   uint8_t dlci = GetDlci(false, 0);
145   EXPECT_EQ(dlci, 0);
146   uint8_t address_field = GetAddressField(true, true, dlci);
147   EXPECT_EQ(address_field, 0x03);
148   uint8_t control_field = GetControlField(false, RFCOMM_UIH);
149   EXPECT_EQ(control_field, 0xEF);
150   uint8_t new_dlci = GetDlci(false, 3);
151   EXPECT_EQ(new_dlci, 6);
152   std::vector<uint8_t> mcc_msc_data = CreateMccMscFrame(new_dlci, false, true, true, false, true);
153   std::vector<uint8_t> mcc_payload = CreateMultiplexerControlFrame(0x38, true, mcc_msc_data);
154   std::vector<uint8_t> rfcomm_packet =
155           CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
156   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
157   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
158   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihMscCmdFrame));
159 }
160 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihMscCmdPacket)161 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihMscCmdPacket) {
162   EXPECT_THAT(CreateQuickMscPacket(true, GetDlci(false, 3), 0x005c, 0x0008, true, false, true, true,
163                                    false, true),
164               ElementsAreArray(kIncomingUihMscCmdFrame));
165 }
166 
167 const uint8_t kIncomingUihMscResponseFrame[] = {0x08, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x5c, 0x00,
168                                                 0x03, 0xef, 0x09, 0xe1, 0x05, 0x1b, 0x8d, 0x70};
169 
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihMscResponsePacket)170 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihMscResponsePacket) {
171   uint8_t dlci = GetDlci(false, 0);
172   EXPECT_EQ(dlci, 0);
173   uint8_t address_field = GetAddressField(true, true, dlci);
174   EXPECT_EQ(address_field, 0x03);
175   uint8_t control_field = GetControlField(false, RFCOMM_UIH);
176   EXPECT_EQ(control_field, 0xEF);
177   uint8_t new_dlci = GetDlci(false, 3);
178   EXPECT_EQ(new_dlci, 6);
179   std::vector<uint8_t> mcc_msc_data = CreateMccMscFrame(new_dlci, false, true, true, false, true);
180   std::vector<uint8_t> mcc_payload = CreateMultiplexerControlFrame(0x38, false, mcc_msc_data);
181   std::vector<uint8_t> rfcomm_packet =
182           CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
183   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
184   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
185   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihMscResponseFrame));
186 }
187 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihMscResponsePacket)188 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihMscResponsePacket) {
189   EXPECT_THAT(CreateQuickMscPacket(true, GetDlci(false, 3), 0x005c, 0x0008, false, false, true,
190                                    true, false, true),
191               ElementsAreArray(kIncomingUihMscResponseFrame));
192 }
193 
194 const uint8_t kIncomingBrsfFrame[] = {0x08, 0x20, 0x15, 0x00, 0x11, 0x00, 0x5c, 0x00, 0x1b,
195                                       0xff, 0x19, 0x06, 0x41, 0x54, 0x2b, 0x42, 0x52, 0x53,
196                                       0x46, 0x3d, 0x39, 0x35, 0x39, 0x0d, 0x93};
197 
TEST(RfcommTestPacketGeneratorTest,TestGenerateDataPacket)198 TEST(RfcommTestPacketGeneratorTest, TestGenerateDataPacket) {
199   uint8_t dlci = GetDlci(false, 3);
200   EXPECT_EQ(dlci, 6);
201   uint8_t address_field = GetAddressField(true, true, dlci);
202   EXPECT_EQ(address_field, 0x1B);
203   uint8_t control_field = GetControlField(true, RFCOMM_UIH);
204   EXPECT_EQ(control_field, 0xFF);
205   const std::string data_str = "AT+BRSF=959\r";
206   const std::vector<uint8_t> data(data_str.begin(), data_str.end());
207   std::vector<uint8_t> rfcomm_packet = CreateRfcommPacket(address_field, control_field, 6, data);
208   std::vector<uint8_t> l2cap_packet = CreateL2capDataPacket(0x005c, rfcomm_packet);
209   std::vector<uint8_t> acl_packet = CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
210   EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingBrsfFrame));
211 }
212 
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateDataPacket)213 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateDataPacket) {
214   EXPECT_THAT(CreateQuickDataPacket(GetDlci(false, 3), true, 0x005c, 0x0008, 6, "AT+BRSF=959\r"),
215               ElementsAreArray(kIncomingBrsfFrame));
216 }
217 
218 }  // namespace
219