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