Skip to content

Commit

Permalink
validate script argument count
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 25, 2020
1 parent 51a4186 commit 82669f5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
29 changes: 19 additions & 10 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (r *interpreterRuntime) ExecuteScript(
checker,
functions,
nil,
scriptExecutionFunction(epSignature.Parameters, len(arguments), arguments, runtimeInterface),
scriptExecutionFunction(epSignature.Parameters, arguments, runtimeInterface),
)
if err != nil {
return nil, newError(err)
Expand All @@ -196,12 +196,11 @@ func (r *interpreterRuntime) ExecuteScript(
return exportValue(value), nil
}

func scriptExecutionFunction(parameters []*sema.Parameter, argumentCount int, arguments [][]byte, runtimeInterface Interface) func(inter *interpreter.Interpreter) (interpreter.Value, error) {
func scriptExecutionFunction(parameters []*sema.Parameter, arguments [][]byte, runtimeInterface Interface) func(inter *interpreter.Interpreter) (interpreter.Value, error) {
return func(inter *interpreter.Interpreter) (interpreter.Value, error) {
values, err := validateArgumentParams(
inter,
runtimeInterface,
argumentCount,
arguments,
parameters)
if err != nil {
Expand Down Expand Up @@ -370,7 +369,6 @@ func (r *interpreterRuntime) ExecuteTransaction(
nil,
r.transactionExecutionFunction(
transactionType.Parameters,
argumentCount,
arguments,
runtimeInterface,
authorizerValues,
Expand Down Expand Up @@ -406,7 +404,6 @@ func wrapPanic(f func()) {

func (r *interpreterRuntime) transactionExecutionFunction(
parameters []*sema.Parameter,
argumentCount int,
arguments [][]byte,
runtimeInterface Interface,
authorizerValues []interpreter.Value,
Expand All @@ -415,9 +412,9 @@ func (r *interpreterRuntime) transactionExecutionFunction(
values, err := validateArgumentParams(
inter,
runtimeInterface,
argumentCount,
arguments,
parameters)
parameters,
)
if err != nil {
return nil, err
}
Expand All @@ -430,11 +427,23 @@ func (r *interpreterRuntime) transactionExecutionFunction(
func validateArgumentParams(
inter *interpreter.Interpreter,
runtimeInterface Interface,
argumentCount int,
arguments [][]byte,
parameters []*sema.Parameter) ([]interpreter.Value, error) {
parameters []*sema.Parameter,
) (
[]interpreter.Value,
error,
) {
argumentCount := len(arguments)
parameterCount := len(parameters)

if argumentCount != parameterCount {
return nil, InvalidEntryPointParameterCountError{
Expected: parameterCount,
Actual: argumentCount,
}
}

argumentValues := make([]interpreter.Value, argumentCount)
argumentValues := make([]interpreter.Value, len(arguments))

// Decode arguments against parameter types
for i, parameter := range parameters {
Expand Down
65 changes: 65 additions & 0 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4698,3 +4698,68 @@ func TestRuntimeTransaction_UpdateAccountCodeUnsafeNotInitializing(t *testing.T)
_, err = runtime.ExecuteScript(script2, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)
}

func TestRuntime(t *testing.T) {

t.Parallel()

runtime := NewInterpreterRuntime()

runtimeInterface := &testRuntimeInterface{
decodeArgument: func(b []byte, t cadence.Type) (cadence.Value, error) {
return jsoncdc.Decode(b)
},
}

script := []byte(`
pub fun main(num: Int) {}
`)

type testCase struct {
name string
arguments [][]byte
valid bool
}

test := func(tc testCase) {
t.Run(tc.name, func(t *testing.T) {

t.Parallel()

_, err := runtime.ExecuteScript(script, tc.arguments, runtimeInterface, ScriptLocation{0x1})

if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
require.IsType(t, Error{}, err)
assert.IsType(t, InvalidEntryPointParameterCountError{}, err.(Error).Unwrap())
}
})
}

for _, testCase := range []testCase{
{
name: "too few arguments",
arguments: [][]byte{},
valid: false,
},
{
name: "correct number of arguments",
arguments: [][]byte{
jsoncdc.MustEncode(cadence.NewInt(1)),
},
valid: true,
},
{
name: "too many arguments",
arguments: [][]byte{
jsoncdc.MustEncode(cadence.NewInt(1)),
jsoncdc.MustEncode(cadence.NewInt(2)),
},
valid: false,
},
} {
test(testCase)
}
}

0 comments on commit 82669f5

Please sign in to comment.