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