Skip to content

Commit 797a4f9

Browse files
committed
Add unit test on ScanEncryptorUtils.
1 parent 76616b1 commit 797a4f9

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2024 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 org.matrix.android.sdk.internal.session.contentscanner
18+
19+
import okio.ByteString.Companion.decodeBase64
20+
import org.amshove.kluent.shouldBeEqualTo
21+
import org.junit.Test
22+
import org.matrix.android.sdk.api.session.crypto.attachments.ElementToDecrypt
23+
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileInfo
24+
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileKey
25+
import org.matrix.android.sdk.internal.crypto.tools.withOlmDecryption
26+
import org.matrix.android.sdk.internal.di.MoshiProvider
27+
import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody
28+
import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody
29+
import org.matrix.olm.OlmPkMessage
30+
31+
class ScanEncryptorUtilsTest {
32+
private val anMxcUrl = "mxc://matrix.org/123456"
33+
private val anElementToDecrypt = ElementToDecrypt(
34+
k = "key",
35+
iv = "iv",
36+
sha256 = "sha256"
37+
)
38+
private val aPrivateKey = "CLYwNaeA9d0KHE0DniO1bxGgmNsPJ/pyanF4b4tcK1M="
39+
40+
@Test
41+
fun whenNoServerKeyIsProvidedTheContentIsNotEncrypted() {
42+
val result = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(
43+
publicServerKey = null,
44+
mxcUrl = anMxcUrl,
45+
elementToDecrypt = anElementToDecrypt
46+
)
47+
result shouldBeEqualTo DownloadBody(
48+
file = EncryptedFileInfo(
49+
url = anMxcUrl,
50+
iv = anElementToDecrypt.iv,
51+
hashes = mapOf("sha256" to anElementToDecrypt.sha256),
52+
key = EncryptedFileKey(
53+
k = anElementToDecrypt.k,
54+
alg = "A256CTR",
55+
keyOps = listOf("encrypt", "decrypt"),
56+
kty = "oct",
57+
ext = true
58+
),
59+
v = "v2"
60+
),
61+
encryptedBody = null
62+
)
63+
}
64+
65+
@Test
66+
fun checkThatTheCodeIsAbleToDecryptContent() {
67+
System.loadLibrary("olm")
68+
val clearInfo = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(
69+
publicServerKey = null,
70+
mxcUrl = anMxcUrl,
71+
elementToDecrypt = anElementToDecrypt
72+
)
73+
// Uncomment to get a new encrypted body
74+
// val aPublicKey = "6n3l15JqsNhpM1OwRIoDCL/3c1B5idcwvy07Y5qFRyw="
75+
// val encryptedBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(
76+
// publicServerKey = aPublicKey,
77+
// mxcUrl = anMxcUrl,
78+
// elementToDecrypt = anElementToDecrypt
79+
// ).encryptedBody!!
80+
// println("libolmEncryptedBody: $encryptedBody")
81+
val libolmEncryptedBody = EncryptedBody(
82+
cipherText = "GTnDhm6xe5fPe/QCr6fyGcZXheFhZlPG" +
83+
"nJZiCK8Xwq6qTg71vSUGWtLdt3uaTmK7" +
84+
"F7fB3PBKchHu2VVv6MMgo8fpUQ7KBbmu" +
85+
"NWTrNmf3QdhXuRwUwz/q4GxsbGR2zjSX" +
86+
"/UoE5S4ymVtOVhvSfXQfssN56wVIzC6S" +
87+
"dy57y6b1IXPihlCUdvb8LMkMvViHYeNf" +
88+
"beFrAfMlsyr1+jdZEXZF5Q7iruhsH2iu" +
89+
"k7+Ayl9rdILCD5tjE9pezwe1V6uc/Agb",
90+
mac = "Wk77HRg50oM",
91+
ephemeral = "rMTK6/CGASinfX4USFS5qmD3r4meffxKc/jCSFIBczw"
92+
)
93+
// Try to decrypt the body
94+
val result = withOlmDecryption { olmPkDecryption ->
95+
olmPkDecryption.setPrivateKey(aPrivateKey.decodeBase64()!!.toByteArray())
96+
olmPkDecryption.decrypt(
97+
OlmPkMessage().apply {
98+
mCipherText = libolmEncryptedBody.cipherText
99+
mMac = libolmEncryptedBody.mac
100+
mEphemeralKey = libolmEncryptedBody.ephemeral
101+
}
102+
)
103+
}
104+
val parseResult = MoshiProvider.providesMoshi()
105+
.adapter(DownloadBody::class.java)
106+
.fromJson(result)
107+
parseResult shouldBeEqualTo clearInfo
108+
}
109+
}

0 commit comments

Comments
 (0)