Skip to content

Commit

Permalink
Add error test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DeividasK committed Feb 19, 2024
1 parent da02459 commit 3e2ddbc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
27 changes: 18 additions & 9 deletions core/chains/evm/abi/selector_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ package abi
import (
"errors"
"fmt"
"regexp"

"github.com/ethereum/go-ethereum/accounts/abi"
)

var alphaRegex = regexp.MustCompile(`[a-zA-Z]`)

func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}
Expand All @@ -42,11 +45,13 @@ func parseToken(unescapedSelector string, isIdent bool) (string, string, error)
if len(unescapedSelector) == 0 {
return "", "", errors.New("empty token")
}

firstChar := unescapedSelector[0]
position := 1
if !(isAlpha(firstChar) || (isIdent && isIdentifierSymbol(firstChar))) {
return "", "", fmt.Errorf("invalid token start: %c", firstChar)
if !(alphaRegex.MatchString(string(firstChar)) || (isIdent && isIdentifierSymbol(firstChar))) {
return "", "", fmt.Errorf("invalid token start. Expected: %s, received: %c", alphaRegex, firstChar)
}

position := 1
for position < len(unescapedSelector) {
char := unescapedSelector[position]
if !(isAlpha(char) || isDigit(char) || (isIdent && isIdentifierSymbol(char))) {
Expand Down Expand Up @@ -235,14 +240,18 @@ func ParseSelector(unescapedSelector string) (abi.SelectorMarshaling, error) {
func ParseSignature(unescapedSelector string) (abi.SelectorMarshaling, error) {
name, rest, err := parseIdentifier(unescapedSelector)
if err != nil {
return abi.SelectorMarshaling{}, fmt.Errorf("failed to parse selector '%s': %v", unescapedSelector, err)
return abi.SelectorMarshaling{}, fmt.Errorf("failed to parse selector identifier '%s': %v", unescapedSelector, err)
}
args := []abi.ArgumentMarshaling{}
if len(rest) < 2 || rest[0] != '(' || rest[1] != ')' {
args, err = parseArgs(rest)
if err != nil {
return abi.SelectorMarshaling{}, fmt.Errorf("failed to parse selector '%s': %v", unescapedSelector, err)
}

// Function has no arguments
if rest == "()" {
return abi.SelectorMarshaling{Name: name, Type: "function", Inputs: args}, nil
}

args, err = parseArgs(rest)
if err != nil {
return abi.SelectorMarshaling{}, fmt.Errorf("failed to parse selector args '%s': %v", unescapedSelector, err)
}

return abi.SelectorMarshaling{Name: name, Type: "function", Inputs: args}, nil
Expand Down
34 changes: 34 additions & 0 deletions core/chains/evm/abi/selector_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseSelector(t *testing.T) {
Expand Down Expand Up @@ -124,3 +126,35 @@ func TestParseSignature(t *testing.T) {
}
}
}

func TestParseSignatureErrors(t *testing.T) {
type errorTestCases struct {
description string
input string
expectedError string
}

for _, scenario := range []errorTestCases{
{
description: "invalid name",
input: "123()",
expectedError: "failed to parse selector identifier '123()': invalid token start. Expected: [a-zA-Z], received: 1",
},
{
description: "missing closing parenthesis",
input: "noargs(",
expectedError: "failed to parse selector args 'noargs(': expected ')', got ''",
},
{
description: "missing opening parenthesis",
input: "noargs)",
expectedError: "failed to parse selector args 'noargs)': expected '(', got )",
},
} {
t.Run(scenario.description, func(t *testing.T) {
_, err := ParseSignature(scenario.input)
require.Error(t, err)
assert.Equal(t, scenario.expectedError, err.Error())
})
}
}

0 comments on commit 3e2ddbc

Please sign in to comment.