Skip to content
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

test(gno.land/sdk/vm): add unit tests for Msg*.ValidateBasic #2855

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Changes from 4 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
302 changes: 302 additions & 0 deletions gno.land/pkg/sdk/vm/msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
package vm

import (
"testing"

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/assert"
)

func TestMsgAddPackage(t *testing.T) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

creator := crypto.AddressFromPreimage([]byte("addr1"))
pkgName := "test"
pkgPath := "gno.land/r/namespace/test"
files := []*std.MemFile{
{
Name: "test.gno",
Body: `package test
func Echo() string {return "hello world"}`,
},
}

tests := []struct {
name string
msg MsgAddPackage
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgAddPackage{
thehowl marked this conversation as resolved.
Show resolved Hide resolved
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"creator":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"deposit":"1000ugnot","package":{"files":[{"body":"package test\n\t\tfunc Echo() string {return \"hello world\"}",` +
`"name":"test.gno"}],"name":"test","path":"gno.land/r/namespace/test"}}`,
expectErr: nil,
},
{
name: "missing creator address",
msg: MsgAddPackage{
Creator: crypto.Address{},
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "missing package path",
msg: MsgAddPackage{
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: "",
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "invalid deposit coins",
msg: MsgAddPackage{
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: -1000, // invalid amount
}},
},
expectErr: std.InvalidCoinsError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}

func TestMsgCall(t *testing.T) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

caller := crypto.AddressFromPreimage([]byte("addr1"))
pkgPath := "gno.land/r/namespace/test"
funcName := "MyFunction"
args := []string{"arg1", "arg2"}

tests := []struct {
name string
msg MsgCall
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgCall{
Caller: caller,
PkgPath: pkgPath,
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"args":["arg1","arg2"],"caller":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"func":"MyFunction","pkg_path":"gno.land/r/namespace/test","send":"1000ugnot"}`,
expectErr: nil,
},
{
name: "invalid caller address",
msg: MsgCall{
Caller: crypto.Address{},
PkgPath: pkgPath,
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "missing package path",
msg: MsgCall{
Caller: caller,
PkgPath: "",
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "pkgPath should not be a realm path",
msg: MsgCall{
Caller: caller,
PkgPath: "gno.land/p/namespace/test", // this is not a valid realm path
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "missing function name to call",
msg: MsgCall{
Caller: caller,
PkgPath: pkgPath,
Func: "",
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidExprError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}

func TestMsgRun(t *testing.T) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

caller := crypto.AddressFromPreimage([]byte("addr1"))
pkgName := "main"
pkgPath := "gno.land/r/" + caller.String() + "/run"
pkgFiles := []*std.MemFile{
{
Name: "main.gno",
Body: `package main
func Echo() string {return "hello world"}`,
},
}

tests := []struct {
name string
msg MsgRun
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgRun{
Caller: caller,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"caller":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"package":{"files":[{"body":"package main\n\t\tfunc Echo() string {return \"hello world\"}",` +
`"name":"main.gno"}],"name":"main","path":"gno.land/r/g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt/run"},` +
`"send":"1000ugnot"}`,
expectErr: nil,
},
{
name: "invalid caller address",
msg: MsgRun{
Caller: crypto.Address{},
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "invalid package path",
msg: MsgRun{
Caller: caller,
Package: &std.MemPackage{
Name: pkgName,
Path: "gno.land/r/namespace/test", // this is not a valid run path
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}
Loading