Skip to content

Commit

Permalink
Test rust panic for regression
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Mar 6, 2023
1 parent 3160256 commit a52e604
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
23 changes: 23 additions & 0 deletions x/wasm/keeper/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,29 @@ func TestWasmRawQueryWithNil(t *testing.T) {
require.Equal(t, []byte{}, reflectRawRes.Data)
}

func TestRustPanicIsHandled(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, ReflectFeatures)
keeper := keepers.ContractKeeper

creator := keepers.Faucet.NewFundedRandomAccount(ctx, sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))...)

// upload code
codeID, _, err := keeper.Create(ctx, creator, testdata.CyberpunkContractWasm(), nil)
require.NoError(t, err)
require.Equal(t, uint64(1), codeID)

contractAddr, _, err := keeper.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "cyberpunk contract", nil)
require.NoError(t, err)
require.NotEmpty(t, contractAddr)

// when panic is triggered
msg := []byte(`{"panic":{}}`)
gotData, err := keeper.Execute(ctx, contractAddr, creator, msg, nil)
require.ErrorIs(t, err, types.ErrExecuteFailed)
assert.Contains(t, err.Error(), "panicked at 'This page intentionally faulted'")
assert.Nil(t, gotData)
}

func checkAccount(t *testing.T, ctx sdk.Context, accKeeper authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, expected sdk.Coins) {
acct := accKeeper.GetAccount(ctx, addr)
if expected == nil {
Expand Down
Binary file added x/wasm/keeper/testdata/cyberpunk.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion x/wasm/keeper/testdata/download_releases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi

tag="$1"

for contract in burner hackatom ibc_reflect ibc_reflect_send reflect staking; do
for contract in burner hackatom ibc_reflect ibc_reflect_send reflect staking cyberpunk; do
url="https://github.com/CosmWasm/cosmwasm/releases/download/$tag/${contract}.wasm"
echo "Downloading $url ..."
wget -O "${contract}.wasm" "$url"
Expand Down
7 changes: 7 additions & 0 deletions x/wasm/keeper/testdata/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var reflectContract []byte
//go:embed reflect_1_1.wasm
var migrateReflectContract []byte

//go:embed cyberpunk.wasm
var cyberpunkContract []byte

func ReflectContractWasm() []byte {
return reflectContract
}
Expand All @@ -21,6 +24,10 @@ func MigrateReflectContractWasm() []byte {
return migrateReflectContract
}

func CyberpunkContractWasm() []byte {
return cyberpunkContract
}

// ReflectHandleMsg is used to encode handle messages
type ReflectHandleMsg struct {
Reflect *ReflectPayload `json:"reflect_msg,omitempty"`
Expand Down

0 comments on commit a52e604

Please sign in to comment.