Skip to content

Table driven tests #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
//go:build cgo && !nolink_libwasmvm

package cosmwasm

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/CosmWasm/wasmvm/v2/internal/api"

Check failure on line 8 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/CosmWasm/wasmvm (goimports)
"github.com/CosmWasm/wasmvm/v2/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const IBC_TEST_CONTRACT = "./testdata/ibc_reflect.wasm"
Expand Down Expand Up @@ -76,6 +73,7 @@
}

func toBytes(t *testing.T, v interface{}) []byte {
t.Helper()
bz, err := json.Marshal(v)
require.NoError(t, err)
return bz
Expand All @@ -90,9 +88,25 @@
const CHANNEL_ID = "channel-432"

vm := withVM(t)

// First store the reflect contract that will be used by ibc_reflect
reflectWasm, err := os.ReadFile("./testdata/reflect.wasm")
require.NoError(t, err)
reflectChecksum, codeID, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
require.NoError(t, err)
t.Logf("Stored reflect contract with checksum: %v and code ID: %d", reflectChecksum, codeID)

// Verify we can access the reflect contract code
reflectCode, err := vm.GetCode(reflectChecksum)
require.NoError(t, err)
t.Logf("Successfully retrieved reflect contract code, size: %d bytes", len(reflectCode))

// Now store the IBC contract
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
t.Logf("Stored IBC contract with checksum: %v", checksum)

deserCost := types.UFraction{Numerator: 1, Denominator: 1}
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
Expand All @@ -105,11 +119,17 @@
init_msg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
i, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
t.Logf("Attempting to instantiate IBC contract with reflect code ID: %d", REFLECT_ID)
t.Logf("Instantiation message: %s", string(toBytes(t, init_msg)))
i, gas_used, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
if err != nil {
t.Logf("Instantiation failed with gas used: %d", gas_used)
}
require.NoError(t, err)
t.Logf("Instantiation response: %+v", i)
assert.NotNil(t, i.Ok)
iResponse := i.Ok
require.Equal(t, 0, len(iResponse.Messages))
require.Empty(t, iResponse.Messages)

// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
Expand All @@ -132,7 +152,7 @@
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)

// check for the expected custom event
expected_events := []types.Event{{
Expand Down Expand Up @@ -200,7 +220,7 @@
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)
id := connResponse.Messages[0].ID

// mock reflect init callback (to store address)
Expand Down Expand Up @@ -237,7 +257,7 @@
var accounts ListAccountsResponse
err = json.Unmarshal(qResponse, &accounts)
require.NoError(t, err)
require.Equal(t, 1, len(accounts.Accounts))
require.Len(t, accounts.Accounts, 1)
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)

Expand Down Expand Up @@ -332,7 +352,7 @@
require.Equal(t, msg1.GetChannel(), msg4.GetChannel())
require.Equal(t, msg1.GetChannel(), msg5.GetChannel())
require.Equal(t, msg1.GetChannel(), msg6.GetChannel())
require.Equal(t, msg1.GetChannel().Endpoint.ChannelID, CHANNEL_ID)
require.Equal(t, CHANNEL_ID, msg1.GetChannel().Endpoint.ChannelID)
}

func TestIBCMsgGetCounterVersion(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
TESTING_CACHE_SIZE = 100 // MiB
)

var TESTING_CAPABILITIES = []string{"staking", "stargate", "iterator", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_3"}
var TESTING_CAPABILITIES = []string{"staking", "stargate", "iterator", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "cosmwasm_2_1", "cosmwasm_2_2"}

func TestInitAndReleaseCache(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "wasmvm-testing")
Expand Down
5 changes: 2 additions & 3 deletions lib_libwasmvm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !nolink_libwasmvm

// This file contains the part of the API that is exposed when libwasmvm
// is available (i.e. cgo is enabled and nolink_libwasmvm is not set).

Expand Down Expand Up @@ -93,7 +91,8 @@ func (vm *VM) SimulateStoreCode(code WasmCode, gasLimit uint64) (Checksum, uint6
// StoreCodeUnchecked is the same as StoreCode but skips static validation checks.
// Use this for adding code that was checked before, particularly in the case of state sync.
func (vm *VM) StoreCodeUnchecked(code WasmCode) (Checksum, error) {
return api.StoreCodeUnchecked(vm.cache, code)
checksum, err := api.StoreCodeUnchecked(vm.cache, code)
return checksum, err
}

func (vm *VM) RemoveCode(checksum Checksum) error {
Expand Down
Loading
Loading