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 <string>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23
24 #include "rfcdefs.h"
25 #include "stack_rfcomm_test_utils.h"
26 #include "stack_test_packet_utils.h"
27
28 namespace {
29
30 using testing::ElementsAreArray;
31
32 using bluetooth::CreateL2capDataPacket;
33 using bluetooth::CreateAclPacket;
34 using bluetooth::AllocateWrappedIncomingL2capAclPacket;
35
36 using bluetooth::rfcomm::GetDlci;
37 using bluetooth::rfcomm::GetAddressField;
38 using bluetooth::rfcomm::GetControlField;
39 using bluetooth::rfcomm::CreateMccPnFrame;
40 using bluetooth::rfcomm::CreateMccMscFrame;
41 using bluetooth::rfcomm::CreateMultiplexerControlFrame;
42 using bluetooth::rfcomm::CreateRfcommPacket;
43 using bluetooth::rfcomm::CreateQuickDataPacket;
44 using bluetooth::rfcomm::CreateQuickPnPacket;
45 using bluetooth::rfcomm::CreateQuickSabmPacket;
46 using bluetooth::rfcomm::CreateQuickUaPacket;
47 using bluetooth::rfcomm::CreateQuickMscPacket;
48
49 const uint8_t kIncomingSabmChannel0[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
50 0x5c, 0x00, 0x03, 0x3f, 0x01, 0x1c};
51
TEST(RfcommTestPacketGeneratorTest,TestGenerateSabmChannel0Packet)52 TEST(RfcommTestPacketGeneratorTest, TestGenerateSabmChannel0Packet) {
53 uint8_t dlci = GetDlci(false, 0);
54 EXPECT_EQ(dlci, 0);
55 uint8_t address_field = GetAddressField(true, true, dlci);
56 EXPECT_EQ(address_field, 0x03);
57 uint8_t control_field = GetControlField(true, RFCOMM_SABME);
58 EXPECT_EQ(control_field, 0x3F);
59 std::vector<uint8_t> rfcomm_packet =
60 CreateRfcommPacket(address_field, control_field, -1, {});
61 std::vector<uint8_t> l2cap_packet =
62 CreateL2capDataPacket(0x005c, rfcomm_packet);
63 std::vector<uint8_t> acl_packet =
64 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
65 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingSabmChannel0));
66 }
67
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateSabmChannel0Packet)68 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateSabmChannel0Packet) {
69 EXPECT_THAT(CreateQuickSabmPacket(RFCOMM_MX_DLCI, 0x005c, 0x0008),
70 ElementsAreArray(kIncomingSabmChannel0));
71 }
72
73 const uint8_t kOutgoingUaChannel0[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
74 0x00, 0x17, 0x03, 0x73, 0x01, 0xd7};
75
TEST(RfcommTestPacketGeneratorTest,TestGenerateUaPacket)76 TEST(RfcommTestPacketGeneratorTest, TestGenerateUaPacket) {
77 uint8_t dlci = GetDlci(false, 0);
78 EXPECT_EQ(dlci, 0);
79 uint8_t address_field = GetAddressField(true, true, dlci);
80 EXPECT_EQ(address_field, 0x03);
81 uint8_t control_field = GetControlField(true, RFCOMM_UA);
82 EXPECT_EQ(control_field, 0x73);
83 std::vector<uint8_t> rfcomm_packet =
84 CreateRfcommPacket(address_field, control_field, -1, {});
85 std::vector<uint8_t> l2cap_packet =
86 CreateL2capDataPacket(0x1700, rfcomm_packet);
87 std::vector<uint8_t> acl_packet =
88 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
89 EXPECT_THAT(acl_packet, ElementsAreArray(kOutgoingUaChannel0));
90 }
91
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUaPacket)92 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUaPacket) {
93 EXPECT_THAT(CreateQuickUaPacket(RFCOMM_MX_DLCI, 0x1700, 0x0008),
94 ElementsAreArray(kOutgoingUaChannel0));
95 }
96
97 const uint8_t kIncomingUihPnSetChannelTo3[] = {
98 0x08, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x5c, 0x00, 0x03, 0xef, 0x15,
99 0x83, 0x11, 0x06, 0xf0, 0x00, 0x00, 0x74, 0x03, 0x00, 0x01, 0x70};
100
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihPnSetChannel3Packet)101 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihPnSetChannel3Packet) {
102 uint8_t dlci = GetDlci(false, 0);
103 EXPECT_EQ(dlci, 0);
104 uint8_t address_field = GetAddressField(true, true, dlci);
105 EXPECT_EQ(address_field, 0x03);
106 uint8_t control_field = GetControlField(false, RFCOMM_UIH);
107 EXPECT_EQ(control_field, 0xEF);
108 uint8_t new_dlci = GetDlci(false, 3);
109 EXPECT_EQ(new_dlci, 6);
110 std::vector<uint8_t> mcc_pn_data =
111 CreateMccPnFrame(new_dlci, 0x0, 0xF, 0, 0, 884, 0, 1);
112 std::vector<uint8_t> mcc_payload =
113 CreateMultiplexerControlFrame(0x20, true, mcc_pn_data);
114 std::vector<uint8_t> rfcomm_packet =
115 CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
116 std::vector<uint8_t> l2cap_packet =
117 CreateL2capDataPacket(0x005c, rfcomm_packet);
118 std::vector<uint8_t> acl_packet =
119 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
120 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihPnSetChannelTo3));
121 }
122
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihPnSetChannel3Packet)123 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihPnSetChannel3Packet) {
124 EXPECT_THAT(CreateQuickPnPacket(true, GetDlci(false, 3), true, 884, 0xF, 0, 1,
125 0x005c, 0x0008),
126 ElementsAreArray(kIncomingUihPnSetChannelTo3));
127 }
128
129 const uint8_t kIncomingSabmChannel3[] = {0x08, 0x20, 0x08, 0x00, 0x04, 0x00,
130 0x5c, 0x00, 0x1b, 0x3f, 0x01, 0xd3};
131
TEST(RfcommTestPacketGeneratorTest,TestGenerateSabmChannel3Packet)132 TEST(RfcommTestPacketGeneratorTest, TestGenerateSabmChannel3Packet) {
133 uint8_t dlci = GetDlci(false, 3);
134 EXPECT_EQ(dlci, 6);
135 uint8_t address_field = GetAddressField(true, true, dlci);
136 EXPECT_EQ(address_field, 0x1b);
137 uint8_t control_field = GetControlField(true, RFCOMM_SABME);
138 EXPECT_EQ(control_field, 0x3F);
139 std::vector<uint8_t> rfcomm_packet =
140 CreateRfcommPacket(address_field, control_field, -1, {});
141 std::vector<uint8_t> l2cap_packet =
142 CreateL2capDataPacket(0x005c, rfcomm_packet);
143 std::vector<uint8_t> acl_packet =
144 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
145 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingSabmChannel3));
146 }
147
TEST(RfcommTestPacketGeneratorTest,TestGenerateQuickSabmChannel3Packet)148 TEST(RfcommTestPacketGeneratorTest, TestGenerateQuickSabmChannel3Packet) {
149 EXPECT_THAT(CreateQuickSabmPacket(GetDlci(false, 3), 0x005c, 0x0008),
150 ElementsAreArray(kIncomingSabmChannel3));
151 }
152
153 const uint8_t kIncomingUihMscCmdFrame[] = {0x08, 0x20, 0x0c, 0x00, 0x08, 0x00,
154 0x5c, 0x00, 0x03, 0xef, 0x09, 0xe3,
155 0x05, 0x1b, 0x8d, 0x70};
156
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihMscCmdPacket)157 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihMscCmdPacket) {
158 uint8_t dlci = GetDlci(false, 0);
159 EXPECT_EQ(dlci, 0);
160 uint8_t address_field = GetAddressField(true, true, dlci);
161 EXPECT_EQ(address_field, 0x03);
162 uint8_t control_field = GetControlField(false, RFCOMM_UIH);
163 EXPECT_EQ(control_field, 0xEF);
164 uint8_t new_dlci = GetDlci(false, 3);
165 EXPECT_EQ(new_dlci, 6);
166 std::vector<uint8_t> mcc_msc_data =
167 CreateMccMscFrame(new_dlci, false, true, true, false, true);
168 std::vector<uint8_t> mcc_payload =
169 CreateMultiplexerControlFrame(0x38, true, mcc_msc_data);
170 std::vector<uint8_t> rfcomm_packet =
171 CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
172 std::vector<uint8_t> l2cap_packet =
173 CreateL2capDataPacket(0x005c, rfcomm_packet);
174 std::vector<uint8_t> acl_packet =
175 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
176 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihMscCmdFrame));
177 }
178
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihMscCmdPacket)179 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihMscCmdPacket) {
180 EXPECT_THAT(CreateQuickMscPacket(true, GetDlci(false, 3), 0x005c, 0x0008,
181 true, false, true, true, false, true),
182 ElementsAreArray(kIncomingUihMscCmdFrame));
183 }
184
185 const uint8_t kIncomingUihMscResponseFrame[] = {
186 0x08, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x5c, 0x00,
187 0x03, 0xef, 0x09, 0xe1, 0x05, 0x1b, 0x8d, 0x70};
188
TEST(RfcommTestPacketGeneratorTest,TestGenerateUihMscResponsePacket)189 TEST(RfcommTestPacketGeneratorTest, TestGenerateUihMscResponsePacket) {
190 uint8_t dlci = GetDlci(false, 0);
191 EXPECT_EQ(dlci, 0);
192 uint8_t address_field = GetAddressField(true, true, dlci);
193 EXPECT_EQ(address_field, 0x03);
194 uint8_t control_field = GetControlField(false, RFCOMM_UIH);
195 EXPECT_EQ(control_field, 0xEF);
196 uint8_t new_dlci = GetDlci(false, 3);
197 EXPECT_EQ(new_dlci, 6);
198 std::vector<uint8_t> mcc_msc_data =
199 CreateMccMscFrame(new_dlci, false, true, true, false, true);
200 std::vector<uint8_t> mcc_payload =
201 CreateMultiplexerControlFrame(0x38, false, mcc_msc_data);
202 std::vector<uint8_t> rfcomm_packet =
203 CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
204 std::vector<uint8_t> l2cap_packet =
205 CreateL2capDataPacket(0x005c, rfcomm_packet);
206 std::vector<uint8_t> acl_packet =
207 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
208 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingUihMscResponseFrame));
209 }
210
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateUihMscResponsePacket)211 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateUihMscResponsePacket) {
212 EXPECT_THAT(CreateQuickMscPacket(true, GetDlci(false, 3), 0x005c, 0x0008,
213 false, false, true, true, false, true),
214 ElementsAreArray(kIncomingUihMscResponseFrame));
215 }
216
217 const uint8_t kIncomingBrsfFrame[] = {0x08, 0x20, 0x15, 0x00, 0x11, 0x00, 0x5c,
218 0x00, 0x1b, 0xff, 0x19, 0x06, 0x41, 0x54,
219 0x2b, 0x42, 0x52, 0x53, 0x46, 0x3d, 0x39,
220 0x35, 0x39, 0x0d, 0x93};
221
TEST(RfcommTestPacketGeneratorTest,TestGenerateDataPacket)222 TEST(RfcommTestPacketGeneratorTest, TestGenerateDataPacket) {
223 uint8_t dlci = GetDlci(false, 3);
224 EXPECT_EQ(dlci, 6);
225 uint8_t address_field = GetAddressField(true, true, dlci);
226 EXPECT_EQ(address_field, 0x1B);
227 uint8_t control_field = GetControlField(true, RFCOMM_UIH);
228 EXPECT_EQ(control_field, 0xFF);
229 const std::string data_str = "AT+BRSF=959\r";
230 const std::vector<uint8_t> data(data_str.begin(), data_str.end());
231 std::vector<uint8_t> rfcomm_packet =
232 CreateRfcommPacket(address_field, control_field, 6, data);
233 std::vector<uint8_t> l2cap_packet =
234 CreateL2capDataPacket(0x005c, rfcomm_packet);
235 std::vector<uint8_t> acl_packet =
236 CreateAclPacket(0x0008, 0b10, 0b00, l2cap_packet);
237 EXPECT_THAT(acl_packet, ElementsAreArray(kIncomingBrsfFrame));
238 }
239
TEST(RfcommTestPacketGeneratorTest,TestQuickGenerateDataPacket)240 TEST(RfcommTestPacketGeneratorTest, TestQuickGenerateDataPacket) {
241 EXPECT_THAT(CreateQuickDataPacket(GetDlci(false, 3), true, 0x005c, 0x0008, 6,
242 "AT+BRSF=959\r"),
243 ElementsAreArray(kIncomingBrsfFrame));
244 }
245
246 } // namespace