|
| 1 | +/* |
| 2 | + * Copyright 2022 The Matrix.org Foundation C.I.C. |
| 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 | + |
| 17 | +package org.matrix.android.sdk.internal.crypto |
| 18 | + |
| 19 | +import io.mockk.mockk |
| 20 | +import kotlinx.coroutines.runBlocking |
| 21 | +import org.amshove.kluent.internal.assertEquals |
| 22 | +import org.junit.Assert |
| 23 | +import org.junit.Test |
| 24 | +import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo |
| 25 | +import org.matrix.android.sdk.api.session.crypto.model.DevicesListResponse |
| 26 | +import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap |
| 27 | +import org.matrix.android.sdk.api.session.events.model.EventType |
| 28 | +import org.matrix.android.sdk.api.session.room.model.message.MessageType |
| 29 | +import org.matrix.android.sdk.internal.crypto.api.CryptoApi |
| 30 | +import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDeviceParams |
| 31 | +import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDevicesParams |
| 32 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeyChangesResponse |
| 33 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimBody |
| 34 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimResponse |
| 35 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysQueryBody |
| 36 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysQueryResponse |
| 37 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadBody |
| 38 | +import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadResponse |
| 39 | +import org.matrix.android.sdk.internal.crypto.model.rest.SendToDeviceBody |
| 40 | +import org.matrix.android.sdk.internal.crypto.model.rest.SignatureUploadResponse |
| 41 | +import org.matrix.android.sdk.internal.crypto.model.rest.UpdateDeviceInfoBody |
| 42 | +import org.matrix.android.sdk.internal.crypto.model.rest.UploadSigningKeysBody |
| 43 | +import org.matrix.android.sdk.internal.crypto.tasks.DefaultSendToDeviceTask |
| 44 | +import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask |
| 45 | + |
| 46 | +class DefaultSendToDeviceTaskTest { |
| 47 | + |
| 48 | + private val users = listOf( |
| 49 | + "@alice:example.com" to listOf("D0", "D1"), |
| 50 | + "bob@example.com" to listOf("D2", "D3") |
| 51 | + ) |
| 52 | + |
| 53 | + private val fakeEncryptedContent = mapOf( |
| 54 | + "algorithm" to "m.olm.v1.curve25519-aes-sha2", |
| 55 | + "sender_key" to "gMObR+/4dqL5T4DisRRRYBJpn+OjzFnkyCFOktP6Eyw", |
| 56 | + "ciphertext" to mapOf( |
| 57 | + "tdwXf7006FDgzmufMCVI4rDdVPO51ecRTTT6HkRxUwE" to mapOf( |
| 58 | + "type" to 0, |
| 59 | + "body" to "AwogCA1ULEc0abGIFxMDIC9iv7ul3jqJSnapTHQ+8JJx" |
| 60 | + ) |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + private val fakeStartVerificationContent = mapOf( |
| 65 | + "method" to "m.sas.v1", |
| 66 | + "from_device" to "MNQHVEISFQ", |
| 67 | + "key_agreement_protocols" to listOf( |
| 68 | + "curve25519-hkdf-sha256", |
| 69 | + "curve25519" |
| 70 | + ), |
| 71 | + "hashes" to listOf("sha256"), |
| 72 | + "message_authentication_codes" to listOf( |
| 73 | + "org.matrix.msc3783.hkdf-hmac-sha256", |
| 74 | + "hkdf-hmac-sha256", |
| 75 | + "hmac-sha256" |
| 76 | + ), |
| 77 | + "short_authentication_string" to listOf( |
| 78 | + "decimal", |
| 79 | + "emoji" |
| 80 | + ), |
| 81 | + "transaction_id" to "4wNOpkHGwGZPXjkZToooCDWfb8hsf7vW" |
| 82 | + ) |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `tracing id should be added to to_device contents`() { |
| 86 | + val fakeCryptoAPi = FakeCryptoApi() |
| 87 | + |
| 88 | + val sendToDeviceTask = DefaultSendToDeviceTask( |
| 89 | + cryptoApi = fakeCryptoAPi, |
| 90 | + globalErrorReceiver = mockk(relaxed = true) |
| 91 | + ) |
| 92 | + |
| 93 | + val contentMap = MXUsersDevicesMap<Any>() |
| 94 | + |
| 95 | + users.forEach { pairOfUserDevices -> |
| 96 | + val userId = pairOfUserDevices.first |
| 97 | + pairOfUserDevices.second.forEach { |
| 98 | + contentMap.setObject(userId, it, fakeEncryptedContent) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + val params = SendToDeviceTask.Params( |
| 103 | + eventType = EventType.ENCRYPTED, |
| 104 | + contentMap = contentMap |
| 105 | + ) |
| 106 | + |
| 107 | + runBlocking { |
| 108 | + sendToDeviceTask.execute(params) |
| 109 | + } |
| 110 | + |
| 111 | + val generatedIds = mutableListOf<String>() |
| 112 | + users.forEach { pairOfUserDevices -> |
| 113 | + val userId = pairOfUserDevices.first |
| 114 | + pairOfUserDevices.second.forEach { |
| 115 | + val modifiedContent = fakeCryptoAPi.body!!.messages!![userId]!![it] as Map<*, *> |
| 116 | + Assert.assertNotNull("Tracing id should have been added", modifiedContent["org.matrix.msgid"]) |
| 117 | + generatedIds.add(modifiedContent["org.matrix.msgid"] as String) |
| 118 | + |
| 119 | + assertEquals( |
| 120 | + "The rest of the content should be the same", |
| 121 | + fakeEncryptedContent.keys, |
| 122 | + modifiedContent.toMutableMap().apply { remove("org.matrix.msgid") }.keys |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + assertEquals("Id should be unique per content", generatedIds.size, generatedIds.toSet().size) |
| 128 | + println("modified content ${fakeCryptoAPi.body}") |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `tracing id should not be added to verification start to_device contents`() { |
| 133 | + val fakeCryptoAPi = FakeCryptoApi() |
| 134 | + |
| 135 | + val sendToDeviceTask = DefaultSendToDeviceTask( |
| 136 | + cryptoApi = fakeCryptoAPi, |
| 137 | + globalErrorReceiver = mockk(relaxed = true) |
| 138 | + ) |
| 139 | + val contentMap = MXUsersDevicesMap<Any>() |
| 140 | + contentMap.setObject("@alice:example.com", "MNQHVEISFQ", fakeStartVerificationContent) |
| 141 | + |
| 142 | + val params = SendToDeviceTask.Params( |
| 143 | + eventType = EventType.KEY_VERIFICATION_START, |
| 144 | + contentMap = contentMap |
| 145 | + ) |
| 146 | + |
| 147 | + runBlocking { |
| 148 | + sendToDeviceTask.execute(params) |
| 149 | + } |
| 150 | + |
| 151 | + val modifiedContent = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *> |
| 152 | + Assert.assertNull("Tracing id should not have been added", modifiedContent["org.matrix.msgid"]) |
| 153 | + |
| 154 | + // try to force |
| 155 | + runBlocking { |
| 156 | + sendToDeviceTask.execute( |
| 157 | + SendToDeviceTask.Params( |
| 158 | + eventType = EventType.KEY_VERIFICATION_START, |
| 159 | + contentMap = contentMap, |
| 160 | + addTracingIds = true |
| 161 | + ) |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + val modifiedContentForced = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *> |
| 166 | + Assert.assertNotNull("Tracing id should have been added", modifiedContentForced["org.matrix.msgid"]) |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + fun `tracing id should not be added to all verification to_device contents`() { |
| 171 | + val fakeCryptoAPi = FakeCryptoApi() |
| 172 | + |
| 173 | + val sendToDeviceTask = DefaultSendToDeviceTask( |
| 174 | + cryptoApi = fakeCryptoAPi, |
| 175 | + globalErrorReceiver = mockk(relaxed = true) |
| 176 | + ) |
| 177 | + val contentMap = MXUsersDevicesMap<Any>() |
| 178 | + contentMap.setObject("@alice:example.com", "MNQHVEISFQ", emptyMap<String, Any>()) |
| 179 | + |
| 180 | + val verificationEvents = listOf( |
| 181 | + MessageType.MSGTYPE_VERIFICATION_REQUEST, |
| 182 | + EventType.KEY_VERIFICATION_START, |
| 183 | + EventType.KEY_VERIFICATION_ACCEPT, |
| 184 | + EventType.KEY_VERIFICATION_KEY, |
| 185 | + EventType.KEY_VERIFICATION_MAC, |
| 186 | + EventType.KEY_VERIFICATION_CANCEL, |
| 187 | + EventType.KEY_VERIFICATION_DONE, |
| 188 | + EventType.KEY_VERIFICATION_READY |
| 189 | + ) |
| 190 | + |
| 191 | + for (type in verificationEvents) { |
| 192 | + val params = SendToDeviceTask.Params( |
| 193 | + eventType = type, |
| 194 | + contentMap = contentMap |
| 195 | + ) |
| 196 | + runBlocking { |
| 197 | + sendToDeviceTask.execute(params) |
| 198 | + } |
| 199 | + |
| 200 | + val modifiedContent = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *> |
| 201 | + Assert.assertNull("Tracing id should not have been added", modifiedContent["org.matrix.msgid"]) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + internal class FakeCryptoApi : CryptoApi { |
| 206 | + override suspend fun getDevices(): DevicesListResponse { |
| 207 | + throw java.lang.AssertionError("Should not be called") |
| 208 | + } |
| 209 | + |
| 210 | + override suspend fun getDeviceInfo(deviceId: String): DeviceInfo { |
| 211 | + throw java.lang.AssertionError("Should not be called") |
| 212 | + } |
| 213 | + |
| 214 | + override suspend fun uploadKeys(body: KeysUploadBody): KeysUploadResponse { |
| 215 | + throw java.lang.AssertionError("Should not be called") |
| 216 | + } |
| 217 | + |
| 218 | + override suspend fun downloadKeysForUsers(params: KeysQueryBody): KeysQueryResponse { |
| 219 | + throw java.lang.AssertionError("Should not be called") |
| 220 | + } |
| 221 | + |
| 222 | + override suspend fun uploadSigningKeys(params: UploadSigningKeysBody): KeysQueryResponse { |
| 223 | + throw java.lang.AssertionError("Should not be called") |
| 224 | + } |
| 225 | + |
| 226 | + override suspend fun uploadSignatures(params: Map<String, Any>?): SignatureUploadResponse { |
| 227 | + throw java.lang.AssertionError("Should not be called") |
| 228 | + } |
| 229 | + |
| 230 | + override suspend fun claimOneTimeKeysForUsersDevices(body: KeysClaimBody): KeysClaimResponse { |
| 231 | + throw java.lang.AssertionError("Should not be called") |
| 232 | + } |
| 233 | + |
| 234 | + var body: SendToDeviceBody? = null |
| 235 | + override suspend fun sendToDevice(eventType: String, transactionId: String, body: SendToDeviceBody) { |
| 236 | + this.body = body |
| 237 | + } |
| 238 | + |
| 239 | + override suspend fun deleteDevice(deviceId: String, params: DeleteDeviceParams) { |
| 240 | + throw java.lang.AssertionError("Should not be called") |
| 241 | + } |
| 242 | + |
| 243 | + override suspend fun deleteDevices(params: DeleteDevicesParams) { |
| 244 | + throw java.lang.AssertionError("Should not be called") |
| 245 | + } |
| 246 | + |
| 247 | + override suspend fun updateDeviceInfo(deviceId: String, params: UpdateDeviceInfoBody) { |
| 248 | + throw java.lang.AssertionError("Should not be called") |
| 249 | + } |
| 250 | + |
| 251 | + override suspend fun getKeyChanges(oldToken: String, newToken: String): KeyChangesResponse { |
| 252 | + throw java.lang.AssertionError("Should not be called") |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments