1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/l2cap/bredr_dynamic_channel.h"
16
17 #include <pw_async/fake_dispatcher_fixture.h>
18
19 #include <vector>
20
21 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
22 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
23 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
24 #include "pw_bluetooth_sapphire/internal/host/l2cap/fake_signaling_channel.h"
25 #include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h"
26 #include "pw_bluetooth_sapphire/internal/host/testing/test_helpers.h"
27 #include "pw_unit_test/framework.h"
28
29 namespace bt::l2cap::internal {
30 namespace {
31
32 // TODO(fxbug.dev/42056068): Add integration test with FakeChannelTest and
33 // BrEdrSignalingChannel using snooped connection data to verify signaling
34 // channel traffic.
35
36 constexpr uint16_t kPsm = 0x0001;
37 constexpr uint16_t kInvalidPsm = 0x0002; // Valid PSMs are odd.
38 constexpr ChannelId kLocalCId = 0x0040;
39 constexpr ChannelId kLocalCId2 = 0x0041;
40 constexpr ChannelId kRemoteCId = 0x60a3;
41 constexpr ChannelId kBadCId = 0x003f; // Not a dynamic channel.
42
43 constexpr ChannelParameters kChannelParams;
44 constexpr ChannelParameters kERTMChannelParams{
45 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
46 std::nullopt,
47 std::nullopt};
48
49 // Commands Reject
50
51 const StaticByteBuffer kRejNotUnderstood(
52 // Reject Reason (Not Understood)
53 0x00,
54 0x00);
55
56 // Connection Requests
57
58 const StaticByteBuffer kConnReq(
59 // PSM
60 LowerBits(kPsm),
61 UpperBits(kPsm),
62
63 // Source CID
64 LowerBits(kLocalCId),
65 UpperBits(kLocalCId));
66
MakeConnectionRequest(ChannelId src_id,Psm psm)67 auto MakeConnectionRequest(ChannelId src_id, Psm psm) {
68 return StaticByteBuffer(
69 // PSM
70 LowerBits(psm),
71 UpperBits(psm),
72
73 // Source CID
74 LowerBits(src_id),
75 UpperBits(src_id));
76 }
77
78 const StaticByteBuffer kInboundConnReq(
79 // PSM
80 LowerBits(kPsm),
81 UpperBits(kPsm),
82
83 // Source CID
84 LowerBits(kRemoteCId),
85 UpperBits(kRemoteCId));
86
87 const StaticByteBuffer kInboundInvalidPsmConnReq(
88 // PSM
89 LowerBits(kInvalidPsm),
90 UpperBits(kInvalidPsm),
91
92 // Source CID
93 LowerBits(kRemoteCId),
94 UpperBits(kRemoteCId));
95
96 const StaticByteBuffer kInboundBadCIdConnReq(
97 // PSM
98 LowerBits(kPsm),
99 UpperBits(kPsm),
100
101 // Source CID
102 LowerBits(kBadCId),
103 UpperBits(kBadCId));
104
105 // Connection Responses
106
107 const StaticByteBuffer kPendingConnRsp(
108 // Destination CID
109 0x00,
110 0x00,
111
112 // Source CID
113 LowerBits(kLocalCId),
114 UpperBits(kLocalCId),
115
116 // Result (Pending)
117 0x01,
118 0x00,
119
120 // Status (Authorization Pending)
121 0x02,
122 0x00);
123
124 const StaticByteBuffer kPendingConnRspWithId(
125 // Destination CID (Wrong endianness but valid)
126 UpperBits(kRemoteCId),
127 LowerBits(kRemoteCId),
128
129 // Source CID
130 LowerBits(kLocalCId),
131 UpperBits(kLocalCId),
132
133 // Result (Pending)
134 0x01,
135 0x00,
136
137 // Status (Authorization Pending)
138 0x02,
139 0x00);
140
MakeConnectionResponseWithResultPending(ChannelId src_id,ChannelId dst_id)141 auto MakeConnectionResponseWithResultPending(ChannelId src_id,
142 ChannelId dst_id) {
143 return StaticByteBuffer(
144 // Destination CID
145 LowerBits(dst_id),
146 UpperBits(dst_id),
147
148 // Source CID
149 LowerBits(src_id),
150 UpperBits(src_id),
151
152 // Result (Pending)
153 0x01,
154 0x00,
155
156 // Status (Authorization Pending)
157 0x02,
158 0x00);
159 }
160
161 const StaticByteBuffer kOkConnRsp(
162 // Destination CID
163 LowerBits(kRemoteCId),
164 UpperBits(kRemoteCId),
165
166 // Source CID
167 LowerBits(kLocalCId),
168 UpperBits(kLocalCId),
169
170 // Result (Successful)
171 0x00,
172 0x00,
173
174 // Status (No further information available)
175 0x00,
176 0x00);
177
MakeConnectionResponse(ChannelId src_id,ChannelId dst_id)178 auto MakeConnectionResponse(ChannelId src_id, ChannelId dst_id) {
179 return StaticByteBuffer(
180 // Destination CID
181 LowerBits(dst_id),
182 UpperBits(dst_id),
183
184 // Source CID
185 LowerBits(src_id),
186 UpperBits(src_id),
187
188 // Result (Successful)
189 0x00,
190 0x00,
191
192 // Status (No further information available)
193 0x00,
194 0x00);
195 }
196
197 const StaticByteBuffer kInvalidConnRsp(
198 // Destination CID (Not a dynamic channel ID)
199 LowerBits(kBadCId),
200 UpperBits(kBadCId),
201
202 // Source CID
203 LowerBits(kLocalCId),
204 UpperBits(kLocalCId),
205
206 // Result (Successful)
207 0x00,
208 0x00,
209
210 // Status (No further information available)
211 0x00,
212 0x00);
213
214 const StaticByteBuffer kRejectConnRsp(
215 // Destination CID (Invalid)
216 LowerBits(kInvalidChannelId),
217 UpperBits(kInvalidChannelId),
218
219 // Source CID
220 LowerBits(kLocalCId),
221 UpperBits(kLocalCId),
222
223 // Result (No resources)
224 0x04,
225 0x00,
226
227 // Status (No further information available)
228 0x00,
229 0x00);
230
231 const StaticByteBuffer kInboundOkConnRsp(
232 // Destination CID
233 LowerBits(kLocalCId),
234 UpperBits(kLocalCId),
235
236 // Source CID
237 LowerBits(kRemoteCId),
238 UpperBits(kRemoteCId),
239
240 // Result (Successful)
241 0x00,
242 0x00,
243
244 // Status (No further information available)
245 0x00,
246 0x00);
247
248 const StaticByteBuffer kOutboundSourceCIdAlreadyAllocatedConnRsp(
249 // Destination CID (Invalid)
250 0x00,
251 0x00,
252
253 // Source CID (Invalid)
254 LowerBits(kRemoteCId),
255 UpperBits(kRemoteCId),
256
257 // Result (Connection refused - source CID already allocated)
258 0x07,
259 0x00,
260
261 // Status (No further information available)
262 0x00,
263 0x00);
264
265 const StaticByteBuffer kInboundBadPsmConnRsp(
266 // Destination CID (Invalid)
267 0x00,
268 0x00,
269
270 // Source CID
271 LowerBits(kRemoteCId),
272 UpperBits(kRemoteCId),
273
274 // Result (PSM Not Supported)
275 0x02,
276 0x00,
277
278 // Status (No further information available)
279 0x00,
280 0x00);
281
282 const StaticByteBuffer kInboundBadCIdConnRsp(
283 // Destination CID (Invalid)
284 0x00,
285 0x00,
286
287 // Source CID
288 LowerBits(kBadCId),
289 UpperBits(kBadCId),
290
291 // Result (Invalid Source CID)
292 0x06,
293 0x00,
294
295 // Status (No further information available)
296 0x00,
297 0x00);
298
299 // Disconnection Requests
300
301 const StaticByteBuffer kDisconReq(
302 // Destination CID
303 LowerBits(kRemoteCId),
304 UpperBits(kRemoteCId),
305
306 // Source CID
307 LowerBits(kLocalCId),
308 UpperBits(kLocalCId));
309
310 const StaticByteBuffer kInboundDisconReq(
311 // Destination CID
312 LowerBits(kLocalCId),
313 UpperBits(kLocalCId),
314
315 // Source CID
316 LowerBits(kRemoteCId),
317 UpperBits(kRemoteCId));
318
319 // Disconnection Responses
320
321 const ByteBuffer& kInboundDisconRsp = kInboundDisconReq;
322
323 const ByteBuffer& kDisconRsp = kDisconReq;
324
325 // Configuration Requests
326
MakeConfigReqWithMtuAndRfc(ChannelId dest_cid,uint16_t mtu,RetransmissionAndFlowControlMode mode,uint8_t tx_window,uint8_t max_transmit,uint16_t retransmission_timeout,uint16_t monitor_timeout,uint16_t mps)327 auto MakeConfigReqWithMtuAndRfc(ChannelId dest_cid,
328 uint16_t mtu,
329
330 RetransmissionAndFlowControlMode mode,
331 uint8_t tx_window,
332 uint8_t max_transmit,
333 uint16_t retransmission_timeout,
334 uint16_t monitor_timeout,
335 uint16_t mps) {
336 return StaticByteBuffer(
337 // Destination CID
338 LowerBits(dest_cid),
339 UpperBits(dest_cid),
340
341 // Flags
342 0x00,
343 0x00,
344
345 // MTU option (Type, Length, MTU value)
346 0x01,
347 0x02,
348 LowerBits(mtu),
349 UpperBits(mtu),
350
351 // Retransmission & Flow Control option (Type, Length = 9, mode, unused
352 // fields)
353 0x04,
354 0x09,
355 static_cast<uint8_t>(mode),
356 tx_window,
357 max_transmit,
358 LowerBits(retransmission_timeout),
359 UpperBits(retransmission_timeout),
360 LowerBits(monitor_timeout),
361 UpperBits(monitor_timeout),
362 LowerBits(mps),
363 UpperBits(mps));
364 }
365
MakeConfigReqWithMtu(ChannelId dest_cid,uint16_t mtu=kMaxMTU,uint16_t flags=0x0000)366 auto MakeConfigReqWithMtu(ChannelId dest_cid,
367 uint16_t mtu = kMaxMTU,
368 uint16_t flags = 0x0000) {
369 return StaticByteBuffer(
370 // Destination CID
371 LowerBits(dest_cid),
372 UpperBits(dest_cid),
373
374 // Flags
375 LowerBits(flags),
376 UpperBits(flags),
377
378 // MTU option (Type, Length, MTU value)
379 0x01,
380 0x02,
381 LowerBits(mtu),
382 UpperBits(mtu));
383 }
384
385 const ByteBuffer& kOutboundConfigReq = MakeConfigReqWithMtu(kRemoteCId);
386
387 const ByteBuffer& kOutboundConfigReqWithErtm = MakeConfigReqWithMtuAndRfc(
388 kRemoteCId,
389 kMaxInboundPduPayloadSize,
390 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
391 kErtmMaxUnackedInboundFrames,
392 kErtmMaxInboundRetransmissions,
393 0,
394 0,
395 kMaxInboundPduPayloadSize);
396
397 const StaticByteBuffer kInboundConfigReq(
398 // Destination CID
399 LowerBits(kLocalCId),
400 UpperBits(kLocalCId),
401
402 // Flags
403 0x00,
404 0x00);
405
406 // Use plausible ERTM parameters that do not necessarily match values in
407 // production. See Core Spec v5.0 Vol 3, Part A, Sec 5.4 for meanings.
408 constexpr uint8_t kErtmNFramesInTxWindow = 32;
409 constexpr uint8_t kErtmMaxTransmissions = 8;
410 constexpr uint16_t kMaxTxPduPayloadSize = 1024;
411
412 const StaticByteBuffer kInboundConfigReqWithERTM(
413 // Destination CID
414 LowerBits(kLocalCId),
415 UpperBits(kLocalCId),
416
417 // Flags
418 0x00,
419 0x00,
420
421 // Retransmission & Flow Control option (Type, Length = 9, mode = ERTM,
422 // arbitrary parameters)
423 0x04,
424 0x09,
425 0x03,
426 kErtmNFramesInTxWindow,
427 kErtmMaxTransmissions,
428 0x00,
429 0x00,
430 0x00,
431 0x00,
432 LowerBits(kMaxTxPduPayloadSize),
433 UpperBits(kMaxTxPduPayloadSize),
434
435 // FCS Option (Type, Length = 1) - turn off which we should accept and
436 // ignore
437 0x05,
438 0x01,
439 static_cast<uint8_t>(FcsType::kNoFcs));
440
441 // Configuration Responses
442
MakeEmptyConfigRsp(ChannelId src_id,ConfigurationResult result=ConfigurationResult::kSuccess,uint16_t flags=0x0000)443 auto MakeEmptyConfigRsp(
444 ChannelId src_id,
445 ConfigurationResult result = ConfigurationResult::kSuccess,
446 uint16_t flags = 0x0000) {
447 return StaticByteBuffer(
448 // Source CID
449 LowerBits(src_id),
450 UpperBits(src_id),
451
452 // Flags
453 LowerBits(flags),
454 UpperBits(flags),
455
456 // Result
457 LowerBits(static_cast<uint16_t>(result)),
458 UpperBits(static_cast<uint16_t>(result)));
459 }
460
461 const ByteBuffer& kOutboundEmptyContinuationConfigRsp = MakeEmptyConfigRsp(
462 kRemoteCId, ConfigurationResult::kSuccess, kConfigurationContinuation);
463
464 const ByteBuffer& kInboundEmptyConfigRsp = MakeEmptyConfigRsp(kLocalCId);
465
466 const ByteBuffer& kUnknownIdConfigRsp = MakeEmptyConfigRsp(kBadCId);
467
468 const ByteBuffer& kOutboundEmptyPendingConfigRsp =
469 MakeEmptyConfigRsp(kRemoteCId, ConfigurationResult::kPending);
470
471 const ByteBuffer& kInboundEmptyPendingConfigRsp =
472 MakeEmptyConfigRsp(kLocalCId, ConfigurationResult::kPending);
473
474 const ByteBuffer& kOutboundConfigRspRejected =
475 MakeEmptyConfigRsp(kRemoteCId, ConfigurationResult::kRejected);
476
MakeConfigRspWithMtu(ChannelId source_cid,uint16_t mtu,ConfigurationResult result=ConfigurationResult::kSuccess,uint16_t flags=0x0000)477 auto MakeConfigRspWithMtu(
478 ChannelId source_cid,
479 uint16_t mtu,
480 ConfigurationResult result = ConfigurationResult::kSuccess,
481 uint16_t flags = 0x0000) {
482 return StaticByteBuffer(
483 // Source CID
484 LowerBits(source_cid),
485 UpperBits(source_cid),
486
487 // Flags
488 LowerBits(flags),
489 UpperBits(flags),
490
491 // Result
492 LowerBits(static_cast<uint16_t>(result)),
493 UpperBits(static_cast<uint16_t>(result)),
494
495 // MTU option (Type, Length, MTU value)
496 0x01,
497 0x02,
498 LowerBits(mtu),
499 UpperBits(mtu));
500 }
501
502 const ByteBuffer& kOutboundOkConfigRsp =
503 MakeConfigRspWithMtu(kRemoteCId, kDefaultMTU);
504
MakeConfigRspWithRfc(ChannelId source_cid,ConfigurationResult result,RetransmissionAndFlowControlMode mode,uint8_t tx_window,uint8_t max_transmit,uint16_t retransmission_timeout,uint16_t monitor_timeout,uint16_t mps)505 auto MakeConfigRspWithRfc(ChannelId source_cid,
506 ConfigurationResult result,
507 RetransmissionAndFlowControlMode mode,
508 uint8_t tx_window,
509 uint8_t max_transmit,
510 uint16_t retransmission_timeout,
511 uint16_t monitor_timeout,
512 uint16_t mps) {
513 return StaticByteBuffer(
514 // Source CID
515 LowerBits(source_cid),
516 UpperBits(source_cid),
517
518 // Flags
519 0x00,
520 0x00,
521
522 // Result
523 LowerBits(static_cast<uint16_t>(result)),
524 UpperBits(static_cast<uint16_t>(result)),
525
526 // Retransmission & Flow Control option (Type, Length: 9, mode, unused
527 // parameters)
528 0x04,
529 0x09,
530 static_cast<uint8_t>(mode),
531 tx_window,
532 max_transmit,
533 LowerBits(retransmission_timeout),
534 UpperBits(retransmission_timeout),
535 LowerBits(monitor_timeout),
536 UpperBits(monitor_timeout),
537 LowerBits(mps),
538 UpperBits(mps));
539 }
540
541 const ByteBuffer& kInboundUnacceptableParamsWithRfcBasicConfigRsp =
542 MakeConfigRspWithRfc(kLocalCId,
543 ConfigurationResult::kUnacceptableParameters,
544 RetransmissionAndFlowControlMode::kBasic,
545 0,
546 0,
547 0,
548 0,
549 0);
550
551 const ByteBuffer& kOutboundUnacceptableParamsWithRfcBasicConfigRsp =
552 MakeConfigRspWithRfc(kRemoteCId,
553 ConfigurationResult::kUnacceptableParameters,
554 RetransmissionAndFlowControlMode::kBasic,
555 0,
556 0,
557 0,
558 0,
559 0);
560
561 const ByteBuffer& kOutboundUnacceptableParamsWithRfcERTMConfigRsp =
562 MakeConfigRspWithRfc(
563 kRemoteCId,
564 ConfigurationResult::kUnacceptableParameters,
565 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
566 0,
567 0,
568 0,
569 0,
570 0);
571
MakeConfigRspWithMtuAndRfc(ChannelId source_cid,ConfigurationResult result,RetransmissionAndFlowControlMode mode,uint16_t mtu,uint8_t tx_window,uint8_t max_transmit,uint16_t retransmission_timeout,uint16_t monitor_timeout,uint16_t mps)572 auto MakeConfigRspWithMtuAndRfc(ChannelId source_cid,
573 ConfigurationResult result,
574 RetransmissionAndFlowControlMode mode,
575 uint16_t mtu,
576 uint8_t tx_window,
577 uint8_t max_transmit,
578 uint16_t retransmission_timeout,
579 uint16_t monitor_timeout,
580 uint16_t mps) {
581 return StaticByteBuffer(
582 // Source CID
583 LowerBits(source_cid),
584 UpperBits(source_cid),
585
586 // Flags
587 0x00,
588 0x00,
589
590 // Result
591 LowerBits(static_cast<uint16_t>(result)),
592 UpperBits(static_cast<uint16_t>(result)),
593
594 // MTU option (Type, Length, MTU value)
595 0x01,
596 0x02,
597 LowerBits(mtu),
598 UpperBits(mtu),
599
600 // Retransmission & Flow Control option (Type, Length = 9, mode, ERTM
601 // fields)
602 0x04,
603 0x09,
604 static_cast<uint8_t>(mode),
605 tx_window,
606 max_transmit,
607 LowerBits(retransmission_timeout),
608 UpperBits(retransmission_timeout),
609 LowerBits(monitor_timeout),
610 UpperBits(monitor_timeout),
611 LowerBits(mps),
612 UpperBits(mps));
613 }
614
615 // Corresponds to kInboundConfigReqWithERTM
616 const ByteBuffer& kOutboundOkConfigRspWithErtm = MakeConfigRspWithMtuAndRfc(
617 kRemoteCId,
618 ConfigurationResult::kSuccess,
619 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
620 kDefaultMTU,
621 kErtmNFramesInTxWindow,
622 kErtmMaxTransmissions,
623 2000,
624 12000,
625 kMaxTxPduPayloadSize);
626
627 // Information Requests
628
MakeInfoReq(InformationType info_type)629 auto MakeInfoReq(InformationType info_type) {
630 const auto type = static_cast<uint16_t>(info_type);
631 return StaticByteBuffer(LowerBits(type), UpperBits(type));
632 }
633
634 const ByteBuffer& kExtendedFeaturesInfoReq =
635 MakeInfoReq(InformationType::kExtendedFeaturesSupported);
636
637 // Information Responses
638
MakeExtendedFeaturesInfoRsp(InformationResult result=InformationResult::kSuccess,ExtendedFeatures features=0u)639 auto MakeExtendedFeaturesInfoRsp(
640 InformationResult result = InformationResult::kSuccess,
641 ExtendedFeatures features = 0u) {
642 const auto type =
643 static_cast<uint16_t>(InformationType::kExtendedFeaturesSupported);
644 const auto res = static_cast<uint16_t>(result);
645 const auto features_bytes = ToBytes(features);
646 return StaticByteBuffer(
647 // Type
648 LowerBits(type),
649 UpperBits(type),
650
651 // Result
652 LowerBits(res),
653 UpperBits(res),
654
655 // Data
656 features_bytes[0],
657 features_bytes[1],
658 features_bytes[2],
659 features_bytes[3]);
660 }
661
662 const ByteBuffer& kExtendedFeaturesInfoRsp =
663 MakeExtendedFeaturesInfoRsp(InformationResult::kSuccess);
664 const ByteBuffer& kExtendedFeaturesInfoRspWithERTM =
665 MakeExtendedFeaturesInfoRsp(InformationResult::kSuccess,
666 kExtendedFeaturesBitEnhancedRetransmission);
667
668 class BrEdrDynamicChannelTest : public pw::async::test::FakeDispatcherFixture {
669 public:
670 BrEdrDynamicChannelTest() = default;
671 ~BrEdrDynamicChannelTest() override = default;
672
673 protected:
674 // Import types for brevity.
675 using DynamicChannelCallback = DynamicChannelRegistry::DynamicChannelCallback;
676 using ServiceRequestCallback = DynamicChannelRegistry::ServiceRequestCallback;
677
678 // TestLoopFixture overrides
SetUp()679 void SetUp() override {
680 channel_close_cb_ = nullptr;
681 service_request_cb_ = nullptr;
682 signaling_channel_ =
683 std::make_unique<testing::FakeSignalingChannel>(dispatcher());
684
685 ext_info_transaction_id_ = EXPECT_OUTBOUND_REQ(
686 *sig(), kInformationRequest, kExtendedFeaturesInfoReq.view());
687 // TODO(fxbug.dev/42141538): Make these tests not rely on strict ordering of
688 // channel IDs.
689 registry_ = std::make_unique<BrEdrDynamicChannelRegistry>(
690 sig(),
691 fit::bind_member<&BrEdrDynamicChannelTest::OnChannelClose>(this),
692 fit::bind_member<&BrEdrDynamicChannelTest::OnServiceRequest>(this),
693 /*random_channel_ids=*/false);
694 }
695
TearDown()696 void TearDown() override {
697 RunUntilIdle();
698 registry_ = nullptr;
699 signaling_channel_ = nullptr;
700 service_request_cb_ = nullptr;
701 channel_close_cb_ = nullptr;
702 }
703
sig() const704 testing::FakeSignalingChannel* sig() const {
705 return signaling_channel_.get();
706 }
707
registry() const708 BrEdrDynamicChannelRegistry* registry() const { return registry_.get(); }
709
set_channel_close_cb(DynamicChannelCallback close_cb)710 void set_channel_close_cb(DynamicChannelCallback close_cb) {
711 channel_close_cb_ = std::move(close_cb);
712 }
713
set_service_request_cb(ServiceRequestCallback service_request_cb)714 void set_service_request_cb(ServiceRequestCallback service_request_cb) {
715 service_request_cb_ = std::move(service_request_cb);
716 }
717
ext_info_transaction_id()718 testing::FakeSignalingChannel::TransactionId ext_info_transaction_id() {
719 return ext_info_transaction_id_;
720 }
721
722 private:
OnChannelClose(const DynamicChannel * channel)723 void OnChannelClose(const DynamicChannel* channel) {
724 if (channel_close_cb_) {
725 channel_close_cb_(channel);
726 }
727 }
728
729 // Default to rejecting all service requests if no test callback is set.
OnServiceRequest(Psm psm)730 std::optional<DynamicChannelRegistry::ServiceInfo> OnServiceRequest(Psm psm) {
731 if (service_request_cb_) {
732 return service_request_cb_(psm);
733 }
734 return std::nullopt;
735 }
736
737 DynamicChannelCallback channel_close_cb_;
738 ServiceRequestCallback service_request_cb_;
739 std::unique_ptr<testing::FakeSignalingChannel> signaling_channel_;
740 std::unique_ptr<BrEdrDynamicChannelRegistry> registry_;
741 testing::FakeSignalingChannel::TransactionId ext_info_transaction_id_;
742
743 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(BrEdrDynamicChannelTest);
744 };
745
TEST_F(BrEdrDynamicChannelTest,InboundConnectionResponseReusingChannelIdCausesInboundChannelFailure)746 TEST_F(BrEdrDynamicChannelTest,
747 InboundConnectionResponseReusingChannelIdCausesInboundChannelFailure) {
748 // make successful connection
749 EXPECT_OUTBOUND_REQ(*sig(),
750 kConnectionRequest,
751 kConnReq.view(),
752 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
753 EXPECT_OUTBOUND_REQ(
754 *sig(),
755 kConfigurationRequest,
756 kOutboundConfigReq.view(),
757 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
758
759 int open_cb_count = 0;
760 auto open_cb = [&open_cb_count](auto chan) {
761 if (open_cb_count == 0) {
762 ASSERT_TRUE(chan);
763 EXPECT_TRUE(chan->IsOpen());
764 EXPECT_TRUE(chan->IsConnected());
765 EXPECT_EQ(kLocalCId, chan->local_cid());
766 EXPECT_EQ(kRemoteCId, chan->remote_cid());
767 }
768 open_cb_count++;
769 };
770
771 int close_cb_count = 0;
772 set_channel_close_cb([&close_cb_count](auto chan) {
773 EXPECT_TRUE(chan);
774 close_cb_count++;
775 });
776
777 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
778
779 RETURN_IF_FATAL(RunUntilIdle());
780
781 RETURN_IF_FATAL(sig()->ReceiveExpect(
782 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
783
784 EXPECT_EQ(1, open_cb_count);
785 EXPECT_EQ(0, close_cb_count);
786
787 // simulate inbound request to make new connection using already allocated
788 // remote CId
789 sig()->ReceiveExpect(kConnectionRequest,
790 kInboundConnReq,
791 kOutboundSourceCIdAlreadyAllocatedConnRsp);
792
793 EXPECT_OUTBOUND_REQ(*sig(),
794 kDisconnectionRequest,
795 kDisconReq.view(),
796 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
797 bool channel_close_cb_called = false;
798 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
799 RETURN_IF_FATAL(RunUntilIdle());
800 EXPECT_TRUE(channel_close_cb_called);
801 }
802
TEST_F(BrEdrDynamicChannelTest,PeerConnectionResponseReusingChannelIdCausesOutboundChannelFailure)803 TEST_F(BrEdrDynamicChannelTest,
804 PeerConnectionResponseReusingChannelIdCausesOutboundChannelFailure) {
805 EXPECT_OUTBOUND_REQ(*sig(),
806 kConnectionRequest,
807 kConnReq.view(),
808 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
809 EXPECT_OUTBOUND_REQ(
810 *sig(),
811 kConfigurationRequest,
812 kOutboundConfigReq.view(),
813 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
814
815 // make successful connection
816 int open_cb_count = 0;
817 auto open_cb = [&open_cb_count](auto chan) {
818 if (open_cb_count == 0) {
819 ASSERT_TRUE(chan);
820 EXPECT_TRUE(chan->IsOpen());
821 EXPECT_TRUE(chan->IsConnected());
822 EXPECT_EQ(kLocalCId, chan->local_cid());
823 EXPECT_EQ(kRemoteCId, chan->remote_cid());
824 }
825 open_cb_count++;
826 };
827
828 int close_cb_count = 0;
829 set_channel_close_cb([&close_cb_count](auto chan) {
830 EXPECT_TRUE(chan);
831 close_cb_count++;
832 });
833
834 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
835
836 RETURN_IF_FATAL(RunUntilIdle());
837
838 RETURN_IF_FATAL(sig()->ReceiveExpect(
839 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
840
841 EXPECT_EQ(1, open_cb_count);
842 EXPECT_EQ(0, close_cb_count);
843
844 // peer responds with already allocated remote CID
845 const auto kConnReq2 = MakeConnectionRequest(kLocalCId2, kPsm);
846 const auto kOkConnRspSamePeerCId =
847 MakeConnectionResponse(kLocalCId2, kRemoteCId);
848
849 EXPECT_OUTBOUND_REQ(
850 *sig(),
851 kConnectionRequest,
852 kConnReq2.view(),
853 {SignalingChannel::Status::kSuccess, kOkConnRspSamePeerCId.view()});
854
855 auto channel = BrEdrDynamicChannel::MakeOutbound(
856 registry(), sig(), kPsm, kLocalCId2, kChannelParams, false);
857 EXPECT_FALSE(channel->IsConnected());
858 EXPECT_FALSE(channel->IsOpen());
859
860 int close_cb_count2 = 0;
861 set_channel_close_cb([&close_cb_count2](auto) { close_cb_count2++; });
862
863 int open_cb_count2 = 0;
864 channel->Open([&open_cb_count2] { open_cb_count2++; });
865
866 RETURN_IF_FATAL(RunUntilIdle());
867
868 EXPECT_FALSE(channel->IsConnected());
869 EXPECT_FALSE(channel->IsOpen());
870 EXPECT_EQ(open_cb_count2, 1);
871 EXPECT_EQ(close_cb_count2, 0);
872
873 EXPECT_OUTBOUND_REQ(*sig(),
874 kDisconnectionRequest,
875 kDisconReq.view(),
876 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
877 bool channel_close_cb_called = false;
878 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
879 RETURN_IF_FATAL(RunUntilIdle());
880 EXPECT_TRUE(channel_close_cb_called);
881 }
882
TEST_F(BrEdrDynamicChannelTest,PeerPendingConnectionResponseReusingChannelIdCausesOutboundChannelFailure)883 TEST_F(
884 BrEdrDynamicChannelTest,
885 PeerPendingConnectionResponseReusingChannelIdCausesOutboundChannelFailure) {
886 EXPECT_OUTBOUND_REQ(*sig(),
887 kConnectionRequest,
888 kConnReq.view(),
889 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
890 EXPECT_OUTBOUND_REQ(
891 *sig(),
892 kConfigurationRequest,
893 kOutboundConfigReq.view(),
894 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
895
896 // make successful connection
897 int open_cb_count = 0;
898 auto open_cb = [&open_cb_count](auto chan) {
899 if (open_cb_count == 0) {
900 ASSERT_TRUE(chan);
901 EXPECT_TRUE(chan->IsOpen());
902 EXPECT_TRUE(chan->IsConnected());
903 EXPECT_EQ(kLocalCId, chan->local_cid());
904 EXPECT_EQ(kRemoteCId, chan->remote_cid());
905 }
906 open_cb_count++;
907 };
908
909 int close_cb_count = 0;
910 set_channel_close_cb([&close_cb_count](auto chan) {
911 EXPECT_TRUE(chan);
912 close_cb_count++;
913 });
914
915 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
916
917 RETURN_IF_FATAL(RunUntilIdle());
918
919 RETURN_IF_FATAL(sig()->ReceiveExpect(
920 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
921
922 EXPECT_EQ(1, open_cb_count);
923 EXPECT_EQ(0, close_cb_count);
924
925 // peer responds with already allocated remote CID
926 const auto kConnReq2 = MakeConnectionRequest(kLocalCId2, kPsm);
927 const auto kOkConnRspWithResultPendingSamePeerCId =
928 MakeConnectionResponseWithResultPending(kLocalCId2, kRemoteCId);
929 EXPECT_OUTBOUND_REQ(*sig(),
930 kConnectionRequest,
931 kConnReq2.view(),
932 {SignalingChannel::Status::kSuccess,
933 kOkConnRspWithResultPendingSamePeerCId.view()});
934
935 int open_cb_count2 = 0;
936 int close_cb_count2 = 0;
937 set_channel_close_cb([&close_cb_count2](auto) { close_cb_count2++; });
938
939 registry()->OpenOutbound(
940 kPsm, kChannelParams, [&open_cb_count2](auto) { open_cb_count2++; });
941
942 RETURN_IF_FATAL(RunUntilIdle());
943
944 EXPECT_EQ(open_cb_count2, 1);
945 // A failed-to-open channel should not invoke the close callback.
946 EXPECT_EQ(close_cb_count2, 0);
947
948 EXPECT_OUTBOUND_REQ(*sig(),
949 kDisconnectionRequest,
950 kDisconReq.view(),
951 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
952 bool channel_close_cb_called = false;
953 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
954 RETURN_IF_FATAL(RunUntilIdle());
955 EXPECT_TRUE(channel_close_cb_called);
956 }
957
TEST_F(BrEdrDynamicChannelTest,PeerConnectionResponseWithSameRemoteChannelIdAsPeerPendingConnectionResponseSucceeds)958 TEST_F(
959 BrEdrDynamicChannelTest,
960 PeerConnectionResponseWithSameRemoteChannelIdAsPeerPendingConnectionResponseSucceeds) {
961 const auto kOkPendingConnRsp =
962 MakeConnectionResponseWithResultPending(kLocalCId, kRemoteCId);
963 auto conn_rsp_id = EXPECT_OUTBOUND_REQ(
964 *sig(),
965 kConnectionRequest,
966 kConnReq.view(),
967 {SignalingChannel::Status::kSuccess, kOkPendingConnRsp.view()});
968 EXPECT_OUTBOUND_REQ(
969 *sig(),
970 kConfigurationRequest,
971 kOutboundConfigReq.view(),
972 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
973
974 int open_cb_count = 0;
975 auto open_cb = [&open_cb_count](auto chan) {
976 if (open_cb_count == 0) {
977 ASSERT_TRUE(chan);
978 EXPECT_TRUE(chan->IsOpen());
979 EXPECT_TRUE(chan->IsConnected());
980 EXPECT_EQ(kLocalCId, chan->local_cid());
981 EXPECT_EQ(kRemoteCId, chan->remote_cid());
982 }
983 open_cb_count++;
984 };
985
986 int close_cb_count = 0;
987 set_channel_close_cb([&close_cb_count](auto) { close_cb_count++; });
988
989 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
990
991 RETURN_IF_FATAL(RunUntilIdle());
992
993 RETURN_IF_FATAL(sig()->ReceiveResponses(
994 conn_rsp_id, {{SignalingChannel::Status::kSuccess, kOkConnRsp.view()}}));
995
996 RETURN_IF_FATAL(RunUntilIdle());
997
998 RETURN_IF_FATAL(sig()->ReceiveExpect(
999 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1000
1001 RETURN_IF_FATAL(RunUntilIdle());
1002
1003 EXPECT_EQ(open_cb_count, 1);
1004 EXPECT_EQ(close_cb_count, 0);
1005
1006 EXPECT_OUTBOUND_REQ(*sig(),
1007 kDisconnectionRequest,
1008 kDisconReq.view(),
1009 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1010 bool channel_close_cb_called = false;
1011 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1012 RETURN_IF_FATAL(RunUntilIdle());
1013 EXPECT_TRUE(channel_close_cb_called);
1014 }
1015
TEST_F(BrEdrDynamicChannelTest,ChannelDeletedBeforeConnectionResponse)1016 TEST_F(BrEdrDynamicChannelTest, ChannelDeletedBeforeConnectionResponse) {
1017 auto conn_id =
1018 EXPECT_OUTBOUND_REQ(*sig(), kConnectionRequest, kConnReq.view());
1019
1020 // Build channel and operate it directly to be able to delete it.
1021 auto channel = BrEdrDynamicChannel::MakeOutbound(
1022 registry(), sig(), kPsm, kLocalCId, kChannelParams, false);
1023 ASSERT_TRUE(channel);
1024
1025 int open_result_cb_count = 0;
1026 channel->Open([&open_result_cb_count] { open_result_cb_count++; });
1027
1028 RETURN_IF_FATAL(RunUntilIdle());
1029
1030 channel = nullptr;
1031 RETURN_IF_FATAL(
1032 sig()->ReceiveResponses(conn_id,
1033 {{SignalingChannel::Status::kSuccess,
1034 kOutboundEmptyPendingConfigRsp.view()}}));
1035
1036 EXPECT_EQ(0, open_result_cb_count);
1037
1038 // No disconnection transaction expected because the channel isn't actually
1039 // owned by the registry.
1040 }
1041
TEST_F(BrEdrDynamicChannelTest,FailConnectChannel)1042 TEST_F(BrEdrDynamicChannelTest, FailConnectChannel) {
1043 EXPECT_OUTBOUND_REQ(
1044 *sig(),
1045 kConnectionRequest,
1046 kConnReq.view(),
1047 {SignalingChannel::Status::kSuccess, kRejectConnRsp.view()});
1048
1049 // Build channel and operate it directly to be able to inspect it in the
1050 // connected but not open state.
1051 auto channel = BrEdrDynamicChannel::MakeOutbound(
1052 registry(), sig(), kPsm, kLocalCId, kChannelParams, false);
1053 EXPECT_FALSE(channel->IsConnected());
1054 EXPECT_FALSE(channel->IsOpen());
1055 EXPECT_EQ(kLocalCId, channel->local_cid());
1056
1057 int open_result_cb_count = 0;
1058 auto open_result_cb = [&open_result_cb_count, &channel] {
1059 if (open_result_cb_count == 0) {
1060 EXPECT_FALSE(channel->IsConnected());
1061 EXPECT_FALSE(channel->IsOpen());
1062 }
1063 open_result_cb_count++;
1064 };
1065 int close_cb_count = 0;
1066 set_channel_close_cb([&close_cb_count](auto) { close_cb_count++; });
1067
1068 channel->Open(std::move(open_result_cb));
1069
1070 RETURN_IF_FATAL(RunUntilIdle());
1071
1072 EXPECT_EQ(1, open_result_cb_count);
1073 EXPECT_FALSE(channel->IsConnected());
1074 EXPECT_FALSE(channel->IsOpen());
1075 EXPECT_EQ(kInvalidChannelId, channel->remote_cid());
1076
1077 // A failed-to-open channel should not invoke the close callback.
1078 EXPECT_EQ(0, close_cb_count);
1079
1080 // No disconnection transaction expected because the channel isn't actually
1081 // owned by the registry.
1082 }
1083
TEST_F(BrEdrDynamicChannelTest,ConnectChannelFailConfig)1084 TEST_F(BrEdrDynamicChannelTest, ConnectChannelFailConfig) {
1085 EXPECT_OUTBOUND_REQ(*sig(),
1086 kConnectionRequest,
1087 kConnReq.view(),
1088 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1089 EXPECT_OUTBOUND_REQ(
1090 *sig(),
1091 kConfigurationRequest,
1092 kOutboundConfigReq.view(),
1093 {SignalingChannel::Status::kReject, kRejNotUnderstood.view()});
1094
1095 // Build channel and operate it directly to be able to inspect it in the
1096 // connected but not open state.
1097 auto channel = BrEdrDynamicChannel::MakeOutbound(
1098 registry(), sig(), kPsm, kLocalCId, kChannelParams, false);
1099 EXPECT_FALSE(channel->IsConnected());
1100 EXPECT_FALSE(channel->IsOpen());
1101 EXPECT_EQ(kLocalCId, channel->local_cid());
1102
1103 int open_result_cb_count = 0;
1104 auto open_result_cb = [&open_result_cb_count, &channel] {
1105 if (open_result_cb_count == 0) {
1106 EXPECT_TRUE(channel->IsConnected());
1107 EXPECT_FALSE(channel->IsOpen());
1108 }
1109 open_result_cb_count++;
1110 };
1111 int close_cb_count = 0;
1112 set_channel_close_cb([&close_cb_count](auto) { close_cb_count++; });
1113
1114 channel->Open(std::move(open_result_cb));
1115 RETURN_IF_FATAL(RunUntilIdle());
1116 EXPECT_TRUE(channel->IsConnected());
1117
1118 // A connected channel should have a valid remote channel ID.
1119 EXPECT_EQ(kRemoteCId, channel->remote_cid());
1120
1121 EXPECT_FALSE(channel->IsOpen());
1122 EXPECT_EQ(1, open_result_cb_count);
1123
1124 // A failed-to-open channel should not invoke the close callback.
1125 EXPECT_EQ(0, close_cb_count);
1126
1127 // No disconnection transaction expected because the channel isn't actually
1128 // owned by the registry.
1129 }
1130
TEST_F(BrEdrDynamicChannelTest,ConnectChannelFailInvalidResponse)1131 TEST_F(BrEdrDynamicChannelTest, ConnectChannelFailInvalidResponse) {
1132 EXPECT_OUTBOUND_REQ(
1133 *sig(),
1134 kConnectionRequest,
1135 kConnReq.view(),
1136 {SignalingChannel::Status::kSuccess, kInvalidConnRsp.view()});
1137
1138 // Build channel and operate it directly to be able to inspect it in the
1139 // connected but not open state.
1140 auto channel = BrEdrDynamicChannel::MakeOutbound(
1141 registry(), sig(), kPsm, kLocalCId, kChannelParams, false);
1142
1143 int open_result_cb_count = 0;
1144 auto open_result_cb = [&open_result_cb_count, &channel] {
1145 if (open_result_cb_count == 0) {
1146 EXPECT_FALSE(channel->IsConnected());
1147 EXPECT_FALSE(channel->IsOpen());
1148 }
1149 open_result_cb_count++;
1150 };
1151 int close_cb_count = 0;
1152 set_channel_close_cb([&close_cb_count](auto) { close_cb_count++; });
1153
1154 channel->Open(std::move(open_result_cb));
1155 RETURN_IF_FATAL(RunUntilIdle());
1156 EXPECT_FALSE(channel->IsConnected());
1157 EXPECT_FALSE(channel->IsOpen());
1158 EXPECT_EQ(1, open_result_cb_count);
1159 EXPECT_EQ(0, close_cb_count);
1160
1161 // No disconnection transaction expected because the channel isn't actually
1162 // owned by the registry.
1163 }
1164
TEST_F(BrEdrDynamicChannelTest,OutboundFailsAfterRtxExpiryForConnectionResponse)1165 TEST_F(BrEdrDynamicChannelTest,
1166 OutboundFailsAfterRtxExpiryForConnectionResponse) {
1167 EXPECT_OUTBOUND_REQ(*sig(),
1168 kConnectionRequest,
1169 kConnReq.view(),
1170 {SignalingChannel::Status::kTimeOut, BufferView()});
1171
1172 int open_cb_count = 0;
1173 auto open_cb = [&open_cb_count](auto chan) {
1174 if (open_cb_count == 0) {
1175 EXPECT_FALSE(chan);
1176 }
1177 open_cb_count++;
1178 };
1179
1180 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1181
1182 // FakeSignalingChannel doesn't need to be clocked in order to simulate a
1183 // timeout.
1184 RETURN_IF_FATAL(RunUntilIdle());
1185
1186 EXPECT_EQ(1, open_cb_count);
1187 }
1188
TEST_F(BrEdrDynamicChannelTest,OutboundFailsAfterErtxExpiryForConnectionResponse)1189 TEST_F(BrEdrDynamicChannelTest,
1190 OutboundFailsAfterErtxExpiryForConnectionResponse) {
1191 EXPECT_OUTBOUND_REQ(
1192 *sig(),
1193 kConnectionRequest,
1194 kConnReq.view(),
1195 {SignalingChannel::Status::kSuccess, kPendingConnRsp.view()},
1196 {SignalingChannel::Status::kTimeOut, BufferView()});
1197
1198 int open_cb_count = 0;
1199 auto open_cb = [&open_cb_count](auto chan) {
1200 if (open_cb_count == 0) {
1201 EXPECT_FALSE(chan);
1202 }
1203 open_cb_count++;
1204 };
1205
1206 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1207
1208 // FakeSignalingChannel doesn't need to be clocked in order to simulate a
1209 // timeout.
1210 RETURN_IF_FATAL(RunUntilIdle());
1211
1212 EXPECT_EQ(1, open_cb_count);
1213 }
1214
1215 // In L2CAP Test Spec v5.0.2, this is L2CAP/COS/CED/BV-08-C [Disconnect on
1216 // Timeout].
TEST_F(BrEdrDynamicChannelTest,OutboundFailsAndDisconnectsAfterRtxExpiryForConfigurationResponse)1217 TEST_F(BrEdrDynamicChannelTest,
1218 OutboundFailsAndDisconnectsAfterRtxExpiryForConfigurationResponse) {
1219 EXPECT_OUTBOUND_REQ(*sig(),
1220 kConnectionRequest,
1221 kConnReq.view(),
1222 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1223 EXPECT_OUTBOUND_REQ(*sig(),
1224 kConfigurationRequest,
1225 kOutboundConfigReq.view(),
1226 {SignalingChannel::Status::kTimeOut, BufferView()});
1227 EXPECT_OUTBOUND_REQ(*sig(),
1228 kDisconnectionRequest,
1229 kDisconReq.view(),
1230 {SignalingChannel::Status::kTimeOut, BufferView()});
1231
1232 int open_cb_count = 0;
1233 auto open_cb = [&open_cb_count](auto chan) {
1234 if (open_cb_count == 0) {
1235 EXPECT_FALSE(chan);
1236 }
1237 open_cb_count++;
1238 };
1239
1240 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1241
1242 // FakeSignalingChannel doesn't need to be clocked in order to simulate a
1243 // timeout.
1244 RETURN_IF_FATAL(RunUntilIdle());
1245
1246 EXPECT_EQ(1, open_cb_count);
1247 }
1248
1249 // Simulate a ERTX timer expiry after a Configuration Response with "Pending"
1250 // status.
TEST_F(BrEdrDynamicChannelTest,OutboundFailsAndDisconnectsAfterErtxExpiryForConfigurationResponse)1251 TEST_F(BrEdrDynamicChannelTest,
1252 OutboundFailsAndDisconnectsAfterErtxExpiryForConfigurationResponse) {
1253 EXPECT_OUTBOUND_REQ(*sig(),
1254 kConnectionRequest,
1255 kConnReq.view(),
1256 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1257 EXPECT_OUTBOUND_REQ(*sig(),
1258 kConfigurationRequest,
1259 kOutboundConfigReq.view(),
1260 {SignalingChannel::Status::kSuccess,
1261 kInboundEmptyPendingConfigRsp.view()},
1262 {SignalingChannel::Status::kTimeOut, BufferView()});
1263 EXPECT_OUTBOUND_REQ(*sig(),
1264 kDisconnectionRequest,
1265 kDisconReq.view(),
1266 {SignalingChannel::Status::kTimeOut, BufferView()});
1267
1268 int open_cb_count = 0;
1269 auto open_cb = [&open_cb_count](auto chan) {
1270 if (open_cb_count == 0) {
1271 EXPECT_FALSE(chan);
1272 }
1273 open_cb_count++;
1274 };
1275
1276 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1277
1278 // FakeSignalingChannel doesn't need to be clocked in order to simulate a
1279 // timeout.
1280 RETURN_IF_FATAL(RunUntilIdle());
1281
1282 EXPECT_EQ(1, open_cb_count);
1283 }
1284
TEST_F(BrEdrDynamicChannelTest,OpenAndLocalCloseChannel)1285 TEST_F(BrEdrDynamicChannelTest, OpenAndLocalCloseChannel) {
1286 EXPECT_OUTBOUND_REQ(*sig(),
1287 kConnectionRequest,
1288 kConnReq.view(),
1289 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1290 EXPECT_OUTBOUND_REQ(
1291 *sig(),
1292 kConfigurationRequest,
1293 kOutboundConfigReq.view(),
1294 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1295 EXPECT_OUTBOUND_REQ(*sig(),
1296 kDisconnectionRequest,
1297 kDisconReq.view(),
1298 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1299
1300 int open_cb_count = 0;
1301 auto open_cb = [&open_cb_count](auto chan) {
1302 if (open_cb_count == 0) {
1303 ASSERT_TRUE(chan);
1304 EXPECT_TRUE(chan->IsOpen());
1305 EXPECT_TRUE(chan->IsConnected());
1306 EXPECT_EQ(kLocalCId, chan->local_cid());
1307 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1308 EXPECT_EQ(RetransmissionAndFlowControlMode::kBasic, chan->info().mode);
1309 }
1310 open_cb_count++;
1311 };
1312
1313 int close_cb_count = 0;
1314 set_channel_close_cb([&close_cb_count](auto chan) {
1315 EXPECT_TRUE(chan);
1316 close_cb_count++;
1317 });
1318
1319 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1320
1321 RETURN_IF_FATAL(RunUntilIdle());
1322
1323 RETURN_IF_FATAL(sig()->ReceiveExpect(
1324 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1325
1326 EXPECT_EQ(1, open_cb_count);
1327 EXPECT_EQ(0, close_cb_count);
1328
1329 bool channel_close_cb_called = false;
1330 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1331 RETURN_IF_FATAL(RunUntilIdle());
1332
1333 EXPECT_EQ(1, open_cb_count);
1334
1335 // Local channel closure shouldn't trigger the close callback.
1336 EXPECT_EQ(0, close_cb_count);
1337
1338 // Callback passed to CloseChannel should be called nonetheless.
1339 EXPECT_TRUE(channel_close_cb_called);
1340
1341 // Repeated closure of the same channel should not have any effect.
1342 channel_close_cb_called = false;
1343 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1344 EXPECT_TRUE(channel_close_cb_called);
1345 RETURN_IF_FATAL(RunUntilIdle());
1346
1347 EXPECT_EQ(1, open_cb_count);
1348 EXPECT_EQ(0, close_cb_count);
1349 }
1350
TEST_F(BrEdrDynamicChannelTest,OpenAndRemoteCloseChannel)1351 TEST_F(BrEdrDynamicChannelTest, OpenAndRemoteCloseChannel) {
1352 EXPECT_OUTBOUND_REQ(*sig(),
1353 kConnectionRequest,
1354 kConnReq.view(),
1355 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1356 EXPECT_OUTBOUND_REQ(
1357 *sig(),
1358 kConfigurationRequest,
1359 kOutboundConfigReq.view(),
1360 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1361
1362 int open_cb_count = 0;
1363 auto open_cb = [&open_cb_count](auto) { open_cb_count++; };
1364
1365 int close_cb_count = 0;
1366 set_channel_close_cb([&close_cb_count](auto chan) {
1367 ASSERT_TRUE(chan);
1368 EXPECT_FALSE(chan->IsOpen());
1369 EXPECT_FALSE(chan->IsConnected());
1370 EXPECT_EQ(kLocalCId, chan->local_cid());
1371 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1372 close_cb_count++;
1373 });
1374
1375 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1376
1377 RETURN_IF_FATAL(RunUntilIdle());
1378
1379 RETURN_IF_FATAL(sig()->ReceiveExpect(
1380 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1381
1382 EXPECT_EQ(1, open_cb_count);
1383 EXPECT_EQ(0, close_cb_count);
1384
1385 RETURN_IF_FATAL(sig()->ReceiveExpect(
1386 kDisconnectionRequest, kInboundDisconReq, kInboundDisconRsp));
1387
1388 EXPECT_EQ(1, open_cb_count);
1389
1390 // Remote channel closure should trigger the close callback.
1391 EXPECT_EQ(1, close_cb_count);
1392 }
1393
TEST_F(BrEdrDynamicChannelTest,OpenChannelWithPendingConn)1394 TEST_F(BrEdrDynamicChannelTest, OpenChannelWithPendingConn) {
1395 EXPECT_OUTBOUND_REQ(
1396 *sig(),
1397 kConnectionRequest,
1398 kConnReq.view(),
1399 {SignalingChannel::Status::kSuccess, kPendingConnRsp.view()},
1400 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1401 EXPECT_OUTBOUND_REQ(
1402 *sig(),
1403 kConfigurationRequest,
1404 kOutboundConfigReq.view(),
1405 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1406
1407 int open_cb_count = 0;
1408 registry()->OpenOutbound(kPsm, kChannelParams, [&open_cb_count](auto chan) {
1409 open_cb_count++;
1410 ASSERT_TRUE(chan);
1411 EXPECT_EQ(kLocalCId, chan->local_cid());
1412 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1413 });
1414
1415 RETURN_IF_FATAL(RunUntilIdle());
1416
1417 RETURN_IF_FATAL(sig()->ReceiveExpect(
1418 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1419
1420 EXPECT_EQ(1, open_cb_count);
1421
1422 EXPECT_OUTBOUND_REQ(*sig(),
1423 kDisconnectionRequest,
1424 kDisconReq.view(),
1425 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1426 bool channel_close_cb_called = false;
1427 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1428 RETURN_IF_FATAL(RunUntilIdle());
1429 EXPECT_TRUE(channel_close_cb_called);
1430 }
1431
TEST_F(BrEdrDynamicChannelTest,OpenChannelMismatchConnRsp)1432 TEST_F(BrEdrDynamicChannelTest, OpenChannelMismatchConnRsp) {
1433 // The first Connection Response (pending) has a different ID than the final
1434 // Connection Response (success).
1435 EXPECT_OUTBOUND_REQ(
1436 *sig(),
1437 kConnectionRequest,
1438 kConnReq.view(),
1439 {SignalingChannel::Status::kSuccess, kPendingConnRspWithId.view()},
1440 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1441 EXPECT_OUTBOUND_REQ(
1442 *sig(),
1443 kConfigurationRequest,
1444 kOutboundConfigReq.view(),
1445 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1446
1447 int open_cb_count = 0;
1448 registry()->OpenOutbound(kPsm, kChannelParams, [&open_cb_count](auto chan) {
1449 open_cb_count++;
1450 ASSERT_TRUE(chan);
1451 EXPECT_EQ(kLocalCId, chan->local_cid());
1452 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1453 });
1454
1455 RETURN_IF_FATAL(RunUntilIdle());
1456
1457 RETURN_IF_FATAL(sig()->ReceiveExpect(
1458 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1459
1460 EXPECT_EQ(1, open_cb_count);
1461
1462 EXPECT_OUTBOUND_REQ(*sig(),
1463 kDisconnectionRequest,
1464 kDisconReq.view(),
1465 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1466 bool channel_close_cb_called = false;
1467 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1468 RETURN_IF_FATAL(RunUntilIdle());
1469 EXPECT_TRUE(channel_close_cb_called);
1470 }
1471
TEST_F(BrEdrDynamicChannelTest,OpenChannelConfigPending)1472 TEST_F(BrEdrDynamicChannelTest, OpenChannelConfigPending) {
1473 EXPECT_OUTBOUND_REQ(*sig(),
1474 kConnectionRequest,
1475 kConnReq.view(),
1476 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1477 EXPECT_OUTBOUND_REQ(
1478 *sig(),
1479 kConfigurationRequest,
1480 kOutboundConfigReq.view(),
1481 {SignalingChannel::Status::kSuccess,
1482 kOutboundEmptyPendingConfigRsp.view()},
1483 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1484
1485 int open_cb_count = 0;
1486 registry()->OpenOutbound(kPsm, kChannelParams, [&open_cb_count](auto chan) {
1487 open_cb_count++;
1488 ASSERT_TRUE(chan);
1489 EXPECT_EQ(kLocalCId, chan->local_cid());
1490 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1491 });
1492
1493 RETURN_IF_FATAL(RunUntilIdle());
1494
1495 RETURN_IF_FATAL(sig()->ReceiveExpect(
1496 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1497
1498 EXPECT_EQ(1, open_cb_count);
1499
1500 EXPECT_OUTBOUND_REQ(*sig(),
1501 kDisconnectionRequest,
1502 kDisconReq.view(),
1503 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1504 bool channel_close_cb_called = false;
1505 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1506 RETURN_IF_FATAL(RunUntilIdle());
1507 EXPECT_TRUE(channel_close_cb_called);
1508 }
1509
TEST_F(BrEdrDynamicChannelTest,OpenChannelRemoteDisconnectWhileConfiguring)1510 TEST_F(BrEdrDynamicChannelTest, OpenChannelRemoteDisconnectWhileConfiguring) {
1511 EXPECT_OUTBOUND_REQ(*sig(),
1512 kConnectionRequest,
1513 kConnReq.view(),
1514 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1515 auto config_id = EXPECT_OUTBOUND_REQ(
1516 *sig(), kConfigurationRequest, kOutboundConfigReq.view());
1517
1518 int open_cb_count = 0;
1519 registry()->OpenOutbound(kPsm, kChannelParams, [&open_cb_count](auto chan) {
1520 open_cb_count++;
1521 EXPECT_FALSE(chan);
1522 });
1523
1524 RETURN_IF_FATAL(RunUntilIdle());
1525
1526 RETURN_IF_FATAL(sig()->ReceiveExpect(
1527 kDisconnectionRequest, kInboundDisconReq, kInboundDisconRsp));
1528
1529 // Response handler should return false ("no more responses") when called, so
1530 // trigger single responses rather than a set of two.
1531 RETURN_IF_FATAL(
1532 sig()->ReceiveResponses(config_id,
1533 {{SignalingChannel::Status::kSuccess,
1534 kOutboundEmptyPendingConfigRsp.view()}}));
1535 RETURN_IF_FATAL(sig()->ReceiveResponses(
1536 config_id,
1537 {{SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()}}));
1538
1539 EXPECT_EQ(1, open_cb_count);
1540 }
1541
TEST_F(BrEdrDynamicChannelTest,ChannelIdNotReusedUntilDisconnectionCompletes)1542 TEST_F(BrEdrDynamicChannelTest, ChannelIdNotReusedUntilDisconnectionCompletes) {
1543 EXPECT_OUTBOUND_REQ(*sig(),
1544 kConnectionRequest,
1545 kConnReq.view(),
1546 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1547 EXPECT_OUTBOUND_REQ(
1548 *sig(),
1549 kConfigurationRequest,
1550 kOutboundConfigReq.view(),
1551 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1552 auto disconn_id =
1553 EXPECT_OUTBOUND_REQ(*sig(), kDisconnectionRequest, kDisconReq.view());
1554
1555 int open_cb_count = 0;
1556 auto open_cb = [&open_cb_count](auto chan) {
1557 ASSERT_TRUE(chan);
1558 open_cb_count++;
1559 };
1560
1561 int close_cb_count = 0;
1562 set_channel_close_cb([&close_cb_count](auto chan) {
1563 EXPECT_TRUE(chan);
1564 close_cb_count++;
1565 });
1566
1567 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1568
1569 RETURN_IF_FATAL(RunUntilIdle());
1570
1571 // Complete opening the channel.
1572 RETURN_IF_FATAL(sig()->ReceiveExpect(
1573 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1574
1575 EXPECT_EQ(1, open_cb_count);
1576 EXPECT_EQ(0, close_cb_count);
1577
1578 bool channel_close_cb_called = false;
1579 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1580 RETURN_IF_FATAL(RunUntilIdle());
1581
1582 // Disconnection Response hasn't been received yet so the second channel
1583 // should use a different channel ID.
1584 const StaticByteBuffer kSecondChannelConnReq(
1585 // PSM
1586 LowerBits(kPsm),
1587 UpperBits(kPsm),
1588
1589 // Source CID
1590 LowerBits(kLocalCId + 1),
1591 UpperBits(kLocalCId + 1));
1592
1593 EXPECT_OUTBOUND_REQ(*sig(), kConnectionRequest, kSecondChannelConnReq.view());
1594 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1595
1596 // CloseChannel callback hasn't been called either.
1597 EXPECT_FALSE(channel_close_cb_called);
1598
1599 // Complete the disconnection on the first channel.
1600 RETURN_IF_FATAL(sig()->ReceiveResponses(
1601 disconn_id, {{SignalingChannel::Status::kSuccess, kDisconRsp.view()}}));
1602
1603 EXPECT_TRUE(channel_close_cb_called);
1604
1605 // Now the first channel ID gets reused.
1606 EXPECT_OUTBOUND_REQ(*sig(), kConnectionRequest, kConnReq.view());
1607 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1608 }
1609
1610 // DisconnectDoneCallback is load-bearing as a signal for the registry to delete
1611 // a channel's state and recycle its channel ID, so test that it's called even
1612 // when the peer doesn't actually send a Disconnect Response.
TEST_F(BrEdrDynamicChannelTest,DisconnectDoneCallbackCalledAfterDisconnectResponseTimeOut)1613 TEST_F(BrEdrDynamicChannelTest,
1614 DisconnectDoneCallbackCalledAfterDisconnectResponseTimeOut) {
1615 EXPECT_OUTBOUND_REQ(*sig(),
1616 kConnectionRequest,
1617 kConnReq.view(),
1618 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1619 EXPECT_OUTBOUND_REQ(
1620 *sig(),
1621 kConfigurationRequest,
1622 kOutboundConfigReq.view(),
1623 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1624
1625 // Build channel and operate it directly to be able to disconnect it.
1626 auto channel = BrEdrDynamicChannel::MakeOutbound(
1627 registry(), sig(), kPsm, kLocalCId, kChannelParams, false);
1628 ASSERT_TRUE(channel);
1629 channel->Open([] {});
1630
1631 RETURN_IF_FATAL(RunUntilIdle());
1632
1633 EXPECT_TRUE(channel->IsConnected());
1634
1635 EXPECT_OUTBOUND_REQ(*sig(),
1636 kDisconnectionRequest,
1637 kDisconReq.view(),
1638 {SignalingChannel::Status::kTimeOut, BufferView()});
1639
1640 bool disconnect_done_cb_called = false;
1641 channel->Disconnect(
1642 [&disconnect_done_cb_called] { disconnect_done_cb_called = true; });
1643 EXPECT_FALSE(channel->IsConnected());
1644
1645 RETURN_IF_FATAL(RunUntilIdle());
1646
1647 EXPECT_TRUE(disconnect_done_cb_called);
1648 }
1649
TEST_F(BrEdrDynamicChannelTest,OpenChannelConfigWrongId)1650 TEST_F(BrEdrDynamicChannelTest, OpenChannelConfigWrongId) {
1651 EXPECT_OUTBOUND_REQ(*sig(),
1652 kConnectionRequest,
1653 kConnReq.view(),
1654 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1655 EXPECT_OUTBOUND_REQ(
1656 *sig(),
1657 kConfigurationRequest,
1658 kOutboundConfigReq.view(),
1659 {SignalingChannel::Status::kSuccess, kUnknownIdConfigRsp.view()});
1660 EXPECT_OUTBOUND_REQ(*sig(),
1661 kDisconnectionRequest,
1662 kDisconReq.view(),
1663 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1664
1665 int open_cb_count = 0;
1666 registry()->OpenOutbound(kPsm, kChannelParams, [&open_cb_count](auto chan) {
1667 open_cb_count++;
1668 EXPECT_FALSE(chan);
1669 });
1670
1671 RETURN_IF_FATAL(RunUntilIdle());
1672
1673 RETURN_IF_FATAL(sig()->ReceiveExpectRejectInvalidChannelId(
1674 kConfigurationRequest, kInboundConfigReq, kLocalCId, kInvalidChannelId));
1675
1676 EXPECT_EQ(1, open_cb_count);
1677 }
1678
TEST_F(BrEdrDynamicChannelTest,InboundConnectionOk)1679 TEST_F(BrEdrDynamicChannelTest, InboundConnectionOk) {
1680 EXPECT_OUTBOUND_REQ(
1681 *sig(),
1682 kConfigurationRequest,
1683 kOutboundConfigReq.view(),
1684 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1685 EXPECT_OUTBOUND_REQ(*sig(),
1686 kDisconnectionRequest,
1687 kDisconReq.view(),
1688 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1689
1690 int open_cb_count = 0;
1691 DynamicChannelCallback open_cb = [&open_cb_count](auto chan) {
1692 open_cb_count++;
1693 ASSERT_TRUE(chan);
1694 EXPECT_EQ(kPsm, chan->psm());
1695 EXPECT_EQ(kLocalCId, chan->local_cid());
1696 EXPECT_EQ(kRemoteCId, chan->remote_cid());
1697 };
1698
1699 int service_request_cb_count = 0;
1700 auto service_request_cb = [&service_request_cb_count,
1701 open_cb = std::move(open_cb)](Psm psm) mutable
1702 -> std::optional<DynamicChannelRegistry::ServiceInfo> {
1703 service_request_cb_count++;
1704 EXPECT_EQ(kPsm, psm);
1705 if (psm == kPsm) {
1706 return DynamicChannelRegistry::ServiceInfo(kChannelParams,
1707 open_cb.share());
1708 }
1709 return std::nullopt;
1710 };
1711
1712 set_service_request_cb(std::move(service_request_cb));
1713
1714 int close_cb_count = 0;
1715 set_channel_close_cb([&close_cb_count](auto) { close_cb_count++; });
1716
1717 RETURN_IF_FATAL(sig()->ReceiveExpect(
1718 kConnectionRequest, kInboundConnReq, kInboundOkConnRsp));
1719 RETURN_IF_FATAL(RunUntilIdle());
1720
1721 EXPECT_EQ(1, service_request_cb_count);
1722 EXPECT_EQ(0, open_cb_count);
1723
1724 RETURN_IF_FATAL(sig()->ReceiveExpect(
1725 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1726
1727 EXPECT_EQ(1, service_request_cb_count);
1728 EXPECT_EQ(1, open_cb_count);
1729
1730 registry()->CloseChannel(kLocalCId, [] {});
1731 EXPECT_EQ(0, close_cb_count);
1732 }
1733
TEST_F(BrEdrDynamicChannelTest,InboundConnectionRemoteDisconnectWhileConfiguring)1734 TEST_F(BrEdrDynamicChannelTest,
1735 InboundConnectionRemoteDisconnectWhileConfiguring) {
1736 auto config_id = EXPECT_OUTBOUND_REQ(
1737 *sig(), kConfigurationRequest, kOutboundConfigReq.view());
1738
1739 int open_cb_count = 0;
1740 DynamicChannelCallback open_cb = [&open_cb_count](auto) {
1741 open_cb_count++;
1742 FAIL() << "Failed-to-open inbound channels shouldn't trip open callback";
1743 };
1744
1745 int service_request_cb_count = 0;
1746 auto service_request_cb = [&service_request_cb_count,
1747 open_cb = std::move(open_cb)](Psm psm) mutable
1748 -> std::optional<DynamicChannelRegistry::ServiceInfo> {
1749 service_request_cb_count++;
1750 EXPECT_EQ(kPsm, psm);
1751 if (psm == kPsm) {
1752 return DynamicChannelRegistry::ServiceInfo(kChannelParams,
1753 open_cb.share());
1754 }
1755 return std::nullopt;
1756 };
1757
1758 set_service_request_cb(std::move(service_request_cb));
1759
1760 RETURN_IF_FATAL(sig()->ReceiveExpect(
1761 kConnectionRequest, kInboundConnReq, kInboundOkConnRsp));
1762 RunUntilIdle();
1763
1764 EXPECT_EQ(1, service_request_cb_count);
1765 EXPECT_EQ(0, open_cb_count);
1766
1767 RETURN_IF_FATAL(sig()->ReceiveExpect(
1768 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
1769 RETURN_IF_FATAL(sig()->ReceiveExpect(
1770 kDisconnectionRequest, kInboundDisconReq, kInboundDisconRsp));
1771
1772 // Drop response received after the channel is disconnected.
1773 RETURN_IF_FATAL(sig()->ReceiveResponses(
1774 config_id,
1775 {{SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()}}));
1776
1777 EXPECT_EQ(1, service_request_cb_count);
1778
1779 // Channel that failed to open shouldn't have triggered channel open callback.
1780 EXPECT_EQ(0, open_cb_count);
1781 }
1782
TEST_F(BrEdrDynamicChannelTest,InboundConnectionInvalidPsm)1783 TEST_F(BrEdrDynamicChannelTest, InboundConnectionInvalidPsm) {
1784 auto service_request_cb =
1785 [](Psm psm) -> std::optional<DynamicChannelRegistry::ServiceInfo> {
1786 // Write user code that accepts the invalid PSM, but control flow may not
1787 // reach here.
1788 EXPECT_EQ(kInvalidPsm, psm);
1789 if (psm == kInvalidPsm) {
1790 return DynamicChannelRegistry::ServiceInfo(
1791 kChannelParams, [](auto /*unused*/) {
1792 FAIL() << "Channel should fail to open for PSM";
1793 });
1794 }
1795 return std::nullopt;
1796 };
1797
1798 set_service_request_cb(std::move(service_request_cb));
1799
1800 RETURN_IF_FATAL(sig()->ReceiveExpect(
1801 kConnectionRequest, kInboundInvalidPsmConnReq, kInboundBadPsmConnRsp));
1802 RunUntilIdle();
1803 }
1804
TEST_F(BrEdrDynamicChannelTest,InboundConnectionUnsupportedPsm)1805 TEST_F(BrEdrDynamicChannelTest, InboundConnectionUnsupportedPsm) {
1806 int service_request_cb_count = 0;
1807 auto service_request_cb =
1808 [&service_request_cb_count](
1809 Psm psm) -> std::optional<DynamicChannelRegistry::ServiceInfo> {
1810 service_request_cb_count++;
1811 EXPECT_EQ(kPsm, psm);
1812
1813 // Reject the service request.
1814 return std::nullopt;
1815 };
1816
1817 set_service_request_cb(std::move(service_request_cb));
1818
1819 RETURN_IF_FATAL(sig()->ReceiveExpect(
1820 kConnectionRequest, kInboundConnReq, kInboundBadPsmConnRsp));
1821 RunUntilIdle();
1822
1823 EXPECT_EQ(1, service_request_cb_count);
1824 }
1825
TEST_F(BrEdrDynamicChannelTest,InboundConnectionInvalidSrcCId)1826 TEST_F(BrEdrDynamicChannelTest, InboundConnectionInvalidSrcCId) {
1827 auto service_request_cb =
1828 [](Psm psm) -> std::optional<DynamicChannelRegistry::ServiceInfo> {
1829 // Control flow may not reach here.
1830 EXPECT_EQ(kPsm, psm);
1831 if (psm == kPsm) {
1832 return DynamicChannelRegistry::ServiceInfo(
1833 kChannelParams, [](auto /*unused*/) {
1834 FAIL() << "Channel from src_cid should fail to open";
1835 });
1836 }
1837 return std::nullopt;
1838 };
1839
1840 set_service_request_cb(std::move(service_request_cb));
1841
1842 RETURN_IF_FATAL(sig()->ReceiveExpect(
1843 kConnectionRequest, kInboundBadCIdConnReq, kInboundBadCIdConnRsp));
1844 RunUntilIdle();
1845 }
1846
TEST_F(BrEdrDynamicChannelTest,RejectConfigReqWithUnknownOptions)1847 TEST_F(BrEdrDynamicChannelTest, RejectConfigReqWithUnknownOptions) {
1848 EXPECT_OUTBOUND_REQ(*sig(),
1849 kConnectionRequest,
1850 kConnReq.view(),
1851 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1852 EXPECT_OUTBOUND_REQ(
1853 *sig(),
1854 kConfigurationRequest,
1855 kOutboundConfigReq.view(),
1856 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1857
1858 size_t open_cb_count = 0;
1859 auto open_cb = [&open_cb_count](auto chan) {
1860 EXPECT_FALSE(chan);
1861 open_cb_count++;
1862 };
1863
1864 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
1865
1866 RETURN_IF_FATAL(RunUntilIdle());
1867
1868 const StaticByteBuffer kInboundConfigReqUnknownOption(
1869 // Destination CID
1870 LowerBits(kLocalCId),
1871 UpperBits(kLocalCId),
1872
1873 // Flags
1874 0x00,
1875 0x00,
1876
1877 // Unknown Option: Type, Length, Data
1878 0x70,
1879 0x01,
1880 0x02);
1881
1882 const StaticByteBuffer kOutboundConfigRspUnknownOption(
1883 // Source CID
1884 LowerBits(kRemoteCId),
1885 UpperBits(kRemoteCId),
1886
1887 // Flags
1888 0x00,
1889 0x00,
1890
1891 // Result (Failure - unknown options)
1892 0x03,
1893 0x00,
1894
1895 // Unknown Option: Type, Length, Data
1896 0x70,
1897 0x01,
1898 0x02);
1899
1900 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
1901 kInboundConfigReqUnknownOption,
1902 kOutboundConfigRspUnknownOption));
1903
1904 EXPECT_EQ(0u, open_cb_count);
1905
1906 RunUntilIdle();
1907 }
1908
TEST_F(BrEdrDynamicChannelTest,ClampErtmChannelInfoMaxTxSduSizeToMaxPduPayloadSize)1909 TEST_F(BrEdrDynamicChannelTest,
1910 ClampErtmChannelInfoMaxTxSduSizeToMaxPduPayloadSize) {
1911 EXPECT_OUTBOUND_REQ(*sig(),
1912 kConnectionRequest,
1913 kConnReq.view(),
1914 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1915 EXPECT_OUTBOUND_REQ(
1916 *sig(),
1917 kConfigurationRequest,
1918 kOutboundConfigReqWithErtm.view(),
1919 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1920
1921 const auto kPeerMps = 1024;
1922 const auto kPeerMtu = kPeerMps + 1;
1923 bool channel_opened = false;
1924 auto open_cb = [&](auto chan) {
1925 channel_opened = true;
1926 ASSERT_TRUE(chan);
1927 EXPECT_TRUE(chan->IsOpen());
1928 EXPECT_EQ(kLocalCId, chan->local_cid());
1929
1930 // Note that max SDU size is the peer's MPS because it's smaller than its
1931 // MTU.
1932 EXPECT_EQ(kPeerMps, chan->info().max_tx_sdu_size);
1933 };
1934
1935 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
1936
1937 sig()->ReceiveResponses(ext_info_transaction_id(),
1938 {{SignalingChannel::Status::kSuccess,
1939 kExtendedFeaturesInfoRspWithERTM.view()}});
1940
1941 RETURN_IF_FATAL(RunUntilIdle());
1942
1943 const auto inbound_config_req = MakeConfigReqWithMtuAndRfc(
1944 kLocalCId,
1945 kPeerMtu,
1946 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
1947 kErtmNFramesInTxWindow,
1948 kErtmMaxTransmissions,
1949 0,
1950 0,
1951 kPeerMps);
1952 const auto kOutboundConfigRsp = MakeConfigRspWithMtuAndRfc(
1953 kRemoteCId,
1954 ConfigurationResult::kSuccess,
1955 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
1956 kPeerMtu,
1957 kErtmNFramesInTxWindow,
1958 kErtmMaxTransmissions,
1959 2000,
1960 12000,
1961 kPeerMps);
1962
1963 RETURN_IF_FATAL(sig()->ReceiveExpect(
1964 kConfigurationRequest, inbound_config_req, kOutboundConfigRsp));
1965
1966 EXPECT_TRUE(channel_opened);
1967
1968 EXPECT_OUTBOUND_REQ(*sig(),
1969 kDisconnectionRequest,
1970 kDisconReq.view(),
1971 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
1972 bool channel_close_cb_called = false;
1973 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
1974 RETURN_IF_FATAL(RunUntilIdle());
1975 EXPECT_TRUE(channel_close_cb_called);
1976 }
1977
1978 struct ReceiveMtuTestParams {
1979 std::optional<uint16_t> request_mtu;
1980 uint16_t response_mtu;
1981 ConfigurationResult response_status;
1982 };
1983 class ReceivedMtuTest
1984 : public BrEdrDynamicChannelTest,
1985 public ::testing::WithParamInterface<ReceiveMtuTestParams> {};
1986
TEST_P(ReceivedMtuTest,ResponseMtuAndStatus)1987 TEST_P(ReceivedMtuTest, ResponseMtuAndStatus) {
1988 EXPECT_OUTBOUND_REQ(*sig(),
1989 kConnectionRequest,
1990 kConnReq.view(),
1991 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
1992 EXPECT_OUTBOUND_REQ(
1993 *sig(),
1994 kConfigurationRequest,
1995 kOutboundConfigReq.view(),
1996 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
1997
1998 bool channel_opened = false;
1999 auto open_cb = [&](auto chan) {
2000 channel_opened = true;
2001 ASSERT_TRUE(chan);
2002 EXPECT_TRUE(chan->IsOpen());
2003 EXPECT_EQ(kLocalCId, chan->local_cid());
2004 EXPECT_EQ(chan->info().max_tx_sdu_size, GetParam().response_mtu);
2005 };
2006
2007 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2008
2009 RETURN_IF_FATAL(RunUntilIdle());
2010
2011 const auto kOutboundConfigRsp = MakeConfigRspWithMtu(
2012 kRemoteCId, GetParam().response_mtu, GetParam().response_status);
2013
2014 if (GetParam().request_mtu) {
2015 RETURN_IF_FATAL(sig()->ReceiveExpect(
2016 kConfigurationRequest,
2017 MakeConfigReqWithMtu(kLocalCId, *GetParam().request_mtu),
2018 kOutboundConfigRsp));
2019 } else {
2020 RETURN_IF_FATAL(sig()->ReceiveExpect(
2021 kConfigurationRequest, kInboundConfigReq, kOutboundConfigRsp));
2022 }
2023
2024 EXPECT_EQ(GetParam().response_status == ConfigurationResult::kSuccess,
2025 channel_opened);
2026
2027 EXPECT_OUTBOUND_REQ(*sig(),
2028 kDisconnectionRequest,
2029 kDisconReq.view(),
2030 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2031 bool channel_close_cb_called = false;
2032 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2033 RETURN_IF_FATAL(RunUntilIdle());
2034 EXPECT_TRUE(channel_close_cb_called);
2035 }
2036
2037 INSTANTIATE_TEST_SUITE_P(
2038 BrEdrDynamicChannelTest,
2039 ReceivedMtuTest,
2040 ::testing::Values(
2041 ReceiveMtuTestParams{
2042 std::nullopt, kDefaultMTU, ConfigurationResult::kSuccess},
2043 ReceiveMtuTestParams{
2044 kMinACLMTU, kMinACLMTU, ConfigurationResult::kSuccess},
2045 ReceiveMtuTestParams{kMinACLMTU - 1,
2046 kMinACLMTU,
2047 ConfigurationResult::kUnacceptableParameters},
2048 ReceiveMtuTestParams{
2049 kDefaultMTU + 1, kDefaultMTU + 1, ConfigurationResult::kSuccess}));
2050
2051 class ConfigRspWithMtuTest : public BrEdrDynamicChannelTest,
2052 public ::testing::WithParamInterface<
2053 std::optional<uint16_t> /*response mtu*/> {};
2054
TEST_P(ConfigRspWithMtuTest,ConfiguredLocalMtu)2055 TEST_P(ConfigRspWithMtuTest, ConfiguredLocalMtu) {
2056 const auto kExpectedConfiguredLocalMtu = GetParam() ? *GetParam() : kMaxMTU;
2057
2058 EXPECT_OUTBOUND_REQ(*sig(),
2059 kConnectionRequest,
2060 kConnReq.view(),
2061 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2062
2063 const auto kInboundConfigRspWithParamMtu =
2064 MakeConfigRspWithMtu(kLocalCId, GetParam() ? *GetParam() : 0);
2065 if (GetParam()) {
2066 EXPECT_OUTBOUND_REQ(*sig(),
2067 kConfigurationRequest,
2068 kOutboundConfigReq.view(),
2069 {SignalingChannel::Status::kSuccess,
2070 kInboundConfigRspWithParamMtu.view()});
2071 } else {
2072 EXPECT_OUTBOUND_REQ(
2073 *sig(),
2074 kConfigurationRequest,
2075 kOutboundConfigReq.view(),
2076 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2077 }
2078
2079 size_t open_cb_count = 0;
2080 auto open_cb = [&](auto chan) {
2081 EXPECT_TRUE(chan->IsOpen());
2082 EXPECT_EQ(kLocalCId, chan->local_cid());
2083 EXPECT_EQ(kExpectedConfiguredLocalMtu, chan->info().max_rx_sdu_size);
2084 open_cb_count++;
2085 };
2086 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2087
2088 RETURN_IF_FATAL(RunUntilIdle());
2089
2090 RETURN_IF_FATAL(sig()->ReceiveExpect(
2091 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2092
2093 EXPECT_EQ(1u, open_cb_count);
2094
2095 EXPECT_OUTBOUND_REQ(*sig(),
2096 kDisconnectionRequest,
2097 kDisconReq.view(),
2098 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2099 bool channel_close_cb_called = false;
2100 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2101 RETURN_IF_FATAL(RunUntilIdle());
2102 EXPECT_TRUE(channel_close_cb_called);
2103 }
2104
TEST_P(ConfigRspWithMtuTest,ConfiguredLocalMtuWithPendingRsp)2105 TEST_P(ConfigRspWithMtuTest, ConfiguredLocalMtuWithPendingRsp) {
2106 const auto kExpectedConfiguredLocalMtu = GetParam() ? *GetParam() : kMaxMTU;
2107
2108 EXPECT_OUTBOUND_REQ(*sig(),
2109 kConnectionRequest,
2110 kConnReq.view(),
2111 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2112
2113 const auto kInboundPendingConfigRspWithMtu = MakeConfigRspWithMtu(
2114 kLocalCId, GetParam() ? *GetParam() : 0, ConfigurationResult::kPending);
2115 if (GetParam()) {
2116 EXPECT_OUTBOUND_REQ(
2117 *sig(),
2118 kConfigurationRequest,
2119 kOutboundConfigReq.view(),
2120 {SignalingChannel::Status::kSuccess,
2121 kInboundPendingConfigRspWithMtu.view()},
2122 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2123 } else {
2124 EXPECT_OUTBOUND_REQ(
2125 *sig(),
2126 kConfigurationRequest,
2127 kOutboundConfigReq.view(),
2128 {SignalingChannel::Status::kSuccess,
2129 kInboundEmptyPendingConfigRsp.view()},
2130 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2131 }
2132
2133 size_t open_cb_count = 0;
2134 auto open_cb = [&](auto chan) {
2135 EXPECT_TRUE(chan->IsOpen());
2136 EXPECT_EQ(kLocalCId, chan->local_cid());
2137 EXPECT_EQ(kExpectedConfiguredLocalMtu, chan->info().max_rx_sdu_size);
2138 open_cb_count++;
2139 };
2140 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2141
2142 RETURN_IF_FATAL(RunUntilIdle());
2143
2144 RETURN_IF_FATAL(sig()->ReceiveExpect(
2145 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2146
2147 EXPECT_EQ(1u, open_cb_count);
2148
2149 EXPECT_OUTBOUND_REQ(*sig(),
2150 kDisconnectionRequest,
2151 kDisconReq.view(),
2152 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2153 bool channel_close_cb_called = false;
2154 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2155 RETURN_IF_FATAL(RunUntilIdle());
2156 EXPECT_TRUE(channel_close_cb_called);
2157 }
2158
2159 INSTANTIATE_TEST_SUITE_P(BrEdrDynamicChannelTest,
2160 ConfigRspWithMtuTest,
2161 ::testing::Values(std::nullopt, kMinACLMTU));
2162
TEST_F(BrEdrDynamicChannelTest,RespondsToInboundExtendedFeaturesRequest)2163 TEST_F(BrEdrDynamicChannelTest, RespondsToInboundExtendedFeaturesRequest) {
2164 const auto kExpectedExtendedFeatures =
2165 kExtendedFeaturesBitFixedChannels | kExtendedFeaturesBitFCSOption |
2166 kExtendedFeaturesBitEnhancedRetransmission;
2167 const auto kExpectedExtendedFeaturesInfoRsp = MakeExtendedFeaturesInfoRsp(
2168 InformationResult::kSuccess, kExpectedExtendedFeatures);
2169 sig()->ReceiveExpect(kInformationRequest,
2170 kExtendedFeaturesInfoReq,
2171 kExpectedExtendedFeaturesInfoRsp);
2172 }
2173
TEST_F(BrEdrDynamicChannelTest,ExtendedFeaturesResponseSaved)2174 TEST_F(BrEdrDynamicChannelTest, ExtendedFeaturesResponseSaved) {
2175 const auto kExpectedExtendedFeatures =
2176 kExtendedFeaturesBitFixedChannels | kExtendedFeaturesBitFCSOption |
2177 kExtendedFeaturesBitEnhancedRetransmission;
2178 const auto kInfoRsp = MakeExtendedFeaturesInfoRsp(InformationResult::kSuccess,
2179 kExpectedExtendedFeatures);
2180
2181 EXPECT_FALSE(registry()->extended_features());
2182
2183 sig()->ReceiveResponses(
2184 ext_info_transaction_id(),
2185 {{SignalingChannel::Status::kSuccess, kInfoRsp.view()}});
2186 EXPECT_TRUE(registry()->extended_features());
2187 EXPECT_EQ(kExpectedExtendedFeatures, *registry()->extended_features());
2188 }
2189
TEST_F(BrEdrDynamicChannelTest,ExtendedFeaturesResponseInvalidFailureResult)2190 TEST_F(BrEdrDynamicChannelTest, ExtendedFeaturesResponseInvalidFailureResult) {
2191 constexpr auto kResult = static_cast<InformationResult>(0xFFFF);
2192 const auto kInfoRsp = MakeExtendedFeaturesInfoRsp(kResult);
2193
2194 EXPECT_FALSE(registry()->extended_features());
2195
2196 sig()->ReceiveResponses(
2197 ext_info_transaction_id(),
2198 {{SignalingChannel::Status::kSuccess, kInfoRsp.view()}});
2199 EXPECT_FALSE(registry()->extended_features());
2200 }
2201
2202 class InformationResultTest
2203 : public BrEdrDynamicChannelTest,
2204 public ::testing::WithParamInterface<InformationResult> {};
2205
TEST_P(InformationResultTest,ERTMChannelWaitsForExtendedFeaturesResultBeforeFallingBackToBasicModeAndStartingConfigFlow)2206 TEST_P(
2207 InformationResultTest,
2208 ERTMChannelWaitsForExtendedFeaturesResultBeforeFallingBackToBasicModeAndStartingConfigFlow) {
2209 EXPECT_OUTBOUND_REQ(*sig(),
2210 kConnectionRequest,
2211 kConnReq.view(),
2212 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2213
2214 size_t open_cb_count = 0;
2215 auto open_cb = [&open_cb_count](auto chan) {
2216 EXPECT_EQ(kLocalCId, chan->local_cid());
2217 open_cb_count++;
2218 };
2219
2220 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
2221
2222 // Config request should not be sent.
2223 RETURN_IF_FATAL(RunUntilIdle());
2224
2225 EXPECT_OUTBOUND_REQ(
2226 *sig(),
2227 kConfigurationRequest,
2228 kOutboundConfigReq.view(),
2229 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2230
2231 const auto extended_features_info_rsp =
2232 MakeExtendedFeaturesInfoRsp(GetParam());
2233 sig()->ReceiveResponses(ext_info_transaction_id(),
2234 {{SignalingChannel::Status::kSuccess,
2235 extended_features_info_rsp.view()}});
2236
2237 RunUntilIdle();
2238
2239 RETURN_IF_FATAL(sig()->ReceiveExpect(
2240 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2241
2242 // Config should have been sent, so channel should be open.
2243 EXPECT_EQ(1u, open_cb_count);
2244
2245 EXPECT_OUTBOUND_REQ(*sig(),
2246 kDisconnectionRequest,
2247 kDisconReq.view(),
2248 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2249 bool channel_close_cb_called = false;
2250 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2251 RETURN_IF_FATAL(RunUntilIdle());
2252 EXPECT_TRUE(channel_close_cb_called);
2253 }
2254
2255 INSTANTIATE_TEST_SUITE_P(
2256 BrEdrDynamicChannelTest,
2257 InformationResultTest,
2258 ::testing::Values(InformationResult::kSuccess,
2259 InformationResult::kNotSupported,
2260 static_cast<InformationResult>(0xFFFF)));
2261
TEST_F(BrEdrDynamicChannelTest,ERTChannelDoesNotSendConfigReqBeforeConnRspReceived)2262 TEST_F(BrEdrDynamicChannelTest,
2263 ERTChannelDoesNotSendConfigReqBeforeConnRspReceived) {
2264 auto conn_id =
2265 EXPECT_OUTBOUND_REQ(*sig(), kConnectionRequest, kConnReq.view(), {});
2266
2267 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2268
2269 RETURN_IF_FATAL(RunUntilIdle());
2270
2271 // Channel will be notified that extended features received.
2272 sig()->ReceiveResponses(
2273 ext_info_transaction_id(),
2274 {{SignalingChannel::Status::kSuccess, kExtendedFeaturesInfoRsp.view()}});
2275
2276 // Config request should not be sent before connection response received.
2277 RunUntilIdle();
2278
2279 EXPECT_OUTBOUND_REQ(
2280 *sig(),
2281 kConfigurationRequest,
2282 kOutboundConfigReq.view(),
2283 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2284 sig()->ReceiveResponses(
2285 conn_id, {{SignalingChannel::Status::kSuccess, kOkConnRsp.view()}});
2286 RunUntilIdle();
2287
2288 EXPECT_OUTBOUND_REQ(*sig(),
2289 kDisconnectionRequest,
2290 kDisconReq.view(),
2291 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2292 bool channel_close_cb_called = false;
2293 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2294 RETURN_IF_FATAL(RunUntilIdle());
2295 EXPECT_TRUE(channel_close_cb_called);
2296 }
2297
TEST_F(BrEdrDynamicChannelTest,SendAndReceiveERTMConfigReq)2298 TEST_F(BrEdrDynamicChannelTest, SendAndReceiveERTMConfigReq) {
2299 constexpr uint16_t kPreferredMtu = kDefaultMTU + 1;
2300 const auto kExpectedOutboundConfigReq = MakeConfigReqWithMtuAndRfc(
2301 kRemoteCId,
2302 kPreferredMtu,
2303 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2304 kErtmMaxUnackedInboundFrames,
2305 kErtmMaxInboundRetransmissions,
2306 0,
2307 0,
2308 kMaxInboundPduPayloadSize);
2309
2310 EXPECT_OUTBOUND_REQ(*sig(),
2311 kConnectionRequest,
2312 kConnReq.view(),
2313 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2314 EXPECT_OUTBOUND_REQ(
2315 *sig(),
2316 kConfigurationRequest,
2317 kExpectedOutboundConfigReq.view(),
2318 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2319
2320 int open_cb_count = 0;
2321 auto open_cb = [kPreferredMtu, &open_cb_count](const DynamicChannel* chan) {
2322 if (open_cb_count == 0) {
2323 ASSERT_TRUE(chan);
2324 EXPECT_TRUE(chan->IsOpen());
2325 EXPECT_EQ(kLocalCId, chan->local_cid());
2326
2327 // Check values of ChannelInfo fields.
2328 EXPECT_EQ(RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2329 chan->info().mode);
2330
2331 // Receive capability even under ERTM is based on MTU option, not on the
2332 // MPS in R&FC option.
2333 EXPECT_EQ(kPreferredMtu, chan->info().max_rx_sdu_size);
2334
2335 // Inbound request has no MTU option, so the peer's receive capability is
2336 // the default.
2337 EXPECT_EQ(kDefaultMTU, chan->info().max_tx_sdu_size);
2338
2339 // These values should match the contents of kInboundConfigReqWithERTM.
2340 EXPECT_EQ(kErtmNFramesInTxWindow, chan->info().n_frames_in_tx_window);
2341 EXPECT_EQ(kErtmMaxTransmissions, chan->info().max_transmissions);
2342 EXPECT_EQ(kMaxTxPduPayloadSize, chan->info().max_tx_pdu_payload_size);
2343 }
2344 open_cb_count++;
2345 };
2346
2347 registry()->OpenOutbound(
2348 kPsm,
2349 {RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2350 kPreferredMtu,
2351 std::nullopt},
2352 std::move(open_cb));
2353
2354 RETURN_IF_FATAL(RunUntilIdle());
2355
2356 sig()->ReceiveResponses(ext_info_transaction_id(),
2357 {{SignalingChannel::Status::kSuccess,
2358 kExtendedFeaturesInfoRspWithERTM.view()}});
2359
2360 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
2361 kInboundConfigReqWithERTM,
2362 kOutboundOkConfigRspWithErtm));
2363
2364 RunUntilIdle();
2365 EXPECT_EQ(1, open_cb_count);
2366
2367 EXPECT_OUTBOUND_REQ(*sig(),
2368 kDisconnectionRequest,
2369 kDisconReq.view(),
2370 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2371 bool channel_close_cb_called = false;
2372 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2373 RETURN_IF_FATAL(RunUntilIdle());
2374 EXPECT_TRUE(channel_close_cb_called);
2375 }
2376
2377 // When the peer rejects ERTM with the result Unacceptable Parameters and the
2378 // R&FC option specifying basic mode, the local device should send a new request
2379 // with basic mode. When the peer then requests basic mode, it should be
2380 // accepted. PTS: L2CAP/CMC/BV-03-C
TEST_F(BrEdrDynamicChannelTest,PeerRejectsERTM)2381 TEST_F(BrEdrDynamicChannelTest, PeerRejectsERTM) {
2382 EXPECT_OUTBOUND_REQ(*sig(),
2383 kConnectionRequest,
2384 kConnReq.view(),
2385 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2386 EXPECT_OUTBOUND_REQ(*sig(),
2387 kConfigurationRequest,
2388 kOutboundConfigReqWithErtm.view(),
2389 {SignalingChannel::Status::kSuccess,
2390 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2391 EXPECT_OUTBOUND_REQ(
2392 *sig(),
2393 kConfigurationRequest,
2394 kOutboundConfigReq.view(),
2395 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2396
2397 int open_cb_count = 0;
2398 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2399 if (open_cb_count == 0) {
2400 ASSERT_TRUE(chan);
2401 EXPECT_TRUE(chan->IsOpen());
2402 EXPECT_EQ(kLocalCId, chan->local_cid());
2403 }
2404 open_cb_count++;
2405 };
2406
2407 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
2408
2409 RETURN_IF_FATAL(RunUntilIdle());
2410
2411 sig()->ReceiveResponses(ext_info_transaction_id(),
2412 {{SignalingChannel::Status::kSuccess,
2413 kExtendedFeaturesInfoRspWithERTM.view()}});
2414
2415 RETURN_IF_FATAL(sig()->ReceiveExpect(
2416 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2417
2418 RunUntilIdle();
2419 EXPECT_EQ(1, open_cb_count);
2420
2421 EXPECT_OUTBOUND_REQ(*sig(),
2422 kDisconnectionRequest,
2423 kDisconReq.view(),
2424 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2425 bool channel_close_cb_called = false;
2426 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2427 RETURN_IF_FATAL(RunUntilIdle());
2428 EXPECT_TRUE(channel_close_cb_called);
2429 }
2430
2431 // Local device that prefers ERTM will renegotiate channel mode to basic mode
2432 // after peer negotiates basic mode and rejects ERTM. PTS: L2CAP/CMC/BV-07-C
TEST_F(BrEdrDynamicChannelTest,RenegotiateChannelModeAfterPeerRequestsBasicModeAndRejectsERTM)2433 TEST_F(BrEdrDynamicChannelTest,
2434 RenegotiateChannelModeAfterPeerRequestsBasicModeAndRejectsERTM) {
2435 EXPECT_OUTBOUND_REQ(*sig(),
2436 kConnectionRequest,
2437 kConnReq.view(),
2438 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2439 auto config_req_id = EXPECT_OUTBOUND_REQ(
2440 *sig(), kConfigurationRequest, kOutboundConfigReqWithErtm.view());
2441
2442 int open_cb_count = 0;
2443 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2444 if (open_cb_count == 0) {
2445 ASSERT_TRUE(chan);
2446 EXPECT_TRUE(chan->IsOpen());
2447 EXPECT_EQ(kLocalCId, chan->local_cid());
2448 }
2449 open_cb_count++;
2450 };
2451
2452 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
2453
2454 RunUntilIdle();
2455
2456 sig()->ReceiveResponses(ext_info_transaction_id(),
2457 {{SignalingChannel::Status::kSuccess,
2458 kExtendedFeaturesInfoRspWithERTM.view()}});
2459 RunUntilIdle();
2460
2461 // Peer requests basic mode.
2462 RETURN_IF_FATAL(sig()->ReceiveExpect(
2463 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2464 RunUntilIdle();
2465
2466 // New config request requesting basic mode should be sent in response to
2467 // unacceptable params response.
2468 EXPECT_OUTBOUND_REQ(
2469 *sig(),
2470 kConfigurationRequest,
2471 kOutboundConfigReq.view(),
2472 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2473 sig()->ReceiveResponses(
2474 config_req_id,
2475 {{SignalingChannel::Status::kSuccess,
2476 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()}});
2477
2478 RunUntilIdle();
2479 EXPECT_EQ(1, open_cb_count);
2480
2481 EXPECT_OUTBOUND_REQ(*sig(),
2482 kDisconnectionRequest,
2483 kDisconReq.view(),
2484 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2485 bool channel_close_cb_called = false;
2486 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2487 RETURN_IF_FATAL(RunUntilIdle());
2488 EXPECT_TRUE(channel_close_cb_called);
2489 }
2490
2491 // The local device should configure basic mode if peer does not indicate
2492 // support for ERTM when it is preferred. PTS: L2CAP/CMC/BV-10-C
TEST_F(BrEdrDynamicChannelTest,PreferredModeIsERTMButERTMIsNotInPeerFeatureMask)2493 TEST_F(BrEdrDynamicChannelTest,
2494 PreferredModeIsERTMButERTMIsNotInPeerFeatureMask) {
2495 EXPECT_OUTBOUND_REQ(*sig(),
2496 kConnectionRequest,
2497 kConnReq.view(),
2498 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2499 EXPECT_OUTBOUND_REQ(
2500 *sig(),
2501 kConfigurationRequest,
2502 kOutboundConfigReq.view(),
2503 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2504
2505 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2506
2507 RETURN_IF_FATAL(RunUntilIdle());
2508
2509 // Receive features mask without ERTM bit set.
2510 sig()->ReceiveResponses(
2511 ext_info_transaction_id(),
2512 {{SignalingChannel::Status::kSuccess, kExtendedFeaturesInfoRsp.view()}});
2513
2514 EXPECT_OUTBOUND_REQ(*sig(),
2515 kDisconnectionRequest,
2516 kDisconReq.view(),
2517 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2518 bool channel_close_cb_called = false;
2519 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2520 RETURN_IF_FATAL(RunUntilIdle());
2521 EXPECT_TRUE(channel_close_cb_called);
2522 }
2523
TEST_F(BrEdrDynamicChannelTest,RejectERTMRequestWhenPreferredModeIsBasic)2524 TEST_F(BrEdrDynamicChannelTest, RejectERTMRequestWhenPreferredModeIsBasic) {
2525 EXPECT_OUTBOUND_REQ(*sig(),
2526 kConnectionRequest,
2527 kConnReq.view(),
2528 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2529 EXPECT_OUTBOUND_REQ(
2530 *sig(),
2531 kConfigurationRequest,
2532 kOutboundConfigReq.view(),
2533 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2534
2535 registry()->OpenOutbound(kPsm, kChannelParams, {});
2536
2537 RETURN_IF_FATAL(RunUntilIdle());
2538
2539 // Peer requests ERTM. Local device should reject with unacceptable params.
2540 RETURN_IF_FATAL(
2541 sig()->ReceiveExpect(kConfigurationRequest,
2542 kInboundConfigReqWithERTM,
2543 kOutboundUnacceptableParamsWithRfcBasicConfigRsp));
2544
2545 EXPECT_OUTBOUND_REQ(*sig(),
2546 kDisconnectionRequest,
2547 kDisconReq.view(),
2548 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2549 bool channel_close_cb_called = false;
2550 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2551 RETURN_IF_FATAL(RunUntilIdle());
2552 EXPECT_TRUE(channel_close_cb_called);
2553 }
2554
2555 // Core Spec v5.1, Vol 3, Part A, Sec 5.4:
2556 // If the mode in the remote device's negative Configuration Response does
2557 // not match the mode in the remote device's Configuration Request then the
2558 // local device shall disconnect the channel.
2559 //
2560 // Inbound config request received BEFORE outbound config request:
2561 // <- ConfigurationRequest (with ERTM)
2562 // -> ConfigurationResponse (Ok)
2563 // -> ConfigurationRequest (with ERTM)
2564 // <- ConfigurationResponse (Unacceptable, with Basic)
TEST_F(BrEdrDynamicChannelTest,DisconnectWhenInboundConfigReqReceivedBeforeOutboundConfigReqSentModeInInboundUnacceptableParamsConfigRspDoesNotMatchPeerConfigReq)2565 TEST_F(
2566 BrEdrDynamicChannelTest,
2567 DisconnectWhenInboundConfigReqReceivedBeforeOutboundConfigReqSentModeInInboundUnacceptableParamsConfigRspDoesNotMatchPeerConfigReq) {
2568 EXPECT_OUTBOUND_REQ(*sig(),
2569 kConnectionRequest,
2570 kConnReq.view(),
2571 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2572 EXPECT_OUTBOUND_REQ(*sig(),
2573 kConfigurationRequest,
2574 kOutboundConfigReqWithErtm.view(),
2575 {SignalingChannel::Status::kSuccess,
2576 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2577 EXPECT_OUTBOUND_REQ(*sig(),
2578 kDisconnectionRequest,
2579 kDisconReq.view(),
2580 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2581
2582 int open_cb_count = 0;
2583 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2584 if (open_cb_count == 0) {
2585 EXPECT_FALSE(chan);
2586 }
2587 open_cb_count++;
2588 };
2589
2590 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
2591
2592 RETURN_IF_FATAL(RunUntilIdle());
2593
2594 // Receive inbound config request.
2595 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
2596 kInboundConfigReqWithERTM,
2597 kOutboundOkConfigRspWithErtm));
2598
2599 sig()->ReceiveResponses(ext_info_transaction_id(),
2600 {{SignalingChannel::Status::kSuccess,
2601 kExtendedFeaturesInfoRspWithERTM.view()}});
2602 // Send outbound config request.
2603 RunUntilIdle();
2604 EXPECT_EQ(1, open_cb_count);
2605 }
2606
2607 // Same as above, but inbound config request received AFTER outbound
2608 // configuration request:
2609 // -> ConfigurationRequest (with ERTM)
2610 // <- ConfigurationRequest (with ERTM)
2611 // -> ConfigurationResponse (Ok)
2612 // <- ConfigurationResponse (Unacceptable, with Basic)
TEST_F(BrEdrDynamicChannelTest,DisconnectWhenInboundConfigReqReceivedAfterOutboundConfigReqSentAndModeInInboundUnacceptableParamsConfigRspDoesNotMatchPeerConfigReq)2613 TEST_F(
2614 BrEdrDynamicChannelTest,
2615 DisconnectWhenInboundConfigReqReceivedAfterOutboundConfigReqSentAndModeInInboundUnacceptableParamsConfigRspDoesNotMatchPeerConfigReq) {
2616 EXPECT_OUTBOUND_REQ(*sig(),
2617 kConnectionRequest,
2618 kConnReq.view(),
2619 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2620 const auto outbound_config_req_id = EXPECT_OUTBOUND_REQ(
2621 *sig(), kConfigurationRequest, kOutboundConfigReqWithErtm.view());
2622 EXPECT_OUTBOUND_REQ(*sig(),
2623 kDisconnectionRequest,
2624 kDisconReq.view(),
2625 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2626
2627 int open_cb_count = 0;
2628 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2629 if (open_cb_count == 0) {
2630 EXPECT_FALSE(chan);
2631 }
2632 open_cb_count++;
2633 };
2634
2635 ChannelParameters params;
2636 params.mode = RetransmissionAndFlowControlMode::kEnhancedRetransmission;
2637 registry()->OpenOutbound(kPsm, params, std::move(open_cb));
2638
2639 RETURN_IF_FATAL(RunUntilIdle());
2640
2641 sig()->ReceiveResponses(ext_info_transaction_id(),
2642 {{SignalingChannel::Status::kSuccess,
2643 kExtendedFeaturesInfoRspWithERTM.view()}});
2644 // Send outbound config request.
2645 RunUntilIdle();
2646
2647 // Receive inbound config request.
2648 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
2649 kInboundConfigReqWithERTM,
2650 kOutboundOkConfigRspWithErtm));
2651
2652 sig()->ReceiveResponses(
2653 outbound_config_req_id,
2654 {{SignalingChannel::Status::kSuccess,
2655 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()}});
2656 RunUntilIdle();
2657 EXPECT_EQ(1, open_cb_count);
2658 }
2659
TEST_F(BrEdrDynamicChannelTest,DisconnectAfterReceivingTwoConfigRequestsWithoutDesiredMode)2660 TEST_F(BrEdrDynamicChannelTest,
2661 DisconnectAfterReceivingTwoConfigRequestsWithoutDesiredMode) {
2662 EXPECT_OUTBOUND_REQ(*sig(),
2663 kConnectionRequest,
2664 kConnReq.view(),
2665 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2666 EXPECT_OUTBOUND_REQ(
2667 *sig(),
2668 kConfigurationRequest,
2669 kOutboundConfigReq.view(),
2670 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2671 EXPECT_OUTBOUND_REQ(*sig(),
2672 kDisconnectionRequest,
2673 kDisconReq.view(),
2674 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2675
2676 int open_cb_count = 0;
2677 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2678 if (open_cb_count == 0) {
2679 EXPECT_FALSE(chan);
2680 }
2681 open_cb_count++;
2682 };
2683
2684 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2685
2686 RETURN_IF_FATAL(RunUntilIdle());
2687
2688 RETURN_IF_FATAL(
2689 sig()->ReceiveExpect(kConfigurationRequest,
2690 kInboundConfigReqWithERTM,
2691 kOutboundUnacceptableParamsWithRfcBasicConfigRsp));
2692 RETURN_IF_FATAL(
2693 sig()->ReceiveExpect(kConfigurationRequest,
2694 kInboundConfigReqWithERTM,
2695 kOutboundUnacceptableParamsWithRfcBasicConfigRsp));
2696
2697 RunUntilIdle();
2698 EXPECT_EQ(1, open_cb_count);
2699 }
2700
TEST_F(BrEdrDynamicChannelTest,RetryWhenPeerRejectsConfigReqWithBasicMode)2701 TEST_F(BrEdrDynamicChannelTest, RetryWhenPeerRejectsConfigReqWithBasicMode) {
2702 EXPECT_OUTBOUND_REQ(*sig(),
2703 kConnectionRequest,
2704 kConnReq.view(),
2705 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2706 EXPECT_OUTBOUND_REQ(*sig(),
2707 kConfigurationRequest,
2708 kOutboundConfigReq.view(),
2709 {SignalingChannel::Status::kSuccess,
2710 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2711 EXPECT_OUTBOUND_REQ(
2712 *sig(),
2713 kConfigurationRequest,
2714 kOutboundConfigReq.view(),
2715 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2716
2717 int open_cb_count = 0;
2718 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2719 EXPECT_FALSE(chan);
2720 open_cb_count++;
2721 };
2722
2723 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2724
2725 RETURN_IF_FATAL(RunUntilIdle());
2726
2727 EXPECT_EQ(0, open_cb_count);
2728 }
2729
TEST_F(BrEdrDynamicChannelTest,RetryNTimesWhenPeerRejectsConfigReqWithBasicMode)2730 TEST_F(BrEdrDynamicChannelTest,
2731 RetryNTimesWhenPeerRejectsConfigReqWithBasicMode) {
2732 EXPECT_OUTBOUND_REQ(*sig(),
2733 kConnectionRequest,
2734 kConnReq.view(),
2735 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2736 uint8_t retry_limit = 2;
2737 for (int i = 0; i < retry_limit; i++) {
2738 EXPECT_OUTBOUND_REQ(
2739 *sig(),
2740 kConfigurationRequest,
2741 kOutboundConfigReq.view(),
2742 {SignalingChannel::Status::kSuccess,
2743 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2744 }
2745 EXPECT_OUTBOUND_REQ(*sig(),
2746 kDisconnectionRequest,
2747 kDisconReq.view(),
2748 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2749
2750 int open_cb_count = 0;
2751 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2752 ASSERT_TRUE(chan == nullptr);
2753 open_cb_count++;
2754 };
2755
2756 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
2757
2758 RETURN_IF_FATAL(RunUntilIdle());
2759
2760 EXPECT_EQ(1, open_cb_count);
2761 }
2762
TEST_F(BrEdrDynamicChannelTest,RetryNTimesWhenPeerRejectsERTMConfigReqWithBasicMode)2763 TEST_F(BrEdrDynamicChannelTest,
2764 RetryNTimesWhenPeerRejectsERTMConfigReqWithBasicMode) {
2765 EXPECT_OUTBOUND_REQ(*sig(),
2766 kConnectionRequest,
2767 kConnReq.view(),
2768 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2769 EXPECT_OUTBOUND_REQ(*sig(),
2770 kConfigurationRequest,
2771 kOutboundConfigReqWithErtm.view(),
2772 {SignalingChannel::Status::kSuccess,
2773 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2774 uint8_t retry_limit = 2;
2775 for (int i = 0; i < retry_limit; i++) {
2776 EXPECT_OUTBOUND_REQ(
2777 *sig(),
2778 kConfigurationRequest,
2779 kOutboundConfigReq.view(),
2780 {SignalingChannel::Status::kSuccess,
2781 kInboundUnacceptableParamsWithRfcBasicConfigRsp.view()});
2782 }
2783 EXPECT_OUTBOUND_REQ(*sig(),
2784 kDisconnectionRequest,
2785 kDisconReq.view(),
2786 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2787
2788 int open_cb_count = 0;
2789 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
2790 ASSERT_TRUE(chan == nullptr);
2791 open_cb_count++;
2792 };
2793
2794 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
2795
2796 RETURN_IF_FATAL(RunUntilIdle());
2797
2798 sig()->ReceiveResponses(ext_info_transaction_id(),
2799 {{SignalingChannel::Status::kSuccess,
2800 kExtendedFeaturesInfoRspWithERTM.view()}});
2801
2802 RETURN_IF_FATAL(sig()->ReceiveExpect(
2803 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
2804
2805 RunUntilIdle();
2806 EXPECT_EQ(1, open_cb_count);
2807 }
2808
TEST_F(BrEdrDynamicChannelTest,SendUnacceptableParamsResponseWhenPeerRequestsUnsupportedChannelMode)2809 TEST_F(BrEdrDynamicChannelTest,
2810 SendUnacceptableParamsResponseWhenPeerRequestsUnsupportedChannelMode) {
2811 EXPECT_OUTBOUND_REQ(*sig(),
2812 kConnectionRequest,
2813 kConnReq.view(),
2814 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2815
2816 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2817
2818 RETURN_IF_FATAL(RunUntilIdle());
2819
2820 // Retransmission mode is not supported.
2821 const auto kInboundConfigReqWithRetransmissionMode =
2822 MakeConfigReqWithMtuAndRfc(
2823 kLocalCId,
2824 kMaxMTU,
2825 RetransmissionAndFlowControlMode::kRetransmission,
2826 0,
2827 0,
2828 0,
2829 0,
2830 0);
2831 RETURN_IF_FATAL(
2832 sig()->ReceiveExpect(kConfigurationRequest,
2833 kInboundConfigReqWithRetransmissionMode,
2834 kOutboundUnacceptableParamsWithRfcBasicConfigRsp));
2835
2836 EXPECT_OUTBOUND_REQ(*sig(),
2837 kDisconnectionRequest,
2838 kDisconReq.view(),
2839 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2840 bool channel_close_cb_called = false;
2841 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2842 RETURN_IF_FATAL(RunUntilIdle());
2843 EXPECT_TRUE(channel_close_cb_called);
2844 }
2845
TEST_F(BrEdrDynamicChannelTest,SendUnacceptableParamsResponseWhenPeerRequestsUnsupportedChannelModeAndSupportsErtm)2846 TEST_F(
2847 BrEdrDynamicChannelTest,
2848 SendUnacceptableParamsResponseWhenPeerRequestsUnsupportedChannelModeAndSupportsErtm) {
2849 EXPECT_OUTBOUND_REQ(*sig(),
2850 kConnectionRequest,
2851 kConnReq.view(),
2852 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2853 EXPECT_OUTBOUND_REQ(
2854 *sig(), kConfigurationRequest, kOutboundConfigReqWithErtm.view(), {});
2855
2856 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2857
2858 RETURN_IF_FATAL(RunUntilIdle());
2859
2860 sig()->ReceiveResponses(ext_info_transaction_id(),
2861 {{SignalingChannel::Status::kSuccess,
2862 kExtendedFeaturesInfoRspWithERTM.view()}});
2863
2864 RETURN_IF_FATAL(RunUntilIdle());
2865
2866 // Retransmission mode is not supported.
2867 const auto kInboundConfigReqWithRetransmissionMode =
2868 MakeConfigReqWithMtuAndRfc(
2869 kLocalCId,
2870 kMaxMTU,
2871 RetransmissionAndFlowControlMode::kRetransmission,
2872 0,
2873 0,
2874 0,
2875 0,
2876 0);
2877 RETURN_IF_FATAL(
2878 sig()->ReceiveExpect(kConfigurationRequest,
2879 kInboundConfigReqWithRetransmissionMode,
2880 kOutboundUnacceptableParamsWithRfcERTMConfigRsp));
2881
2882 EXPECT_OUTBOUND_REQ(*sig(),
2883 kDisconnectionRequest,
2884 kDisconReq.view(),
2885 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2886 bool channel_close_cb_called = false;
2887 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2888 RETURN_IF_FATAL(RunUntilIdle());
2889 EXPECT_TRUE(channel_close_cb_called);
2890 }
2891
TEST_F(BrEdrDynamicChannelTest,SendUnacceptableParamsResponseWhenPeerRequestErtmWithZeroTxWindow)2892 TEST_F(BrEdrDynamicChannelTest,
2893 SendUnacceptableParamsResponseWhenPeerRequestErtmWithZeroTxWindow) {
2894 EXPECT_OUTBOUND_REQ(*sig(),
2895 kConnectionRequest,
2896 kConnReq.view(),
2897 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2898 EXPECT_OUTBOUND_REQ(
2899 *sig(),
2900 kConfigurationRequest,
2901 kOutboundConfigReqWithErtm.view(),
2902 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2903
2904 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2905
2906 RETURN_IF_FATAL(RunUntilIdle());
2907
2908 sig()->ReceiveResponses(ext_info_transaction_id(),
2909 {{SignalingChannel::Status::kSuccess,
2910 kExtendedFeaturesInfoRspWithERTM.view()}});
2911
2912 RETURN_IF_FATAL(RunUntilIdle());
2913
2914 constexpr uint8_t kMaxTransmit = 31;
2915 constexpr auto kMps = kMaxTxPduPayloadSize;
2916
2917 // TxWindow of zero is out of range.
2918 const auto kInboundConfigReqWithZeroTxWindow = MakeConfigReqWithMtuAndRfc(
2919 kLocalCId,
2920 kDefaultMTU,
2921 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2922 /*tx_window=*/0,
2923 /*max_transmit=*/kMaxTransmit,
2924 /*retransmission_timeout=*/0,
2925 /*monitor_timeout=*/0,
2926 /*mps=*/kMps);
2927 const auto kOutboundConfigRsp = MakeConfigRspWithRfc(
2928 kRemoteCId,
2929 ConfigurationResult::kUnacceptableParameters,
2930 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2931 /*tx_window=*/1,
2932 /*max_transmit=*/kMaxTransmit,
2933 /*retransmission_timeout=*/0,
2934 /*monitor_timeout=*/0,
2935 /*mps=*/kMps);
2936 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
2937 kInboundConfigReqWithZeroTxWindow,
2938 kOutboundConfigRsp));
2939 }
2940
TEST_F(BrEdrDynamicChannelTest,SendUnacceptableParamsResponseWhenPeerRequestErtmWithOversizeTxWindow)2941 TEST_F(BrEdrDynamicChannelTest,
2942 SendUnacceptableParamsResponseWhenPeerRequestErtmWithOversizeTxWindow) {
2943 EXPECT_OUTBOUND_REQ(*sig(),
2944 kConnectionRequest,
2945 kConnReq.view(),
2946 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
2947 EXPECT_OUTBOUND_REQ(
2948 *sig(),
2949 kConfigurationRequest,
2950 kOutboundConfigReqWithErtm.view(),
2951 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
2952
2953 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
2954
2955 RETURN_IF_FATAL(RunUntilIdle());
2956
2957 sig()->ReceiveResponses(ext_info_transaction_id(),
2958 {{SignalingChannel::Status::kSuccess,
2959 kExtendedFeaturesInfoRspWithERTM.view()}});
2960
2961 RETURN_IF_FATAL(RunUntilIdle());
2962
2963 constexpr uint8_t kMaxTransmit = 31;
2964 constexpr auto kMps = kMaxTxPduPayloadSize;
2965
2966 // TxWindow of 200 is out of range.
2967 const auto kInboundConfigReqWithOversizeTxWindow = MakeConfigReqWithMtuAndRfc(
2968 kLocalCId,
2969 kDefaultMTU,
2970 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2971 /*tx_window=*/200,
2972 /*max_transmit=*/kMaxTransmit,
2973 /*retransmission_timeout=*/0,
2974 /*monitor_timeout=*/0,
2975 /*mps=*/kMps);
2976 const auto kOutboundConfigRsp = MakeConfigRspWithRfc(
2977 kRemoteCId,
2978 ConfigurationResult::kUnacceptableParameters,
2979 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
2980 /*tx_window=*/63,
2981 /*max_transmit=*/kMaxTransmit,
2982 /*retransmission_timeout=*/0,
2983 /*monitor_timeout=*/0,
2984 /*mps=*/kMps);
2985 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
2986 kInboundConfigReqWithOversizeTxWindow,
2987 kOutboundConfigRsp));
2988
2989 EXPECT_OUTBOUND_REQ(*sig(),
2990 kDisconnectionRequest,
2991 kDisconReq.view(),
2992 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
2993 bool channel_close_cb_called = false;
2994 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
2995 RETURN_IF_FATAL(RunUntilIdle());
2996 EXPECT_TRUE(channel_close_cb_called);
2997 }
2998
TEST_F(BrEdrDynamicChannelTest,SendUnacceptableParamsResponseWhenPeerRequestErtmWithUndersizeMps)2999 TEST_F(BrEdrDynamicChannelTest,
3000 SendUnacceptableParamsResponseWhenPeerRequestErtmWithUndersizeMps) {
3001 EXPECT_OUTBOUND_REQ(*sig(),
3002 kConnectionRequest,
3003 kConnReq.view(),
3004 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3005 EXPECT_OUTBOUND_REQ(
3006 *sig(),
3007 kConfigurationRequest,
3008 kOutboundConfigReqWithErtm.view(),
3009 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3010
3011 registry()->OpenOutbound(kPsm, kERTMChannelParams, {});
3012
3013 RETURN_IF_FATAL(RunUntilIdle());
3014
3015 sig()->ReceiveResponses(ext_info_transaction_id(),
3016 {{SignalingChannel::Status::kSuccess,
3017 kExtendedFeaturesInfoRspWithERTM.view()}});
3018
3019 RETURN_IF_FATAL(RunUntilIdle());
3020
3021 constexpr uint8_t kMaxTransmit = 31;
3022 constexpr uint8_t kTxWindow = kErtmMaxUnackedInboundFrames;
3023
3024 // MPS of 16 would not be able to fit a 48-byte (minimum MTU) SDU without
3025 // segmentation.
3026 const auto kInboundConfigReqWithUndersizeMps = MakeConfigReqWithMtuAndRfc(
3027 kLocalCId,
3028 kDefaultMTU,
3029 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3030 /*tx_window=*/kTxWindow,
3031 /*max_transmit=*/kMaxTransmit,
3032 /*retransmission_timeout=*/0,
3033 /*monitor_timeout=*/0,
3034 /*mps=*/16);
3035 const auto kOutboundConfigRsp = MakeConfigRspWithRfc(
3036 kRemoteCId,
3037 ConfigurationResult::kUnacceptableParameters,
3038 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3039 /*tx_window=*/kTxWindow,
3040 /*max_transmit=*/kMaxTransmit,
3041 /*retransmission_timeout=*/0,
3042 /*monitor_timeout=*/0,
3043 /*mps=*/kMinACLMTU);
3044 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
3045 kInboundConfigReqWithUndersizeMps,
3046 kOutboundConfigRsp));
3047
3048 EXPECT_OUTBOUND_REQ(*sig(),
3049 kDisconnectionRequest,
3050 kDisconReq.view(),
3051 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3052 bool channel_close_cb_called = false;
3053 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3054 RETURN_IF_FATAL(RunUntilIdle());
3055 EXPECT_TRUE(channel_close_cb_called);
3056 }
3057
3058 // Local config with ERTM incorrectly accepted by peer, then peer requests basic
3059 // mode which the local device must accept. These modes are incompatible, so the
3060 // local device should default to Basic Mode.
TEST_F(BrEdrDynamicChannelTest,OpenBasicModeChannelAfterPeerAcceptsErtmThenPeerRequestsBasicMode)3061 TEST_F(BrEdrDynamicChannelTest,
3062 OpenBasicModeChannelAfterPeerAcceptsErtmThenPeerRequestsBasicMode) {
3063 EXPECT_OUTBOUND_REQ(*sig(),
3064 kConnectionRequest,
3065 kConnReq.view(),
3066 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3067 EXPECT_OUTBOUND_REQ(
3068 *sig(),
3069 kConfigurationRequest,
3070 kOutboundConfigReqWithErtm.view(),
3071 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3072
3073 int open_cb_count = 0;
3074 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
3075 if (open_cb_count == 0) {
3076 ASSERT_TRUE(chan);
3077 EXPECT_EQ(kLocalCId, chan->local_cid());
3078 EXPECT_EQ(RetransmissionAndFlowControlMode::kBasic, chan->info().mode);
3079 }
3080 open_cb_count++;
3081 };
3082
3083 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
3084
3085 RETURN_IF_FATAL(RunUntilIdle());
3086
3087 sig()->ReceiveResponses(ext_info_transaction_id(),
3088 {{SignalingChannel::Status::kSuccess,
3089 kExtendedFeaturesInfoRspWithERTM.view()}});
3090
3091 // Request ERTM.
3092 RunUntilIdle();
3093
3094 // Peer requests basic mode.
3095 RETURN_IF_FATAL(sig()->ReceiveExpect(
3096 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
3097
3098 // Disconnect
3099 RunUntilIdle();
3100 EXPECT_EQ(1, open_cb_count);
3101
3102 EXPECT_OUTBOUND_REQ(*sig(),
3103 kDisconnectionRequest,
3104 kDisconReq.view(),
3105 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3106 bool channel_close_cb_called = false;
3107 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3108 RETURN_IF_FATAL(RunUntilIdle());
3109 EXPECT_TRUE(channel_close_cb_called);
3110 }
3111
3112 // Same as above, but the peer sends its positive response after sending its
3113 // Basic Mode request.
TEST_F(BrEdrDynamicChannelTest,OpenBasicModeChannelAfterPeerRequestsBasicModeThenPeerAcceptsErtm)3114 TEST_F(BrEdrDynamicChannelTest,
3115 OpenBasicModeChannelAfterPeerRequestsBasicModeThenPeerAcceptsErtm) {
3116 EXPECT_OUTBOUND_REQ(*sig(),
3117 kConnectionRequest,
3118 kConnReq.view(),
3119 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3120 EXPECT_OUTBOUND_REQ(
3121 *sig(),
3122 kConfigurationRequest,
3123 kOutboundConfigReqWithErtm.view(),
3124 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3125
3126 int open_cb_count = 0;
3127 auto open_cb = [&open_cb_count](const DynamicChannel* chan) {
3128 if (open_cb_count == 0) {
3129 ASSERT_TRUE(chan);
3130 EXPECT_EQ(kLocalCId, chan->local_cid());
3131 EXPECT_EQ(RetransmissionAndFlowControlMode::kBasic, chan->info().mode);
3132 }
3133 open_cb_count++;
3134 };
3135
3136 registry()->OpenOutbound(kPsm, kERTMChannelParams, std::move(open_cb));
3137
3138 RETURN_IF_FATAL(RunUntilIdle());
3139
3140 // Peer requests basic mode.
3141 RETURN_IF_FATAL(sig()->ReceiveExpect(
3142 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
3143
3144 // Local device will request ERTM.
3145 sig()->ReceiveResponses(ext_info_transaction_id(),
3146 {{SignalingChannel::Status::kSuccess,
3147 kExtendedFeaturesInfoRspWithERTM.view()}});
3148 // Request ERTM & Disconnect
3149 RunUntilIdle();
3150 EXPECT_EQ(1, open_cb_count);
3151
3152 EXPECT_OUTBOUND_REQ(*sig(),
3153 kDisconnectionRequest,
3154 kDisconReq.view(),
3155 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3156 bool channel_close_cb_called = false;
3157 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3158 RETURN_IF_FATAL(RunUntilIdle());
3159 EXPECT_TRUE(channel_close_cb_called);
3160 }
3161
TEST_F(BrEdrDynamicChannelTest,MtuChannelParameterSentInConfigReq)3162 TEST_F(BrEdrDynamicChannelTest, MtuChannelParameterSentInConfigReq) {
3163 constexpr uint16_t kPreferredMtu = kDefaultMTU + 1;
3164 const auto kExpectedOutboundConfigReq =
3165 MakeConfigReqWithMtu(kRemoteCId, kPreferredMtu);
3166
3167 EXPECT_OUTBOUND_REQ(*sig(),
3168 kConnectionRequest,
3169 kConnReq.view(),
3170 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3171 EXPECT_OUTBOUND_REQ(
3172 *sig(),
3173 kConfigurationRequest,
3174 kExpectedOutboundConfigReq.view(),
3175 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3176
3177 int open_cb_count = 0;
3178 auto open_cb = [&](const DynamicChannel* chan) {
3179 if (open_cb_count == 0) {
3180 ASSERT_TRUE(chan);
3181 EXPECT_EQ(kLocalCId, chan->local_cid());
3182 EXPECT_EQ(kPreferredMtu, chan->info().max_rx_sdu_size);
3183 }
3184 open_cb_count++;
3185 };
3186
3187 registry()->OpenOutbound(
3188 kPsm,
3189 {RetransmissionAndFlowControlMode::kBasic, kPreferredMtu, std::nullopt},
3190 open_cb);
3191 RunUntilIdle();
3192
3193 sig()->ReceiveExpect(
3194 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp);
3195 RunUntilIdle();
3196 EXPECT_EQ(1, open_cb_count);
3197
3198 EXPECT_OUTBOUND_REQ(*sig(),
3199 kDisconnectionRequest,
3200 kDisconReq.view(),
3201 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3202 bool channel_close_cb_called = false;
3203 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3204 RETURN_IF_FATAL(RunUntilIdle());
3205 EXPECT_TRUE(channel_close_cb_called);
3206 }
3207
TEST_F(BrEdrDynamicChannelTest,UseMinMtuWhenMtuChannelParameterIsBelowMin)3208 TEST_F(BrEdrDynamicChannelTest, UseMinMtuWhenMtuChannelParameterIsBelowMin) {
3209 constexpr uint16_t kMtu = kMinACLMTU - 1;
3210 const auto kExpectedOutboundConfigReq =
3211 MakeConfigReqWithMtu(kRemoteCId, kMinACLMTU);
3212
3213 EXPECT_OUTBOUND_REQ(*sig(),
3214 kConnectionRequest,
3215 kConnReq.view(),
3216 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3217 EXPECT_OUTBOUND_REQ(
3218 *sig(),
3219 kConfigurationRequest,
3220 kExpectedOutboundConfigReq.view(),
3221 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3222
3223 int open_cb_count = 0;
3224 auto open_cb = [&](const DynamicChannel* chan) {
3225 if (open_cb_count == 0) {
3226 ASSERT_TRUE(chan);
3227 EXPECT_EQ(kLocalCId, chan->local_cid());
3228 EXPECT_EQ(kMinACLMTU, chan->info().max_rx_sdu_size);
3229 }
3230 open_cb_count++;
3231 };
3232
3233 registry()->OpenOutbound(
3234 kPsm,
3235 {RetransmissionAndFlowControlMode::kBasic, kMtu, std::nullopt},
3236 open_cb);
3237 RunUntilIdle();
3238
3239 sig()->ReceiveExpect(
3240 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp);
3241 RunUntilIdle();
3242 EXPECT_EQ(1, open_cb_count);
3243
3244 EXPECT_OUTBOUND_REQ(*sig(),
3245 kDisconnectionRequest,
3246 kDisconReq.view(),
3247 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3248 bool channel_close_cb_called = false;
3249 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3250 RETURN_IF_FATAL(RunUntilIdle());
3251 EXPECT_TRUE(channel_close_cb_called);
3252 }
3253
TEST_F(BrEdrDynamicChannelTest,UseMaxPduPayloadSizeWhenMtuChannelParameterExceedsItWithErtm)3254 TEST_F(BrEdrDynamicChannelTest,
3255 UseMaxPduPayloadSizeWhenMtuChannelParameterExceedsItWithErtm) {
3256 constexpr uint16_t kPreferredMtu = kMaxInboundPduPayloadSize + 1;
3257 const auto kExpectedOutboundConfigReq = MakeConfigReqWithMtuAndRfc(
3258 kRemoteCId,
3259 kMaxInboundPduPayloadSize,
3260 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3261 kErtmMaxUnackedInboundFrames,
3262 kErtmMaxInboundRetransmissions,
3263 0,
3264 0,
3265 kMaxInboundPduPayloadSize);
3266
3267 EXPECT_OUTBOUND_REQ(*sig(),
3268 kConnectionRequest,
3269 kConnReq.view(),
3270 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3271 EXPECT_OUTBOUND_REQ(
3272 *sig(),
3273 kConfigurationRequest,
3274 kExpectedOutboundConfigReq.view(),
3275 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3276
3277 int open_cb_count = 0;
3278 auto open_cb = [&](const DynamicChannel* chan) {
3279 if (open_cb_count == 0) {
3280 ASSERT_TRUE(chan);
3281 EXPECT_EQ(kLocalCId, chan->local_cid());
3282 EXPECT_EQ(kMaxInboundPduPayloadSize, chan->info().max_rx_sdu_size);
3283 }
3284 open_cb_count++;
3285 };
3286
3287 registry()->OpenOutbound(
3288 kPsm,
3289 {RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3290 kPreferredMtu,
3291 std::nullopt},
3292 std::move(open_cb));
3293
3294 RETURN_IF_FATAL(RunUntilIdle());
3295
3296 sig()->ReceiveResponses(ext_info_transaction_id(),
3297 {{SignalingChannel::Status::kSuccess,
3298 kExtendedFeaturesInfoRspWithERTM.view()}});
3299
3300 RETURN_IF_FATAL(sig()->ReceiveExpect(kConfigurationRequest,
3301 kInboundConfigReqWithERTM,
3302 kOutboundOkConfigRspWithErtm));
3303
3304 RunUntilIdle();
3305 EXPECT_EQ(1, open_cb_count);
3306
3307 EXPECT_OUTBOUND_REQ(*sig(),
3308 kDisconnectionRequest,
3309 kDisconReq.view(),
3310 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3311 bool channel_close_cb_called = false;
3312 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3313 RETURN_IF_FATAL(RunUntilIdle());
3314 EXPECT_TRUE(channel_close_cb_called);
3315 }
3316
TEST_F(BrEdrDynamicChannelTest,BasicModeChannelReportsChannelInfoWithBasicModeAndSduCapacities)3317 TEST_F(BrEdrDynamicChannelTest,
3318 BasicModeChannelReportsChannelInfoWithBasicModeAndSduCapacities) {
3319 constexpr uint16_t kPreferredMtu = kDefaultMTU + 1;
3320 const auto kExpectedOutboundConfigReq =
3321 MakeConfigReqWithMtu(kRemoteCId, kPreferredMtu);
3322
3323 EXPECT_OUTBOUND_REQ(*sig(),
3324 kConnectionRequest,
3325 kConnReq.view(),
3326 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3327 EXPECT_OUTBOUND_REQ(
3328 *sig(),
3329 kConfigurationRequest,
3330 kExpectedOutboundConfigReq.view(),
3331 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3332
3333 constexpr uint16_t kPeerMtu = kDefaultMTU + 2;
3334 const auto inbound_config_req = MakeConfigReqWithMtu(kLocalCId, kPeerMtu);
3335
3336 int open_cb_count = 0;
3337 auto open_cb = [&](const DynamicChannel* chan) {
3338 ASSERT_TRUE(chan);
3339 EXPECT_EQ(kLocalCId, chan->local_cid());
3340 EXPECT_EQ(RetransmissionAndFlowControlMode::kBasic, chan->info().mode);
3341 EXPECT_EQ(kPreferredMtu, chan->info().max_rx_sdu_size);
3342 EXPECT_EQ(kPeerMtu, chan->info().max_tx_sdu_size);
3343 open_cb_count++;
3344 };
3345
3346 registry()->OpenOutbound(
3347 kPsm,
3348 {RetransmissionAndFlowControlMode::kBasic, kPreferredMtu, std::nullopt},
3349 open_cb);
3350 RunUntilIdle();
3351
3352 const ByteBuffer& kExpectedOutboundOkConfigRsp =
3353 MakeConfigRspWithMtu(kRemoteCId, kPeerMtu);
3354 sig()->ReceiveExpect(
3355 kConfigurationRequest, inbound_config_req, kExpectedOutboundOkConfigRsp);
3356 RunUntilIdle();
3357 EXPECT_EQ(1, open_cb_count);
3358
3359 EXPECT_OUTBOUND_REQ(*sig(),
3360 kDisconnectionRequest,
3361 kDisconReq.view(),
3362 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3363 bool channel_close_cb_called = false;
3364 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3365 RETURN_IF_FATAL(RunUntilIdle());
3366 EXPECT_TRUE(channel_close_cb_called);
3367 }
3368
TEST_F(BrEdrDynamicChannelTest,Receive2ConfigReqsWithContinuationFlagInFirstReq)3369 TEST_F(BrEdrDynamicChannelTest,
3370 Receive2ConfigReqsWithContinuationFlagInFirstReq) {
3371 constexpr uint16_t kTxMtu = kMinACLMTU;
3372 EXPECT_OUTBOUND_REQ(*sig(),
3373 kConnectionRequest,
3374 kConnReq.view(),
3375 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3376 EXPECT_OUTBOUND_REQ(
3377 *sig(),
3378 kConfigurationRequest,
3379 kOutboundConfigReqWithErtm.view(),
3380 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3381
3382 const auto kInboundConfigReq0 =
3383 MakeConfigReqWithMtu(kLocalCId, kTxMtu, kConfigurationContinuation);
3384 const auto kOutboundConfigRsp1 = MakeConfigRspWithMtuAndRfc(
3385 kRemoteCId,
3386 ConfigurationResult::kSuccess,
3387 RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3388 kTxMtu,
3389 kErtmNFramesInTxWindow,
3390 kErtmMaxTransmissions,
3391 2000,
3392 12000,
3393 kMaxTxPduPayloadSize);
3394
3395 size_t open_cb_count = 0;
3396 auto open_cb = [&](const DynamicChannel* chan) {
3397 if (open_cb_count == 0) {
3398 ASSERT_TRUE(chan);
3399 EXPECT_EQ(kLocalCId, chan->local_cid());
3400 EXPECT_EQ(kTxMtu, chan->info().max_tx_sdu_size);
3401 EXPECT_EQ(RetransmissionAndFlowControlMode::kEnhancedRetransmission,
3402 chan->info().mode);
3403 }
3404 open_cb_count++;
3405 };
3406
3407 sig()->ReceiveResponses(ext_info_transaction_id(),
3408 {{SignalingChannel::Status::kSuccess,
3409 kExtendedFeaturesInfoRspWithERTM.view()}});
3410 registry()->OpenOutbound(kPsm, kERTMChannelParams, open_cb);
3411 RunUntilIdle();
3412
3413 sig()->ReceiveExpect(kConfigurationRequest,
3414 kInboundConfigReq0,
3415 kOutboundEmptyContinuationConfigRsp);
3416 RunUntilIdle();
3417 EXPECT_EQ(0u, open_cb_count);
3418
3419 sig()->ReceiveExpect(
3420 kConfigurationRequest, kInboundConfigReqWithERTM, kOutboundConfigRsp1);
3421 RunUntilIdle();
3422 EXPECT_EQ(1u, open_cb_count);
3423
3424 EXPECT_OUTBOUND_REQ(*sig(),
3425 kDisconnectionRequest,
3426 kDisconReq.view(),
3427 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3428 bool channel_close_cb_called = false;
3429 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3430 RETURN_IF_FATAL(RunUntilIdle());
3431 EXPECT_TRUE(channel_close_cb_called);
3432 }
3433
3434 // The unknown options from both configuration requests should be included when
3435 // responding with the "unknown options" result.
TEST_F(BrEdrDynamicChannelTest,Receive2ConfigReqsWithContinuationFlagInFirstReqAndUnknownOptionInBothReqs)3436 TEST_F(
3437 BrEdrDynamicChannelTest,
3438 Receive2ConfigReqsWithContinuationFlagInFirstReqAndUnknownOptionInBothReqs) {
3439 EXPECT_OUTBOUND_REQ(*sig(),
3440 kConnectionRequest,
3441 kConnReq.view(),
3442 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3443 EXPECT_OUTBOUND_REQ(
3444 *sig(),
3445 kConfigurationRequest,
3446 kOutboundConfigReq.view(),
3447 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3448
3449 constexpr uint8_t kUnknownOption0Type = 0x70;
3450 constexpr uint8_t kUnknownOption1Type = 0x71;
3451 const StaticByteBuffer kInboundConfigReq0(
3452 // Destination CID
3453 LowerBits(kLocalCId),
3454 UpperBits(kLocalCId),
3455 // Flags (C = 1)
3456 0x01,
3457 0x00,
3458 // Unknown Option
3459 kUnknownOption0Type,
3460 0x01,
3461 0x00);
3462 const StaticByteBuffer kInboundConfigReq1(
3463 // Destination CID
3464 LowerBits(kLocalCId),
3465 UpperBits(kLocalCId),
3466 // Flags (C = 0)
3467 0x00,
3468 0x00,
3469 // Unknown Option
3470 kUnknownOption1Type,
3471 0x01,
3472 0x00);
3473
3474 const StaticByteBuffer kOutboundUnknownOptionsConfigRsp(
3475 // Source CID
3476 LowerBits(kRemoteCId),
3477 UpperBits(kRemoteCId),
3478 // Flags (C = 0)
3479 0x00,
3480 0x00,
3481 // Result
3482 LowerBits(static_cast<uint16_t>(ConfigurationResult::kUnknownOptions)),
3483 UpperBits(static_cast<uint16_t>(ConfigurationResult::kUnknownOptions)),
3484 // Unknown Options
3485 kUnknownOption0Type,
3486 0x01,
3487 0x00,
3488 kUnknownOption1Type,
3489 0x01,
3490 0x00);
3491
3492 size_t open_cb_count = 0;
3493 auto open_cb = [&](const DynamicChannel* chan) {
3494 EXPECT_FALSE(chan);
3495 open_cb_count++;
3496 };
3497
3498 registry()->OpenOutbound(kPsm, kChannelParams, open_cb);
3499 RunUntilIdle();
3500
3501 sig()->ReceiveExpect(kConfigurationRequest,
3502 kInboundConfigReq0,
3503 kOutboundEmptyContinuationConfigRsp);
3504 RunUntilIdle();
3505 EXPECT_EQ(0u, open_cb_count);
3506
3507 sig()->ReceiveExpect(kConfigurationRequest,
3508 kInboundConfigReq1,
3509 kOutboundUnknownOptionsConfigRsp);
3510 RunUntilIdle();
3511 EXPECT_EQ(0u, open_cb_count);
3512 }
3513
TEST_F(BrEdrDynamicChannelTest,RejectReconfigurationAfterChannelOpen)3514 TEST_F(BrEdrDynamicChannelTest, RejectReconfigurationAfterChannelOpen) {
3515 EXPECT_OUTBOUND_REQ(*sig(),
3516 kConnectionRequest,
3517 kConnReq.view(),
3518 {SignalingChannel::Status::kSuccess, kOkConnRsp.view()});
3519 EXPECT_OUTBOUND_REQ(
3520 *sig(),
3521 kConfigurationRequest,
3522 kOutboundConfigReq.view(),
3523 {SignalingChannel::Status::kSuccess, kInboundEmptyConfigRsp.view()});
3524 EXPECT_OUTBOUND_REQ(*sig(),
3525 kDisconnectionRequest,
3526 kDisconReq.view(),
3527 {SignalingChannel::Status::kSuccess, kDisconRsp.view()});
3528
3529 int open_cb_count = 0;
3530 auto open_cb = [&open_cb_count](auto chan) {
3531 if (open_cb_count == 0) {
3532 ASSERT_TRUE(chan);
3533 EXPECT_TRUE(chan->IsOpen());
3534 }
3535 open_cb_count++;
3536 };
3537
3538 int close_cb_count = 0;
3539 set_channel_close_cb([&close_cb_count](auto chan) {
3540 EXPECT_TRUE(chan);
3541 close_cb_count++;
3542 });
3543
3544 registry()->OpenOutbound(kPsm, kChannelParams, std::move(open_cb));
3545
3546 RETURN_IF_FATAL(RunUntilIdle());
3547
3548 RETURN_IF_FATAL(sig()->ReceiveExpect(
3549 kConfigurationRequest, kInboundConfigReq, kOutboundOkConfigRsp));
3550
3551 EXPECT_EQ(1, open_cb_count);
3552 EXPECT_EQ(0, close_cb_count);
3553
3554 // Inbound reconfiguration requests should be rejected. The channel should not
3555 // be closed.
3556 RETURN_IF_FATAL(sig()->ReceiveExpect(
3557 kConfigurationRequest, kInboundConfigReq, kOutboundConfigRspRejected));
3558 EXPECT_EQ(0, close_cb_count);
3559
3560 bool channel_close_cb_called = false;
3561 registry()->CloseChannel(kLocalCId, [&] { channel_close_cb_called = true; });
3562 RETURN_IF_FATAL(RunUntilIdle());
3563 EXPECT_EQ(1, open_cb_count);
3564 EXPECT_EQ(0, close_cb_count);
3565 EXPECT_TRUE(channel_close_cb_called);
3566 }
3567
3568 } // namespace
3569 } // namespace bt::l2cap::internal
3570