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

Add and test JSON object matcher #819

Merged
merged 2 commits into from
Apr 27, 2022
Merged
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
12 changes: 12 additions & 0 deletions x/wasm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ var (

// error if an address does not belong to a contract (just for registration)
_ = sdkErrors.Register(DefaultCodespace, 22, "no such contract")

// ErrNotAJSONObject error if given data is not a JSON object
ErrNotAJSONObject = sdkErrors.Register(DefaultCodespace, 23, "not a JSON object")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff with the unique error codes


// ErrNoTopLevelKey error if a JSON object has no top-level key
ErrNoTopLevelKey = sdkErrors.Register(DefaultCodespace, 24, "no top-level key")

// ErrMultipleTopLevelKeys error if a JSON object has more than one top-level key
ErrMultipleTopLevelKeys = sdkErrors.Register(DefaultCodespace, 25, "multiple top-level keys")

// ErrTopKevelKeyNotAllowed error if a JSON object has a top-level key that is not allowed
ErrTopKevelKeyNotAllowed = sdkErrors.Register(DefaultCodespace, 26, "top-level key is not allowed")
)

type ErrNoSuchContract struct {
Expand Down
36 changes: 36 additions & 0 deletions x/wasm/types/json_matching.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package types

import (
"encoding/json"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// IsJSONObjectWithTopLevelKey checks if the given bytes are a valid JSON object
// with exactly one top-level key that is contained in the list of allowed keys.
func IsJSONObjectWithTopLevelKey(jsonBytes []byte, allowedKeys []string) error {
document := map[string]interface{}{}
if err := json.Unmarshal(jsonBytes, &document); err != nil {
return sdkerrors.Wrap(ErrNotAJSONObject, "failed to unmarshal JSON to map")
}

if len(document) == 0 {
return sdkerrors.Wrap(ErrNoTopLevelKey, "JSON object has no top-level key")
}

if len(document) > 1 {
return sdkerrors.Wrap(ErrMultipleTopLevelKeys, "JSON object has multiple top-level keys")
}

// Loop is executed exactly once
for topLevelKey := range document {
for _, allowedKey := range allowedKeys {
if allowedKey == topLevelKey {
return nil
}
}
return sdkerrors.Wrapf(ErrTopKevelKeyNotAllowed, "JSON object has a top-level key which is not allowed: '%s'", topLevelKey)
}

panic("Reached unreachable code. This is a bug.")
}
115 changes: 115 additions & 0 deletions x/wasm/types/json_matching_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package types

import (
"testing"

// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
)

func TestIsJSONObjectWithTopLevelKey(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful test vectors here!

specs := map[string]struct {
src []byte
allowedKeys []string
exp error
}{
"happy": {
src: []byte(`{"msg": {"foo":"bar"}}`),
allowedKeys: []string{"msg"},
exp: nil,
},
"happy with many allowed keys 1": {
src: []byte(`{"claim": {"foo":"bar"}}`),
allowedKeys: []string{"claim", "swap", "burn", "mint"},
exp: nil,
},
"happy with many allowed keys 2": {
src: []byte(`{"burn": {"foo":"bar"}}`),
allowedKeys: []string{"claim", "swap", "burn", "mint"},
exp: nil,
},
"happy with many allowed keys 3": {
src: []byte(`{"mint": {"foo":"bar"}}`),
allowedKeys: []string{"claim", "swap", "burn", "mint"},
exp: nil,
},
"happy with number": {
src: []byte(`{"msg": 123}`),
allowedKeys: []string{"msg"},
exp: nil,
},
"happy with array": {
src: []byte(`{"msg": [1, 2, 3, 4]}`),
allowedKeys: []string{"msg"},
exp: nil,
},
"happy with null": {
src: []byte(`{"msg": null}`),
allowedKeys: []string{"msg"},
exp: nil,
},
"happy with whitespace": {
src: []byte(`{
"msg": null }`),
allowedKeys: []string{"msg"},
exp: nil,
},
"happy with excaped key": {
src: []byte(`{"event\u2468thing": {"foo":"bar"}}`),
allowedKeys: []string{"event⑨thing"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

exp: nil,
},

// Invalid JSON object
"errors for bytes that are no JSON": {
src: []byte(`nope`),
allowedKeys: []string{"claim"},
exp: ErrNotAJSONObject,
},
"errors for valid JSON (string)": {
src: []byte(`"nope"`),
allowedKeys: []string{"claim"},
exp: ErrNotAJSONObject,
},
"errors for valid JSON (array)": {
src: []byte(`[1, 2, 3]`),
allowedKeys: []string{"claim"},
exp: ErrNotAJSONObject,
},

// Not one top-level key
"errors for no top-level key": {
src: []byte(`{}`),
allowedKeys: []string{"claim"},
exp: ErrNoTopLevelKey,
},
"errors for multiple top-level keys": {
src: []byte(`{"claim": {}, "and_swap": {}}`),
allowedKeys: []string{"claim"},
exp: ErrMultipleTopLevelKeys,
},

// Wrong top-level key
"errors for wrong top-level key 1": {
src: []byte(`{"claim": {}}`),
allowedKeys: []string{""},
exp: ErrTopKevelKeyNotAllowed,
},
"errors for wrong top-level key 2": {
src: []byte(`{"claim": {}}`),
allowedKeys: []string{"swap", "burn", "mint"},
exp: ErrTopKevelKeyNotAllowed,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
result := IsJSONObjectWithTopLevelKey(spec.src, spec.allowedKeys)
if spec.exp == nil {
require.NoError(t, result)
} else {
require.Error(t, result)
require.Contains(t, result.Error(), spec.exp.Error())
}
})
}
}