1 /*
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 #include "security/pairing/classic_pairing_handler.h"
19 
20 #include <bluetooth/log.h>
21 #include <gtest/gtest.h>
22 
23 #include <memory>
24 #include <utility>
25 
26 #include "hci/hci_packets.h"
27 #include "packet/raw_builder.h"
28 #include "security/channel/security_manager_channel.h"
29 #include "security/initial_informations.h"
30 #include "security/test/fake_hci_layer.h"
31 #include "security/test/fake_name_db.h"
32 #include "security/test/fake_security_interface.h"
33 
34 namespace bluetooth {
35 namespace security {
36 namespace pairing {
37 namespace {
38 
39 using bluetooth::security::channel::SecurityManagerChannel;
40 using hci::Address;
41 using hci::AuthenticationRequirements;
42 using hci::CommandCompleteBuilder;
43 using hci::IoCapabilityRequestReplyBuilder;
44 using hci::IoCapabilityRequestView;
45 using hci::OobDataPresent;
46 using hci::OpCode;
47 using os::Handler;
48 using os::Thread;
49 using packet::RawBuilder;
50 
51 class FakeSecurityManagerChannel : public channel::SecurityManagerChannel {
52  public:
FakeSecurityManagerChannel(os::Handler * handler,hci::HciLayer * hci_layer)53   FakeSecurityManagerChannel(os::Handler* handler, hci::HciLayer* hci_layer)
54       : channel::SecurityManagerChannel(handler, hci_layer) {}
~FakeSecurityManagerChannel()55   ~FakeSecurityManagerChannel() {}
56 
OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link)57   void OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link) override {
58     log::error("CALLED");
59   }
60 
OnLinkDisconnected(hci::Address address)61   void OnLinkDisconnected(hci::Address address) override {
62     log::error("CALLED");
63   }
64 
OnEncryptionChange(hci::Address address,bool encrypted)65   void OnEncryptionChange(hci::Address address, bool encrypted) override {
66     log::error("CALLED");
67   }
68 
OnAuthenticationComplete(hci::ErrorCode hci_status,hci::Address remote)69   void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address remote) override {
70     log::error("CALLED");
71   }
72 };
73 
74 class TestUI : public UI {
75  public:
76   ~TestUI() = default;
DisplayPairingPrompt(const hci::AddressWithType & address,std::string name)77   void DisplayPairingPrompt(const hci::AddressWithType& address, std::string name) override {}
Cancel(const hci::AddressWithType & address)78   void Cancel(const hci::AddressWithType& address) override {}
DisplayConfirmValue(ConfirmationData data)79   void DisplayConfirmValue(ConfirmationData data) override {}
DisplayYesNoDialog(ConfirmationData data)80   void DisplayYesNoDialog(ConfirmationData data) override {}
DisplayEnterPasskeyDialog(ConfirmationData data)81   void DisplayEnterPasskeyDialog(ConfirmationData data) override {}
DisplayPasskey(ConfirmationData data)82   void DisplayPasskey(ConfirmationData data) override {}
DisplayEnterPinDialog(ConfirmationData data)83   void DisplayEnterPinDialog(ConfirmationData data) override {}
84 };
85 
86 class SecurityManagerChannelCallback : public channel::ISecurityManagerChannelListener {
87  public:
SecurityManagerChannelCallback(pairing::ClassicPairingHandler * pairing_handler)88   explicit SecurityManagerChannelCallback(pairing::ClassicPairingHandler* pairing_handler)
89       : pairing_handler_(pairing_handler) {}
OnHciEventReceived(hci::EventView packet)90   void OnHciEventReceived(hci::EventView packet) override {
91     auto event = hci::EventView::Create(packet);
92     log::assert_that(event.IsValid(), "Received invalid packet");
93     const hci::EventCode code = event.GetEventCode();
94     switch (code) {
95       case hci::EventCode::PIN_CODE_REQUEST:
96         pairing_handler_->OnReceive(hci::PinCodeRequestView::Create(event));
97         break;
98       case hci::EventCode::LINK_KEY_REQUEST:
99         pairing_handler_->OnReceive(hci::LinkKeyRequestView::Create(event));
100         break;
101       case hci::EventCode::LINK_KEY_NOTIFICATION:
102         pairing_handler_->OnReceive(hci::LinkKeyNotificationView::Create(event));
103         break;
104       case hci::EventCode::IO_CAPABILITY_REQUEST:
105         pairing_handler_->OnReceive(hci::IoCapabilityRequestView::Create(event));
106         break;
107       case hci::EventCode::IO_CAPABILITY_RESPONSE:
108         pairing_handler_->OnReceive(hci::IoCapabilityResponseView::Create(event));
109         break;
110       case hci::EventCode::SIMPLE_PAIRING_COMPLETE:
111         pairing_handler_->OnReceive(hci::SimplePairingCompleteView::Create(event));
112         break;
113       case hci::EventCode::RETURN_LINK_KEYS:
114         pairing_handler_->OnReceive(hci::ReturnLinkKeysView::Create(event));
115         break;
116       case hci::EventCode::REMOTE_OOB_DATA_REQUEST:
117         pairing_handler_->OnReceive(hci::RemoteOobDataRequestView::Create(event));
118         break;
119       case hci::EventCode::USER_PASSKEY_NOTIFICATION:
120         pairing_handler_->OnReceive(hci::UserPasskeyNotificationView::Create(event));
121         break;
122       case hci::EventCode::KEYPRESS_NOTIFICATION:
123         pairing_handler_->OnReceive(hci::KeypressNotificationView::Create(event));
124         break;
125       case hci::EventCode::USER_CONFIRMATION_REQUEST:
126         pairing_handler_->OnReceive(hci::UserConfirmationRequestView::Create(event));
127         break;
128       case hci::EventCode::USER_PASSKEY_REQUEST:
129         pairing_handler_->OnReceive(hci::UserPasskeyRequestView::Create(event));
130         break;
131       default:
132         log::fatal("Cannot handle received packet: {}", hci::EventCodeText(code));
133         break;
134     }
135   }
136 
OnConnectionClosed(hci::Address address)137   void OnConnectionClosed(hci::Address address) override {
138     log::info("Called");
139   }
140 
141  private:
142   pairing::ClassicPairingHandler* pairing_handler_ = nullptr;
143 };
144 
145 bool expect_success_ = true;
146 
pairing_complete_callback(bluetooth::hci::Address address,PairingResultOrFailure status)147 static void pairing_complete_callback(bluetooth::hci::Address address, PairingResultOrFailure status) {
148   if (expect_success_) {
149     ASSERT_TRUE(std::holds_alternative<PairingResult>(status));
150   } else {
151     ASSERT_FALSE(std::holds_alternative<PairingResult>(status));
152   }
153 }
154 
155 class ClassicPairingHandlerTest : public ::testing::Test {
156  protected:
SetUp()157   void SetUp() override {
158     expect_success_ = true;
159     hci_layer_ = new FakeHciLayer();
160     name_db_module_ = new FakeNameDbModule();
161     fake_registry_.InjectTestModule(&FakeHciLayer::Factory, hci_layer_);
162     fake_registry_.InjectTestModule(&neighbor::NameDbModule::Factory, name_db_module_);
163     handler_ = fake_registry_.GetTestModuleHandler(&FakeHciLayer::Factory);
164     channel_ = new FakeSecurityManagerChannel(handler_, hci_layer_);
165     security_record_ = std::make_shared<record::SecurityRecord>(device_);
166     user_interface_ = new TestUI();
167     user_interface_handler_ = handler_;
168     pairing_handler_ = new pairing::ClassicPairingHandler(
169         channel_,
170         security_record_,
171         handler_,
172         common::Bind(&pairing_complete_callback),
173         user_interface_,
174         user_interface_handler_,
175         "Fake name",
176         name_db_module_);
177     channel_callback_ = new SecurityManagerChannelCallback(pairing_handler_);
178     channel_->SetChannelListener(channel_callback_);
179     security_interface_ = new FakeSecurityInterface(handler_, channel_);
180     channel_->SetSecurityInterface(security_interface_);
181   }
182 
TearDown()183   void TearDown() override {
184     channel_->SetChannelListener(nullptr);
185     synchronize();
186     fake_registry_.StopAll();
187     delete user_interface_;
188     delete pairing_handler_;
189     delete channel_;
190     delete channel_callback_;
191     delete security_interface_;
192   }
193 
synchronize()194   void synchronize() {
195     fake_registry_.SynchronizeModuleHandler(&FakeHciLayer::Factory, std::chrono::milliseconds(20));
196     fake_registry_.SynchronizeModuleHandler(&FakeNameDbModule::Factory, std::chrono::milliseconds(20));
197   }
198 
ReceiveLinkKeyRequest(hci::AddressWithType device)199   void ReceiveLinkKeyRequest(hci::AddressWithType device) {
200     hci_layer_->IncomingEvent(hci::LinkKeyRequestBuilder::Create(device.GetAddress()));
201     synchronize();
202   }
203 
ReceiveIoCapabilityRequest(hci::AddressWithType device)204   void ReceiveIoCapabilityRequest(hci::AddressWithType device) {
205     hci_layer_->IncomingEvent(hci::IoCapabilityRequestBuilder::Create(device.GetAddress()));
206     synchronize();
207   }
208 
ReceiveIoCapabilityResponse(hci::AddressWithType device,hci::IoCapability io_cap,hci::OobDataPresent oob_present,hci::AuthenticationRequirements auth_reqs)209   void ReceiveIoCapabilityResponse(hci::AddressWithType device, hci::IoCapability io_cap,
210                                    hci::OobDataPresent oob_present, hci::AuthenticationRequirements auth_reqs) {
211     hci_layer_->IncomingEvent(
212         hci::IoCapabilityResponseBuilder::Create(device.GetAddress(), io_cap, oob_present, auth_reqs));
213     synchronize();
214   }
215 
ReceiveOobDataRequest(hci::AddressWithType device)216   void ReceiveOobDataRequest(hci::AddressWithType device) {
217     hci_layer_->IncomingEvent(hci::RemoteOobDataRequestBuilder::Create(device.GetAddress()));
218     synchronize();
219   }
220 
ReceiveUserConfirmationRequest(hci::AddressWithType device,uint32_t numeric_value)221   void ReceiveUserConfirmationRequest(hci::AddressWithType device, uint32_t numeric_value) {
222     hci_layer_->IncomingEvent(hci::UserConfirmationRequestBuilder::Create(device.GetAddress(), numeric_value));
223     synchronize();
224   }
225 
ReceiveSimplePairingComplete(hci::ErrorCode status,hci::AddressWithType device)226   void ReceiveSimplePairingComplete(hci::ErrorCode status, hci::AddressWithType device) {
227     hci_layer_->IncomingEvent(hci::SimplePairingCompleteBuilder::Create(status, device.GetAddress()));
228     synchronize();
229   }
230 
ReceiveLinkKeyNotification(hci::AddressWithType device,std::array<uint8_t,16> link_key,hci::KeyType key_type)231   void ReceiveLinkKeyNotification(hci::AddressWithType device, std::array<uint8_t, 16> link_key,
232                                   hci::KeyType key_type) {
233     hci_layer_->IncomingEvent(hci::LinkKeyNotificationBuilder::Create(device.GetAddress(), link_key, key_type));
234     synchronize();
235   }
236 
237   TestModuleRegistry fake_registry_;
238   Thread& thread_ = fake_registry_.GetTestThread();
239   Handler* handler_ = nullptr;
240   FakeHciLayer* hci_layer_ = nullptr;
241   hci::AddressWithType device_;
242   SecurityManagerChannelCallback* channel_callback_ = nullptr;
243   channel::SecurityManagerChannel* channel_ = nullptr;
244   pairing::ClassicPairingHandler* pairing_handler_ = nullptr;
245   std::shared_ptr<record::SecurityRecord> security_record_ = nullptr;
246   UI* user_interface_;
247   os::Handler* user_interface_handler_;
248   l2cap::classic::SecurityInterface* security_interface_ = nullptr;
249   FakeNameDbModule* name_db_module_ = nullptr;
250 };
251 
252 // Security Manager Boot Sequence (Required for SSP, these are already set at boot time)
253 //  - WriteSimplePairingMode
254 //  - WriteSecureConnectionsHostSupport
255 //  - WriteAuthenticatedPayloadTimeout
256 
257 /*** Locally initiated ***/
258 // Security Pairing Sequence (JustWorks)
259 //  -> *Establish L2CAP connection*
260 //  -> AuthenticationRequested (L2CAP handles this)
261 //  <- LinkKeyRequest   // This is entry point for remote initiated
262 //  -> LinkKeyRequestNegativeReply
263 //  <- IoCapabilityRequest
264 //  -> IoCapabilityRequestReply
265 //  <- IoCapabilityResponse
266 //  <- UserConfirmationRequest
267 //  -> UserConfirmationRequestReply (auto)
268 //  <- SimplePairingComplete
269 //  <- LinkKeyNotification
270 //  <- AuthenticationComplete
271 //  -> SetConnectionEncryption
272 //  <- EncryptionChange
273 //  -> L2capConnectionResponse (if triggered by L2cap connection request)
274 
GetLastCommand(FakeHciLayer * hci_layer)275 hci::SecurityCommandView GetLastCommand(FakeHciLayer* hci_layer) {
276   auto last_command = std::move(hci_layer->GetLastCommand()->command);
277   auto command_packet = GetPacketView(std::move(last_command));
278   auto command_packet_view = hci::CommandView::Create(command_packet);
279   auto security_command_view = hci::SecurityCommandView::Create(command_packet_view);
280   if (!security_command_view.IsValid()) {
281     log::error("Invalid security command received");
282   }
283   return security_command_view;
284 }
285 
TEST_F(ClassicPairingHandlerTest,setup_teardown)286 TEST_F(ClassicPairingHandlerTest, setup_teardown) {}
287 
288 /*** JustWorks (Numeric Comparison w/ no UI) ***/
289 // display_only + display_only is JustWorks no confirmation
290 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_display_only_display_only_temp)291 TEST_F(ClassicPairingHandlerTest, locally_initiatied_display_only_display_only_temp) {
292   hci::IoCapability injected_io_capability = hci::IoCapability::DISPLAY_ONLY;
293   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
294   pairing_handler_->Initiate(
295       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
296   ReceiveLinkKeyRequest(device_);
297   auto security_command_view = GetLastCommand(hci_layer_);
298   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
299   ASSERT_TRUE(link_key_neg_reply.IsValid());
300   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
301   ReceiveIoCapabilityRequest(device_);
302   security_command_view = GetLastCommand(hci_layer_);
303   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
304   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
305   ASSERT_TRUE(io_cap_request_reply.IsValid());
306   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
307   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
308   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
309   ReceiveIoCapabilityResponse(device_, hci::IoCapability::DISPLAY_ONLY, hci::OobDataPresent::NOT_PRESENT,
310                               hci::AuthenticationRequirements::NO_BONDING);
311   uint32_t numeric_value = 0x123;
312   ReceiveUserConfirmationRequest(device_, numeric_value);
313   security_command_view = GetLastCommand(hci_layer_);
314   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
315   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
316   ASSERT_TRUE(user_conf_request_reply.IsValid());
317   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
318   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
319   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
320   ReceiveLinkKeyNotification(device_, link_key, key_type);
321   ASSERT_EQ(link_key, security_record_->GetLinkKey());
322   ASSERT_EQ(key_type, security_record_->GetKeyType());
323   ASSERT_FALSE(security_record_->IsAuthenticated());
324   ASSERT_FALSE(security_record_->RequiresMitmProtection());
325 }
326 
327 // display_only + display_yes_no is JustWorks no confirmation
328 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_display_only_display_yes_no_temp)329 TEST_F(ClassicPairingHandlerTest, locally_initiatied_display_only_display_yes_no_temp) {
330   hci::IoCapability injected_io_capability = hci::IoCapability::DISPLAY_ONLY;
331   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
332   pairing_handler_->Initiate(
333       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
334   ReceiveLinkKeyRequest(device_);
335   auto security_command_view = GetLastCommand(hci_layer_);
336   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
337   ASSERT_TRUE(link_key_neg_reply.IsValid());
338   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
339   ReceiveIoCapabilityRequest(device_);
340   security_command_view = GetLastCommand(hci_layer_);
341   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
342   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
343   ASSERT_TRUE(io_cap_request_reply.IsValid());
344   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
345   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
346   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
347   ReceiveIoCapabilityResponse(device_, hci::IoCapability::DISPLAY_YES_NO, hci::OobDataPresent::NOT_PRESENT,
348                               hci::AuthenticationRequirements::NO_BONDING);
349   uint32_t numeric_value = 0x123;
350   ReceiveUserConfirmationRequest(device_, numeric_value);
351   security_command_view = GetLastCommand(hci_layer_);
352   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
353   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
354   ASSERT_TRUE(user_conf_request_reply.IsValid());
355   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
356   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
357   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
358   ReceiveLinkKeyNotification(device_, link_key, key_type);
359   ASSERT_EQ(link_key, security_record_->GetLinkKey());
360   ASSERT_EQ(key_type, security_record_->GetKeyType());
361   ASSERT_TRUE(security_record_->IsAuthenticated());
362   ASSERT_FALSE(security_record_->RequiresMitmProtection());
363 }
364 
365 // display_only + no_input_no_output is JustWorks no confirmation
366 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_display_only_no_input_no_output_temp)367 TEST_F(ClassicPairingHandlerTest, locally_initiatied_display_only_no_input_no_output_temp) {
368   hci::IoCapability injected_io_capability = hci::IoCapability::DISPLAY_ONLY;
369   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
370   pairing_handler_->Initiate(
371       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
372   ReceiveLinkKeyRequest(device_);
373   auto security_command_view = GetLastCommand(hci_layer_);
374   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
375   ASSERT_TRUE(link_key_neg_reply.IsValid());
376   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
377   ReceiveIoCapabilityRequest(device_);
378   security_command_view = GetLastCommand(hci_layer_);
379   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
380   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
381   ASSERT_TRUE(io_cap_request_reply.IsValid());
382   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
383   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
384   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
385   ReceiveIoCapabilityResponse(device_, hci::IoCapability::NO_INPUT_NO_OUTPUT, hci::OobDataPresent::NOT_PRESENT,
386                               hci::AuthenticationRequirements::NO_BONDING);
387   uint32_t numeric_value = 0x123;
388   ReceiveUserConfirmationRequest(device_, numeric_value);
389   security_command_view = GetLastCommand(hci_layer_);
390   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
391   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
392   ASSERT_TRUE(user_conf_request_reply.IsValid());
393   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
394   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
395   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
396   ReceiveLinkKeyNotification(device_, link_key, key_type);
397   ASSERT_EQ(link_key, security_record_->GetLinkKey());
398   ASSERT_EQ(key_type, security_record_->GetKeyType());
399   ASSERT_TRUE(security_record_->IsAuthenticated());
400   ASSERT_FALSE(security_record_->RequiresMitmProtection());
401 }
402 
403 // keyboard_only + no_input_no_output is JustWorks no confirmation
404 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_keyboard_only_no_input_no_output_temp)405 TEST_F(ClassicPairingHandlerTest, locally_initiatied_keyboard_only_no_input_no_output_temp) {
406   hci::IoCapability injected_io_capability = hci::IoCapability::KEYBOARD_ONLY;
407   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
408   pairing_handler_->Initiate(
409       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
410   ReceiveLinkKeyRequest(device_);
411   auto security_command_view = GetLastCommand(hci_layer_);
412   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
413   ASSERT_TRUE(link_key_neg_reply.IsValid());
414   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
415   ReceiveIoCapabilityRequest(device_);
416   security_command_view = GetLastCommand(hci_layer_);
417   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
418   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
419   ASSERT_TRUE(io_cap_request_reply.IsValid());
420   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
421   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
422   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
423   ReceiveIoCapabilityResponse(device_, hci::IoCapability::NO_INPUT_NO_OUTPUT, hci::OobDataPresent::NOT_PRESENT,
424                               hci::AuthenticationRequirements::NO_BONDING);
425   uint32_t numeric_value = 0x123;
426   ReceiveUserConfirmationRequest(device_, numeric_value);
427   security_command_view = GetLastCommand(hci_layer_);
428   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
429   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
430   ASSERT_TRUE(user_conf_request_reply.IsValid());
431   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
432   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
433   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
434   ReceiveLinkKeyNotification(device_, link_key, key_type);
435   ASSERT_EQ(link_key, security_record_->GetLinkKey());
436   ASSERT_EQ(key_type, security_record_->GetKeyType());
437   ASSERT_FALSE(security_record_->IsAuthenticated());
438   ASSERT_FALSE(security_record_->RequiresMitmProtection());
439 }
440 
441 // no_input_no_output + display_only is JustWorks no confirmation
442 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_display_only_temp)443 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_display_only_temp) {
444   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
445   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
446   pairing_handler_->Initiate(
447       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
448   ReceiveLinkKeyRequest(device_);
449   auto security_command_view = GetLastCommand(hci_layer_);
450   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
451   ASSERT_TRUE(link_key_neg_reply.IsValid());
452   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
453   ReceiveIoCapabilityRequest(device_);
454   security_command_view = GetLastCommand(hci_layer_);
455   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
456   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
457   ASSERT_TRUE(io_cap_request_reply.IsValid());
458   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
459   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
460   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
461   ReceiveIoCapabilityResponse(device_, hci::IoCapability::DISPLAY_ONLY, hci::OobDataPresent::NOT_PRESENT,
462                               hci::AuthenticationRequirements::NO_BONDING);
463   uint32_t numeric_value = 0x123;
464   ReceiveUserConfirmationRequest(device_, numeric_value);
465   security_command_view = GetLastCommand(hci_layer_);
466   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
467   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
468   ASSERT_TRUE(user_conf_request_reply.IsValid());
469   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
470   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
471   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
472   ReceiveLinkKeyNotification(device_, link_key, key_type);
473   ASSERT_EQ(link_key, security_record_->GetLinkKey());
474   ASSERT_EQ(key_type, security_record_->GetKeyType());
475   ASSERT_FALSE(security_record_->IsAuthenticated());
476   ASSERT_FALSE(security_record_->RequiresMitmProtection());
477 }
478 
479 // no_input_no_output + display_yes_no is JustWorks no confirmation
480 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_display_yes_no_temp)481 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_display_yes_no_temp) {
482   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
483   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
484   pairing_handler_->Initiate(
485       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
486   ReceiveLinkKeyRequest(device_);
487   auto security_command_view = GetLastCommand(hci_layer_);
488   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
489   ASSERT_TRUE(link_key_neg_reply.IsValid());
490   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
491   ReceiveIoCapabilityRequest(device_);
492   security_command_view = GetLastCommand(hci_layer_);
493   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
494   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
495   ASSERT_TRUE(io_cap_request_reply.IsValid());
496   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
497   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
498   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
499   ReceiveIoCapabilityResponse(device_, hci::IoCapability::DISPLAY_YES_NO, hci::OobDataPresent::NOT_PRESENT,
500                               hci::AuthenticationRequirements::NO_BONDING);
501   uint32_t numeric_value = 0x123;
502   ReceiveUserConfirmationRequest(device_, numeric_value);
503   security_command_view = GetLastCommand(hci_layer_);
504   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
505   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
506   ASSERT_TRUE(user_conf_request_reply.IsValid());
507   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
508   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
509   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
510   ReceiveLinkKeyNotification(device_, link_key, key_type);
511   ASSERT_EQ(link_key, security_record_->GetLinkKey());
512   ASSERT_EQ(key_type, security_record_->GetKeyType());
513   ASSERT_FALSE(security_record_->IsAuthenticated());
514   ASSERT_FALSE(security_record_->RequiresMitmProtection());
515 }
516 
517 // no_input_no_output + keyboard_only is JustWorks no confirmation
518 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_keyboard_only_temp)519 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_keyboard_only_temp) {
520   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
521   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
522   pairing_handler_->Initiate(
523       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
524   ReceiveLinkKeyRequest(device_);
525   auto security_command_view = GetLastCommand(hci_layer_);
526   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
527   ASSERT_TRUE(link_key_neg_reply.IsValid());
528   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
529   ReceiveIoCapabilityRequest(device_);
530   security_command_view = GetLastCommand(hci_layer_);
531   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
532   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
533   ASSERT_TRUE(io_cap_request_reply.IsValid());
534   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
535   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
536   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
537   ReceiveIoCapabilityResponse(device_, hci::IoCapability::KEYBOARD_ONLY, hci::OobDataPresent::NOT_PRESENT,
538                               hci::AuthenticationRequirements::NO_BONDING);
539   uint32_t numeric_value = 0x123;
540   ReceiveUserConfirmationRequest(device_, numeric_value);
541   security_command_view = GetLastCommand(hci_layer_);
542   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
543   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
544   ASSERT_TRUE(user_conf_request_reply.IsValid());
545   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
546   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
547   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
548   ReceiveLinkKeyNotification(device_, link_key, key_type);
549   ASSERT_EQ(link_key, security_record_->GetLinkKey());
550   ASSERT_EQ(key_type, security_record_->GetKeyType());
551   ASSERT_FALSE(security_record_->IsAuthenticated());
552   ASSERT_FALSE(security_record_->RequiresMitmProtection());
553 }
554 
555 // no_input_no_output + no_input_no_output is JustWorks no confirmation
556 // Needs dialog as per security a bug unless pairing is temporary
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_no_input_no_output_temp)557 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_no_input_no_output_temp) {
558   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
559   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
560   pairing_handler_->Initiate(
561       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
562   ReceiveLinkKeyRequest(device_);
563   auto security_command_view = GetLastCommand(hci_layer_);
564   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
565   ASSERT_TRUE(link_key_neg_reply.IsValid());
566   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
567   ReceiveIoCapabilityRequest(device_);
568   security_command_view = GetLastCommand(hci_layer_);
569   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
570   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
571   ASSERT_TRUE(io_cap_request_reply.IsValid());
572   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
573   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
574   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
575   ReceiveIoCapabilityResponse(device_, hci::IoCapability::NO_INPUT_NO_OUTPUT, hci::OobDataPresent::NOT_PRESENT,
576                               hci::AuthenticationRequirements::NO_BONDING);
577   uint32_t numeric_value = 0x123;
578   ReceiveUserConfirmationRequest(device_, numeric_value);
579   security_command_view = GetLastCommand(hci_layer_);
580   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, security_command_view.GetOpCode());
581   auto user_conf_request_reply = hci::UserConfirmationRequestReplyView::Create(security_command_view);
582   ASSERT_TRUE(user_conf_request_reply.IsValid());
583   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
584   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
585   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
586   ReceiveLinkKeyNotification(device_, link_key, key_type);
587   ASSERT_EQ(link_key, security_record_->GetLinkKey());
588   ASSERT_EQ(key_type, security_record_->GetKeyType());
589   ASSERT_FALSE(security_record_->IsAuthenticated());
590   ASSERT_FALSE(security_record_->RequiresMitmProtection());
591 }
592 
TEST_F(ClassicPairingHandlerTest,remote_initiatied_no_input_no_output_no_input_no_output_with_missing_oob_data)593 TEST_F(ClassicPairingHandlerTest, remote_initiatied_no_input_no_output_no_input_no_output_with_missing_oob_data) {}
594 
595 // CreateBondOutOfBand no_input_no_output + no_input_no_output OOB Data missing when asked
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_no_input_no_output_with_missing_oob_data)596 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_no_input_no_output_with_missing_oob_data) {
597   expect_success_ = false;
598   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
599   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
600   pairing_handler_->Initiate(
601       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), pairing::OobData());
602 
603   ReceiveLinkKeyRequest(device_);
604   auto security_command_view = GetLastCommand(hci_layer_);
605   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
606   ASSERT_TRUE(link_key_neg_reply.IsValid());
607   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
608   ReceiveIoCapabilityRequest(device_);
609   security_command_view = GetLastCommand(hci_layer_);
610   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
611   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
612   ASSERT_TRUE(io_cap_request_reply.IsValid());
613   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
614   ASSERT_EQ(hci::OobDataPresent::NOT_PRESENT, io_cap_request_reply.GetOobPresent());
615   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
616   ReceiveIoCapabilityResponse(
617       device_,
618       hci::IoCapability::NO_INPUT_NO_OUTPUT,
619       hci::OobDataPresent::NOT_PRESENT,
620       hci::AuthenticationRequirements::NO_BONDING);
621   // At this point the pairing handler thinks it has NOT_PRESENT
622   ReceiveOobDataRequest(device_);
623   security_command_view = GetLastCommand(hci_layer_);
624   auto oob_data_req_neg_reply = hci::RemoteOobDataRequestNegativeReplyView::Create(security_command_view);
625   ASSERT_TRUE(oob_data_req_neg_reply.IsValid());
626   ASSERT_EQ(OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY, oob_data_req_neg_reply.GetOpCode());
627   ReceiveSimplePairingComplete(hci::ErrorCode::AUTHENTICATION_FAILURE, device_);
628 }
629 
630 // CreateBondOutOfBand no_input_no_output + no_input_no_output OOB Data P192
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_no_input_no_output_p192_oob_data)631 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_no_input_no_output_p192_oob_data) {
632   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
633   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
634   pairing::OobData oob_data(
635       {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
636   pairing_handler_->Initiate(
637       true, injected_io_capability, injected_authentication_requirements, oob_data, pairing::OobData());
638 
639   ReceiveLinkKeyRequest(device_);
640   auto security_command_view = GetLastCommand(hci_layer_);
641   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
642   ASSERT_TRUE(link_key_neg_reply.IsValid());
643   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
644   ReceiveIoCapabilityRequest(device_);
645   security_command_view = GetLastCommand(hci_layer_);
646   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
647   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
648   ASSERT_TRUE(io_cap_request_reply.IsValid());
649   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
650   ASSERT_EQ(hci::OobDataPresent::P_192_PRESENT, io_cap_request_reply.GetOobPresent());
651   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
652   ReceiveIoCapabilityResponse(
653       device_,
654       hci::IoCapability::NO_INPUT_NO_OUTPUT,
655       hci::OobDataPresent::NOT_PRESENT,
656       hci::AuthenticationRequirements::NO_BONDING);
657   // At this point the pairing handler thinks it has NOT_PRESENT
658   ReceiveOobDataRequest(device_);
659   security_command_view = GetLastCommand(hci_layer_);
660   // NOTE(optedoblivion): Extended data is manually disabled in the pairing handler
661   // since the controller doesn't seem to currently have support.
662   auto oob_data_req_reply = hci::RemoteOobDataRequestReplyView::Create(security_command_view);
663   ASSERT_TRUE(oob_data_req_reply.IsValid());
664   ASSERT_EQ(OpCode::REMOTE_OOB_DATA_REQUEST_REPLY, oob_data_req_reply.GetOpCode());
665   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
666   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
667   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
668   ReceiveLinkKeyNotification(device_, link_key, key_type);
669   ASSERT_EQ(link_key, security_record_->GetLinkKey());
670   ASSERT_EQ(key_type, security_record_->GetKeyType());
671   ASSERT_FALSE(security_record_->IsAuthenticated());
672   ASSERT_FALSE(security_record_->RequiresMitmProtection());
673 }
674 
675 // CreateBondOutOfBand no_input_no_output + no_input_no_output OOB Data P256
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_no_input_no_output_p256_oob_data)676 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_no_input_no_output_p256_oob_data) {
677   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
678   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
679   pairing::OobData oob_data(
680       {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
681   pairing_handler_->Initiate(
682       true, injected_io_capability, injected_authentication_requirements, pairing::OobData(), oob_data);
683   ReceiveLinkKeyRequest(device_);
684   auto security_command_view = GetLastCommand(hci_layer_);
685   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
686   ASSERT_TRUE(link_key_neg_reply.IsValid());
687   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
688   ReceiveIoCapabilityRequest(device_);
689   security_command_view = GetLastCommand(hci_layer_);
690   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
691   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
692   ASSERT_TRUE(io_cap_request_reply.IsValid());
693   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
694   ASSERT_EQ(hci::OobDataPresent::P_256_PRESENT, io_cap_request_reply.GetOobPresent());
695   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
696   ReceiveIoCapabilityResponse(
697       device_,
698       hci::IoCapability::NO_INPUT_NO_OUTPUT,
699       hci::OobDataPresent::NOT_PRESENT,
700       hci::AuthenticationRequirements::NO_BONDING);
701   // At this point the pairing handler thinks it has NOT_PRESENT
702   ReceiveOobDataRequest(device_);
703   security_command_view = GetLastCommand(hci_layer_);
704   auto oob_data_req_reply = hci::RemoteOobExtendedDataRequestReplyView::Create(security_command_view);
705   ASSERT_TRUE(oob_data_req_reply.IsValid());
706   ASSERT_EQ(OpCode::REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY, oob_data_req_reply.GetOpCode());
707   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
708   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
709   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
710   ReceiveLinkKeyNotification(device_, link_key, key_type);
711   ASSERT_EQ(link_key, security_record_->GetLinkKey());
712   ASSERT_EQ(key_type, security_record_->GetKeyType());
713   ASSERT_FALSE(security_record_->IsAuthenticated());
714   ASSERT_FALSE(security_record_->RequiresMitmProtection());
715 }
716 
717 // CreateBondOutOfBand no_input_no_output + no_input_no_output OOB Data P192 and 256
TEST_F(ClassicPairingHandlerTest,locally_initiatied_no_input_no_output_no_input_no_output_p192_and_256_oob_data)718 TEST_F(ClassicPairingHandlerTest, locally_initiatied_no_input_no_output_no_input_no_output_p192_and_256_oob_data) {
719   hci::IoCapability injected_io_capability = hci::IoCapability::NO_INPUT_NO_OUTPUT;
720   hci::AuthenticationRequirements injected_authentication_requirements = hci::AuthenticationRequirements::NO_BONDING;
721   pairing::OobData oob_data(
722       {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
723   pairing_handler_->Initiate(true, injected_io_capability, injected_authentication_requirements, oob_data, oob_data);
724   ReceiveLinkKeyRequest(device_);
725   auto security_command_view = GetLastCommand(hci_layer_);
726   auto link_key_neg_reply = hci::LinkKeyRequestNegativeReplyView::Create(security_command_view);
727   ASSERT_TRUE(link_key_neg_reply.IsValid());
728   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, link_key_neg_reply.GetOpCode());
729   ReceiveIoCapabilityRequest(device_);
730   security_command_view = GetLastCommand(hci_layer_);
731   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, security_command_view.GetOpCode());
732   auto io_cap_request_reply = hci::IoCapabilityRequestReplyView::Create(security_command_view);
733   ASSERT_TRUE(io_cap_request_reply.IsValid());
734   ASSERT_EQ(injected_io_capability, io_cap_request_reply.GetIoCapability());
735   ASSERT_EQ(hci::OobDataPresent::P_192_AND_256_PRESENT, io_cap_request_reply.GetOobPresent());
736   ASSERT_EQ(injected_authentication_requirements, io_cap_request_reply.GetAuthenticationRequirements());
737   ReceiveIoCapabilityResponse(
738       device_,
739       hci::IoCapability::NO_INPUT_NO_OUTPUT,
740       hci::OobDataPresent::NOT_PRESENT,
741       hci::AuthenticationRequirements::NO_BONDING);
742   // At this point the pairing handler thinks it has NOT_PRESENT
743   ReceiveOobDataRequest(device_);
744   security_command_view = GetLastCommand(hci_layer_);
745   auto oob_data_req_reply = hci::RemoteOobExtendedDataRequestReplyView::Create(security_command_view);
746   ASSERT_TRUE(oob_data_req_reply.IsValid());
747   ASSERT_EQ(OpCode::REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY, oob_data_req_reply.GetOpCode());
748   ReceiveSimplePairingComplete(hci::ErrorCode::SUCCESS, device_);
749   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
750   hci::KeyType key_type = hci::KeyType::DEBUG_COMBINATION;
751   ReceiveLinkKeyNotification(device_, link_key, key_type);
752   ASSERT_EQ(link_key, security_record_->GetLinkKey());
753   ASSERT_EQ(key_type, security_record_->GetKeyType());
754   ASSERT_FALSE(security_record_->IsAuthenticated());
755   ASSERT_FALSE(security_record_->RequiresMitmProtection());
756 }
757 
758 /*** Numeric Comparison ***/
759 // display_yes_no + display_only
760 
761 // display_yes_no + display_yes_no
762 // display_yes_no + keyboard_only
763 // display_yes_no + no_input_no_output
764 
765 // keyboard_only + display_only
766 // keyboard_only + display_yes_no
767 
768 // keyboard_only + keyboard_only  (a just works I missed)
769 
770 // Remotely initiated
771 
772 // Collisions
773 
774 }  // namespace
775 }  // namespace pairing
776 }  // namespace security
777 }  // namespace bluetooth
778