Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Sep 12, 2023
1 parent da737db commit 116540f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
15 changes: 6 additions & 9 deletions x/wasm/types/authz.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"bytes"
"strings"

wasmvm "github.com/CosmWasm/wasmvm"
Expand Down Expand Up @@ -56,12 +55,12 @@ func (a *StoreCodeAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authztype
if ioutils.IsGzip(code) {
gasRegister, ok := GasRegister(ctx)
if !ok {
panic("gas register not found") // TODO: check how to handle this
return authztypes.AcceptResponse{}, sdkerrors.ErrNotFound.Wrap("gas register")

Check warning on line 58 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}
ctx.GasMeter().ConsumeGas(gasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
wasmCode, err := ioutils.Uncompress(code, int64(MaxWasmSize))
if err != nil {
return authztypes.AcceptResponse{}, sdkerrors.ErrInvalidRequest.Wrap(errorsmod.Wrap(err, "uncompress wasm archive").Error())
return authztypes.AcceptResponse{}, sdkerrors.ErrInvalidRequest.Wrap("uncompress wasm archive")

Check warning on line 63 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L60-L63

Added lines #L60 - L63 were not covered by tests
}
code = wasmCode

Check warning on line 65 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L65

Added line #L65 was not covered by tests
}
Expand Down Expand Up @@ -120,17 +119,15 @@ func (g CodeGrant) ValidateBasic() error {
if len(g.CodeHash) == 0 {
return ErrEmpty.Wrap("code hash")
}
if g.InstantiatePermission != nil {
if err := g.InstantiatePermission.ValidateBasic(); err != nil {
return errorsmod.Wrap(err, "instantiate permission")
}
if g.InstantiatePermission == nil {
return ErrEmpty.Wrap("permission")

Check warning on line 123 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L123

Added line #L123 was not covered by tests
}
return nil
return g.InstantiatePermission.ValidateBasic()
}

// Accept checks if checksum and permission match the grant
func (g CodeGrant) Accept(checksum []byte, permission AccessConfig) bool {
if !bytes.Equal(g.CodeHash, []byte(CodehashWildcard)) && !bytes.Equal(g.CodeHash, checksum) {
if !strings.EqualFold(string(g.CodeHash), CodehashWildcard) && !strings.EqualFold(string(g.CodeHash), string(checksum)) {
return false
}
return permission.IsSubset(*g.InstantiatePermission)
Expand Down
27 changes: 26 additions & 1 deletion x/wasm/types/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"math"
"strings"
"testing"

wasmvm "github.com/CosmWasm/wasmvm"
Expand Down Expand Up @@ -778,6 +779,8 @@ func TestValidateCodeGrant(t *testing.T) {
func TestValidateStoreCodeAuthorization(t *testing.T) {
validGrant, err := NewCodeGrant([]byte("any_valid_checksum"), AllowEverybody)
require.NoError(t, err)
validGrantUpperCase, err := NewCodeGrant([]byte("ANY_VALID_CHECKSUM"), AllowEverybody)
require.NoError(t, err)
invalidGrant, err := NewCodeGrant(nil, AllowEverybody)
require.NoError(t, err)
wildcardGrant, err := NewCodeGrant([]byte("*"), AllowEverybody)
Expand All @@ -803,12 +806,18 @@ func TestValidateStoreCodeAuthorization(t *testing.T) {
},
expErr: true,
},
"duplicate grants": {
"duplicate grants - same case code hash": {
setup: func(t *testing.T) []CodeGrant {
return []CodeGrant{*validGrant, *validGrant}
},
expErr: true,
},
"duplicate grants - different case code hash": {
setup: func(t *testing.T) []CodeGrant {
return []CodeGrant{*validGrant, *validGrantUpperCase}
},
expErr: true,
},
"invalid grant": {
setup: func(t *testing.T) []CodeGrant {
return []CodeGrant{*validGrant, *invalidGrant}
Expand Down Expand Up @@ -838,12 +847,17 @@ func TestStoreCodeAuthorizationAccept(t *testing.T) {
reflectCodeHash, err := wasmvm.CreateChecksum(reflectWasmCode)
require.NoError(t, err)

reflectCodeHashUpperCase := strings.ToUpper(string(reflectCodeHash))

grantWildcard, err := NewCodeGrant([]byte("*"), AllowEverybody)
require.NoError(t, err)

grantReflectCode, err := NewCodeGrant(reflectCodeHash, AllowNobody)
require.NoError(t, err)

grantReflectCodeUpperCase, err := NewCodeGrant([]byte(reflectCodeHashUpperCase), AllowNobody)
require.NoError(t, err)

grantOtherCode, err := NewCodeGrant([]byte("any_valid_checksum"), AllowEverybody)
require.NoError(t, err)

Expand Down Expand Up @@ -875,6 +889,17 @@ func TestStoreCodeAuthorizationAccept(t *testing.T) {
Accept: true,
},
},
"accepted reflect code - different case": {
auth: NewStoreCodeAuthorization(*grantReflectCodeUpperCase),
msg: &MsgStoreCode{
Sender: sdk.AccAddress(randBytes(SDKAddrLen)).String(),
WASMByteCode: reflectWasmCode,
InstantiatePermission: &AllowNobody,
},
expResult: authztypes.AcceptResponse{
Accept: true,
},
},
"not accepted - no matching code": {
auth: NewStoreCodeAuthorization(*grantOtherCode),
msg: &MsgStoreCode{
Expand Down

0 comments on commit 116540f

Please sign in to comment.