Fix issue with long transactionId

This commit is contained in:
Benoit Marty 2020-02-19 18:59:39 +01:00
parent 932c478ed5
commit 382fc6f05c
2 changed files with 28 additions and 1 deletions

View File

@ -218,6 +218,22 @@ class QrCodeTest : InstrumentedTest {
compareArray(byteArray.copyOfRange(23 + 64, byteArray.size), sharedSecretByteArray)
}
@Test
fun testLongTransactionId() {
// Size on two bytes (2_000 = 0x07D0)
val longTransactionId = "PatternId_".repeat(200)
val qrCode = qrCode1.copy(transactionId = longTransactionId)
val result = qrCode.toEncodedString()
val expected = value1.replace("\u0000\u000DMaTransaction", "\u0007\u00D0$longTransactionId")
result shouldEqual expected
// Reverse operation
expected.toQrCodeData() shouldEqual qrCode
}
// Error cases
@Test
fun testErrorHeader() {

View File

@ -94,7 +94,10 @@ fun String.toQrCodeData(): QrCodeData? {
cursor++
// Get transaction length
val transactionLength = (byteArray[cursor].toInt() shr 8) + byteArray[cursor + 1].toInt()
val bigEndian1 = ensurePositive(byteArray[cursor])
val bigEndian2 = ensurePositive(byteArray[cursor + 1])
val transactionLength = bigEndian1 * 0x0100 + bigEndian2
cursor++
cursor++
@ -121,3 +124,11 @@ fun String.toQrCodeData(): QrCodeData? {
else -> null
}
}
fun ensurePositive(byte: Byte): Int {
return if (byte < 0) {
256 + byte
} else {
byte.toInt()
}
}