Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.google.android.connecteddevice.connection |
| 17 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 18 | import android.content.Context |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 19 | import android.os.ParcelUuid |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 20 | import android.util.Base64 |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 21 | import androidx.room.Room |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 22 | import androidx.test.core.app.ApplicationProvider |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 23 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 24 | import com.google.android.companionprotos.VerificationCode |
| 25 | import com.google.android.companionprotos.VerificationCodeState |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 26 | import com.google.android.connecteddevice.connection.MultiProtocolSecureChannel.ChannelError |
| 27 | import com.google.android.connecteddevice.connection.MultiProtocolSecureChannel.MessageError |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 28 | import com.google.android.connecteddevice.connection.MultiProtocolSecureChannel.ShowVerificationCodeListener |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 29 | import com.google.android.connecteddevice.model.DeviceMessage |
| 30 | import com.google.android.connecteddevice.model.DeviceMessage.OperationType |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 31 | import com.google.android.connecteddevice.oob.OobRunner |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 32 | import com.google.android.connecteddevice.storage.ConnectedDeviceDatabase |
| 33 | import com.google.android.connecteddevice.storage.ConnectedDeviceStorage |
| 34 | import com.google.android.connecteddevice.storage.CryptoHelper |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 35 | import com.google.android.connecteddevice.transport.ConnectChallenge |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 36 | import com.google.android.connecteddevice.transport.ConnectionProtocol |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 37 | import com.google.android.connecteddevice.transport.IDataSendCallback |
| 38 | import com.google.android.connecteddevice.transport.IDiscoveryCallback |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 39 | import com.google.android.connecteddevice.transport.ProtocolDevice |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 40 | import com.google.android.connecteddevice.util.ByteUtils |
| 41 | import com.google.android.encryptionrunner.EncryptionRunnerFactory |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 42 | import com.google.android.encryptionrunner.FakeEncryptionRunner |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 43 | import com.google.android.encryptionrunner.HandshakeException |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 44 | import com.google.common.truth.Truth.assertThat |
| 45 | import com.google.common.util.concurrent.MoreExecutors.directExecutor |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 46 | import com.google.protobuf.ByteString |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 47 | import com.nhaarman.mockitokotlin2.any |
| 48 | import com.nhaarman.mockitokotlin2.argumentCaptor |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 49 | import com.nhaarman.mockitokotlin2.eq |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 50 | import com.nhaarman.mockitokotlin2.mock |
| 51 | import com.nhaarman.mockitokotlin2.never |
| 52 | import com.nhaarman.mockitokotlin2.spy |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 53 | import com.nhaarman.mockitokotlin2.times |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 54 | import com.nhaarman.mockitokotlin2.verify |
| 55 | import com.nhaarman.mockitokotlin2.whenever |
| 56 | import java.security.SignatureException |
| 57 | import java.util.UUID |
| 58 | import java.util.zip.DataFormatException |
| 59 | import java.util.zip.Inflater |
| 60 | import org.junit.Assert.fail |
| 61 | import org.junit.Before |
| 62 | import org.junit.Test |
| 63 | import org.junit.runner.RunWith |
| 64 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 65 | private const val PROTOCOL_ID_1 = "testProtocol1" |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 66 | private const val PROTOCOL_ID_2 = "testProtocol2" |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 67 | private val SERVER_DEVICE_ID = UUID.fromString("a29f0c74-2014-4b14-ac02-be6ed15b545a") |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 68 | |
| 69 | @RunWith(AndroidJUnit4::class) |
| 70 | class MultiProtocolSecureChannelTest { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 71 | private val context = ApplicationProvider.getApplicationContext<Context>() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 72 | private val stream1 = spy(ProtocolStream(ProtocolDevice(TestProtocol(), PROTOCOL_ID_1))) |
| 73 | private val stream2 = spy(ProtocolStream(ProtocolDevice(TestProtocol(), PROTOCOL_ID_2))) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 74 | private val mockInflater: Inflater = mock() |
| 75 | private val mockCallback: MultiProtocolSecureChannel.Callback = mock() |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 76 | private val mockOobRunner: OobRunner = mock() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 77 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 78 | private lateinit var secureChannel: MultiProtocolSecureChannel |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 79 | private lateinit var spyStorage: ConnectedDeviceStorage |
| 80 | private val mockShowVerificationCodeListener: ShowVerificationCodeListener = mock() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 81 | |
| 82 | @Before |
| 83 | @Throws(SignatureException::class) |
| 84 | fun setUp() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 85 | val database = |
| 86 | Room.inMemoryDatabaseBuilder(context, ConnectedDeviceDatabase::class.java) |
| 87 | .allowMainThreadQueries() |
| 88 | .setQueryExecutor(directExecutor()) |
| 89 | .build() |
| 90 | .associatedDeviceDao() |
Android Auto Companion | a34dde2 | 2021-12-08 09:12:15 -0800 | [diff] [blame] | 91 | spyStorage = |
| 92 | spy(ConnectedDeviceStorage(context, Base64CryptoHelper(), database, directExecutor())) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 93 | whenever(spyStorage.uniqueId).thenReturn(SERVER_DEVICE_ID) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | @Throws(SignatureException::class) |
| 98 | fun decryptMessage_doesNothingForUnencryptedMessage() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 99 | val testPayload = ByteUtils.randomBytes(10) |
| 100 | completeHandshakeAndSaveTheKey() |
| 101 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 102 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 103 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 104 | UUID.randomUUID(), |
| 105 | /* isMessageEncrypted= */ false, |
| 106 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 107 | testPayload |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 108 | ) |
| 109 | secureChannel.decryptMessage(message) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 110 | assertThat(message.message).isEqualTo(testPayload) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | @Throws(SignatureException::class) |
| 115 | fun decryptMessage_decryptsEncryptedMessage() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 116 | completeHandshakeAndSaveTheKey() |
| 117 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 118 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 119 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 120 | UUID.randomUUID(), |
| 121 | /* isMessageEncrypted= */ true, |
| 122 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 123 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 124 | ) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 125 | assertThat(secureChannel.decryptMessage(message)).isTrue() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | @Throws(InterruptedException::class) |
| 130 | fun decryptMessage_onMessageReceivedErrorForEncryptedMessageWithNoKey() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 131 | setupSecureChannel(true) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 132 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 133 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 134 | UUID.randomUUID(), |
| 135 | /* isMessageEncrypted= */ true, |
| 136 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 137 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 138 | ) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 139 | assertThat(secureChannel.decryptMessage(message)).isFalse() |
| 140 | verify(mockCallback).onMessageReceivedError(MessageError.MESSAGE_ERROR_DECRYPTION_FAILURE) |
| 141 | assertThat(message.message).isEmpty() |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | fun onDeviceMessageReceived_onEstablishSecureChannelFailureBadHandshakeMessage() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 146 | setupSecureChannel(true) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 147 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 148 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 149 | UUID.randomUUID(), |
| 150 | /* isMessageEncrypted= */ true, |
| 151 | OperationType.ENCRYPTION_HANDSHAKE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 152 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 153 | ) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 154 | secureChannel.onDeviceMessageReceived(message) |
| 155 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_MSG) |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | fun onDeviceMessageReceived_onMessageReceivedNotIssuedForEmptyMessage() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 160 | completeHandshakeAndSaveTheKey() |
| 161 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 162 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 163 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 164 | UUID.randomUUID(), |
| 165 | /* isMessageEncrypted= */ false, |
| 166 | OperationType.CLIENT_MESSAGE, |
| 167 | /* message= */ byteArrayOf() |
| 168 | ) |
| 169 | secureChannel.onDeviceMessageReceived(message) |
| 170 | verify(mockCallback, never()).onMessageReceived(any()) |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | fun onDeviceMessageReceived_processHandshakeExceptionIssuesSecureChannelFailureCallback() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 175 | setupSecureChannel(true) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 176 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 177 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 178 | UUID.randomUUID(), |
| 179 | /* isMessageEncrypted= */ false, |
| 180 | OperationType.ENCRYPTION_HANDSHAKE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 181 | FakeEncryptionRunner.INIT_MESSAGE |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 182 | ) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 183 | |
| 184 | whenever(secureChannel.processHandshake(message.message)).then { |
| 185 | throw HandshakeException("test") |
| 186 | } |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 187 | secureChannel.onDeviceMessageReceived(message) |
| 188 | verify(mockCallback) |
| 189 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_HANDSHAKE) |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | fun onDeviceMessageReceived_processClientMessageIssuesMessageReceivedErrorCallback() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 194 | setupSecureChannel(true) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 195 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 196 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 197 | UUID.randomUUID(), |
| 198 | /* isMessageEncrypted= */ true, |
| 199 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 200 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 201 | ) |
| 202 | secureChannel.onDeviceMessageReceived(message) |
| 203 | verify(mockCallback).onMessageReceivedError(MessageError.MESSAGE_ERROR_DECRYPTION_FAILURE) |
| 204 | } |
| 205 | |
| 206 | @Test |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 207 | fun onDeviceMessageReceived_noExceptionWhenReceivedUnknownMessageType() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 208 | completeHandshakeAndSaveTheKey() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 209 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 210 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 211 | UUID.randomUUID(), |
| 212 | /* isMessageEncrypted= */ true, |
| 213 | OperationType.UNKNOWN, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 214 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 215 | ) |
| 216 | try { |
| 217 | secureChannel.onDeviceMessageReceived(message) |
| 218 | } catch (e: Exception) { |
| 219 | fail("Should not throw exception.") |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | @Test |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 224 | fun onDeviceMessageReceived_issueOnMessageReceivedWhenHandshakeFinished() { |
| 225 | completeHandshakeAndSaveTheKey() |
| 226 | val message = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 227 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 228 | UUID.randomUUID(), |
| 229 | /* isMessageEncrypted= */ true, |
| 230 | OperationType.ENCRYPTION_HANDSHAKE, |
| 231 | ByteUtils.randomBytes(10) |
| 232 | ) |
| 233 | secureChannel.onDeviceMessageReceived(message) |
| 234 | verify(mockCallback).onMessageReceived(message) |
| 235 | } |
| 236 | |
| 237 | @Test |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 238 | fun decompressMessage_returnsOriginalMessageIfOriginalSizeIsZero() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 239 | val testPayload = ByteUtils.randomBytes(10) |
| 240 | completeHandshakeAndSaveTheKey() |
| 241 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 242 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 243 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 244 | UUID.randomUUID(), |
| 245 | /* isMessageEncrypted= */ false, |
| 246 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 247 | testPayload |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 248 | ) |
| 249 | deviceMessage.originalMessageSize = 0 |
| 250 | assertThat(secureChannel.decompressMessage(deviceMessage)).isTrue() |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 251 | assertThat(deviceMessage.message).isEqualTo(testPayload) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Test |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 255 | fun decompressMessage_returnsFalseWhenThereAreDataFormatException() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 256 | completeHandshakeAndSaveTheKey() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 257 | whenever(mockInflater.inflate(any())).then { throw DataFormatException() } |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 258 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 259 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 260 | UUID.randomUUID(), |
| 261 | /* isMessageEncrypted= */ false, |
| 262 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 263 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 264 | ) |
| 265 | secureChannel.callback = mockCallback |
| 266 | deviceMessage.originalMessageSize = 1 |
| 267 | assertThat(secureChannel.decompressMessage(deviceMessage)).isFalse() |
| 268 | verify(mockCallback).onMessageReceivedError(MessageError.MESSAGE_ERROR_DECOMPRESSION_FAILURE) |
| 269 | } |
| 270 | |
| 271 | @Test |
| 272 | fun compressMessage_returnsCompressedMessageWithOriginalSize() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 273 | completeHandshakeAndSaveTheKey() |
| 274 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 275 | val message = ByteArray(100) |
| 276 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 277 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 278 | UUID.randomUUID(), |
| 279 | /* isMessageEncrypted= */ false, |
| 280 | OperationType.CLIENT_MESSAGE, |
| 281 | message |
| 282 | ) |
| 283 | secureChannel.compressMessage(deviceMessage) |
| 284 | assertThat(deviceMessage.message).isNotEqualTo(message) |
| 285 | assertThat(deviceMessage.originalMessageSize).isEqualTo(message.size) |
| 286 | } |
| 287 | |
| 288 | @Test |
| 289 | fun compressedMessageCanBeDecompressed() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 290 | completeHandshakeAndSaveTheKey() |
| 291 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 292 | val message = ByteArray(100) |
| 293 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 294 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 295 | UUID.randomUUID(), |
| 296 | /* isMessageEncrypted= */ false, |
| 297 | OperationType.CLIENT_MESSAGE, |
| 298 | message |
| 299 | ) |
| 300 | secureChannel.compressMessage(deviceMessage) |
| 301 | assertThat(secureChannel.decompressMessage(deviceMessage)).isTrue() |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 302 | assertThat(deviceMessage.message).isEqualTo(message) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Test |
| 306 | fun addStream_notifyCallbackWhenMessageReceived() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 307 | completeHandshakeAndSaveTheKey() |
| 308 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 309 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 310 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 311 | UUID.randomUUID(), |
| 312 | /* isMessageEncrypted= */ true, |
| 313 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 314 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 315 | ) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 316 | secureChannel.addStream(stream2) |
| 317 | assertThat(stream2.messageReceivedListener).isNotNull() |
| 318 | stream2.messageReceivedListener!!.onMessageReceived(deviceMessage) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 319 | verify(mockCallback).onMessageReceived(any()) |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | fun addStream_notifyCallbackWhenProtocolDisconnected() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 324 | setupSecureChannel(true) |
| 325 | |
| 326 | assertThat(stream1.protocolDisconnectListener).isNotNull() |
| 327 | stream1.protocolDisconnectListener!!.onProtocolDisconnected() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 328 | verify(mockCallback) |
| 329 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_DEVICE_DISCONNECTED) |
| 330 | } |
| 331 | |
| 332 | @Test |
| 333 | fun addStream_doNotNotifyCallbackWhenProtocolDisconnected() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 334 | setupSecureChannel(true) |
| 335 | secureChannel.addStream(stream2) |
| 336 | assertThat(stream2.protocolDisconnectListener).isNotNull() |
| 337 | stream2.protocolDisconnectListener!!.onProtocolDisconnected() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 338 | verify(mockCallback, never()) |
| 339 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_DEVICE_DISCONNECTED) |
| 340 | } |
| 341 | |
| 342 | @Test |
| 343 | fun sendClientMessage_FailedToSendMessageWithNullKey() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 344 | setupSecureChannel(true) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 345 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 346 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 347 | UUID.randomUUID(), |
| 348 | /* isMessageEncrypted= */ true, |
| 349 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 350 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 351 | ) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 352 | |
| 353 | assertThat(secureChannel.sendClientMessage(deviceMessage)).isFalse() |
| 354 | } |
| 355 | |
| 356 | @Test |
| 357 | fun sendClientMessage_FailedToSendMessageWithEmptyStream() { |
| 358 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 359 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 360 | UUID.randomUUID(), |
| 361 | /* isMessageEncrypted= */ true, |
| 362 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 363 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 364 | ) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 365 | completeHandshakeAndSaveTheKey() |
| 366 | |
| 367 | stream1.protocolDisconnectListener!!.onProtocolDisconnected() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 368 | assertThat(secureChannel.sendClientMessage(deviceMessage)).isFalse() |
| 369 | } |
| 370 | |
| 371 | @Test |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 372 | fun sendClientMessage_successfullySendMessage() { |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 373 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 374 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 375 | UUID.randomUUID(), |
| 376 | /* isMessageEncrypted= */ true, |
| 377 | OperationType.CLIENT_MESSAGE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 378 | ByteUtils.randomBytes(10) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 379 | ) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 380 | |
| 381 | completeHandshakeAndSaveTheKey() |
| 382 | |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 383 | assertThat(secureChannel.sendClientMessage(deviceMessage)).isTrue() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Test |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 387 | fun sendClientMessage_successfullyEncryptAndSetMessage() { |
| 388 | val testPayload = ByteUtils.randomBytes(10) |
| 389 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 390 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 391 | UUID.randomUUID(), |
| 392 | /* isMessageEncrypted= */ true, |
| 393 | OperationType.CLIENT_MESSAGE, |
| 394 | testPayload |
| 395 | ) |
| 396 | |
| 397 | completeHandshakeAndSaveTheKey() |
| 398 | secureChannel.sendClientMessage(deviceMessage) |
| 399 | |
| 400 | assertThat(deviceMessage.message).isNotEqualTo(testPayload) |
| 401 | } |
| 402 | |
| 403 | @Test |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 404 | fun association_secureChannelEstablishedSuccessfully() { |
| 405 | val clientId = UUID.randomUUID() |
| 406 | setupSecureChannel(false) |
| 407 | secureChannel.showVerificationCodeListener = mockShowVerificationCodeListener |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 408 | argumentCaptor<DeviceMessage>().apply { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 409 | initHandshakeMessage() |
| 410 | verify(stream1).sendMessage(capture()) |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 411 | val response = firstValue.message |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 412 | assertThat(response).isEqualTo(FakeEncryptionRunner.INIT_RESPONSE) |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 413 | } |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 414 | respondToContinueMessage() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 415 | val testVerificationCodeMessage = |
| 416 | VerificationCode.newBuilder().setState(VerificationCodeState.VISUAL_VERIFICATION).build() |
| 417 | val deviceMessage = |
| 418 | DeviceMessage.createOutgoingMessage( |
| 419 | /* recipient= */ null, |
| 420 | /* isMessageEncrypted= */ false, |
| 421 | OperationType.ENCRYPTION_HANDSHAKE, |
| 422 | testVerificationCodeMessage.toByteArray() |
| 423 | ) |
| 424 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 425 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 426 | verify(mockShowVerificationCodeListener).showVerificationCode(any()) |
| 427 | |
| 428 | secureChannel.notifyVerificationCodeAccepted() |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 429 | secureChannel.setDeviceIdDuringAssociation(clientId) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 430 | verify(spyStorage).saveEncryptionKey(eq(clientId.toString()), any()) |
| 431 | verify(mockCallback).onSecureChannelEstablished() |
| 432 | } |
| 433 | |
| 434 | @Test |
| 435 | fun association_wrongInitHandshakeMessage_issueInvalidHandshakeError() { |
| 436 | setupSecureChannel(false) |
| 437 | |
| 438 | // Wrong init handshake message |
| 439 | respondToContinueMessage() |
| 440 | verify(mockCallback) |
| 441 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_HANDSHAKE) |
| 442 | } |
| 443 | |
| 444 | @Test |
| 445 | @Throws(InterruptedException::class) |
| 446 | fun association_wrongRespondToContinueMessage_issueInvalidHandshakeError() { |
| 447 | setupSecureChannel(false) |
| 448 | initHandshakeMessage() |
| 449 | |
| 450 | // Wrong respond to continue message |
| 451 | initHandshakeMessage() |
| 452 | verify(mockCallback) |
| 453 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_HANDSHAKE) |
| 454 | } |
| 455 | |
| 456 | @Test |
| 457 | fun reconnect_secureChannelEstablishedSuccessfully() { |
| 458 | val clientId = UUID.randomUUID() |
| 459 | whenever(spyStorage.getEncryptionKey(clientId.toString())).thenReturn(byteArrayOf()) |
| 460 | setupSecureChannel(true, clientId.toString()) |
| 461 | initHandshakeMessage() |
| 462 | respondToContinueMessage() |
| 463 | respondToResumeMessage() |
| 464 | |
| 465 | verify(spyStorage).saveEncryptionKey(eq(clientId.toString()), any()) |
| 466 | verify(mockCallback).onSecureChannelEstablished() |
| 467 | } |
| 468 | |
| 469 | @Test |
| 470 | fun reconnect_deviceIdNotSet_issueInvalidStateError() { |
| 471 | setupSecureChannel(true) |
| 472 | |
| 473 | initHandshakeMessage() |
| 474 | respondToContinueMessage() |
| 475 | respondToResumeMessage() |
| 476 | |
| 477 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_STATE) |
| 478 | } |
| 479 | |
| 480 | @Test |
| 481 | fun reconnect_keyNotSet_issueInvalidKeyError() { |
| 482 | setupSecureChannel(true, UUID.randomUUID().toString()) |
| 483 | initHandshakeMessage() |
| 484 | respondToContinueMessage() |
| 485 | respondToResumeMessage() |
| 486 | |
| 487 | verify(mockCallback) |
| 488 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_ENCRYPTION_KEY) |
| 489 | } |
| 490 | |
| 491 | @Test |
| 492 | fun processHandshakeResumingSession_incorrectHandshakeState_issueInvalidStateError() { |
| 493 | val clientId = UUID.randomUUID().toString() |
| 494 | setupSecureChannel(true, clientId) |
| 495 | whenever(spyStorage.getEncryptionKey(clientId)).thenReturn(byteArrayOf()) |
| 496 | initHandshakeMessage() |
| 497 | respondToContinueMessage() |
| 498 | respondToResumeMessage(FakeEncryptionRunner.RECONNECTION_MESSAGE_STATE_ERROR) |
| 499 | |
| 500 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_STATE) |
| 501 | } |
| 502 | |
| 503 | @Test |
| 504 | fun processHandshakeResumingSession_emptyNewKey_issueInvalidKeyError() { |
| 505 | val clientId = UUID.randomUUID().toString() |
| 506 | setupSecureChannel(true, clientId) |
| 507 | whenever(spyStorage.getEncryptionKey(clientId)).thenReturn(byteArrayOf()) |
| 508 | initHandshakeMessage() |
| 509 | respondToContinueMessage() |
| 510 | respondToResumeMessage(FakeEncryptionRunner.RECONNECTION_MESSAGE_KEY_ERROR) |
| 511 | |
| 512 | verify(mockCallback) |
| 513 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_ENCRYPTION_KEY) |
| 514 | } |
| 515 | |
| 516 | @Test |
| 517 | fun processHandshakeResumingSession_emptyNextMessage_issueInvalidMessageError() { |
| 518 | val clientId = UUID.randomUUID().toString() |
| 519 | setupSecureChannel(true, clientId) |
| 520 | whenever(spyStorage.getEncryptionKey(clientId)).thenReturn(byteArrayOf()) |
| 521 | initHandshakeMessage() |
| 522 | respondToContinueMessage() |
| 523 | respondToResumeMessage(FakeEncryptionRunner.RECONNECTION_MESSAGE_EMPTY_RESPONSE) |
| 524 | |
| 525 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_MSG) |
| 526 | } |
| 527 | |
| 528 | @Test |
| 529 | fun processHandshake_invalidState_issueInvalidStateError() { |
| 530 | setupSecureChannel(false) |
| 531 | secureChannel.showVerificationCodeListener = mockShowVerificationCodeListener |
| 532 | initHandshakeMessage() |
| 533 | respondToContinueMessage() |
| 534 | secureChannel.processHandshake(byteArrayOf()) |
| 535 | |
| 536 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_STATE) |
| 537 | } |
| 538 | |
| 539 | @Test |
| 540 | fun processHandshake_receivedMessageAfterFinish_ignoreMessage() { |
| 541 | setupSecureChannel(false) |
| 542 | secureChannel.showVerificationCodeListener = mockShowVerificationCodeListener |
| 543 | initHandshakeMessage() |
| 544 | respondToContinueMessage() |
| 545 | secureChannel.notifyVerificationCodeAccepted() |
| 546 | secureChannel.processHandshake(byteArrayOf()) |
| 547 | |
| 548 | verify(mockCallback, never()).onEstablishSecureChannelFailure(any()) |
| 549 | } |
| 550 | |
| 551 | @Test |
| 552 | fun processHandshakeInitialization_nextMessageIsNull_issueInvalidHandshakeError() { |
| 553 | setupSecureChannel(false) |
| 554 | initHandshakeMessage(FakeEncryptionRunner.INIT_MESSAGE_EMPTY_RESPONSE) |
| 555 | |
| 556 | verify(mockCallback) |
| 557 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_HANDSHAKE) |
| 558 | } |
| 559 | |
| 560 | @Test |
| 561 | fun processVerificationCode_listenerNotSet_issueInvalidStateError() { |
| 562 | setupSecureChannel(false) |
| 563 | initHandshakeMessage() |
| 564 | respondToContinueMessage() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 565 | |
| 566 | val testVerificationCodeMessage = |
| 567 | VerificationCode.newBuilder().setState(VerificationCodeState.VISUAL_VERIFICATION).build() |
| 568 | val deviceMessage = |
| 569 | DeviceMessage.createOutgoingMessage( |
| 570 | /* recipient= */ null, |
| 571 | /* isMessageEncrypted= */ false, |
| 572 | OperationType.ENCRYPTION_HANDSHAKE, |
| 573 | testVerificationCodeMessage.toByteArray() |
| 574 | ) |
| 575 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 576 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 577 | verify(mockCallback).onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_STATE) |
| 578 | } |
| 579 | |
| 580 | @Test |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 581 | fun setDeviceIdDuringAssociation_encryptionKeyNotSet_issueInvalidKeyError() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 582 | setupSecureChannel(false) |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 583 | secureChannel.setDeviceIdDuringAssociation(UUID.randomUUID()) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 584 | |
| 585 | verify(mockCallback) |
| 586 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_ENCRYPTION_KEY) |
| 587 | } |
| 588 | |
| 589 | @Test |
| 590 | fun notifyVerificationCodeAccepted_invalidState_issueInvalidVerificationError() { |
| 591 | setupSecureChannel(false) |
| 592 | initHandshakeMessage() |
| 593 | secureChannel.notifyVerificationCodeAccepted() |
| 594 | |
| 595 | verify(mockCallback) |
| 596 | .onEstablishSecureChannelFailure(ChannelError.CHANNEL_ERROR_INVALID_VERIFICATION) |
| 597 | } |
| 598 | |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 599 | @Test |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 600 | fun processVerificationCodeMessage_oobVerification_verifyOobCode() { |
| 601 | setupSecureChannel(false) |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 602 | initHandshakeMessage() |
| 603 | respondToContinueMessage() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 604 | val testPayload = "testPayload".toByteArray() |
| 605 | val testVerificationCodeMessage = |
| 606 | VerificationCode.newBuilder().run { |
| 607 | setState(VerificationCodeState.OOB_VERIFICATION) |
| 608 | setPayload(ByteString.copyFrom(testPayload)) |
| 609 | build() |
| 610 | } |
| 611 | val deviceMessage = |
| 612 | DeviceMessage.createOutgoingMessage( |
| 613 | /* recipient= */ null, |
| 614 | /* isMessageEncrypted= */ false, |
| 615 | OperationType.ENCRYPTION_HANDSHAKE, |
| 616 | testVerificationCodeMessage.toByteArray() |
| 617 | ) |
| 618 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 619 | |
| 620 | verify(mockOobRunner).decryptData(testPayload) |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | @Test |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 624 | fun createOobResponse_oobCodeMatch_sendCorrectMessage() { |
| 625 | val testPayload = "testPayload".toByteArray() |
| 626 | whenever(mockOobRunner.decryptData(testPayload)) |
| 627 | .thenReturn(FakeEncryptionRunner.VERIFICATION_CODE) |
| 628 | whenever(mockOobRunner.encryptData(FakeEncryptionRunner.VERIFICATION_CODE)) |
| 629 | .thenReturn(testPayload) |
| 630 | setupSecureChannel(false) |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 631 | initHandshakeMessage() |
| 632 | respondToContinueMessage() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 633 | val testVerificationCodeMessage = |
| 634 | VerificationCode.newBuilder().run { |
| 635 | setState(VerificationCodeState.OOB_VERIFICATION) |
| 636 | setPayload(ByteString.copyFrom(testPayload)) |
| 637 | build() |
| 638 | } |
| 639 | val deviceMessage = |
| 640 | DeviceMessage.createOutgoingMessage( |
| 641 | /* recipient= */ null, |
| 642 | /* isMessageEncrypted= */ false, |
| 643 | OperationType.ENCRYPTION_HANDSHAKE, |
| 644 | testVerificationCodeMessage.toByteArray() |
| 645 | ) |
| 646 | secureChannel.onDeviceMessageReceived(deviceMessage) |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 647 | |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 648 | val confirmationMessage = |
| 649 | argumentCaptor<DeviceMessage>().run { |
| 650 | verify(stream1, times(2)).sendMessage(capture()) |
| 651 | secondValue.message |
| 652 | } |
| 653 | val verificationCodeMessage = VerificationCode.parseFrom(confirmationMessage) |
| 654 | assertThat(verificationCodeMessage.state).isEqualTo(VerificationCodeState.OOB_VERIFICATION) |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | @Test |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 658 | fun onVisualVerificationCodeConfirmed_sendConfirmationMessage() { |
| 659 | setupSecureChannel(false) |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 660 | initHandshakeMessage() |
| 661 | respondToContinueMessage() |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 662 | val testVerificationCodeMessage = |
| 663 | VerificationCode.newBuilder().setState(VerificationCodeState.VISUAL_VERIFICATION).build() |
| 664 | val deviceMessage = |
| 665 | DeviceMessage.createOutgoingMessage( |
| 666 | /* recipient= */ null, |
| 667 | /* isMessageEncrypted= */ false, |
| 668 | OperationType.ENCRYPTION_HANDSHAKE, |
| 669 | testVerificationCodeMessage.toByteArray() |
| 670 | ) |
| 671 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 672 | secureChannel.notifyVerificationCodeAccepted() |
| 673 | val confirmationMessage = |
| 674 | argumentCaptor<DeviceMessage>().run { |
| 675 | verify(stream1, times(2)).sendMessage(capture()) |
| 676 | secondValue.message |
| 677 | } |
| 678 | val verificationCodeMessage = VerificationCode.parseFrom(confirmationMessage) |
| 679 | assertThat(verificationCodeMessage.state).isEqualTo(VerificationCodeState.VISUAL_CONFIRMATION) |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 682 | private fun setupSecureChannel(isReconnect: Boolean, deviceId: String? = null) { |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 683 | val encryptionRunner = EncryptionRunnerFactory.newFakeRunner() |
| 684 | encryptionRunner.setIsReconnect(isReconnect) |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 685 | secureChannel = |
| 686 | spy( |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 687 | MultiProtocolSecureChannel( |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 688 | stream1, |
| 689 | spyStorage, |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 690 | encryptionRunner, |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 691 | mockOobRunner, |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 692 | deviceId = deviceId, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 693 | inflater = mockInflater |
| 694 | ) |
| 695 | .apply { callback = mockCallback } |
| 696 | ) |
| 697 | } |
| 698 | |
| 699 | private fun initHandshakeMessage(message: ByteArray = FakeEncryptionRunner.INIT_MESSAGE) { |
| 700 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 701 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 702 | /* recipient= */ null, |
| 703 | /* isMessageEncrypted= */ false, |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 704 | OperationType.ENCRYPTION_HANDSHAKE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 705 | message |
| 706 | ) |
| 707 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 708 | } |
| 709 | |
| 710 | private fun respondToContinueMessage(message: ByteArray = FakeEncryptionRunner.CLIENT_RESPONSE) { |
| 711 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 712 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 713 | /* recipient= */ null, |
| 714 | /* isMessageEncrypted= */ false, |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 715 | OperationType.ENCRYPTION_HANDSHAKE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 716 | message |
| 717 | ) |
| 718 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 719 | } |
| 720 | |
| 721 | private fun respondToResumeMessage(message: ByteArray = "Placeholder Message".toByteArray()) { |
| 722 | val deviceMessage = |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 723 | DeviceMessage.createOutgoingMessage( |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 724 | /* recipient= */ null, |
| 725 | /* isMessageEncrypted= */ false, |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 726 | OperationType.ENCRYPTION_HANDSHAKE, |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 727 | message |
| 728 | ) |
| 729 | secureChannel.onDeviceMessageReceived(deviceMessage) |
| 730 | } |
| 731 | |
| 732 | private fun completeHandshakeAndSaveTheKey() { |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 733 | setupSecureChannel(false) |
| 734 | secureChannel.showVerificationCodeListener = mockShowVerificationCodeListener |
| 735 | initHandshakeMessage() |
| 736 | respondToContinueMessage() |
| 737 | secureChannel.notifyVerificationCodeAccepted() |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Android Auto Companion | 6b2e691 | 2021-11-10 10:01:49 -0800 | [diff] [blame] | 741 | private open class TestProtocol : ConnectionProtocol() { |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 742 | override fun isDeviceVerificationRequired() = false |
Android Auto Companion | e1421e9 | 2021-07-21 10:33:24 -0700 | [diff] [blame] | 743 | |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 744 | override fun startAssociationDiscovery( |
| 745 | name: String, |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 746 | identifier: ParcelUuid, |
| 747 | callback: IDiscoveryCallback |
Android Auto Companion | 9a35cab | 2021-10-13 10:00:56 -0700 | [diff] [blame] | 748 | ) {} |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 749 | |
| 750 | override fun startConnectionDiscovery( |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 751 | id: ParcelUuid, |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 752 | challenge: ConnectChallenge, |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 753 | callback: IDiscoveryCallback |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 754 | ) {} |
| 755 | |
| 756 | override fun stopAssociationDiscovery() {} |
| 757 | |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 758 | override fun stopConnectionDiscovery(id: ParcelUuid) {} |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 759 | |
Android Auto Companion | 16c608d | 2022-02-02 10:13:13 -0800 | [diff] [blame^] | 760 | override fun sendData(protocolId: String, data: ByteArray, callback: IDataSendCallback?) {} |
Android Auto Companion | a7288fb | 2021-06-09 12:52:29 -0700 | [diff] [blame] | 761 | |
| 762 | override fun disconnectDevice(protocolId: String) {} |
| 763 | |
| 764 | override fun reset() {} |
| 765 | |
| 766 | override fun getMaxWriteSize(protocolId: String): Int { |
| 767 | return 0 |
| 768 | } |
| 769 | } |
| 770 | |
Android Auto Companion | 179c3e3 | 2021-06-23 09:04:17 -0700 | [diff] [blame] | 771 | private class Base64CryptoHelper : CryptoHelper { |
| 772 | override fun encrypt(value: ByteArray?): String? = Base64.encodeToString(value, Base64.DEFAULT) |
| 773 | |
| 774 | override fun decrypt(value: String?): ByteArray? = Base64.decode(value, Base64.DEFAULT) |
| 775 | } |