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