test: add overflow test for coin Add (#19344)

This commit is contained in:
zoereco 2024-02-05 10:49:05 +01:00 committed by GitHub
parent 1b5c30f297
commit fb6da9a767
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@ package types_test
import (
"fmt"
"math/big"
"strings"
"testing"
@ -156,6 +157,9 @@ func (s *coinTestSuite) TestCoinsDenoms() {
}
func (s *coinTestSuite) TestAddCoin() {
// 2**256 - 1 value to check for overflows
maxUint256 := math.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)))
cases := []struct {
inputOne sdk.Coin
inputTwo sdk.Coin
@ -165,6 +169,10 @@ func (s *coinTestSuite) TestAddCoin() {
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 2), false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom1, 1), false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), sdk.NewInt64Coin(testDenom1, 1), true},
// addition cannot lead to a number with more than 256 bits
{sdk.NewCoin(testDenom1, maxUint256), sdk.NewCoin(testDenom1, math.NewInt(0)), sdk.NewCoin(testDenom1, maxUint256), false},
{sdk.NewCoin(testDenom1, maxUint256), sdk.NewCoin(testDenom1, math.NewInt(1)), sdk.NewInt64Coin(testDenom1, 1), true},
}
for tcIndex, tc := range cases {