Skip to content

Commit

Permalink
fix(wire): move wire_test to package wire to prevent init dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Minh Huy Tran <huy@perun.network>
  • Loading branch information
NhoxxKienn committed Jul 2, 2024
1 parent 03ce1db commit 3f6ca74
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
6 changes: 3 additions & 3 deletions wire/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewAddress() *Address {
}

// Equal returns whether the two addresses are equal.
func (a Address) Equal(b wire.Address) bool {
func (a *Address) Equal(b wire.Address) bool {
bTyped, ok := b.(*Address)
if !ok {
panic("wrong type")
Expand All @@ -47,7 +47,7 @@ func (a Address) Equal(b wire.Address) bool {

// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
// returns -1 if a < b, 0 if a == b, 1 if a > b.
func (a Address) Cmp(b wire.Address) int {
func (a *Address) Cmp(b wire.Address) int {
bTyped, ok := b.(*Address)
if !ok {
panic("wrong type")
Expand All @@ -63,7 +63,7 @@ func NewRandomAddress(rng *rand.Rand) *Address {

// Verify verifies a message signature.
// It returns an error if the signature is invalid.
func (a Address) Verify(msg []byte, sig []byte) error {
func (a *Address) Verify(msg []byte, sig []byte) error {
hash := PrefixedHash(msg)
sigCopy := make([]byte, SigLen)
copy(sigCopy, sig)
Expand Down
37 changes: 0 additions & 37 deletions wire/init_test.go

This file was deleted.

20 changes: 10 additions & 10 deletions wire/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"math/rand"
"testing"

"github.com/perun-network/perun-eth-backend/wire"
ethwire "github.com/perun-network/perun-eth-backend/wire"
"github.com/stretchr/testify/assert"
perunwire "perun.network/go-perun/wire"
"perun.network/go-perun/wire/test"
Expand All @@ -29,24 +29,24 @@ var dataToSign = []byte("SomeLongDataThatShouldBeSignedPlease")

func TestAddress(t *testing.T) {
test.TestAddressImplementation(t, func() perunwire.Address {
return wire.NewAddress()
return ethwire.NewAddress()
}, func(rng *rand.Rand) perunwire.Address {
return wire.NewRandomAddress(rng)
return ethwire.NewRandomAddress(rng)
})
}

func TestSignatures_Success(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)
assert.Equal(t, len(sig), wire.SigLen, "Ethereum signature has wrong length")
assert.Equal(t, len(sig), ethwire.SigLen, "Ethereum signature has wrong length")
err = acc.Address().Verify(dataToSign, sig)
assert.NoError(t, err, "Verification should succeed")
}

func TestSignatures_ModifyData_Failure(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)
Expand All @@ -61,7 +61,7 @@ func TestSignatures_ModifyData_Failure(t *testing.T) {
}

func TestSignatures_ModifySignature_Failure(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)
Expand All @@ -76,7 +76,7 @@ func TestSignatures_ModifySignature_Failure(t *testing.T) {
}

func TestSignatures_ModifyLastByteOfSignature_Failure(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)
Expand All @@ -91,13 +91,13 @@ func TestSignatures_ModifyLastByteOfSignature_Failure(t *testing.T) {
}

func TestSignatures_WrongAccount_Failure(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)

// Verify with a wrong account
wrongAcc := wire.NewRandomAccount(pkgtest.Prng(t))
wrongAcc := ethwire.NewRandomAccount(pkgtest.Prng(t))
err = wrongAcc.Address().Verify(dataToSign, sig)
assert.Error(t, err, "Verification should fail with wrong account")
}

0 comments on commit 3f6ca74

Please sign in to comment.