Add Unit tests from legacy SDK

This commit is contained in:
Benoit Marty 2020-01-03 17:09:43 +01:00
parent b6a1ff1ca4
commit ae3381227c
3 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto.keysbackup.util
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runners.MethodSorters
@FixMethodOrder(MethodSorters.JVM)
class Base58Test {
@Test
fun encode() {
// Example comes from https://github.com/keis/base58
assertEquals("StV1DL6CwTryKyV", base58encode("hello world".toByteArray()))
}
@Test
fun decode() {
// Example comes from https://github.com/keis/base58
assertArrayEquals("hello world".toByteArray(), base58decode("StV1DL6CwTryKyV"));
}
@Test
fun encode_curve25519() {
// Encode a 32 bytes key
assertEquals("4F85ZySpwyY6FuH7mQYyyr5b8nV9zFRBLj92AJa37sMr",
base58encode(("0123456789" + "0123456789" + "0123456789" + "01").toByteArray()))
}
@Test
fun decode_curve25519() {
assertArrayEquals(("0123456789" + "0123456789" + "0123456789" + "01").toByteArray(),
base58decode("4F85ZySpwyY6FuH7mQYyyr5b8nV9zFRBLj92AJa37sMr"))
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto.keysbackup.util
import org.junit.Assert.*
import org.junit.Test
class RecoveryKeyTest {
private val curve25519Key = byteArrayOf(
0x77.toByte(), 0x07.toByte(), 0x6D.toByte(), 0x0A.toByte(), 0x73.toByte(), 0x18.toByte(), 0xA5.toByte(), 0x7D.toByte(),
0x3C.toByte(), 0x16.toByte(), 0xC1.toByte(), 0x72.toByte(), 0x51.toByte(), 0xB2.toByte(), 0x66.toByte(), 0x45.toByte(),
0xDF.toByte(), 0x4C.toByte(), 0x2F.toByte(), 0x87.toByte(), 0xEB.toByte(), 0xC0.toByte(), 0x99.toByte(), 0x2A.toByte(),
0xB1.toByte(), 0x77.toByte(), 0xFB.toByte(), 0xA5.toByte(), 0x1D.toByte(), 0xB9.toByte(), 0x2C.toByte(), 0x2A.toByte())
@Test
fun isValidRecoveryKey_valid_true() {
assertTrue(isValidRecoveryKey("EsTcLW2KPGiFwKEA3As5g5c4BXwkqeeJZJV8Q9fugUMNUE4d"))
// Space should be ignored
assertTrue(isValidRecoveryKey("EsTc LW2K PGiF wKEA 3As5 g5c4 BXwk qeeJ ZJV8 Q9fu gUMN UE4d"))
// All whitespace should be ignored
assertTrue(isValidRecoveryKey("EsTc LW2K PGiF wKEA 3As5 g5c4\r\nBXwk qeeJ ZJV8 Q9fu gUMN UE4d"))
}
@Test
fun isValidRecoveryKey_null_false() {
assertFalse(isValidRecoveryKey(null))
}
@Test
fun isValidRecoveryKey_empty_false() {
assertFalse(isValidRecoveryKey(""))
}
@Test
fun isValidRecoveryKey_wrong_size_false() {
assertFalse(isValidRecoveryKey("abc"))
}
@Test
fun isValidRecoveryKey_bad_first_byte_false() {
assertFalse(isValidRecoveryKey("FsTc LW2K PGiF wKEA 3As5 g5c4 BXwk qeeJ ZJV8 Q9fu gUMN UE4d"))
}
@Test
fun isValidRecoveryKey_bad_second_byte_false() {
assertFalse(isValidRecoveryKey("EqTc LW2K PGiF wKEA 3As5 g5c4 BXwk qeeJ ZJV8 Q9fu gUMN UE4d"))
}
@Test
fun isValidRecoveryKey_bad_parity_false() {
assertFalse(isValidRecoveryKey("EsTc LW2K PGiF wKEA 3As5 g5c4 BXwk qeeJ ZJV8 Q9fu gUMN UE4e"))
}
@Test
fun computeRecoveryKey_ok() {
assertEquals("EsTcLW2KPGiFwKEA3As5g5c4BXwkqeeJZJV8Q9fugUMNUE4d", computeRecoveryKey(curve25519Key))
}
@Test
fun extractCurveKeyFromRecoveryKey_ok() {
assertArrayEquals(curve25519Key, extractCurveKeyFromRecoveryKey("EsTc LW2K PGiF wKEA 3As5 g5c4 BXwk qeeJ ZJV8 Q9fu gUMN UE4d"))
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.crypto.store.db
import im.vector.matrix.android.internal.util.md5
import org.junit.Assert.assertEquals
import org.junit.Test
class HelperTest {
@Test
fun testHash_ok() {
assertEquals("e9ee13b1ba2afc0825f4e556114785dd", "alice_15428931567802abf5ba7-d685-4333-af47-d38417ab3724:localhost:8480".md5())
}
@Test
fun testHash_size_ok() {
// Any String will have a 32 char hash
for (i in 1..100) {
assertEquals(32, "a".repeat(i).md5().length)
}
}
}