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 <base/logging.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "bt_types.h"
24 #include "btm_api.h"
25 #include "l2c_api.h"
26 #include "osi/include/osi.h"
27 #include "port_api.h"
28 
29 #include "rfc_int.h"
30 
31 #include "mock_btm_layer.h"
32 #include "mock_l2cap_layer.h"
33 #include "stack_rfcomm_test_utils.h"
34 #include "stack_test_packet_utils.h"
35 
DumpByteBufferToString(uint8_t * p_data,size_t len)36 std::string DumpByteBufferToString(uint8_t* p_data, size_t len) {
37   std::stringstream str;
38   str.setf(std::ios_base::hex, std::ios::basefield);
39   str.setf(std::ios_base::uppercase);
40   str.fill('0');
41   for (size_t i = 0; i < len; ++i) {
42     str << std::setw(2) << static_cast<uint16_t>(p_data[i]);
43     str << " ";
44   }
45   return str.str();
46 }
47 
DumpBtHdrToString(BT_HDR * p_hdr)48 std::string DumpBtHdrToString(BT_HDR* p_hdr) {
49   uint8_t* p_hdr_data = p_hdr->data + p_hdr->offset;
50   return DumpByteBufferToString(p_hdr_data, p_hdr->len);
51 }
52 
PrintTo(BT_HDR * value,::std::ostream * os)53 void PrintTo(BT_HDR* value, ::std::ostream* os) {
54   *os << DumpBtHdrToString(value);
55 }
56 
PrintTo(tL2CAP_CFG_INFO * value,::std::ostream * os)57 void PrintTo(tL2CAP_CFG_INFO* value, ::std::ostream* os) {
58   *os << DumpByteBufferToString((uint8_t*)value, sizeof(tL2CAP_CFG_INFO));
59 }
60 
61 namespace {
62 
63 using testing::_;
64 using testing::DoAll;
65 using testing::Return;
66 using testing::Test;
67 using testing::StrictMock;
68 using testing::SaveArg;
69 using testing::SaveArgPointee;
70 using testing::Pointee;
71 using testing::StrEq;
72 using testing::NotNull;
73 
74 using bluetooth::CreateL2capDataPacket;
75 using bluetooth::CreateAclPacket;
76 using bluetooth::AllocateWrappedIncomingL2capAclPacket;
77 using bluetooth::AllocateWrappedOutgoingL2capAclPacket;
78 
79 using bluetooth::rfcomm::GetDlci;
80 using bluetooth::rfcomm::GetAddressField;
81 using bluetooth::rfcomm::GetControlField;
82 using bluetooth::rfcomm::CreateMccPnFrame;
83 using bluetooth::rfcomm::CreateMccMscFrame;
84 using bluetooth::rfcomm::CreateMultiplexerControlFrame;
85 using bluetooth::rfcomm::CreateRfcommPacket;
86 using bluetooth::rfcomm::CreateQuickDataPacket;
87 using bluetooth::rfcomm::CreateQuickPnPacket;
88 using bluetooth::rfcomm::CreateQuickSabmPacket;
89 using bluetooth::rfcomm::CreateQuickUaPacket;
90 using bluetooth::rfcomm::CreateQuickMscPacket;
91 
92 MATCHER_P(PointerMemoryEqual, ptr,
93           DumpByteBufferToString((uint8_t*)ptr, sizeof(*ptr))) {
94   return memcmp(arg, ptr, sizeof(*ptr)) == 0;
95 }
96 
MATCHER_P(BtHdrEqual,expected,DumpBtHdrToString (expected))97 MATCHER_P(BtHdrEqual, expected, DumpBtHdrToString(expected)) {
98   auto arg_hdr = static_cast<BT_HDR*>(arg);
99   uint8_t* arg_data = arg_hdr->data + arg_hdr->offset;
100   auto expected_hdr = static_cast<BT_HDR*>(expected);
101   uint8_t* expected_data = expected_hdr->data + expected_hdr->offset;
102   return memcmp(arg_data, expected_data, expected_hdr->len) == 0;
103 }
104 
105 bluetooth::rfcomm::MockRfcommCallback* rfcomm_callback = nullptr;
106 
port_mgmt_cback_0(uint32_t code,uint16_t port_handle)107 void port_mgmt_cback_0(uint32_t code, uint16_t port_handle) {
108   rfcomm_callback->PortManagementCallback(code, port_handle, 0);
109 }
110 
port_mgmt_cback_1(uint32_t code,uint16_t port_handle)111 void port_mgmt_cback_1(uint32_t code, uint16_t port_handle) {
112   rfcomm_callback->PortManagementCallback(code, port_handle, 1);
113 }
114 
port_event_cback_0(uint32_t code,uint16_t port_handle)115 void port_event_cback_0(uint32_t code, uint16_t port_handle) {
116   rfcomm_callback->PortEventCallback(code, port_handle, 0);
117 }
118 
port_event_cback_1(uint32_t code,uint16_t port_handle)119 void port_event_cback_1(uint32_t code, uint16_t port_handle) {
120   rfcomm_callback->PortEventCallback(code, port_handle, 1);
121 }
122 
GetTestAddress(int index)123 RawAddress GetTestAddress(int index) {
124   CHECK_LT(index, UINT8_MAX);
125   RawAddress result = {
126       {0xAA, 0x00, 0x11, 0x22, 0x33, static_cast<uint8_t>(index)}};
127   return result;
128 }
129 
130 class StackRfcommTest : public Test {
131  public:
StartServerPort(uint16_t uuid,uint8_t scn,uint16_t mtu,tPORT_CALLBACK * management_callback,tPORT_CALLBACK * event_callback,uint16_t * server_handle)132   void StartServerPort(uint16_t uuid, uint8_t scn, uint16_t mtu,
133                        tPORT_CALLBACK* management_callback,
134                        tPORT_CALLBACK* event_callback,
135                        uint16_t* server_handle) {
136     VLOG(1) << "Step 1";
137     ASSERT_EQ(RFCOMM_CreateConnection(uuid, scn, true, mtu, RawAddress::kAny,
138                                       server_handle, management_callback),
139               PORT_SUCCESS);
140     ASSERT_EQ(PORT_SetEventMask(*server_handle, PORT_EV_RXCHAR), PORT_SUCCESS);
141     ASSERT_EQ(PORT_SetEventCallback(*server_handle, event_callback),
142               PORT_SUCCESS);
143   }
144 
ConnectServerL2cap(const RawAddress & peer_addr,uint16_t acl_handle,uint16_t lcid)145   void ConnectServerL2cap(const RawAddress& peer_addr, uint16_t acl_handle,
146                           uint16_t lcid) {
147     VLOG(1) << "Step 1";
148     // Remote device connect to this channel, we shall accept
149     static const uint8_t cmd_id = 0x07;
150     EXPECT_CALL(l2cap_interface_,
151                 ConnectResponse(peer_addr, cmd_id, lcid, L2CAP_CONN_OK, 0));
152     tL2CAP_CFG_INFO cfg_req = {.mtu_present = true, .mtu = L2CAP_MTU_SIZE};
153     EXPECT_CALL(l2cap_interface_,
154                 ConfigRequest(lcid, PointerMemoryEqual(&cfg_req)))
155         .WillOnce(Return(true));
156     l2cap_appl_info_.pL2CA_ConnectInd_Cb(peer_addr, lcid, BT_PSM_RFCOMM,
157                                          cmd_id);
158 
159     VLOG(1) << "Step 2";
160     // MTU configuration is done
161     cfg_req.mtu_present = false;
162     l2cap_appl_info_.pL2CA_ConfigCfm_Cb(lcid, L2CAP_CFG_OK, {});
163 
164     VLOG(1) << "Step 3";
165     // Remote device also ask to configure MTU size
166     EXPECT_CALL(l2cap_interface_,
167                 ConfigResponse(lcid, PointerMemoryEqual(&cfg_req)))
168         .WillOnce(Return(true));
169     l2cap_appl_info_.pL2CA_ConfigInd_Cb(lcid, &cfg_req);
170 
171     VLOG(1) << "Step 4";
172     // Remote device connect to server channel 0
173     BT_HDR* sabm_channel_0 = AllocateWrappedIncomingL2capAclPacket(
174         CreateQuickSabmPacket(RFCOMM_MX_DLCI, lcid, acl_handle));
175     BT_HDR* ua_channel_0 = AllocateWrappedOutgoingL2capAclPacket(
176         CreateQuickUaPacket(RFCOMM_MX_DLCI, lcid, acl_handle));
177     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(ua_channel_0)))
178         .WillOnce(Return(L2CAP_DW_SUCCESS));
179     // Packet should be freed by RFCOMM
180     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, sabm_channel_0);
181     osi_free(ua_channel_0);
182   }
183 
ConnectServerPort(const RawAddress & peer_addr,uint16_t port_handle,uint8_t scn,uint16_t mtu,uint16_t acl_handle,uint16_t lcid,int port_callback_index)184   void ConnectServerPort(const RawAddress& peer_addr, uint16_t port_handle,
185                          uint8_t scn, uint16_t mtu, uint16_t acl_handle,
186                          uint16_t lcid, int port_callback_index) {
187     VLOG(1) << "Step 1";
188     // Negotiate parameters on scn
189     BT_HDR* uih_pn_cmd_from_peer = AllocateWrappedIncomingL2capAclPacket(
190         CreateQuickPnPacket(true, GetDlci(false, scn), true, mtu,
191                             RFCOMM_PN_CONV_LAYER_CBFC_I >> 4, 0, RFCOMM_K_MAX,
192                             lcid, acl_handle));
193     BT_HDR* uih_pn_rsp_to_peer = AllocateWrappedOutgoingL2capAclPacket(
194         CreateQuickPnPacket(false, GetDlci(false, scn), false, mtu,
195                             RFCOMM_PN_CONV_LAYER_CBFC_R >> 4, 0, RFCOMM_K_MAX,
196                             lcid, acl_handle));
197     EXPECT_CALL(l2cap_interface_,
198                 DataWrite(lcid, BtHdrEqual(uih_pn_rsp_to_peer)))
199         .WillOnce(Return(L2CAP_DW_SUCCESS));
200     // uih_pn_cmd_from_peer should be freed by this method
201     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_pn_cmd_from_peer);
202     osi_free(uih_pn_rsp_to_peer);
203 
204     VLOG(1) << "Step 2";
205     // Remote device connect to scn
206     tBTM_SEC_CALLBACK* security_callback = nullptr;
207     void* p_port = nullptr;
208     BT_HDR* sabm_channel_dlci = AllocateWrappedIncomingL2capAclPacket(
209         CreateQuickSabmPacket(GetDlci(false, scn), lcid, acl_handle));
210     EXPECT_CALL(btm_security_internal_interface_,
211                 MultiplexingProtocolAccessRequest(peer_addr, BT_PSM_RFCOMM,
212                                                   false, BTM_SEC_PROTO_RFCOMM,
213                                                   scn, NotNull(), NotNull()))
214         .WillOnce(DoAll(SaveArg<5>(&security_callback), SaveArg<6>(&p_port),
215                         Return(BTM_SUCCESS)));
216     // sabm_channel_dlci should be freed by this method
217     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, sabm_channel_dlci);
218 
219     VLOG(1) << "Step 3";
220     // Confirm security check should trigger port as connected
221     EXPECT_CALL(
222         rfcomm_callback_,
223         PortManagementCallback(PORT_SUCCESS, port_handle, port_callback_index));
224     BT_HDR* ua_channel_dlci = AllocateWrappedOutgoingL2capAclPacket(
225         CreateQuickUaPacket(GetDlci(false, scn), lcid, acl_handle));
226     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(ua_channel_dlci)))
227         .WillOnce(Return(L2CAP_DW_SUCCESS));
228     ASSERT_TRUE(security_callback);
229     security_callback(&peer_addr, BT_TRANSPORT_BR_EDR, p_port, BTM_SUCCESS);
230     osi_free(ua_channel_dlci);
231 
232     VLOG(1) << "Step 4";
233     // Remote also need to configure its modem signal before we can send data
234     BT_HDR* uih_msc_cmd_from_peer = AllocateWrappedIncomingL2capAclPacket(
235         CreateQuickMscPacket(true, GetDlci(false, scn), lcid, acl_handle, true,
236                              false, true, true, false, true));
237     BT_HDR* uih_msc_response_to_peer = AllocateWrappedOutgoingL2capAclPacket(
238         CreateQuickMscPacket(false, GetDlci(false, scn), lcid, acl_handle,
239                              false, false, true, true, false, true));
240     // We also have to do modem configuration ourself
241     EXPECT_CALL(l2cap_interface_,
242                 DataWrite(lcid, BtHdrEqual(uih_msc_response_to_peer)))
243         .WillOnce(Return(L2CAP_DW_SUCCESS));
244     BT_HDR* uih_msc_cmd_to_peer = AllocateWrappedOutgoingL2capAclPacket(
245         CreateQuickMscPacket(false, GetDlci(false, scn), lcid, acl_handle, true,
246                              false, true, true, false, true));
247     EXPECT_CALL(l2cap_interface_,
248                 DataWrite(lcid, BtHdrEqual(uih_msc_cmd_to_peer)))
249         .WillOnce(Return(L2CAP_DW_SUCCESS));
250     // uih_msc_cmd_from_peer should be freed by this method
251     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_msc_cmd_from_peer);
252     osi_free(uih_msc_response_to_peer);
253 
254     VLOG(1) << "Step 5";
255     // modem configuration is done
256     BT_HDR* uih_msc_response_from_peer = AllocateWrappedIncomingL2capAclPacket(
257         CreateQuickMscPacket(true, GetDlci(false, scn), lcid, acl_handle, false,
258                              false, true, true, false, true));
259     // uih_msc_response_from_peer should be freed by this method
260     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_msc_response_from_peer);
261   }
262 
StartClientPort(const RawAddress & peer_bd_addr,uint16_t uuid,uint8_t scn,uint16_t mtu,tPORT_CALLBACK * management_callback,tPORT_CALLBACK * event_callback,uint16_t lcid,uint16_t acl_handle,uint16_t * client_handle,bool is_first_connection)263   void StartClientPort(const RawAddress& peer_bd_addr, uint16_t uuid,
264                        uint8_t scn, uint16_t mtu,
265                        tPORT_CALLBACK* management_callback,
266                        tPORT_CALLBACK* event_callback, uint16_t lcid,
267                        uint16_t acl_handle, uint16_t* client_handle,
268                        bool is_first_connection) {
269     VLOG(1) << "Step 1";
270     BT_HDR* uih_pn_channel_3 =
271         AllocateWrappedOutgoingL2capAclPacket(CreateQuickPnPacket(
272             true, GetDlci(false, scn), true, mtu, RFCOMM_PN_CONV_LAYER_TYPE_1,
273             RFCOMM_PN_PRIORITY_0, RFCOMM_K, lcid, acl_handle));
274     if (is_first_connection) {
275       EXPECT_CALL(l2cap_interface_, ConnectRequest(BT_PSM_RFCOMM, peer_bd_addr))
276           .WillOnce(Return(lcid));
277     } else {
278       EXPECT_CALL(l2cap_interface_,
279                   DataWrite(lcid, BtHdrEqual(uih_pn_channel_3)))
280           .WillOnce(Return(L2CAP_DW_SUCCESS));
281     }
282     ASSERT_EQ(RFCOMM_CreateConnection(uuid, scn, false, mtu, peer_bd_addr,
283                                       client_handle, management_callback),
284               PORT_SUCCESS);
285     ASSERT_EQ(PORT_SetEventMask(*client_handle, PORT_EV_RXCHAR), PORT_SUCCESS);
286     ASSERT_EQ(PORT_SetEventCallback(*client_handle, event_callback),
287               PORT_SUCCESS);
288     osi_free(uih_pn_channel_3);
289   }
290 
TestConnectClientPortL2cap(uint16_t acl_handle,uint16_t lcid)291   void TestConnectClientPortL2cap(uint16_t acl_handle, uint16_t lcid) {
292     VLOG(1) << "Step 1";
293     // Send configuration request when L2CAP connect is succsseful
294     tL2CAP_CFG_INFO cfg_req = {.mtu_present = true, .mtu = L2CAP_MTU_SIZE};
295     EXPECT_CALL(l2cap_interface_,
296                 ConfigRequest(lcid, PointerMemoryEqual(&cfg_req)))
297         .WillOnce(Return(true));
298     l2cap_appl_info_.pL2CA_ConnectCfm_Cb(lcid, L2CAP_CONN_OK);
299 
300     VLOG(1) << "Step 2";
301     // Remote device confirms our configuration request
302     cfg_req.mtu_present = false;
303     l2cap_appl_info_.pL2CA_ConfigCfm_Cb(lcid, L2CAP_CFG_OK, {});
304 
305     VLOG(1) << "Step 3";
306     // Remote device also asks to configure MTU
307     // Once configuration is done, we connect to multiplexer control channel 0
308     EXPECT_CALL(l2cap_interface_,
309                 ConfigResponse(lcid, PointerMemoryEqual(&cfg_req)))
310         .WillOnce(Return(true));
311     // multiplexer control channel's DLCI is always 0
312     BT_HDR* sabm_channel_0 = AllocateWrappedOutgoingL2capAclPacket(
313         CreateQuickSabmPacket(RFCOMM_MX_DLCI, lcid, acl_handle));
314     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(sabm_channel_0)))
315         .WillOnce(Return(L2CAP_DW_SUCCESS));
316     l2cap_appl_info_.pL2CA_ConfigInd_Cb(lcid, &cfg_req);
317     osi_free(sabm_channel_0);
318   }
319 
ConnectClientPort(const RawAddress & peer_addr,uint16_t port_handle,uint8_t scn,uint16_t mtu,uint16_t acl_handle,uint16_t lcid,int port_callback_index,bool is_first_connection)320   void ConnectClientPort(const RawAddress& peer_addr, uint16_t port_handle,
321                          uint8_t scn, uint16_t mtu, uint16_t acl_handle,
322                          uint16_t lcid, int port_callback_index,
323                          bool is_first_connection) {
324     VLOG(1) << "Step 1";
325     if (is_first_connection) {
326       VLOG(1) << "Step 1.5";
327       // Once remote accept multiplexer control channel 0
328       // We change to desired channel on non-initiating device (remote device)
329       BT_HDR* ua_channel_0 = AllocateWrappedIncomingL2capAclPacket(
330           CreateQuickUaPacket(RFCOMM_MX_DLCI, lcid, acl_handle));
331       BT_HDR* uih_pn_channel_3 =
332           AllocateWrappedOutgoingL2capAclPacket(CreateQuickPnPacket(
333               true, GetDlci(false, scn), true, mtu,
334               RFCOMM_PN_CONV_LAYER_CBFC_I >> 4, RFCOMM_PN_PRIORITY_0,
335               RFCOMM_K_MAX, lcid, acl_handle));
336       EXPECT_CALL(l2cap_interface_,
337                   DataWrite(lcid, BtHdrEqual(uih_pn_channel_3)))
338           .WillOnce(Return(L2CAP_DW_SUCCESS));
339       l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, ua_channel_0);
340       osi_free(uih_pn_channel_3);
341     }
342 
343     VLOG(1) << "Step 2";
344     // Once remote accept service channel change, we start security procedure
345     BT_HDR* uih_pn_channel_3_accept =
346         AllocateWrappedIncomingL2capAclPacket(CreateQuickPnPacket(
347             false, GetDlci(false, scn), false, mtu,
348             RFCOMM_PN_CONV_LAYER_CBFC_I >> 4, RFCOMM_PN_PRIORITY_0,
349             RFCOMM_K_MAX, lcid, acl_handle));
350     tBTM_SEC_CALLBACK* security_callback = nullptr;
351     void* p_port = nullptr;
352     EXPECT_CALL(btm_security_internal_interface_,
353                 MultiplexingProtocolAccessRequest(peer_addr, BT_PSM_RFCOMM,
354                                                   true, BTM_SEC_PROTO_RFCOMM,
355                                                   scn, NotNull(), NotNull()))
356         .WillOnce(DoAll(SaveArg<5>(&security_callback), SaveArg<6>(&p_port),
357                         Return(BTM_SUCCESS)));
358     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_pn_channel_3_accept);
359 
360     VLOG(1) << "Step 3";
361     // Once security procedure is done, we officially connect to target scn
362     BT_HDR* sabm_channel_3 = AllocateWrappedOutgoingL2capAclPacket(
363         CreateQuickSabmPacket(GetDlci(false, scn), lcid, acl_handle));
364     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(sabm_channel_3)))
365         .WillOnce(Return(L2CAP_DW_SUCCESS));
366     ASSERT_TRUE(security_callback);
367     security_callback(&peer_addr, BT_TRANSPORT_BR_EDR, p_port, BTM_SUCCESS);
368     osi_free(sabm_channel_3);
369 
370     VLOG(1) << "Step 4";
371     // When target scn is accepted by remote, we need to configure modem signal
372     // state beofre using the port
373     EXPECT_CALL(
374         rfcomm_callback_,
375         PortManagementCallback(PORT_SUCCESS, port_handle, port_callback_index));
376     BT_HDR* uih_msc_cmd = AllocateWrappedOutgoingL2capAclPacket(
377         CreateQuickMscPacket(true, GetDlci(false, scn), lcid, acl_handle, true,
378                              false, true, true, false, true));
379     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(uih_msc_cmd)))
380         .WillOnce(Return(L2CAP_DW_SUCCESS));
381     BT_HDR* ua_channel_3 = AllocateWrappedIncomingL2capAclPacket(
382         CreateQuickUaPacket(GetDlci(false, scn), lcid, acl_handle));
383     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, ua_channel_3);
384     osi_free(uih_msc_cmd);
385 
386     VLOG(1) << "Step 5";
387     // modem configuration is done
388     BT_HDR* uih_msc_response = AllocateWrappedIncomingL2capAclPacket(
389         CreateQuickMscPacket(false, GetDlci(false, scn), lcid, acl_handle,
390                              false, false, true, true, false, true));
391     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_msc_response);
392 
393     VLOG(1) << "Step 6";
394     // Remote also need to configure its modem signal before we can send data
395     BT_HDR* uih_msc_cmd_from_peer = AllocateWrappedIncomingL2capAclPacket(
396         CreateQuickMscPacket(false, GetDlci(false, scn), lcid, acl_handle, true,
397                              false, true, true, false, true));
398     BT_HDR* uih_msc_response_to_peer = AllocateWrappedOutgoingL2capAclPacket(
399         CreateQuickMscPacket(true, GetDlci(false, scn), lcid, acl_handle, false,
400                              false, true, true, false, true));
401     EXPECT_CALL(l2cap_interface_,
402                 DataWrite(lcid, BtHdrEqual(uih_msc_response_to_peer)))
403         .WillOnce(Return(L2CAP_DW_SUCCESS));
404     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, uih_msc_cmd_from_peer);
405     osi_free(uih_msc_response_to_peer);
406   }
407 
SendAndVerifyOutgoingTransmission(uint16_t port_handle,bool is_initiator,uint8_t scn,bool cr,const std::string & message,int credits,uint16_t acl_handle,uint16_t lcid)408   void SendAndVerifyOutgoingTransmission(uint16_t port_handle,
409                                          bool is_initiator, uint8_t scn,
410                                          bool cr, const std::string& message,
411                                          int credits, uint16_t acl_handle,
412                                          uint16_t lcid) {
413     VLOG(1) << "Step 1";
414     BT_HDR* data_packet = AllocateWrappedOutgoingL2capAclPacket(
415         CreateQuickDataPacket(GetDlci(is_initiator, scn), cr, lcid, acl_handle,
416                               credits, message));
417     uint16_t transmitted_length = 0;
418     EXPECT_CALL(l2cap_interface_, DataWrite(lcid, BtHdrEqual(data_packet)))
419         .WillOnce(Return(L2CAP_DW_SUCCESS));
420     ASSERT_EQ(PORT_WriteData(port_handle, message.data(), message.size(),
421                              &transmitted_length),
422               PORT_SUCCESS);
423     ASSERT_EQ(transmitted_length, message.size());
424   }
425 
ReceiveAndVerifyIncomingTransmission(uint16_t port_handle,bool is_initiator,uint8_t scn,bool cr,const std::string & message,int credits,uint16_t acl_handle,uint16_t lcid,int port_callback_index)426   void ReceiveAndVerifyIncomingTransmission(uint16_t port_handle,
427                                             bool is_initiator, uint8_t scn,
428                                             bool cr, const std::string& message,
429                                             int credits, uint16_t acl_handle,
430                                             uint16_t lcid,
431                                             int port_callback_index) {
432     VLOG(1) << "Step 1";
433     BT_HDR* data_packet = AllocateWrappedIncomingL2capAclPacket(
434         CreateQuickDataPacket(GetDlci(is_initiator, scn), cr, lcid, acl_handle,
435                               credits, message));
436     EXPECT_CALL(rfcomm_callback_,
437                 PortEventCallback(_, port_handle, port_callback_index));
438     l2cap_appl_info_.pL2CA_DataInd_Cb(lcid, data_packet);
439 
440     VLOG(1) << "Step 2";
441     char buffer[L2CAP_MTU_SIZE] = {};
442     uint16_t length = 0;
443     int status = PORT_ReadData(port_handle, buffer, L2CAP_MTU_SIZE, &length);
444     ASSERT_EQ(status, PORT_SUCCESS);
445     ASSERT_THAT(buffer, StrEq(message));
446   }
447 
448  protected:
SetUp()449   void SetUp() override {
450     Test::SetUp();
451     bluetooth::manager::SetMockSecurityInternalInterface(
452         &btm_security_internal_interface_);
453     bluetooth::l2cap::SetMockInterface(&l2cap_interface_);
454     rfcomm_callback = &rfcomm_callback_;
455     EXPECT_CALL(l2cap_interface_, Register(BT_PSM_RFCOMM, _, _, _))
456         .WillOnce(Return(BT_PSM_RFCOMM));
457     RFCOMM_Init();
458     rfc_cb.trace_level = BT_TRACE_LEVEL_DEBUG;
459   }
460 
TearDown()461   void TearDown() override {
462     rfcomm_callback = nullptr;
463     bluetooth::l2cap::SetMockInterface(nullptr);
464     bluetooth::manager::SetMockSecurityInternalInterface(nullptr);
465     testing::Test::TearDown();
466   }
467   StrictMock<bluetooth::manager::MockBtmSecurityInternalInterface>
468       btm_security_internal_interface_;
469   StrictMock<bluetooth::l2cap::MockL2capInterface> l2cap_interface_;
470   StrictMock<bluetooth::rfcomm::MockRfcommCallback> rfcomm_callback_;
471   tL2CAP_APPL_INFO l2cap_appl_info_;
472 };
473 
TEST_F(StackRfcommTest,SingleServerConnectionHelloWorld)474 TEST_F(StackRfcommTest, SingleServerConnectionHelloWorld) {
475   // Prepare a server channel at kTestChannelNumber0
476   static const uint16_t acl_handle = 0x0009;
477   static const uint16_t lcid = 0x0054;
478   static const uint16_t test_uuid = 0x1112;
479   static const uint8_t test_scn = 8;
480   static const uint16_t test_mtu = 1600;
481   static const RawAddress test_address = GetTestAddress(0);
482   uint16_t server_handle = 0;
483   ASSERT_NO_FATAL_FAILURE(StartServerPort(test_uuid, test_scn, test_mtu,
484                                           port_mgmt_cback_0, port_event_cback_0,
485                                           &server_handle));
486   ASSERT_NO_FATAL_FAILURE(ConnectServerL2cap(test_address, acl_handle, lcid));
487   ASSERT_NO_FATAL_FAILURE(ConnectServerPort(
488       test_address, server_handle, test_scn, test_mtu, acl_handle, lcid, 0));
489   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
490       server_handle, false, test_scn, true, "Hello World!\r", 50, acl_handle,
491       lcid, 0));
492   ASSERT_NO_FATAL_FAILURE(
493       SendAndVerifyOutgoingTransmission(server_handle, false, test_scn, false,
494                                         "\r!dlroW olleH", 4, acl_handle, lcid));
495 }
496 
TEST_F(StackRfcommTest,MultiServerPortSameDeviceHelloWorld)497 TEST_F(StackRfcommTest, MultiServerPortSameDeviceHelloWorld) {
498   // Prepare a server channel at kTestChannelNumber0
499   static const uint16_t acl_handle = 0x0009;
500   static const uint16_t lcid = 0x0054;
501   static const uint16_t test_mtu = 1600;
502   static const RawAddress test_address = GetTestAddress(0);
503 
504   // Service 0
505   uint16_t server_handle_0 = 0;
506   static const uint8_t test_scn_0 = 8;
507   static const uint16_t test_uuid_0 = 0x1112;
508   ASSERT_NO_FATAL_FAILURE(StartServerPort(test_uuid_0, test_scn_0, test_mtu,
509                                           port_mgmt_cback_0, port_event_cback_0,
510                                           &server_handle_0));
511   ASSERT_NO_FATAL_FAILURE(ConnectServerL2cap(test_address, acl_handle, lcid));
512   ASSERT_NO_FATAL_FAILURE(ConnectServerPort(test_address, server_handle_0,
513                                             test_scn_0, test_mtu, acl_handle,
514                                             lcid, 0));
515 
516   // Service 1
517   uint16_t server_handle_1 = 0;
518   static const uint8_t test_scn_1 = 10;
519   static const uint16_t test_uuid_1 = 0x111F;
520   ASSERT_NE(test_scn_1, test_scn_0);
521   ASSERT_NE(test_uuid_1, test_uuid_0);
522   ASSERT_NO_FATAL_FAILURE(StartServerPort(test_uuid_1, test_scn_1, test_mtu,
523                                           port_mgmt_cback_1, port_event_cback_1,
524                                           &server_handle_1));
525   // No L2CAP setup for 2nd device
526   ASSERT_NO_FATAL_FAILURE(ConnectServerPort(test_address, server_handle_1,
527                                             test_scn_1, test_mtu, acl_handle,
528                                             lcid, 1));
529 
530   // Use service 0
531   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
532       server_handle_0, false, test_scn_0, true, "Hello World0!\r", 50,
533       acl_handle, lcid, 0));
534   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
535       server_handle_0, false, test_scn_0, false, "\r!0dlroW olleH", 4,
536       acl_handle, lcid));
537   // Use service 1
538   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
539       server_handle_1, false, test_scn_1, true, "Hello World1!\r", 50,
540       acl_handle, lcid, 1));
541   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
542       server_handle_1, false, test_scn_1, false, "\r!1dlroW olleH", 4,
543       acl_handle, lcid));
544 }
545 
TEST_F(StackRfcommTest,SameServerPortMultiDeviceHelloWorld)546 TEST_F(StackRfcommTest, SameServerPortMultiDeviceHelloWorld) {
547   // Prepare a server channel at kTestChannelNumber0
548   static const uint16_t test_mtu = 1600;
549   static const uint8_t test_scn = 3;
550   static const uint16_t test_uuid = 0x1112;
551 
552   // Service 0
553   static const RawAddress test_address_0 = GetTestAddress(0);
554   static const uint16_t acl_handle_0 = 0x0009;
555   static const uint16_t lcid_0 = 0x0054;
556   uint16_t server_handle_0 = 0;
557   ASSERT_NO_FATAL_FAILURE(StartServerPort(test_uuid, test_scn, test_mtu,
558                                           port_mgmt_cback_0, port_event_cback_0,
559                                           &server_handle_0));
560   ASSERT_NO_FATAL_FAILURE(
561       ConnectServerL2cap(test_address_0, acl_handle_0, lcid_0));
562   ASSERT_NO_FATAL_FAILURE(ConnectServerPort(test_address_0, server_handle_0,
563                                             test_scn, test_mtu, acl_handle_0,
564                                             lcid_0, 0));
565 
566   // Service 1
567   static const RawAddress test_address_1 = GetTestAddress(1);
568   static const uint16_t acl_handle_1 = 0x0012;
569   static const uint16_t lcid_1 = 0x0045;
570   uint16_t server_handle_1 = 0;
571   ASSERT_NO_FATAL_FAILURE(StartServerPort(test_uuid, test_scn, test_mtu,
572                                           port_mgmt_cback_1, port_event_cback_1,
573                                           &server_handle_1));
574   ASSERT_NO_FATAL_FAILURE(
575       ConnectServerL2cap(test_address_1, acl_handle_1, lcid_1));
576   ASSERT_NO_FATAL_FAILURE(ConnectServerPort(test_address_1, server_handle_1,
577                                             test_scn, test_mtu, acl_handle_1,
578                                             lcid_1, 1));
579 
580   // Use service 0
581   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
582       server_handle_0, false, test_scn, true, "Hello World0!\r", 50,
583       acl_handle_0, lcid_0, 0));
584   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
585       server_handle_0, false, test_scn, false, "\r!0dlroW olleH", 4,
586       acl_handle_0, lcid_0));
587   // Use service 1
588   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
589       server_handle_1, false, test_scn, true, "Hello World1!\r", 50,
590       acl_handle_1, lcid_1, 1));
591   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
592       server_handle_1, false, test_scn, false, "\r!1dlroW olleH", 4,
593       acl_handle_1, lcid_1));
594 }
595 
TEST_F(StackRfcommTest,SingleClientConnectionHelloWorld)596 TEST_F(StackRfcommTest, SingleClientConnectionHelloWorld) {
597   static const uint16_t acl_handle = 0x0009;
598   static const uint16_t lcid = 0x0054;
599   static const uint16_t test_uuid = 0x1112;
600   static const uint8_t test_scn = 8;
601   static const uint16_t test_mtu = 1600;
602   static const RawAddress test_address = GetTestAddress(0);
603   uint16_t client_handle = 0;
604   ASSERT_NO_FATAL_FAILURE(StartClientPort(
605       test_address, test_uuid, test_scn, test_mtu, port_mgmt_cback_0,
606       port_event_cback_0, lcid, acl_handle, &client_handle, true));
607   ASSERT_NO_FATAL_FAILURE(TestConnectClientPortL2cap(acl_handle, lcid));
608   ASSERT_NO_FATAL_FAILURE(ConnectClientPort(test_address, client_handle,
609                                             test_scn, test_mtu, acl_handle,
610                                             lcid, 0, true));
611   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
612       client_handle, false, test_scn, true, "\r!dlroW olleH", -1, acl_handle,
613       lcid));
614   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
615       client_handle, false, test_scn, false, "Hello World!\r", -1, acl_handle,
616       lcid, 0));
617 }
618 
TEST_F(StackRfcommTest,MultiClientPortSameDeviceHelloWorld)619 TEST_F(StackRfcommTest, MultiClientPortSameDeviceHelloWorld) {
620   static const uint16_t acl_handle = 0x0009;
621   static const uint16_t lcid = 0x0054;
622   static const uint16_t test_mtu = 1600;
623   static const RawAddress test_address = GetTestAddress(0);
624 
625   // Connection 0
626   static const uint16_t test_uuid_0 = 0x1112;
627   static const uint8_t test_scn_0 = 8;
628   uint16_t client_handle_0 = 0;
629   ASSERT_NO_FATAL_FAILURE(StartClientPort(
630       test_address, test_uuid_0, test_scn_0, test_mtu, port_mgmt_cback_0,
631       port_event_cback_0, lcid, acl_handle, &client_handle_0, true));
632   ASSERT_NO_FATAL_FAILURE(TestConnectClientPortL2cap(acl_handle, lcid));
633   ASSERT_NO_FATAL_FAILURE(ConnectClientPort(test_address, client_handle_0,
634                                             test_scn_0, test_mtu, acl_handle,
635                                             lcid, 0, true));
636 
637   // Connection 1
638   static const uint16_t test_uuid_1 = 0x111F;
639   static const uint8_t test_scn_1 = 10;
640   uint16_t client_handle_1 = 0;
641   ASSERT_NO_FATAL_FAILURE(StartClientPort(
642       test_address, test_uuid_1, test_scn_1, test_mtu, port_mgmt_cback_1,
643       port_event_cback_1, lcid, acl_handle, &client_handle_1, false));
644   ASSERT_NO_FATAL_FAILURE(ConnectClientPort(test_address, client_handle_1,
645                                             test_scn_1, test_mtu, acl_handle,
646                                             lcid, 1, false));
647 
648   // Use connection 0
649   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
650       client_handle_0, false, test_scn_0, true, "\r!dlroW olleH", -1,
651       acl_handle, lcid));
652   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
653       client_handle_0, false, test_scn_0, false, "Hello World!\r", -1,
654       acl_handle, lcid, 0));
655 
656   // Use connection 1
657   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
658       client_handle_1, false, test_scn_1, true, "\r!dlroW olleH", -1,
659       acl_handle, lcid));
660   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
661       client_handle_1, false, test_scn_1, false, "Hello World!\r", -1,
662       acl_handle, lcid, 1));
663 }
664 
TEST_F(StackRfcommTest,SameClientPortMultiDeviceHelloWorld)665 TEST_F(StackRfcommTest, SameClientPortMultiDeviceHelloWorld) {
666   static const uint16_t test_uuid = 0x1112;
667   static const uint8_t test_scn = 8;
668   static const uint16_t test_mtu = 1600;
669 
670   // Connection 0
671   static const RawAddress test_address_0 = GetTestAddress(0);
672   static const uint16_t acl_handle_0 = 0x0009;
673   static const uint16_t lcid_0 = 0x0054;
674   uint16_t client_handle_0 = 0;
675   ASSERT_NO_FATAL_FAILURE(StartClientPort(
676       test_address_0, test_uuid, test_scn, test_mtu, port_mgmt_cback_0,
677       port_event_cback_0, lcid_0, acl_handle_0, &client_handle_0, true));
678   ASSERT_NO_FATAL_FAILURE(TestConnectClientPortL2cap(acl_handle_0, lcid_0));
679   ASSERT_NO_FATAL_FAILURE(ConnectClientPort(test_address_0, client_handle_0,
680                                             test_scn, test_mtu, acl_handle_0,
681                                             lcid_0, 0, true));
682 
683   // Connection 1
684   static const RawAddress test_address_1 = GetTestAddress(1);
685   static const uint16_t acl_handle_1 = 0x0012;
686   static const uint16_t lcid_1 = 0x0045;
687   uint16_t client_handle_1 = 0;
688   ASSERT_NO_FATAL_FAILURE(StartClientPort(
689       test_address_1, test_uuid, test_scn, test_mtu, port_mgmt_cback_1,
690       port_event_cback_1, lcid_1, acl_handle_1, &client_handle_1, true));
691   ASSERT_NO_FATAL_FAILURE(TestConnectClientPortL2cap(acl_handle_1, lcid_1));
692   ASSERT_NO_FATAL_FAILURE(ConnectClientPort(test_address_1, client_handle_1,
693                                             test_scn, test_mtu, acl_handle_1,
694                                             lcid_1, 1, true));
695 
696   // Use connection 0
697   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
698       client_handle_0, false, test_scn, true, "\r!dlroW olleH", -1,
699       acl_handle_0, lcid_0));
700   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
701       client_handle_0, false, test_scn, false, "Hello World!\r", -1,
702       acl_handle_0, lcid_0, 0));
703 
704   // Use connection 1
705   ASSERT_NO_FATAL_FAILURE(SendAndVerifyOutgoingTransmission(
706       client_handle_1, false, test_scn, true, "\r!dlroW olleH", -1,
707       acl_handle_1, lcid_1));
708   ASSERT_NO_FATAL_FAILURE(ReceiveAndVerifyIncomingTransmission(
709       client_handle_1, false, test_scn, false, "Hello World!\r", -1,
710       acl_handle_1, lcid_1, 1));
711 }
712 
TEST_F(StackRfcommTest,TestConnectionCollision)713 TEST_F(StackRfcommTest, TestConnectionCollision) {
714   static const uint16_t acl_handle = 0x0008;
715   static const uint16_t old_lcid = 0x004a;
716   static const uint16_t new_lcid = 0x005c;
717   static const uint16_t test_uuid = 0x1112;
718   static const uint8_t test_server_scn = 3;
719   static const uint8_t test_peer_scn = 10;
720   // Must be smaller than L2CAP_MTU_SIZE by at least 4 bytes
721   static const uint16_t test_mtu = 1000;
722   static const RawAddress test_address = GetTestAddress(0);
723   uint16_t server_handle = 0;
724   VLOG(1) << "Step 1";
725   // Prepare a server port
726   int status = RFCOMM_CreateConnection(test_uuid, test_server_scn, true,
727                                        test_mtu, RawAddress::kAny,
728                                        &server_handle, port_mgmt_cback_0);
729   ASSERT_EQ(status, PORT_SUCCESS);
730   status = PORT_SetEventMask(server_handle, PORT_EV_RXCHAR);
731   ASSERT_EQ(status, PORT_SUCCESS);
732   status = PORT_SetEventCallback(server_handle, port_event_cback_0);
733   ASSERT_EQ(status, PORT_SUCCESS);
734 
735   VLOG(1) << "Step 2";
736   // Try to connect to a client port
737   uint16_t client_handle_1 = 0;
738   EXPECT_CALL(l2cap_interface_, ConnectRequest(BT_PSM_RFCOMM, test_address))
739       .Times(1)
740       .WillOnce(Return(old_lcid));
741   status = RFCOMM_CreateConnection(test_uuid, test_peer_scn, false, test_mtu,
742                                    test_address, &client_handle_1,
743                                    port_mgmt_cback_1);
744   ASSERT_EQ(status, PORT_SUCCESS);
745   status = PORT_SetEventMask(client_handle_1, PORT_EV_RXCHAR);
746   ASSERT_EQ(status, PORT_SUCCESS);
747   status = PORT_SetEventCallback(client_handle_1, port_event_cback_1);
748   ASSERT_EQ(status, PORT_SUCCESS);
749 
750   VLOG(1) << "Step 3";
751   // While our connection is pending, remote device tries to connect to
752   // new_lcid, with L2CAP command id: pending_cmd_id
753   static const uint8_t pending_cmd_id = 0x05;
754   // RFCOMM starts timer for collision:
755   l2cap_appl_info_.pL2CA_ConnectInd_Cb(test_address, new_lcid, BT_PSM_RFCOMM,
756                                        pending_cmd_id);
757 
758   VLOG(1) << "Step 4";
759   // Remote reject our connection request saying PSM not allowed
760   // This should trigger RFCOMM to accept remote L2CAP connection at new_lcid
761   EXPECT_CALL(l2cap_interface_, ConnectResponse(test_address, pending_cmd_id,
762                                                 new_lcid, L2CAP_CONN_OK, 0))
763       .WillOnce(Return(true));
764   // Followed by configure request for MTU size
765   tL2CAP_CFG_INFO our_cfg_req = {.mtu_present = true, .mtu = L2CAP_MTU_SIZE};
766   EXPECT_CALL(l2cap_interface_,
767               ConfigRequest(new_lcid, PointerMemoryEqual(&our_cfg_req)))
768       .WillOnce(Return(true));
769   l2cap_appl_info_.pL2CA_ConnectCfm_Cb(old_lcid, L2CAP_CONN_NO_PSM);
770 
771   VLOG(1) << "Step 5";
772   // Remote device also ask to configure MTU size as well
773   tL2CAP_CFG_INFO peer_cfg_req = {.mtu_present = true, .mtu = test_mtu};
774   // We responded by saying OK
775   tL2CAP_CFG_INFO our_cfg_rsp = {.result = L2CAP_CFG_OK,
776                                  .mtu = peer_cfg_req.mtu};
777   EXPECT_CALL(l2cap_interface_,
778               ConfigResponse(new_lcid, PointerMemoryEqual(&our_cfg_rsp)))
779       .WillOnce(Return(true));
780   l2cap_appl_info_.pL2CA_ConfigInd_Cb(new_lcid, &peer_cfg_req);
781 
782   VLOG(1) << "Step 6";
783   // Remote device accepted our MTU size
784   l2cap_appl_info_.pL2CA_ConfigCfm_Cb(new_lcid, L2CAP_CFG_OK, {});
785 
786   // L2CAP collision and connection setup done
787 
788   VLOG(1) << "Step 7";
789   // Remote device connect multiplexer channel
790   BT_HDR* sabm_channel_0 = AllocateWrappedIncomingL2capAclPacket(
791       CreateQuickSabmPacket(RFCOMM_MX_DLCI, new_lcid, acl_handle));
792   // We accept
793   BT_HDR* ua_channel_0 = AllocateWrappedOutgoingL2capAclPacket(
794       CreateQuickUaPacket(RFCOMM_MX_DLCI, new_lcid, acl_handle));
795   EXPECT_CALL(l2cap_interface_, DataWrite(new_lcid, BtHdrEqual(ua_channel_0)))
796       .WillOnce(Return(L2CAP_DW_SUCCESS));
797   // And immediately try to configure test_peer_scn
798   BT_HDR* uih_pn_cmd_to_peer = AllocateWrappedOutgoingL2capAclPacket(
799       CreateQuickPnPacket(false, GetDlci(true, test_peer_scn), true, test_mtu,
800                           RFCOMM_PN_CONV_LAYER_CBFC_I >> 4, 0, RFCOMM_K_MAX,
801                           new_lcid, acl_handle));
802   EXPECT_CALL(l2cap_interface_,
803               DataWrite(new_lcid, BtHdrEqual(uih_pn_cmd_to_peer)))
804       .WillOnce(Return(L2CAP_DW_SUCCESS));
805   // Packet should be freed by this method
806   l2cap_appl_info_.pL2CA_DataInd_Cb(new_lcid, sabm_channel_0);
807   osi_free(ua_channel_0);
808   osi_free(uih_pn_cmd_to_peer);
809 
810   VLOG(1) << "Step 8";
811   // Peer tries to configure test_server_scn
812   BT_HDR* uih_pn_cmd_from_peer = AllocateWrappedIncomingL2capAclPacket(
813       CreateQuickPnPacket(true, GetDlci(false, test_server_scn), true, test_mtu,
814                           RFCOMM_PN_CONV_LAYER_CBFC_I >> 4, 0, RFCOMM_K_MAX,
815                           new_lcid, acl_handle));
816   // We, as acceptor, must accept
817   BT_HDR* uih_pn_rsp_to_peer = AllocateWrappedOutgoingL2capAclPacket(
818       CreateQuickPnPacket(false, GetDlci(false, test_server_scn), false,
819                           test_mtu, RFCOMM_PN_CONV_LAYER_CBFC_R >> 4, 0,
820                           RFCOMM_K_MAX, new_lcid, acl_handle));
821   EXPECT_CALL(l2cap_interface_,
822               DataWrite(new_lcid, BtHdrEqual(uih_pn_rsp_to_peer)))
823       .Times(1)
824       .WillOnce(Return(L2CAP_DW_SUCCESS));
825   l2cap_appl_info_.pL2CA_DataInd_Cb(new_lcid, uih_pn_cmd_from_peer);
826   osi_free(uih_pn_rsp_to_peer);
827 
828   VLOG(1) << "Step 9";
829   // Remote never replies our configuration request for test_peer_scn
830   // But instead connect to test_server_scn directly
831   BT_HDR* sabm_server_scn =
832       AllocateWrappedIncomingL2capAclPacket(CreateQuickSabmPacket(
833           GetDlci(false, test_server_scn), new_lcid, acl_handle));
834   // We must do security check first
835   tBTM_SEC_CALLBACK* security_callback = nullptr;
836   void* p_port = nullptr;
837   EXPECT_CALL(btm_security_internal_interface_,
838               MultiplexingProtocolAccessRequest(
839                   test_address, BT_PSM_RFCOMM, false, BTM_SEC_PROTO_RFCOMM,
840                   test_server_scn, NotNull(), NotNull()))
841       .WillOnce(DoAll(SaveArg<5>(&security_callback), SaveArg<6>(&p_port),
842                       Return(BTM_SUCCESS)));
843   l2cap_appl_info_.pL2CA_DataInd_Cb(new_lcid, sabm_server_scn);
844 
845   VLOG(1) << "Step 10";
846   // After security check, we accept the connection
847   ASSERT_TRUE(security_callback);
848   BT_HDR* ua_server_scn =
849       AllocateWrappedOutgoingL2capAclPacket(CreateQuickUaPacket(
850           GetDlci(false, test_server_scn), new_lcid, acl_handle));
851   EXPECT_CALL(l2cap_interface_, DataWrite(new_lcid, BtHdrEqual(ua_server_scn)))
852       .WillOnce(Return(L2CAP_DW_SUCCESS));
853   // Callback should come from server port instead, client port will timeout
854   // in 20 seconds
855   EXPECT_CALL(rfcomm_callback_,
856               PortManagementCallback(PORT_SUCCESS, server_handle, 0));
857   security_callback(&test_address, BT_TRANSPORT_BR_EDR, p_port, BTM_SUCCESS);
858   osi_free(ua_server_scn);
859 
860   VLOG(1) << "Step 11";
861   // MPX_CTRL Modem Status Command (MSC)
862   BT_HDR* uih_msc_cmd_from_peer = AllocateWrappedIncomingL2capAclPacket(
863       CreateQuickMscPacket(true, GetDlci(false, test_server_scn), new_lcid,
864                            acl_handle, true, false, true, true, false, true));
865   BT_HDR* uih_msc_rsp_to_peer = AllocateWrappedOutgoingL2capAclPacket(
866       CreateQuickMscPacket(false, GetDlci(false, test_server_scn), new_lcid,
867                            acl_handle, false, false, true, true, false, true));
868   // MPX_CTRL Modem Status Response (MSC)
869   EXPECT_CALL(l2cap_interface_,
870               DataWrite(new_lcid, BtHdrEqual(uih_msc_rsp_to_peer)))
871       .WillOnce(Return(L2CAP_DW_SUCCESS));
872   BT_HDR* uih_msc_cmd_to_peer = AllocateWrappedOutgoingL2capAclPacket(
873       CreateQuickMscPacket(false, GetDlci(false, test_server_scn), new_lcid,
874                            acl_handle, true, false, true, true, false, true));
875   EXPECT_CALL(l2cap_interface_,
876               DataWrite(new_lcid, BtHdrEqual(uih_msc_cmd_to_peer)))
877       .WillOnce(Return(L2CAP_DW_SUCCESS));
878   l2cap_appl_info_.pL2CA_DataInd_Cb(new_lcid, uih_msc_cmd_from_peer);
879   osi_free(uih_msc_rsp_to_peer);
880   osi_free(uih_msc_cmd_to_peer);
881 
882   VLOG(1) << "Step 12";
883   BT_HDR* uih_msc_rsp_from_peer = AllocateWrappedIncomingL2capAclPacket(
884       CreateQuickMscPacket(true, GetDlci(false, test_server_scn), new_lcid,
885                            acl_handle, false, false, true, true, false, true));
886   l2cap_appl_info_.pL2CA_DataInd_Cb(new_lcid, uih_msc_rsp_from_peer);
887 }
888 
889 }  // namespace
890