Skip to content

Commit 393036f

Browse files
committed
Test panic and unreachable
1 parent 7b66e54 commit 393036f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

internal/api/lib_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,64 @@ func TestExecute(t *testing.T) {
392392
assert.Equal(t, expectedData, result.Ok.Data)
393393
}
394394

395+
func TestExecutePanic(t *testing.T) {
396+
cache, cleanup := withCache(t)
397+
defer cleanup()
398+
checksum := createCyberpunkContract(t, cache)
399+
400+
maxGas := TESTING_GAS_LIMIT
401+
gasMeter1 := NewMockGasMeter(maxGas)
402+
igasMeter1 := GasMeter(gasMeter1)
403+
// instantiate it with this store
404+
store := NewLookup(gasMeter1)
405+
api := NewMockAPI()
406+
balance := types.Coins{types.NewCoin(250, "ATOM")}
407+
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, balance)
408+
env := MockEnvBin(t)
409+
info := MockInfoBin(t, "creator")
410+
411+
res, _, err := Instantiate(cache, checksum, env, info, []byte(`{}`), &igasMeter1, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
412+
require.NoError(t, err)
413+
requireOkResponse(t, res, 0)
414+
415+
// execute a panic
416+
gasMeter2 := NewMockGasMeter(maxGas)
417+
igasMeter2 := GasMeter(gasMeter2)
418+
store.SetGasMeter(gasMeter2)
419+
info = MockInfoBin(t, "fred")
420+
res, _, err = Execute(cache, checksum, env, info, []byte(`{"panic":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
421+
require.ErrorContains(t, err, "RuntimeError: Aborted: panicked at 'This page intentionally faulted'")
422+
}
423+
424+
func TestExecuteUnreachable(t *testing.T) {
425+
cache, cleanup := withCache(t)
426+
defer cleanup()
427+
checksum := createCyberpunkContract(t, cache)
428+
429+
maxGas := TESTING_GAS_LIMIT
430+
gasMeter1 := NewMockGasMeter(maxGas)
431+
igasMeter1 := GasMeter(gasMeter1)
432+
// instantiate it with this store
433+
store := NewLookup(gasMeter1)
434+
api := NewMockAPI()
435+
balance := types.Coins{types.NewCoin(250, "ATOM")}
436+
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, balance)
437+
env := MockEnvBin(t)
438+
info := MockInfoBin(t, "creator")
439+
440+
res, _, err := Instantiate(cache, checksum, env, info, []byte(`{}`), &igasMeter1, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
441+
require.NoError(t, err)
442+
requireOkResponse(t, res, 0)
443+
444+
// execute a panic
445+
gasMeter2 := NewMockGasMeter(maxGas)
446+
igasMeter2 := GasMeter(gasMeter2)
447+
store.SetGasMeter(gasMeter2)
448+
info = MockInfoBin(t, "fred")
449+
res, _, err = Execute(cache, checksum, env, info, []byte(`{"unreachable":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
450+
require.ErrorContains(t, err, "RuntimeError: unreachable")
451+
}
452+
395453
func TestExecuteStorageLoop(t *testing.T) {
396454
cache, cleanup := withCache(t)
397455
defer cleanup()

0 commit comments

Comments
 (0)