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/channel/security_manager_channel.h"
19 
20 #include <bluetooth/log.h>
21 #include <gtest/gtest.h>
22 
23 #include "hci/address.h"
24 #include "hci/hci_packets.h"
25 #include "packet/raw_builder.h"
26 #include "security/test/fake_hci_layer.h"
27 #include "security/test/fake_security_interface.h"
28 
29 namespace bluetooth {
30 namespace security {
31 namespace channel {
32 namespace {
33 
34 using bluetooth::security::channel::SecurityManagerChannel;
35 using hci::Address;
36 using hci::AuthenticationRequirements;
37 using hci::CommandCompleteBuilder;
38 using hci::IoCapabilityRequestReplyBuilder;
39 using hci::IoCapabilityRequestView;
40 using hci::OobDataPresent;
41 using hci::OpCode;
42 using os::Handler;
43 using os::Thread;
44 using packet::RawBuilder;
45 
46 static bool on_link_connected_called = false;
47 static bool on_link_disconnected_called = false;
48 
49 class FakeSecurityManagerChannel : public SecurityManagerChannel {
50  public:
FakeSecurityManagerChannel(os::Handler * handler,hci::HciLayer * hci_layer)51   FakeSecurityManagerChannel(os::Handler* handler, hci::HciLayer* hci_layer)
52       : SecurityManagerChannel(handler, hci_layer) {}
~FakeSecurityManagerChannel()53   ~FakeSecurityManagerChannel() {}
54 
OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link)55   void OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link) {
56     on_link_connected_called = true;
57   }
58 
OnLinkDisconnected(hci::Address address)59   void OnLinkDisconnected(hci::Address address) {
60     on_link_disconnected_called = true;
61   }
62 
OnAuthenticationComplete(hci::ErrorCode hci_status,hci::Address remote)63   void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address remote) override {}
64 
OnEncryptionChange(hci::Address address,bool encrypted)65   void OnEncryptionChange(hci::Address address, bool encrypted) override {}
66 };
67 
68 class SecurityManagerChannelCallback : public ISecurityManagerChannelListener {
69  public:
70   // HCI
71   bool receivedChangeConnectionLinkKeyComplete = false;
72   bool receivedCentralLinkKeyComplete = false;
73   bool receivedPinCodeRequest = false;
74   bool receivedLinkKeyRequest = false;
75   bool receivedLinkKeyNotification = false;
76   bool receivedIoCapabilityRequest = false;
77   bool receivedIoCapabilityResponse = false;
78   bool receivedSimplePairingComplete = false;
79   bool receivedReturnLinkKeys = false;
80   bool receivedEncryptionChange = false;
81   bool receivedEncryptionKeyRefreshComplete = false;
82   bool receivedRemoteOobDataRequest = false;
83   bool receivedUserPasskeyNotification = false;
84   bool receivedUserPasskeyRequest = false;
85   bool receivedKeypressNotification = false;
86   bool receivedUserConfirmationRequest = false;
87 
OnReceive(hci::AddressWithType device,hci::ChangeConnectionLinkKeyCompleteView packet)88   void OnReceive(hci::AddressWithType device, hci::ChangeConnectionLinkKeyCompleteView packet) {
89     ASSERT_TRUE(packet.IsValid());
90     receivedChangeConnectionLinkKeyComplete = true;
91   }
OnReceive(hci::AddressWithType device,hci::CentralLinkKeyCompleteView packet)92   void OnReceive(hci::AddressWithType device, hci::CentralLinkKeyCompleteView packet) {
93     ASSERT_TRUE(packet.IsValid());
94     receivedCentralLinkKeyComplete = true;
95   }
OnReceive(hci::AddressWithType device,hci::PinCodeRequestView packet)96   void OnReceive(hci::AddressWithType device, hci::PinCodeRequestView packet) {
97     ASSERT_TRUE(packet.IsValid());
98     receivedPinCodeRequest = true;
99   }
OnReceive(hci::AddressWithType device,hci::LinkKeyRequestView packet)100   void OnReceive(hci::AddressWithType device, hci::LinkKeyRequestView packet) {
101     ASSERT_TRUE(packet.IsValid());
102     receivedLinkKeyRequest = true;
103   }
OnReceive(hci::AddressWithType device,hci::LinkKeyNotificationView packet)104   void OnReceive(hci::AddressWithType device, hci::LinkKeyNotificationView packet) {
105     ASSERT_TRUE(packet.IsValid());
106     receivedLinkKeyNotification = true;
107   }
OnReceive(hci::AddressWithType device,hci::IoCapabilityRequestView packet)108   void OnReceive(hci::AddressWithType device, hci::IoCapabilityRequestView packet) {
109     ASSERT_TRUE(packet.IsValid());
110     receivedIoCapabilityRequest = true;
111   }
OnReceive(hci::AddressWithType device,hci::IoCapabilityResponseView packet)112   void OnReceive(hci::AddressWithType device, hci::IoCapabilityResponseView packet) {
113     ASSERT_TRUE(packet.IsValid());
114     receivedIoCapabilityResponse = true;
115   }
OnReceive(hci::AddressWithType device,hci::SimplePairingCompleteView packet)116   void OnReceive(hci::AddressWithType device, hci::SimplePairingCompleteView packet) {
117     ASSERT_TRUE(packet.IsValid());
118     receivedSimplePairingComplete = true;
119   }
OnReceive(hci::AddressWithType device,hci::ReturnLinkKeysView packet)120   void OnReceive(hci::AddressWithType device, hci::ReturnLinkKeysView packet) {
121     ASSERT_TRUE(packet.IsValid());
122     receivedReturnLinkKeys = true;
123   }
OnReceive(hci::AddressWithType device,hci::EncryptionChangeView packet)124   void OnReceive(hci::AddressWithType device, hci::EncryptionChangeView packet) {
125     ASSERT_TRUE(packet.IsValid());
126     receivedEncryptionChange = true;
127   }
OnReceive(hci::AddressWithType device,hci::EncryptionKeyRefreshCompleteView packet)128   void OnReceive(hci::AddressWithType device, hci::EncryptionKeyRefreshCompleteView packet) {
129     ASSERT_TRUE(packet.IsValid());
130     receivedEncryptionKeyRefreshComplete = true;
131   }
OnReceive(hci::AddressWithType device,hci::RemoteOobDataRequestView packet)132   void OnReceive(hci::AddressWithType device, hci::RemoteOobDataRequestView packet) {
133     ASSERT_TRUE(packet.IsValid());
134     receivedRemoteOobDataRequest = true;
135   }
OnReceive(hci::AddressWithType device,hci::UserPasskeyNotificationView packet)136   void OnReceive(hci::AddressWithType device, hci::UserPasskeyNotificationView packet) {
137     ASSERT_TRUE(packet.IsValid());
138     receivedUserPasskeyNotification = true;
139   }
OnReceive(hci::AddressWithType device,hci::KeypressNotificationView packet)140   void OnReceive(hci::AddressWithType device, hci::KeypressNotificationView packet) {
141     ASSERT_TRUE(packet.IsValid());
142     receivedKeypressNotification = true;
143   }
OnReceive(hci::AddressWithType device,hci::UserConfirmationRequestView packet)144   void OnReceive(hci::AddressWithType device, hci::UserConfirmationRequestView packet) {
145     ASSERT_TRUE(packet.IsValid());
146     receivedUserConfirmationRequest = true;
147   }
OnReceive(hci::AddressWithType device,hci::UserPasskeyRequestView packet)148   void OnReceive(hci::AddressWithType device, hci::UserPasskeyRequestView packet) {
149     ASSERT_TRUE(packet.IsValid());
150     receivedUserPasskeyRequest = true;
151   }
152 
OnHciEventReceived(EventView packet)153   void OnHciEventReceived(EventView packet) override {
154     auto event = EventView::Create(packet);
155     log::assert_that(event.IsValid(), "Received invalid packet");
156     const hci::EventCode code = event.GetEventCode();
157     switch (code) {
158       case hci::EventCode::CHANGE_CONNECTION_LINK_KEY_COMPLETE:
159         OnReceive(hci::AddressWithType(), hci::ChangeConnectionLinkKeyCompleteView::Create(event));
160         break;
161       case hci::EventCode::CENTRAL_LINK_KEY_COMPLETE:
162         OnReceive(hci::AddressWithType(), hci::CentralLinkKeyCompleteView::Create(event));
163         break;
164       case hci::EventCode::PIN_CODE_REQUEST:
165         OnReceive(hci::AddressWithType(), hci::PinCodeRequestView::Create(event));
166         break;
167       case hci::EventCode::LINK_KEY_REQUEST:
168         OnReceive(hci::AddressWithType(), hci::LinkKeyRequestView::Create(event));
169         break;
170       case hci::EventCode::LINK_KEY_NOTIFICATION:
171         OnReceive(hci::AddressWithType(), hci::LinkKeyNotificationView::Create(event));
172         break;
173       case hci::EventCode::IO_CAPABILITY_REQUEST:
174         OnReceive(hci::AddressWithType(), hci::IoCapabilityRequestView::Create(event));
175         break;
176       case hci::EventCode::IO_CAPABILITY_RESPONSE:
177         OnReceive(hci::AddressWithType(), hci::IoCapabilityResponseView::Create(event));
178         break;
179       case hci::EventCode::SIMPLE_PAIRING_COMPLETE:
180         OnReceive(hci::AddressWithType(), hci::SimplePairingCompleteView::Create(event));
181         break;
182       case hci::EventCode::RETURN_LINK_KEYS:
183         OnReceive(hci::AddressWithType(), hci::ReturnLinkKeysView::Create(event));
184         break;
185       case hci::EventCode::ENCRYPTION_CHANGE:
186         OnReceive(hci::AddressWithType(), hci::EncryptionChangeView::Create(event));
187         break;
188       case hci::EventCode::ENCRYPTION_KEY_REFRESH_COMPLETE:
189         OnReceive(hci::AddressWithType(), hci::EncryptionKeyRefreshCompleteView::Create(event));
190         break;
191       case hci::EventCode::REMOTE_OOB_DATA_REQUEST:
192         OnReceive(hci::AddressWithType(), hci::RemoteOobDataRequestView::Create(event));
193         break;
194       case hci::EventCode::USER_PASSKEY_NOTIFICATION:
195         OnReceive(hci::AddressWithType(), hci::UserPasskeyNotificationView::Create(event));
196         break;
197       case hci::EventCode::KEYPRESS_NOTIFICATION:
198         OnReceive(hci::AddressWithType(), hci::KeypressNotificationView::Create(event));
199         break;
200       case hci::EventCode::USER_CONFIRMATION_REQUEST:
201         OnReceive(hci::AddressWithType(), hci::UserConfirmationRequestView::Create(event));
202         break;
203       case hci::EventCode::USER_PASSKEY_REQUEST:
204         OnReceive(hci::AddressWithType(), hci::UserPasskeyRequestView::Create(event));
205         break;
206       default:
207         log::fatal("Cannot handle received packet: {}", hci::EventCodeText(code));
208         break;
209     }
210   }
211 
OnConnectionClosed(hci::Address address)212   void OnConnectionClosed(hci::Address address) override {
213     log::info("Called");
214   }
215 };
216 
217 class SecurityManagerChannelTest : public ::testing::Test {
218  protected:
SetUp()219   void SetUp() override {
220     hci::Address address;
221     hci::Address::FromString("01:23:45:67:89:AB:CD", address);
222     device_ = hci::AddressWithType(address, hci::AddressType::PUBLIC_DEVICE_ADDRESS);
223     on_link_connected_called = false;
224     on_link_disconnected_called = false;
225     handler_ = new Handler(&thread_);
226     callback_ = new SecurityManagerChannelCallback();
227     hci_layer_ = new FakeHciLayer();
228     fake_registry_.InjectTestModule(&FakeHciLayer::Factory, hci_layer_);
229     fake_registry_.Start<FakeHciLayer>(&thread_);
230     channel_ = new FakeSecurityManagerChannel(handler_, hci_layer_);
231     channel_->SetChannelListener(callback_);
232     security_interface_ = new FakeSecurityInterface(handler_, channel_);
233     channel_->SetSecurityInterface(security_interface_);
234   }
235 
TearDown()236   void TearDown() override {
237     channel_->SetChannelListener(nullptr);
238     handler_->Clear();
239     synchronize();
240     fake_registry_.StopAll();
241     delete handler_;
242     delete channel_;
243     delete callback_;
244     delete security_interface_;
245   }
246 
synchronize()247   void synchronize() {
248     fake_registry_.SynchronizeModuleHandler(&FakeHciLayer::Factory, std::chrono::milliseconds(20));
249   }
250 
251   TestModuleRegistry fake_registry_;
252   Thread& thread_ = fake_registry_.GetTestThread();
253   Handler* handler_ = nullptr;
254   FakeHciLayer* hci_layer_ = nullptr;
255   l2cap::classic::SecurityInterface* security_interface_ = nullptr;
256   SecurityManagerChannel* channel_ = nullptr;
257   SecurityManagerChannelCallback* callback_ = nullptr;
258   hci::AddressWithType device_;
259 };
260 
TEST_F(SecurityManagerChannelTest,setup_teardown)261 TEST_F(SecurityManagerChannelTest, setup_teardown) {}
262 
TEST_F(SecurityManagerChannelTest,recv_io_cap_request)263 TEST_F(SecurityManagerChannelTest, recv_io_cap_request) {
264   hci_layer_->IncomingEvent(hci::IoCapabilityRequestBuilder::Create(device_.GetAddress()));
265   synchronize();
266   ASSERT_TRUE(callback_->receivedIoCapabilityRequest);
267 }
268 
TEST_F(SecurityManagerChannelTest,send_io_cap_request_reply)269 TEST_F(SecurityManagerChannelTest, send_io_cap_request_reply) {
270   // Arrange
271   hci::IoCapability io_capability = (hci::IoCapability)0x00;
272   OobDataPresent oob_present = (OobDataPresent)0x00;
273   AuthenticationRequirements authentication_requirements = (AuthenticationRequirements)0x00;
274   auto packet = hci::IoCapabilityRequestReplyBuilder::Create(device_.GetAddress(), io_capability, oob_present,
275                                                              authentication_requirements);
276 
277   // Act
278   channel_->SendCommand(std::move(packet));
279   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
280   auto command_packet = GetPacketView(std::move(last_command));
281   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
282 
283   // Assert
284   ASSERT_TRUE(packet_view.IsValid());
285   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_REPLY, packet_view.GetOpCode());
286 }
287 
TEST_F(SecurityManagerChannelTest,send_io_cap_request_neg_reply)288 TEST_F(SecurityManagerChannelTest, send_io_cap_request_neg_reply) {
289   // Arrange
290   auto packet =
291       hci::IoCapabilityRequestNegativeReplyBuilder::Create(device_.GetAddress(), hci::ErrorCode::COMMAND_DISALLOWED);
292 
293   // Act
294   channel_->SendCommand(std::move(packet));
295   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
296   auto command_packet = GetPacketView(std::move(last_command));
297   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
298 
299   // Assert
300   ASSERT_TRUE(packet_view.IsValid());
301   ASSERT_EQ(OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY, packet_view.GetOpCode());
302 }
303 
TEST_F(SecurityManagerChannelTest,recv_io_cap_response)304 TEST_F(SecurityManagerChannelTest, recv_io_cap_response) {
305   hci::IoCapability io_capability = (hci::IoCapability)0x00;
306   OobDataPresent oob_present = (OobDataPresent)0x00;
307   AuthenticationRequirements authentication_requirements = (AuthenticationRequirements)0x00;
308   hci_layer_->IncomingEvent(hci::IoCapabilityResponseBuilder::Create(device_.GetAddress(), io_capability, oob_present,
309                                                                      authentication_requirements));
310   synchronize();
311   ASSERT_TRUE(callback_->receivedIoCapabilityResponse);
312 }
313 
TEST_F(SecurityManagerChannelTest,recv_pin_code_request)314 TEST_F(SecurityManagerChannelTest, recv_pin_code_request) {
315   hci_layer_->IncomingEvent(hci::PinCodeRequestBuilder::Create(device_.GetAddress()));
316   synchronize();
317   ASSERT_TRUE(callback_->receivedPinCodeRequest);
318 }
319 
TEST_F(SecurityManagerChannelTest,send_pin_code_request_reply)320 TEST_F(SecurityManagerChannelTest, send_pin_code_request_reply) {
321   // Arrange
322   uint8_t pin_code_length = 6;
323   std::array<uint8_t, 16> pin_code = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
324   auto packet = hci::PinCodeRequestReplyBuilder::Create(device_.GetAddress(), pin_code_length, pin_code);
325 
326   // Act
327   channel_->SendCommand(std::move(packet));
328   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
329   auto command_packet = GetPacketView(std::move(last_command));
330   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
331 
332   // Assert
333   ASSERT_TRUE(packet_view.IsValid());
334   ASSERT_EQ(OpCode::PIN_CODE_REQUEST_REPLY, packet_view.GetOpCode());
335 }
336 
TEST_F(SecurityManagerChannelTest,send_pin_code_request_neg_reply)337 TEST_F(SecurityManagerChannelTest, send_pin_code_request_neg_reply) {
338   // Arrange
339   auto packet = hci::PinCodeRequestNegativeReplyBuilder::Create(device_.GetAddress());
340 
341   // Act
342   channel_->SendCommand(std::move(packet));
343   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
344   auto command_packet = GetPacketView(std::move(last_command));
345   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
346 
347   // Assert
348   ASSERT_TRUE(packet_view.IsValid());
349   ASSERT_EQ(OpCode::PIN_CODE_REQUEST_NEGATIVE_REPLY, packet_view.GetOpCode());
350 }
351 
TEST_F(SecurityManagerChannelTest,recv_user_passkey_notification)352 TEST_F(SecurityManagerChannelTest, recv_user_passkey_notification) {
353   uint32_t passkey = 0x00;
354   hci_layer_->IncomingEvent(hci::UserPasskeyNotificationBuilder::Create(device_.GetAddress(), passkey));
355   synchronize();
356   ASSERT_TRUE(callback_->receivedUserPasskeyNotification);
357 }
358 
TEST_F(SecurityManagerChannelTest,recv_user_confirmation_request)359 TEST_F(SecurityManagerChannelTest, recv_user_confirmation_request) {
360   uint32_t numeric_value = 0x0;
361   hci_layer_->IncomingEvent(hci::UserConfirmationRequestBuilder::Create(device_.GetAddress(), numeric_value));
362   synchronize();
363   ASSERT_TRUE(callback_->receivedUserConfirmationRequest);
364 }
365 
TEST_F(SecurityManagerChannelTest,send_user_confirmation_request_reply)366 TEST_F(SecurityManagerChannelTest, send_user_confirmation_request_reply) {
367   // Arrange
368   auto packet = hci::UserConfirmationRequestReplyBuilder::Create(device_.GetAddress());
369 
370   // Act
371   channel_->SendCommand(std::move(packet));
372   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
373   auto command_packet = GetPacketView(std::move(last_command));
374   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
375 
376   // Assert
377   ASSERT_TRUE(packet_view.IsValid());
378   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_REPLY, packet_view.GetOpCode());
379 }
380 
TEST_F(SecurityManagerChannelTest,send_user_confirmation_request_negative_reply)381 TEST_F(SecurityManagerChannelTest, send_user_confirmation_request_negative_reply) {
382   // Arrange
383   auto packet = hci::UserConfirmationRequestNegativeReplyBuilder::Create(device_.GetAddress());
384 
385   // Act
386   channel_->SendCommand(std::move(packet));
387   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
388   auto command_packet = GetPacketView(std::move(last_command));
389   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
390 
391   // Assert
392   ASSERT_TRUE(packet_view.IsValid());
393   ASSERT_EQ(OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY, packet_view.GetOpCode());
394 }
395 
TEST_F(SecurityManagerChannelTest,recv_remote_oob_data_request)396 TEST_F(SecurityManagerChannelTest, recv_remote_oob_data_request) {
397   hci_layer_->IncomingEvent(hci::RemoteOobDataRequestBuilder::Create(device_.GetAddress()));
398   synchronize();
399   ASSERT_TRUE(callback_->receivedRemoteOobDataRequest);
400 }
401 
TEST_F(SecurityManagerChannelTest,send_remote_oob_data_request_reply)402 TEST_F(SecurityManagerChannelTest, send_remote_oob_data_request_reply) {
403   // Arrange
404   std::array<uint8_t, 16> c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
405   std::array<uint8_t, 16> r = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
406   auto packet = hci::RemoteOobDataRequestReplyBuilder::Create(device_.GetAddress(), c, r);
407 
408   // Act
409   channel_->SendCommand(std::move(packet));
410   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
411   auto command_packet = GetPacketView(std::move(last_command));
412   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
413 
414   // Assert
415   ASSERT_TRUE(packet_view.IsValid());
416   ASSERT_EQ(OpCode::REMOTE_OOB_DATA_REQUEST_REPLY, packet_view.GetOpCode());
417 }
418 
TEST_F(SecurityManagerChannelTest,send_remote_oob_data_request_neg_reply)419 TEST_F(SecurityManagerChannelTest, send_remote_oob_data_request_neg_reply) {
420   // Arrange
421   auto packet = hci::RemoteOobDataRequestNegativeReplyBuilder::Create(device_.GetAddress());
422 
423   // Act
424   channel_->SendCommand(std::move(packet));
425   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
426   auto command_packet = GetPacketView(std::move(last_command));
427   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
428 
429   // Assert
430   ASSERT_TRUE(packet_view.IsValid());
431   ASSERT_EQ(OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY, packet_view.GetOpCode());
432 }
433 
TEST_F(SecurityManagerChannelTest,send_read_local_oob_data)434 TEST_F(SecurityManagerChannelTest, send_read_local_oob_data) {
435   // Arrange
436   auto packet = hci::ReadLocalOobDataBuilder::Create();
437 
438   // Act
439   channel_->SendCommand(std::move(packet));
440   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
441   auto command_packet = GetPacketView(std::move(last_command));
442   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
443 
444   // Assert
445   ASSERT_TRUE(packet_view.IsValid());
446   ASSERT_EQ(OpCode::READ_LOCAL_OOB_DATA, packet_view.GetOpCode());
447 }
448 
TEST_F(SecurityManagerChannelTest,send_read_local_oob_extended_data)449 TEST_F(SecurityManagerChannelTest, send_read_local_oob_extended_data) {
450   // Arrange
451   auto packet = hci::ReadLocalOobExtendedDataBuilder::Create();
452 
453   // Act
454   channel_->SendCommand(std::move(packet));
455   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
456   auto command_packet = GetPacketView(std::move(last_command));
457   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
458 
459   // Assert
460   ASSERT_TRUE(packet_view.IsValid());
461   ASSERT_EQ(OpCode::READ_LOCAL_OOB_EXTENDED_DATA, packet_view.GetOpCode());
462 }
463 
TEST_F(SecurityManagerChannelTest,recv_link_key_request)464 TEST_F(SecurityManagerChannelTest, recv_link_key_request) {
465   hci_layer_->IncomingEvent(hci::LinkKeyRequestBuilder::Create(device_.GetAddress()));
466   synchronize();
467   ASSERT_TRUE(callback_->receivedLinkKeyRequest);
468 }
469 
TEST_F(SecurityManagerChannelTest,recv_link_key_notification)470 TEST_F(SecurityManagerChannelTest, recv_link_key_notification) {
471   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
472   hci_layer_->IncomingEvent(
473       hci::LinkKeyNotificationBuilder::Create(device_.GetAddress(), link_key, hci::KeyType::DEBUG_COMBINATION));
474   synchronize();
475   ASSERT_TRUE(callback_->receivedLinkKeyNotification);
476 }
477 
TEST_F(SecurityManagerChannelTest,recv_central_link_key_complete)478 TEST_F(SecurityManagerChannelTest, recv_central_link_key_complete) {
479   uint16_t connection_handle = 0x0;
480   hci_layer_->IncomingEvent(
481       hci::CentralLinkKeyCompleteBuilder::Create(hci::ErrorCode::SUCCESS, connection_handle, hci::KeyFlag::TEMPORARY));
482   synchronize();
483   ASSERT_TRUE(callback_->receivedCentralLinkKeyComplete);
484 }
485 
TEST_F(SecurityManagerChannelTest,recv_change_connection_link_key_complete)486 TEST_F(SecurityManagerChannelTest, recv_change_connection_link_key_complete) {
487   uint16_t connection_handle = 0x0;
488   hci_layer_->IncomingEvent(
489       hci::ChangeConnectionLinkKeyCompleteBuilder::Create(hci::ErrorCode::SUCCESS, connection_handle));
490   synchronize();
491   ASSERT_TRUE(callback_->receivedChangeConnectionLinkKeyComplete);
492 }
493 
TEST_F(SecurityManagerChannelTest,recv_return_link_keys)494 TEST_F(SecurityManagerChannelTest, recv_return_link_keys) {
495   std::vector<hci::ZeroKeyAndAddress> keys;
496   hci_layer_->IncomingEvent(hci::ReturnLinkKeysBuilder::Create(keys));
497   synchronize();
498   ASSERT_TRUE(callback_->receivedReturnLinkKeys);
499 }
500 
TEST_F(SecurityManagerChannelTest,send_link_key_request_reply)501 TEST_F(SecurityManagerChannelTest, send_link_key_request_reply) {
502   // Arrange
503   std::array<uint8_t, 16> link_key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
504   auto packet = hci::LinkKeyRequestReplyBuilder::Create(device_.GetAddress(), link_key);
505 
506   // Act
507   channel_->SendCommand(std::move(packet));
508   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
509   auto command_packet = GetPacketView(std::move(last_command));
510   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
511 
512   // Assert
513   ASSERT_TRUE(packet_view.IsValid());
514   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_REPLY, packet_view.GetOpCode());
515 }
516 
TEST_F(SecurityManagerChannelTest,send_link_key_request_neg_reply)517 TEST_F(SecurityManagerChannelTest, send_link_key_request_neg_reply) {
518   // Arrange
519   auto packet = hci::LinkKeyRequestNegativeReplyBuilder::Create(device_.GetAddress());
520 
521   // Act
522   channel_->SendCommand(std::move(packet));
523   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
524   auto command_packet = GetPacketView(std::move(last_command));
525   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
526 
527   // Assert
528   ASSERT_TRUE(packet_view.IsValid());
529   ASSERT_EQ(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, packet_view.GetOpCode());
530 }
531 
TEST_F(SecurityManagerChannelTest,send_read_stored_link_key)532 TEST_F(SecurityManagerChannelTest, send_read_stored_link_key) {
533   // Arrange
534   auto packet = hci::ReadStoredLinkKeyBuilder::Create(device_.GetAddress(), hci::ReadStoredLinkKeyReadAllFlag::ALL);
535 
536   // Act
537   channel_->SendCommand(std::move(packet));
538   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
539   auto command_packet = GetPacketView(std::move(last_command));
540   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
541 
542   // Assert
543   ASSERT_TRUE(packet_view.IsValid());
544   ASSERT_EQ(OpCode::READ_STORED_LINK_KEY, packet_view.GetOpCode());
545 }
546 
TEST_F(SecurityManagerChannelTest,send_write_stored_link_key)547 TEST_F(SecurityManagerChannelTest, send_write_stored_link_key) {
548   // Arrange
549   std::vector<hci::KeyAndAddress> keys_to_write;
550   auto packet = hci::WriteStoredLinkKeyBuilder::Create(keys_to_write);
551 
552   // Act
553   channel_->SendCommand(std::move(packet));
554   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
555   auto command_packet = GetPacketView(std::move(last_command));
556   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
557 
558   // Assert
559   ASSERT_TRUE(packet_view.IsValid());
560   ASSERT_EQ(OpCode::WRITE_STORED_LINK_KEY, packet_view.GetOpCode());
561 }
562 
TEST_F(SecurityManagerChannelTest,send_delete_stored_link_key)563 TEST_F(SecurityManagerChannelTest, send_delete_stored_link_key) {
564   // Arrange
565   auto packet =
566       hci::DeleteStoredLinkKeyBuilder::Create(device_.GetAddress(), hci::DeleteStoredLinkKeyDeleteAllFlag::ALL);
567 
568   // Act
569   channel_->SendCommand(std::move(packet));
570   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
571   auto command_packet = GetPacketView(std::move(last_command));
572   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
573 
574   // Assert
575   ASSERT_TRUE(packet_view.IsValid());
576   ASSERT_EQ(OpCode::DELETE_STORED_LINK_KEY, packet_view.GetOpCode());
577 }
578 
TEST_F(SecurityManagerChannelTest,recv_encryption_change)579 TEST_F(SecurityManagerChannelTest, recv_encryption_change) {
580   uint16_t connection_handle = 0x0;
581   hci_layer_->IncomingEvent(
582       hci::EncryptionChangeBuilder::Create(hci::ErrorCode::SUCCESS, connection_handle, hci::EncryptionEnabled::ON));
583   synchronize();
584   ASSERT_TRUE(callback_->receivedEncryptionChange);
585 }
586 
TEST_F(SecurityManagerChannelTest,recv_encryption_key_refresh)587 TEST_F(SecurityManagerChannelTest, recv_encryption_key_refresh) {
588   uint16_t connection_handle = 0x0;
589   hci_layer_->IncomingEvent(
590       hci::EncryptionKeyRefreshCompleteBuilder::Create(hci::ErrorCode::SUCCESS, connection_handle));
591   synchronize();
592   ASSERT_TRUE(callback_->receivedEncryptionKeyRefreshComplete);
593 }
594 
TEST_F(SecurityManagerChannelTest,send_refresh_encryption_key)595 TEST_F(SecurityManagerChannelTest, send_refresh_encryption_key) {
596   // Arrange
597   uint16_t connection_handle = 0x0;
598   auto packet = hci::RefreshEncryptionKeyBuilder::Create(connection_handle);
599 
600   // Act
601   channel_->SendCommand(std::move(packet));
602   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
603   auto command_packet = GetPacketView(std::move(last_command));
604   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
605 
606   // Assert
607   ASSERT_TRUE(packet_view.IsValid());
608   ASSERT_EQ(OpCode::REFRESH_ENCRYPTION_KEY, packet_view.GetOpCode());
609 }
610 
TEST_F(SecurityManagerChannelTest,send_read_encryption_key_size)611 TEST_F(SecurityManagerChannelTest, send_read_encryption_key_size) {
612   // Arrange
613   uint16_t connection_handle = 0x0;
614   auto packet = hci::ReadEncryptionKeySizeBuilder::Create(connection_handle);
615 
616   // Act
617   channel_->SendCommand(std::move(packet));
618   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
619   auto command_packet = GetPacketView(std::move(last_command));
620   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
621 
622   // Assert
623   ASSERT_TRUE(packet_view.IsValid());
624   ASSERT_EQ(OpCode::READ_ENCRYPTION_KEY_SIZE, packet_view.GetOpCode());
625 }
626 
TEST_F(SecurityManagerChannelTest,recv_simple_pairing_complete)627 TEST_F(SecurityManagerChannelTest, recv_simple_pairing_complete) {
628   hci_layer_->IncomingEvent(hci::SimplePairingCompleteBuilder::Create(hci::ErrorCode::SUCCESS, device_.GetAddress()));
629   synchronize();
630   ASSERT_TRUE(callback_->receivedSimplePairingComplete);
631 }
632 
TEST_F(SecurityManagerChannelTest,send_read_simple_pairing_mode)633 TEST_F(SecurityManagerChannelTest, send_read_simple_pairing_mode) {
634   // Arrange
635   auto packet = hci::ReadSimplePairingModeBuilder::Create();
636 
637   // Act
638   channel_->SendCommand(std::move(packet));
639   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
640   auto command_packet = GetPacketView(std::move(last_command));
641   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
642 
643   // Assert
644   ASSERT_TRUE(packet_view.IsValid());
645   ASSERT_EQ(OpCode::READ_SIMPLE_PAIRING_MODE, packet_view.GetOpCode());
646 }
647 
TEST_F(SecurityManagerChannelTest,send_write_simple_pairing_mode)648 TEST_F(SecurityManagerChannelTest, send_write_simple_pairing_mode) {
649   // Arrange
650   auto packet = hci::WriteSimplePairingModeBuilder::Create(hci::Enable::ENABLED);
651 
652   // Act
653   channel_->SendCommand(std::move(packet));
654   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
655   auto command_packet = GetPacketView(std::move(last_command));
656   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
657 
658   // Assert
659   ASSERT_TRUE(packet_view.IsValid());
660   ASSERT_EQ(OpCode::WRITE_SIMPLE_PAIRING_MODE, packet_view.GetOpCode());
661 }
662 
TEST_F(SecurityManagerChannelTest,recv_keypress_notification)663 TEST_F(SecurityManagerChannelTest, recv_keypress_notification) {
664   hci_layer_->IncomingEvent(
665       hci::KeypressNotificationBuilder::Create(device_.GetAddress(), hci::KeypressNotificationType::ENTRY_COMPLETED));
666   synchronize();
667   ASSERT_TRUE(callback_->receivedKeypressNotification);
668 }
669 
TEST_F(SecurityManagerChannelTest,send_keypress_notification)670 TEST_F(SecurityManagerChannelTest, send_keypress_notification) {
671   // Arrange
672   auto packet =
673       hci::SendKeypressNotificationBuilder::Create(device_.GetAddress(), hci::KeypressNotificationType::ENTRY_STARTED);
674 
675   // Act
676   channel_->SendCommand(std::move(packet));
677   auto last_command = std::move(hci_layer_->GetLastCommand()->command);
678   auto command_packet = GetPacketView(std::move(last_command));
679   hci::CommandView packet_view = hci::CommandView::Create(command_packet);
680 
681   // Assert
682   ASSERT_TRUE(packet_view.IsValid());
683   ASSERT_EQ(OpCode::SEND_KEYPRESS_NOTIFICATION, packet_view.GetOpCode());
684 }
685 
TEST_F(SecurityManagerChannelTest,recv_user_passkey_request)686 TEST_F(SecurityManagerChannelTest, recv_user_passkey_request) {
687   hci_layer_->IncomingEvent(hci::UserPasskeyRequestBuilder::Create(device_.GetAddress()));
688   synchronize();
689   ASSERT_TRUE(callback_->receivedUserPasskeyRequest);
690 }
691 
TEST_F(SecurityManagerChannelTest,test_l2cap_security_interface_api)692 TEST_F(SecurityManagerChannelTest, test_l2cap_security_interface_api) {
693   ASSERT_FALSE(on_link_connected_called);
694   channel_->Connect(device_.GetAddress());
695   ASSERT_TRUE(on_link_connected_called);
696   ASSERT_FALSE(on_link_disconnected_called);
697   channel_->Release(device_.GetAddress());
698   // TODO(optedoblivion): Lock and wait
699   // ASSERT_TRUE(on_link_disconnected_called);
700 }
701 
702 }  // namespace
703 }  // namespace channel
704 }  // namespace security
705 }  // namespace bluetooth
706