Skip to content

Commit

Permalink
Rewrote test using table-driven tests for improved organization and r…
Browse files Browse the repository at this point in the history
…eadability. Employed standard Golang test messages for enhanced clarity and consistency
  • Loading branch information
Aviksaikat committed Nov 15, 2023
1 parent 36f5f41 commit 4836c27
Showing 1 changed file with 52 additions and 82 deletions.
134 changes: 52 additions & 82 deletions pkg/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,87 +256,57 @@ func TestNewEthereumAddress(t *testing.T) {
func TestNewOverlayFromEthereumAddress(t *testing.T) {
t.Parallel()

// Test Case 1
ethAddress := "1815cac638d1525b47f848daf02b7953e4edd15c"
ethAddressBytes, err := hex.DecodeString(ethAddress)
if err != nil {
t.Fatal(err)
}

newAddress1, err := crypto.NewOverlayFromEthereumAddress(ethAddressBytes, 1, common.HexToHash("0x1").Bytes())
if err != nil {
t.Fatal(err)
}

if l := len(newAddress1.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32)
}

expectedAddress1 := "a38f7a814d4b249ae9d3821e9b898019c78ac9abe248fff171782c32a3849a17"

if newAddress1.String() != expectedAddress1 {
t.Errorf("Expected %s, but got %s", expectedAddress1, newAddress1.String())
}

// Test Case 2
newAddress2, err := crypto.NewOverlayFromEthereumAddress(ethAddressBytes, 1, common.HexToHash("0x2").Bytes())

if err != nil {
t.Fatal(err)
}

if l := len(newAddress2.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32)
}

expectedAddress2 := "c63c10b1728dfc463c64c264f71a621fe640196979375840be42dc496b702610"

if newAddress2.String() != expectedAddress2 {
t.Errorf("Expected %s, but got %s", expectedAddress2, newAddress2.String())
}

// Test Case 3
ethAddress2 := "d26bc1715e933bd5f8fad16310042f13abc16159"
ethAddressBytes2, err := hex.DecodeString(ethAddress2)
if err != nil {
t.Fatal(err)
}

newAddress3, err := crypto.NewOverlayFromEthereumAddress(ethAddressBytes2, 2, common.HexToHash("0x1").Bytes())
if err != nil {
t.Fatal(err)
}

if l := len(newAddress3.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32)
}

expectedAddress3 := "9f421f9149b8e31e238cfbdc6e5e833bacf1e42f77f60874d49291292858968e"

if newAddress3.String() != expectedAddress3 {
t.Errorf("Expected %s, but got %s", expectedAddress3, newAddress3.String())
}

// Test Case 4
ethAddress3 := "ac485e3c63dcf9b4cda9f007628bb0b6fed1c063"
ethAddressBytes4, err := hex.DecodeString(ethAddress3)
if err != nil {
t.Fatal(err)
// Define the test cases
testCases := []struct {
ethAddress string
networkID uint64
hash []byte
expected string
}{
{
ethAddress: "1815cac638d1525b47f848daf02b7953e4edd15c",
networkID: 1,
hash: common.HexToHash("0x1").Bytes(),
expected: "a38f7a814d4b249ae9d3821e9b898019c78ac9abe248fff171782c32a3849a17",
},
{
ethAddress: "1815cac638d1525b47f848daf02b7953e4edd15c",
networkID: 1,
hash: common.HexToHash("0x2").Bytes(),
expected: "c63c10b1728dfc463c64c264f71a621fe640196979375840be42dc496b702610",
},
{
ethAddress: "d26bc1715e933bd5f8fad16310042f13abc16159",
networkID: 2,
hash: common.HexToHash("0x1").Bytes(),
expected: "9f421f9149b8e31e238cfbdc6e5e833bacf1e42f77f60874d49291292858968e",
},
{
ethAddress: "ac485e3c63dcf9b4cda9f007628bb0b6fed1c063",
networkID: 1,
hash: common.HexToHash("0x0").Bytes(),
expected: "fe3a6d582c577404fb19df64a44e00d3a3b71230a8464c0dd34af3f0791b45f2",
},
}

// Run the test cases
for _, tc := range testCases {
ethAddressBytes, err := hex.DecodeString(tc.ethAddress)
if err != nil {
t.Fatal(err)
}

newAddress, err := crypto.NewOverlayFromEthereumAddress(ethAddressBytes, tc.networkID, tc.hash)
if err != nil {
t.Fatal(err)
}

if l := len(newAddress.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32)
}

if newAddress.String() != tc.expected {
t.Errorf("want: %s; got: %s", tc.expected, newAddress.String())
}
}

newAddress4, err := crypto.NewOverlayFromEthereumAddress(ethAddressBytes4, 1, common.HexToHash("0x0").Bytes())
if err != nil {
t.Fatal(err)
}

if l := len(newAddress4.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32)
}

expectedAddress4 := "fe3a6d582c577404fb19df64a44e00d3a3b71230a8464c0dd34af3f0791b45f2"

if newAddress4.String() != expectedAddress4 {
t.Errorf("Expected %s, but got %s", expectedAddress4, newAddress4.String())
}

}

0 comments on commit 4836c27

Please sign in to comment.