-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(gno.land/sdk/vm): add unit tests for
Msg*.ValidateBasic
(#2855)
The `vm Msg` is not yet covered by unit tests <!-- please provide a detailed description of the changes made in this pull request. --> <details><summary>Contributors' checklist...</summary> - [ ] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Morgan <[email protected]>
- Loading branch information
1 parent
7f39d04
commit ee2b1fa
Showing
1 changed file
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
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_ValidateBasic(t *testing.T) { | ||
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: NewMsgAddPackage(creator, pkgPath, files), | ||
expectSignBytes: `{"creator":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt","deposit":"",` + | ||
`"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_ValidateBasic(t *testing.T) { | ||
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: NewMsgCall(caller, std.NewCoins(std.NewCoin("ugnot", 1000)), pkgPath, funcName, args), | ||
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_ValidateBasic(t *testing.T) { | ||
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: NewMsgRun(caller, std.NewCoins(std.NewCoin("ugnot", 1000)), pkgFiles), | ||
expectSignBytes: `{"caller":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` + | ||
`"package":{"files":[{"body":"package main\n\t\tfunc Echo() string {return \"hello world\"}",` + | ||
`"name":"main.gno"}],"name":"main","path":""},` + | ||
`"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())) | ||
} | ||
}) | ||
} | ||
} |