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