Skip to content

Commit

Permalink
test: add overflow test for coin Add (#19344)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoereco authored Feb 5, 2024
1 parent 1b5c30f commit fb6da9a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"fmt"
"math/big"
"strings"
"testing"

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit fb6da9a

Please sign in to comment.