From e44e9016769450c4e312c25992ad73d3d123936e Mon Sep 17 00:00:00 2001 From: Honza Rychnovsky Date: Tue, 12 Apr 2022 14:13:48 +0200 Subject: [PATCH] [#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. --- .../z/ecc/sdk/ext/ui/model/ZecStringTest.kt | 19 +++++++++++++++++++ .../cash/z/ecc/sdk/ext/ui/model/ZecString.kt | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/sdk-ext-ui-lib/src/androidTest/java/cash/z/ecc/sdk/ext/ui/model/ZecStringTest.kt b/sdk-ext-ui-lib/src/androidTest/java/cash/z/ecc/sdk/ext/ui/model/ZecStringTest.kt index 2eff7a46..76846af4 100644 --- a/sdk-ext-ui-lib/src/androidTest/java/cash/z/ecc/sdk/ext/ui/model/ZecStringTest.kt +++ b/sdk-ext-ui-lib/src/androidTest/java/cash/z/ecc/sdk/ext/ui/model/ZecStringTest.kt @@ -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)) + } } diff --git a/sdk-ext-ui-lib/src/main/java/cash/z/ecc/sdk/ext/ui/model/ZecString.kt b/sdk-ext-ui-lib/src/main/java/cash/z/ecc/sdk/ext/ui/model/ZecString.kt index 9309c379..bcae4358 100644 --- a/sdk-ext-ui-lib/src/main/java/cash/z/ecc/sdk/ext/ui/model/ZecString.kt +++ b/sdk-ext-ui-lib/src/main/java/cash/z/ecc/sdk/ext/ui/model/ZecString.kt @@ -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()) }