wormhole/sdk/vaa/payloads_test.go

111 lines
4.7 KiB
Go
Raw Normal View History

2022-03-05 18:10:37 -08:00
package vaa
import (
"encoding/hex"
"testing"
2022-03-05 18:10:37 -08:00
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
func TestCoreModule(t *testing.T) {
hexifiedCoreModule := "00000000000000000000000000000000000000000000000000000000436f7265"
assert.Equal(t, hex.EncodeToString(CoreModule), hexifiedCoreModule)
}
func TestBodyContractUpgrade(t *testing.T) {
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
test := BodyContractUpgrade{ChainID: 1, NewContract: addr}
assert.Equal(t, test.ChainID, ChainID(1))
assert.Equal(t, test.NewContract, addr)
}
func TestBodyGuardianSetUpdate(t *testing.T) {
keys := []common.Address{
common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
}
test := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
assert.Equal(t, test.Keys, keys)
assert.Equal(t, test.NewIndex, uint32(1))
}
func TestBodyTokenBridgeRegisterChain(t *testing.T) {
module := "test"
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
test := BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr}
assert.Equal(t, test.Module, module)
assert.Equal(t, test.ChainID, ChainID(1))
assert.Equal(t, test.EmitterAddress, addr)
}
func TestBodyTokenBridgeUpgradeContract(t *testing.T) {
module := "test"
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
test := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
assert.Equal(t, test.Module, module)
assert.Equal(t, test.TargetChainID, ChainID(1))
assert.Equal(t, test.NewContract, addr)
}
func TestBodyContractUpgradeSerialize(t *testing.T) {
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
bodyContractUpgrade := BodyContractUpgrade{ChainID: 1, NewContract: addr}
expected := "00000000000000000000000000000000000000000000000000000000436f72650100010000000000000000000000000000000000000000000000000000000000000004"
serializedBodyContractUpgrade := bodyContractUpgrade.Serialize()
assert.Equal(t, expected, hex.EncodeToString(serializedBodyContractUpgrade))
2022-03-05 18:10:37 -08:00
}
func TestBodyGuardianSetUpdateSerialize(t *testing.T) {
keys := []common.Address{
common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
}
bodyGuardianSetUpdate := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
expected := "00000000000000000000000000000000000000000000000000000000436f726502000000000001025aaeb6053f3e94c9b9a09f33669435e7ef1beaed5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"
serializedBodyGuardianSetUpdate := bodyGuardianSetUpdate.Serialize()
assert.Equal(t, expected, hex.EncodeToString(serializedBodyGuardianSetUpdate))
2022-03-05 18:10:37 -08:00
}
func TestBodyTokenBridgeRegisterChainSerialize(t *testing.T) {
module := "test"
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
tests := []struct {
name string
expected string
object BodyTokenBridgeRegisterChain
panic bool
}{
{
name: "working_as_expected",
panic: false,
object: BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr},
expected: "000000000000000000000000000000000000000000000000000000007465737401000000010000000000000000000000000000000000000000000000000000000000000004",
},
{
name: "panic_at_the_disco!",
panic: true,
object: BodyTokenBridgeRegisterChain{Module: "123456789012345678901234567890123", ChainID: 1, EmitterAddress: addr},
expected: "module longer than 32 byte",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
if testCase.panic {
assert.PanicsWithValue(t, testCase.expected, func() { testCase.object.Serialize() })
} else {
assert.Equal(t, testCase.expected, hex.EncodeToString(testCase.object.Serialize()))
}
})
}
2022-03-05 18:10:37 -08:00
}
func TestBodyTokenBridgeUpgradeContractSerialize(t *testing.T) {
module := "test"
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
bodyTokenBridgeUpgradeContract := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
expected := "00000000000000000000000000000000000000000000000000000000746573740200010000000000000000000000000000000000000000000000000000000000000004"
serializedBodyTokenBridgeUpgradeContract := bodyTokenBridgeUpgradeContract.Serialize()
assert.Equal(t, expected, hex.EncodeToString(serializedBodyTokenBridgeUpgradeContract))
2022-03-05 18:10:37 -08:00
}