|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 New Vector Ltd |
| 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 im.vector.app.features.crypto |
| 18 | + |
| 19 | +import im.vector.app.E2EMessageDetected |
| 20 | +import im.vector.app.UISIDetector |
| 21 | +import kotlinx.coroutines.CoroutineScope |
| 22 | +import kotlinx.coroutines.Dispatchers |
| 23 | +import kotlinx.coroutines.SupervisorJob |
| 24 | +import kotlinx.coroutines.delay |
| 25 | +import kotlinx.coroutines.launch |
| 26 | +import kotlinx.coroutines.runBlocking |
| 27 | +import org.amshove.kluent.fail |
| 28 | +import org.junit.Assert |
| 29 | +import org.junit.Test |
| 30 | +import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM |
| 31 | +import org.matrix.android.sdk.api.session.crypto.MXCryptoError |
| 32 | +import org.matrix.android.sdk.api.session.events.model.Event |
| 33 | +import org.matrix.android.sdk.api.session.events.model.EventType |
| 34 | +import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent |
| 35 | +import org.matrix.android.sdk.api.session.events.model.toContent |
| 36 | + |
| 37 | +class UISIDetectorTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `trigger detection after grace period`() { |
| 41 | + val gracePeriod = 5_000L |
| 42 | + val uisiDetector = UISIDetector(gracePeriod) |
| 43 | + var detectedEvent: E2EMessageDetected? = null |
| 44 | + |
| 45 | + uisiDetector.callback = object : UISIDetector.UISIDetectorCallback { |
| 46 | + override val enabled = true |
| 47 | + override val reciprocateToDeviceEventType = "foo" |
| 48 | + |
| 49 | + override fun uisiDetected(source: E2EMessageDetected) { |
| 50 | + detectedEvent = source |
| 51 | + } |
| 52 | + |
| 53 | + override fun uisiReciprocateRequest(source: Event) { |
| 54 | + // nop |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // report a decryption error |
| 59 | + val eventId = "0001" |
| 60 | + val event = fakeEncryptedEvent(eventId, "s1", "r1") |
| 61 | + uisiDetector.onEventDecryptionError(event, fakeCryptoError()) |
| 62 | + |
| 63 | + runBlocking { |
| 64 | + delay((gracePeriod * 1.2).toLong()) |
| 65 | + } |
| 66 | + Assert.assertEquals(eventId, detectedEvent?.eventId) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `If event decrypted during grace period should not trigger detection`() { |
| 71 | + val scope = CoroutineScope(SupervisorJob()) |
| 72 | + val gracePeriod = 5_000L |
| 73 | + val uisiDetector = UISIDetector(gracePeriod) |
| 74 | + |
| 75 | + uisiDetector.callback = object : UISIDetector.UISIDetectorCallback { |
| 76 | + override val enabled = true |
| 77 | + override val reciprocateToDeviceEventType = "foo" |
| 78 | + |
| 79 | + override fun uisiDetected(source: E2EMessageDetected) { |
| 80 | + fail("Shouldn't trigger") |
| 81 | + } |
| 82 | + |
| 83 | + override fun uisiReciprocateRequest(source: Event) { |
| 84 | + // nop |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // report a decryption error |
| 89 | + val event = fakeEncryptedEvent("0001", "s1", "r1") |
| 90 | + uisiDetector.onEventDecryptionError(event, fakeCryptoError()) |
| 91 | + |
| 92 | + // the grace period is 30s |
| 93 | + scope.launch(Dispatchers.Default) { |
| 94 | + delay((gracePeriod * 0.5).toLong()) |
| 95 | + uisiDetector.onEventDecrypted(event, emptyMap()) |
| 96 | + } |
| 97 | + |
| 98 | + runBlocking { |
| 99 | + delay((gracePeriod * 1.2).toLong()) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private fun fakeEncryptedEvent(eventId: String, sessionId: String, roomId: String): Event { |
| 104 | + return Event( |
| 105 | + type = EventType.ENCRYPTED, |
| 106 | + eventId = eventId, |
| 107 | + roomId = roomId, |
| 108 | + content = EncryptedEventContent( |
| 109 | + algorithm = MXCRYPTO_ALGORITHM_MEGOLM, |
| 110 | + ciphertext = "AwgBEpACQEKOkd4Gp0+gSXG4M+btcrnPgsF23xs/lUmS2I4YjmqF...", |
| 111 | + sessionId = sessionId, |
| 112 | + senderKey = "5e3EIqg3JfooZnLQ2qHIcBarbassQ4qXblai0", |
| 113 | + deviceId = "FAKEE" |
| 114 | + ).toContent() |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + private fun fakeCryptoError(error: MXCryptoError.ErrorType = MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) = MXCryptoError.Base( |
| 119 | + error, |
| 120 | + "A description", |
| 121 | + "Human readable" |
| 122 | + ) |
| 123 | +} |
0 commit comments