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

Ensuring HexAddress is Wrapped in String format and as HexAddress #278

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
testcases
Signed-off-by: Philip-21 <[email protected]>
Philip-21 committed Dec 23, 2023
commit 132346c8fcc5b7ac35f9c52e16d7843dbb5bdb6a
79 changes: 68 additions & 11 deletions pkg/types/hex_address_test.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,109 @@
package types

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConvertToHexAddress(t *testing.T) {
func TestWrapHexAddress(t *testing.T) {
tests := []struct {
Name string
Hexvalue string
Hexvalue string `yaml:"hexvalue"`
}{
{
Name: "Case 24-Bits",
Hexvalue: "0x123abc",
Hexvalue: "0x123abc0000000000000000000000000000000000",
},
{
Name: "Case 32-Bits",
Hexvalue: "0xABCDEFFEDCBA9876543210ABCDEFFEDC",
Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000",
},
{
Name: "Case 64-Bits",
Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF",
Hexvalue: "0x1234567890abcdef012345670000000000000000",
},
{
Name: "Case 128-Bits",
Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF",
Hexvalue: "0x1234567890abcdef0123456789abcdef00000000",
},
{
Name: "Case 152-Bits",
Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65",
},
{
Name: "Case 16-Bits",
Hexvalue: "0x1F2A",
Hexvalue: "0x1f2a000000000000000000000000000000000000",
},
{
Name: "Case 65-Bits",
Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF6789ABCDEF0123456789ABCDEF012345",
Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd",
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
result, err := ConvertToHexAddress(tc.Hexvalue)
var hexType HexAddress
hexBytes, err := hex.DecodeString(tc.Hexvalue[2:])
if err != nil {
t.Log("Unable to decode values:", err)
}
if len(hexBytes) != 20 {
t.Fatalf("expected 20 bytes, got %d bytes", len(hexBytes))
}
// Copy bytes to a fixed-size array
var hexArray [20]byte
copy(hexArray[:], hexBytes)
result, err := hexType.WrapHexAddress([20]byte(hexArray))
if err != nil {
t.Log("error in generating result", err)
t.Fail()
return
}
t.Log(result)

assert.Equal(t, tc.Hexvalue, result)
})

}
}

//Wrong values to test the accuracy of the WrapHexAddress()
func TestFailWrapHexAddress(t *testing.T) {
testData := []struct {
Name string
HexValue string `yaml:"hexvalue"`
}{
{
Name: "case-1",
HexValue: "px123abc0000000000000000000000000000000000",
},
{
Name: "case-2",
HexValue: "A0234567890abcdef0123456789abcdef00000000",
},
{
Name: "case-3",
HexValue: "06942dc1fC868aF18132C0916dA3ae4ab58142a4",
},
}
for _, tc := range testData {
t.Run(tc.Name, func(t *testing.T) {
var hexType HexAddress
hexBytes, err := hex.DecodeString(tc.HexValue[2:])
if err != nil {
t.Log("unable to decode hexvalue:", err)
}
if len(hexBytes) != 20 {
t.Fatalf("expected 20 bytes, but got %d bytes", len(hexBytes))
}
var hexArray [20]byte
copy(hexArray[:], hexBytes)
result, err := hexType.WrapHexAddress([20]byte(hexArray))
if err != nil {
t.Log("error in generating result", err)
t.Fail()
return
}
assert.Equal(t, tc.HexValue, result)
})
}
}