Skip to content

Commit

Permalink
test: add more tests to cover the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed May 27, 2023
1 parent 6abb071 commit 4a0de7e
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 115 deletions.
19 changes: 2 additions & 17 deletions cmd/gtk/dialog_transaction_bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,10 @@ To: <b>%v</b>
Amount: <b>%v</b>
Fee: <b>%v</b>
THIS ACTION IS NOT REVERSIBLE Do you want to continue?`, sender, receiver,
THIS ACTION IS NOT REVERSIBLE. Do you want to continue?`, sender, receiver,
util.ChangeToString(amount), util.ChangeToString(trx.Fee()))

if showQuestionDialog(dlg, msg) {
password, ok := getWalletPassword(wallet)
if !ok {
return
}
err := wallet.SignTransaction(password, trx)
if err != nil {
errorCheck(err)
return
}
_, err = wallet.BroadcastTransaction(trx)
if err != nil {
errorCheck(err)
return
}
}
signAndBroadcastTransaction(dlg, msg, wallet, trx)

dlg.Close()
}
Expand Down
19 changes: 2 additions & 17 deletions cmd/gtk/dialog_transaction_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,10 @@ To: <b>%v</b>
Amount: <b>%v</b>
Fee: <b>%v</b>
THIS ACTION IS NOT REVERSIBLE Do you want to continue?`, sender, receiver,
THIS ACTION IS NOT REVERSIBLE. Do you want to continue?`, sender, receiver,
util.ChangeToString(amount), util.ChangeToString(trx.Fee()))

if showQuestionDialog(dlg, msg) {
password, ok := getWalletPassword(wallet)
if !ok {
return
}
err := wallet.SignTransaction(password, trx)
if err != nil {
errorCheck(err)
return
}
_, err = wallet.BroadcastTransaction(trx)
if err != nil {
errorCheck(err)
return
}
}
signAndBroadcastTransaction(dlg, msg, wallet, trx)

dlg.Close()
}
Expand Down
26 changes: 26 additions & 0 deletions cmd/gtk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/wallet"
)
Expand Down Expand Up @@ -178,3 +179,28 @@ func updateHintLabel(lbl *gtk.Label, hint string) {
lbl.SetMarkup(
fmt.Sprintf("<span foreground='gray' size='small'>%s</span>", hint))
}

func signAndBroadcastTransaction(parent *gtk.Dialog, msg string, w *wallet.Wallet, trx *tx.Tx) {
if showQuestionDialog(parent, msg) {
password, ok := getWalletPassword(w)
if !ok {
return
}
err := w.SignTransaction(password, trx)
if err != nil {
errorCheck(err)
return
}
_, err = w.BroadcastTransaction(trx)
if err != nil {
errorCheck(err)
return
}

err = w.Save()
if err != nil {
errorCheck(err)
return
}
}
}
10 changes: 10 additions & 0 deletions crypto/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAddressKeyEqualsTo(t *testing.T) {
addr1 := GenerateTestAddress()
addr2 := GenerateTestAddress()

assert.True(t, addr1.EqualsTo(addr1))
assert.False(t, addr1.EqualsTo(addr2))
assert.Equal(t, addr1, addr1)
assert.NotEqual(t, addr1, addr2)
}

func TestFingerprint(t *testing.T) {
addr1 := GenerateTestAddress()
assert.Contains(t, addr1.String(), addr1.Fingerprint())
Expand Down
10 changes: 10 additions & 0 deletions crypto/bls/private_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPrivateKeyEqualsTo(t *testing.T) {
_, prv1 := GenerateTestKeyPair()
_, prv2 := GenerateTestKeyPair()

assert.True(t, prv1.EqualsTo(prv1))
assert.False(t, prv1.EqualsTo(prv2))
assert.Equal(t, prv1, prv1)
assert.NotEqual(t, prv1, prv2)
}

func TestPrivateKeyToString(t *testing.T) {
tests := []struct {
errMsg string
Expand Down
10 changes: 10 additions & 0 deletions crypto/bls/public_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func TestPublicKeyCBORMarshaling(t *testing.T) {
assert.Error(t, pub2.UnmarshalCBOR(data))
}

func TestPublicKeyEqualsTo(t *testing.T) {
pub1, _ := GenerateTestKeyPair()
pub2, _ := GenerateTestKeyPair()

assert.True(t, pub1.EqualsTo(pub1))
assert.False(t, pub1.EqualsTo(pub2))
assert.Equal(t, pub1, pub1)
assert.NotEqual(t, pub1, pub2)
}

func TestPublicKeyEncoding(t *testing.T) {
pub, _ := GenerateTestKeyPair()
w1 := util.NewFixedWriter(20)
Expand Down
11 changes: 11 additions & 0 deletions crypto/bls/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ func TestSignatureCBORMarshaling(t *testing.T) {
assert.Error(t, sig2.UnmarshalCBOR(data))
}

func TestSignatureEqualsTo(t *testing.T) {
signer := GenerateTestSigner()
sig1 := signer.SignData([]byte("foo"))
sig2 := signer.SignData([]byte("bar"))

assert.True(t, sig1.EqualsTo(sig1))
assert.False(t, sig1.EqualsTo(sig2))
assert.Equal(t, sig1, sig1)
assert.NotEqual(t, sig1, sig2)
}

func TestSignatureEncoding(t *testing.T) {
_, prv := GenerateTestKeyPair()
sig := prv.Sign(util.Uint64ToSlice(util.RandUint64(0)))
Expand Down
2 changes: 1 addition & 1 deletion util/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTempFile(t *testing.T) {
tmpFile := TempFilePath()

assert.True(t, IsAbsPath(tmpFile))
t.Run("Should panic because it doesn't exists", func(t *testing.T) {
t.Run("Should panic because it doesn't exist", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
Expand Down
6 changes: 2 additions & 4 deletions wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ type blockchainServer struct{}
type transactionServer struct{}

var tBlockchainInfoResponse *pactus.GetBlockchainInfoResponse
var tAccountRequest *pactus.GetAccountRequest
var tAccountResponse *pactus.GetAccountResponse
var tValidatorRequest *pactus.GetValidatorRequest
var tValidatorResponse *pactus.GetValidatorResponse

func (s *blockchainServer) GetBlockchainInfo(_ context.Context,
Expand Down Expand Up @@ -44,7 +42,7 @@ func (s *blockchainServer) GetBlock(_ context.Context,

func (s *blockchainServer) GetAccount(_ context.Context,
req *pactus.GetAccountRequest) (*pactus.GetAccountResponse, error) {

Check failure on line 44 in wallet/client_test.go

View workflow job for this annotation

GitHub Actions / linting

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 44 in wallet/client_test.go

View workflow job for this annotation

GitHub Actions / build-linux

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
if req.Address == tAccountRequest.Address {
if tAccountResponse != nil {
return tAccountResponse, nil
}
return nil, fmt.Errorf("unknown request")
Expand All @@ -57,7 +55,7 @@ func (s *blockchainServer) GetValidatorByNumber(_ context.Context,

func (s *blockchainServer) GetValidator(_ context.Context,
req *pactus.GetValidatorRequest) (*pactus.GetValidatorResponse, error) {

Check failure on line 57 in wallet/client_test.go

View workflow job for this annotation

GitHub Actions / linting

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 57 in wallet/client_test.go

View workflow job for this annotation

GitHub Actions / build-linux

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
if req.Address == tValidatorRequest.Address {
if tValidatorResponse != nil {
return tValidatorResponse, nil
}
return nil, fmt.Errorf("unknown request")
Expand Down

0 comments on commit 4a0de7e

Please sign in to comment.