[#289] Crash while typing characters to Zec amount text field.

- Filed issue to SDK project.
- Temporary workaround to prevent SDK from returning negative results and thus to avoid app crashing.
- SuppressWarnings - ReturnCount - ZecString.
- Added unit test for validation of implemented workaround.
This commit is contained in:
Honza Rychnovsky 2022-04-12 14:13:48 +02:00 committed by GitHub
parent f091da51ee
commit e44e901676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -3,12 +3,17 @@ package cash.z.ecc.sdk.ext.ui.model
import android.content.Context
import android.content.res.Configuration
import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.SmallTest
import cash.z.ecc.android.sdk.ext.Conversions
import cash.z.ecc.sdk.model.Zatoshi
import org.junit.Assert.assertEquals
import org.junit.Ignore
import org.junit.Test
import java.math.BigDecimal
import java.util.Locale
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
class ZecStringTest {
@ -87,4 +92,18 @@ class ZecStringTest {
assertNull(Zatoshi.fromZecString(context, "1,23,", EN_US_MONETARY_SEPARATORS))
assertNull(Zatoshi.fromZecString(context, "1,234,", EN_US_MONETARY_SEPARATORS))
}
// TODO [472]: https://github.com/zcash/zcash-android-wallet-sdk/issues/472
@Test
@SmallTest
fun overflow_number_test() {
assertNotNull(Zatoshi.fromZecString(context, "1", EN_US_MONETARY_SEPARATORS))
assertNotNull(Zatoshi.fromZecString(context, "1,000", EN_US_MONETARY_SEPARATORS))
assertNotNull(Zatoshi.fromZecString(context, "10,000,000,000", EN_US_MONETARY_SEPARATORS))
assertNull(Zatoshi.fromZecString(context, "100,000,000,000", EN_US_MONETARY_SEPARATORS))
val overflowCausingNumber = 100000000000L
assertTrue(BigDecimal(overflowCausingNumber).times(Conversions.ONE_ZEC_IN_ZATOSHI) > BigDecimal(Long.MAX_VALUE))
assertNull(Zatoshi.fromZecString(context, overflowCausingNumber.toString(), EN_US_MONETARY_SEPARATORS))
}
}

View File

@ -1,6 +1,7 @@
package cash.z.ecc.sdk.ext.ui.model
import android.content.Context
import cash.z.ecc.android.sdk.ext.Conversions
import cash.z.ecc.android.sdk.ext.convertZatoshiToZecString
import cash.z.ecc.android.sdk.ext.convertZecToZatoshi
import cash.z.ecc.sdk.ext.ui.ZecStringExt
@ -67,6 +68,7 @@ fun Zatoshi.toZecString() = value.convertZatoshiToZecString(DECIMALS, DECIMALS)
/**
* @return [zecString] parsed into Zatoshi or null if parsing failed.
*/
@SuppressWarnings("ReturnCount")
fun Zatoshi.Companion.fromZecString(
context: Context,
zecString: String,
@ -97,5 +99,11 @@ fun Zatoshi.Companion.fromZecString(
null
}
// TODO [472]: https://github.com/zcash/zcash-android-wallet-sdk/issues/472
// temporary workaround to prevent SDK to returns us negative result
if (bigDecimal?.times(Conversions.ONE_ZEC_IN_ZATOSHI)?.compareTo(BigDecimal(Long.MAX_VALUE)) ?: 0 > 0) {
return null
}
return Zatoshi(bigDecimal.convertZecToZatoshi())
}