mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-27 20:09:27 +03:00
Add a test and create extension for Byte to avoid using UByte (still experimental)
This commit is contained in:
parent
382fc6f05c
commit
74175ddfa0
3 changed files with 40 additions and 10 deletions
|
@ -234,6 +234,18 @@ class QrCodeTest : InstrumentedTest {
|
|||
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
|
||||
@Test
|
||||
fun testErrorHeader() {
|
||||
|
|
|
@ -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.toBase64NoPadding
|
||||
import im.vector.matrix.android.internal.util.toPositiveInt
|
||||
|
||||
// MATRIX
|
||||
private val prefix = "MATRIX".toByteArray(Charsets.ISO_8859_1)
|
||||
|
@ -94,8 +95,8 @@ fun String.toQrCodeData(): QrCodeData? {
|
|||
cursor++
|
||||
|
||||
// Get transaction length
|
||||
val bigEndian1 = ensurePositive(byteArray[cursor])
|
||||
val bigEndian2 = ensurePositive(byteArray[cursor + 1])
|
||||
val bigEndian1 = byteArray[cursor].toPositiveInt()
|
||||
val bigEndian2 = byteArray[cursor + 1].toPositiveInt()
|
||||
|
||||
val transactionLength = bigEndian1 * 0x0100 + bigEndian2
|
||||
|
||||
|
@ -124,11 +125,3 @@ fun String.toQrCodeData(): QrCodeData? {
|
|||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun ensurePositive(byte: Byte): Int {
|
||||
return if (byte < 0) {
|
||||
256 + byte
|
||||
} else {
|
||||
byte.toInt()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue