forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to jrpc to accept short hashes and addresses strings like…
… 0x00 (0xPolygonHermez#1802)
- Loading branch information
Showing
7 changed files
with
126 additions
and
37 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
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
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
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
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 |
---|---|---|
@@ -1,26 +1,109 @@ | ||
package jsonrpc | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestArgHashUnmarshalFromShortString(t *testing.T) { | ||
str := "0x01" | ||
arg := argHash{} | ||
err := arg.UnmarshalText([]byte(str)) | ||
require.NoError(t, err) | ||
type testCase struct { | ||
name string | ||
input string | ||
expectedResult string | ||
expectedError error | ||
} | ||
testCases := []testCase{ | ||
{ | ||
name: "valid hex value starting with 0x", | ||
input: "0x1", | ||
expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "valid hex value starting without 0x", | ||
input: "1", | ||
expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "valid full hash value", | ||
input: "0x05b21ee5f65c28a0af8e71290fc33625a1279a8b3d6357ce3ca60f22dbf59e63", | ||
expectedResult: "0x05b21ee5f65c28a0af8e71290fc33625a1279a8b3d6357ce3ca60f22dbf59e63", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "invalid hex value starting with 0x", | ||
input: "0xG", | ||
expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
expectedError: fmt.Errorf("invalid hash, it needs to be a hexadecimal value"), | ||
}, | ||
{ | ||
name: "invalid hex value starting without 0x", | ||
input: "G", | ||
expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
expectedError: fmt.Errorf("invalid hash, it needs to be a hexadecimal value"), | ||
}, | ||
} | ||
|
||
assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000001", arg.Hash().String()) | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
arg := argHash{} | ||
err := arg.UnmarshalText([]byte(testCase.input)) | ||
require.Equal(t, testCase.expectedError, err) | ||
assert.Equal(t, testCase.expectedResult, arg.Hash().String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestArgAddressUnmarshalFromShortString(t *testing.T) { | ||
str := "0x01" | ||
arg := argAddress{} | ||
err := arg.UnmarshalText([]byte(str)) | ||
require.NoError(t, err) | ||
type testCase struct { | ||
name string | ||
input string | ||
expectedResult string | ||
expectedError error | ||
} | ||
testCases := []testCase{ | ||
{ | ||
name: "valid hex value starting with 0x", | ||
input: "0x1", | ||
expectedResult: "0x0000000000000000000000000000000000000001", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "valid hex value starting without 0x", | ||
input: "1", | ||
expectedResult: "0x0000000000000000000000000000000000000001", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "valid full address value", | ||
input: "0x748964F22eFd023eB78A246A7AC2506e84CC4545", | ||
expectedResult: "0x748964F22eFd023eB78A246A7AC2506e84CC4545", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "invalid hex value starting with 0x", | ||
input: "0xG", | ||
expectedResult: "0x0000000000000000000000000000000000000000", | ||
expectedError: fmt.Errorf("invalid address, it needs to be a hexadecimal value"), | ||
}, | ||
{ | ||
name: "invalid hex value starting without 0x", | ||
input: "G", | ||
expectedResult: "0x0000000000000000000000000000000000000000", | ||
expectedError: fmt.Errorf("invalid address, it needs to be a hexadecimal value"), | ||
}, | ||
} | ||
|
||
assert.Equal(t, "0x0000000000000000000000000000000000000001", arg.Address().String()) | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
arg := argAddress{} | ||
err := arg.UnmarshalText([]byte(testCase.input)) | ||
require.Equal(t, testCase.expectedError, err) | ||
assert.Equal(t, testCase.expectedResult, arg.Address().String()) | ||
}) | ||
} | ||
} |
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
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