Add a test and create extension for Byte to avoid using UByte (still experimental)

This commit is contained in:
Benoit Marty 2020-02-20 10:12:07 +01:00
parent 382fc6f05c
commit 74175ddfa0
3 changed files with 40 additions and 10 deletions

View File

@ -234,6 +234,18 @@ class QrCodeTest : InstrumentedTest {
expected.toQrCodeData() shouldEqual qrCode expected.toQrCodeData() shouldEqual qrCode
} }
@Test
fun testAnyTransactionId() {
for (qty in 0 until 0x1FFF step 200) {
val longTransactionId = "a".repeat(qty)
val qrCode = qrCode1.copy(transactionId = longTransactionId)
// Symmetric operation
qrCode.toEncodedString().toQrCodeData() shouldEqual qrCode
}
}
// Error cases // Error cases
@Test @Test
fun testErrorHeader() { fun testErrorHeader() {

View File

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.crypto.verification.qrcode
import im.vector.matrix.android.internal.crypto.crosssigning.fromBase64NoPadding import im.vector.matrix.android.internal.crypto.crosssigning.fromBase64NoPadding
import im.vector.matrix.android.internal.crypto.crosssigning.toBase64NoPadding import im.vector.matrix.android.internal.crypto.crosssigning.toBase64NoPadding
import im.vector.matrix.android.internal.util.toPositiveInt
// MATRIX // MATRIX
private val prefix = "MATRIX".toByteArray(Charsets.ISO_8859_1) private val prefix = "MATRIX".toByteArray(Charsets.ISO_8859_1)
@ -94,8 +95,8 @@ fun String.toQrCodeData(): QrCodeData? {
cursor++ cursor++
// Get transaction length // Get transaction length
val bigEndian1 = ensurePositive(byteArray[cursor]) val bigEndian1 = byteArray[cursor].toPositiveInt()
val bigEndian2 = ensurePositive(byteArray[cursor + 1]) val bigEndian2 = byteArray[cursor + 1].toPositiveInt()
val transactionLength = bigEndian1 * 0x0100 + bigEndian2 val transactionLength = bigEndian1 * 0x0100 + bigEndian2
@ -124,11 +125,3 @@ fun String.toQrCodeData(): QrCodeData? {
else -> null else -> null
} }
} }
fun ensurePositive(byte: Byte): Int {
return if (byte < 0) {
256 + byte
} else {
byte.toInt()
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2020 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.util
fun Byte.toPositiveInt(): Int {
return if (this < 0) {
256 + this
} else {
toInt()
}
}