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