|
| 1 | +/* |
| 2 | + * Copyright 2023 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 android.util.Log |
| 20 | +import androidx.lifecycle.Observer |
| 21 | +import androidx.test.filters.LargeTest |
| 22 | +import kotlinx.coroutines.DelicateCoroutinesApi |
| 23 | +import kotlinx.coroutines.Dispatchers |
| 24 | +import kotlinx.coroutines.GlobalScope |
| 25 | +import kotlinx.coroutines.launch |
| 26 | +import kotlinx.coroutines.withContext |
| 27 | +import org.junit.FixMethodOrder |
| 28 | +import org.junit.Test |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.junit.runners.JUnit4 |
| 31 | +import org.junit.runners.MethodSorters |
| 32 | +import org.matrix.android.sdk.InstrumentedTest |
| 33 | +import org.matrix.android.sdk.api.session.Session |
| 34 | +import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel |
| 35 | +import org.matrix.android.sdk.api.session.getRoom |
| 36 | +import org.matrix.android.sdk.api.session.room.model.RoomSummary |
| 37 | +import org.matrix.android.sdk.api.util.Optional |
| 38 | +import org.matrix.android.sdk.common.CommonTestHelper |
| 39 | +import org.matrix.android.sdk.common.SessionTestParams |
| 40 | +import org.matrix.android.sdk.common.TestConstants |
| 41 | +import java.util.concurrent.CountDownLatch |
| 42 | +import java.util.concurrent.TimeUnit |
| 43 | + |
| 44 | +@RunWith(JUnit4::class) |
| 45 | +@FixMethodOrder(MethodSorters.JVM) |
| 46 | +@LargeTest |
| 47 | +class RoomShieldTest : InstrumentedTest { |
| 48 | + |
| 49 | + @Test |
| 50 | + fun testShieldNoVerification() = CommonTestHelper.runCryptoTest(context()) { cryptoTestHelper, _ -> |
| 51 | + val testData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom() |
| 52 | + |
| 53 | + val roomId = testData.roomId |
| 54 | + |
| 55 | + cryptoTestHelper.initializeCrossSigning(testData.firstSession) |
| 56 | + cryptoTestHelper.initializeCrossSigning(testData.secondSession!!) |
| 57 | + |
| 58 | + // Test are flaky unless I use liveData observer on main thread |
| 59 | + // Just calling getRoomSummary() with retryWithBackOff keeps an outdated version of the value |
| 60 | + testData.firstSession.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Default) |
| 61 | + testData.secondSession!!.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Default) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testShieldInOneOne() = CommonTestHelper.runLongCryptoTest(context()) { cryptoTestHelper, testHelper -> |
| 66 | + val testData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom() |
| 67 | + |
| 68 | + val roomId = testData.roomId |
| 69 | + |
| 70 | + Log.v("#E2E TEST", "Initialize cross signing...") |
| 71 | + cryptoTestHelper.initializeCrossSigning(testData.firstSession) |
| 72 | + cryptoTestHelper.initializeCrossSigning(testData.secondSession!!) |
| 73 | + Log.v("#E2E TEST", "... Initialized.") |
| 74 | + |
| 75 | + // let alive and bob verify |
| 76 | + Log.v("#E2E TEST", "Alice and Bob verify each others...") |
| 77 | + cryptoTestHelper.verifySASCrossSign(testData.firstSession, testData.secondSession!!, testData.roomId) |
| 78 | + |
| 79 | + // Add a new session for bob |
| 80 | + // This session will be unverified for now |
| 81 | + |
| 82 | + Log.v("#E2E TEST", "Log in a new bob device...") |
| 83 | + val bobSecondSession = testHelper.logIntoAccount(testData.secondSession!!.myUserId, SessionTestParams(true)) |
| 84 | + |
| 85 | + Log.v("#E2E TEST", "Bob session logged in ${bobSecondSession.myUserId.take(6)}") |
| 86 | + |
| 87 | + Log.v("#E2E TEST", "Assert room shields...") |
| 88 | + testData.firstSession.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Warning) |
| 89 | + // in 1:1 we ignore our own status |
| 90 | + testData.secondSession!!.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Trusted) |
| 91 | + |
| 92 | + // Adding another user should make bob consider his devices now and see same shield as alice |
| 93 | + Log.v("#E2E TEST", "Create Sam account") |
| 94 | + val samSession = testHelper.createAccount(TestConstants.USER_SAM, SessionTestParams(withInitialSync = true)) |
| 95 | + |
| 96 | + // Let alice invite sam |
| 97 | + Log.v("#E2E TEST", "Let alice invite sam") |
| 98 | + testData.firstSession.getRoom(roomId)!!.membershipService().invite(samSession.myUserId) |
| 99 | + testHelper.waitForAndAcceptInviteInRoom(samSession, roomId) |
| 100 | + |
| 101 | + Log.v("#E2E TEST", "Assert room shields...") |
| 102 | + testData.firstSession.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Warning) |
| 103 | + testData.secondSession!!.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Warning) |
| 104 | + |
| 105 | + // Now let's bob verify his session |
| 106 | + |
| 107 | + Log.v("#E2E TEST", "Bob verifies his new session") |
| 108 | + cryptoTestHelper.verifyNewSession(testData.secondSession!!, bobSecondSession) |
| 109 | + |
| 110 | + testData.firstSession.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Trusted) |
| 111 | + testData.secondSession!!.assertRoomShieldIs(roomId, RoomEncryptionTrustLevel.Trusted) |
| 112 | + } |
| 113 | + |
| 114 | + @OptIn(DelicateCoroutinesApi::class) |
| 115 | + private suspend fun Session.assertRoomShieldIs(roomId: String, state: RoomEncryptionTrustLevel?) { |
| 116 | + val lock = CountDownLatch(1) |
| 117 | + val roomLiveData = withContext(Dispatchers.Main) { |
| 118 | + roomService().getRoomSummaryLive(roomId) |
| 119 | + } |
| 120 | + val observer = object : Observer<Optional<RoomSummary>> { |
| 121 | + override fun onChanged(value: Optional<RoomSummary>) { |
| 122 | + Log.v("#E2E TEST ${this@assertRoomShieldIs.myUserId.take(6)}", "Shield Update ${value.getOrNull()?.roomEncryptionTrustLevel}") |
| 123 | + if (value.getOrNull()?.roomEncryptionTrustLevel == state) { |
| 124 | + lock.countDown() |
| 125 | + roomLiveData.removeObserver(this) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + GlobalScope.launch(Dispatchers.Main) { roomLiveData.observeForever(observer) } |
| 130 | + |
| 131 | + lock.await(40_000, TimeUnit.MILLISECONDS) |
| 132 | + } |
| 133 | +} |
0 commit comments