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