mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-31 07:12:17 +00:00
Fix one unexpected case of fiatToCoin overflow and add tests.
This commit is contained in:
parent
8c99e61e01
commit
ca2a9ed8f1
@ -70,6 +70,10 @@ public class ExchangeRate implements Serializable {
|
||||
if (converted.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
|
||||
|| converted.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
|
||||
throw new ArithmeticException("Overflow");
|
||||
return Coin.valueOf(converted.longValue());
|
||||
try {
|
||||
return Coin.valueOf(converted.longValue());
|
||||
} catch (IllegalArgumentException x) {
|
||||
throw new ArithmeticException("Overflow: " + x.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,4 +50,28 @@ public class ExchangeRateTest {
|
||||
ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("EUR", "500"));
|
||||
rate.fiatToCoin(Fiat.parseFiat("USD", "1"));
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void fiatToCoinTooLarge() throws Exception {
|
||||
ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1"));
|
||||
rate.fiatToCoin(Fiat.parseFiat("XXX", "21000001"));
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void fiatToCoinTooSmall() throws Exception {
|
||||
ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1"));
|
||||
rate.fiatToCoin(Fiat.parseFiat("XXX", "-21000001"));
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void coinToFiatTooLarge() throws Exception {
|
||||
ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1000000000"));
|
||||
rate.coinToFiat(Coin.parseCoin("1000000"));
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void coinToFiatTooSmall() throws Exception {
|
||||
ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1000000000"));
|
||||
rate.coinToFiat(Coin.parseCoin("-1000000"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user