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/gap/fake_pairing_delegate.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace bt::gap {
20 namespace {
21
AddFailure(const char *,PeerId peer_id)22 void AddFailure(const char*, PeerId peer_id) {
23 return ADD_FAILURE() << "Unexpected call to " << __func__
24 << ", peer_id: " << peer_id.ToString();
25 }
26
27 } // namespace
28
FakePairingDelegate(sm::IOCapability io_capability)29 FakePairingDelegate::FakePairingDelegate(sm::IOCapability io_capability)
30 : io_capability_(io_capability),
31 complete_pairing_count_(0),
32 confirm_pairing_count_(0),
33 display_passkey_count_(0),
34 request_passkey_count_(0) {}
35
~FakePairingDelegate()36 FakePairingDelegate::~FakePairingDelegate() {
37 if (complete_pairing_cb_ && complete_pairing_count_ == 0) {
38 ADD_FAILURE() << "Expected CompletePairing never called";
39 }
40 if (confirm_pairing_cb_ && confirm_pairing_count_ == 0) {
41 ADD_FAILURE() << "Expected ConfirmPairing never called";
42 }
43 if (display_passkey_cb_ && display_passkey_count_ == 0) {
44 ADD_FAILURE() << "Expected DisplayPasskey never called";
45 }
46 if (request_passkey_cb_ && request_passkey_count_ == 0) {
47 ADD_FAILURE() << "Expected RequestPasskey never called";
48 }
49 }
50
CompletePairing(PeerId peer_id,sm::Result<> status)51 void FakePairingDelegate::CompletePairing(PeerId peer_id, sm::Result<> status) {
52 if (!complete_pairing_cb_) {
53 AddFailure(__func__, peer_id);
54 ADD_FAILURE() << status.error_value();
55 return;
56 }
57 complete_pairing_cb_(peer_id, status);
58 complete_pairing_count_++;
59 }
60
ConfirmPairing(PeerId peer_id,ConfirmCallback confirm)61 void FakePairingDelegate::ConfirmPairing(PeerId peer_id,
62 ConfirmCallback confirm) {
63 if (!confirm_pairing_cb_) {
64 AddFailure(__func__, peer_id);
65 return;
66 }
67 confirm_pairing_cb_(peer_id, std::move(confirm));
68 confirm_pairing_count_++;
69 }
70
DisplayPasskey(PeerId peer_id,uint32_t passkey,DisplayMethod method,ConfirmCallback confirm)71 void FakePairingDelegate::DisplayPasskey(PeerId peer_id,
72 uint32_t passkey,
73 DisplayMethod method,
74 ConfirmCallback confirm) {
75 if (!display_passkey_cb_) {
76 AddFailure(__func__, peer_id);
77 ADD_FAILURE() << "passkey: " << passkey << ", method: " << method;
78 return;
79 }
80 display_passkey_cb_(peer_id, passkey, method, std::move(confirm));
81 display_passkey_count_++;
82 }
83
RequestPasskey(PeerId peer_id,PasskeyResponseCallback respond)84 void FakePairingDelegate::RequestPasskey(PeerId peer_id,
85 PasskeyResponseCallback respond) {
86 if (!request_passkey_cb_) {
87 AddFailure(__func__, peer_id);
88 return;
89 }
90 request_passkey_cb_(peer_id, std::move(respond));
91 request_passkey_count_++;
92 }
93
94 } // namespace bt::gap
95