diff --git a/src/coin/skycoin/models/cipher_test.go b/src/coin/skycoin/models/cipher_test.go index 2f3a0773..de71861a 100644 --- a/src/coin/skycoin/models/cipher_test.go +++ b/src/coin/skycoin/models/cipher_test.go @@ -2,10 +2,13 @@ package skycoin import ( "encoding/hex" + "errors" "github.com/fibercrypto/fibercryptowallet/src/core" + "github.com/fibercrypto/fibercryptowallet/src/util" "github.com/skycoin/skycoin/src/cipher" "github.com/skycoin/skycoin/src/cipher/base58" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "reflect" "testing" ) @@ -78,7 +81,7 @@ func TestNewSkycoinAddress(t *testing.T) { return } cAddrs, err := cipher.AddressFromBytes(got.Bytes()) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, got.String(), cAddrs.String()) assert.False(t, got.IsBip32()) assert.False(t, got.Null()) @@ -122,7 +125,7 @@ func TestNewSkycoinAddressIterator(t *testing.T) { var args []core.Address for e := range tt.args.addresses { addrs, err := NewSkycoinAddress(tt.args.addresses[e]) - assert.NoError(t, err) + require.NoError(t, err) args = append(args, &addrs) } got = NewSkycoinAddressIterator(args) @@ -146,7 +149,7 @@ func TestSkycoinAddress_Bytes(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { addr, err := NewSkycoinAddress(tt.address) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, tt.address, addr.String()) }) } @@ -155,14 +158,14 @@ func TestSkycoinAddress_Bytes(t *testing.T) { func TestSkycoinAddress_Verify(t *testing.T) { addrsFromString := func(s string) core.Address { skyAddrs, err := NewSkycoinAddress(s) - assert.NoError(t, err) + require.NoError(t, err) return &skyAddrs } pubkeyFromString := func(s string) core.PubKey { b, err := hex.DecodeString(s) - assert.NoError(t, err) + require.NoError(t, err) spk, err := skyPubKeyFromBytes(b) - assert.NoError(t, err) + require.NoError(t, err) return spk } tests := []struct { @@ -188,10 +191,10 @@ func TestSkycoinAddress_Verify(t *testing.T) { addrs := addrsFromString(tt.addrsString) pk := pubkeyFromString(tt.pkHex) if tt.wantErr { - assert.Error(t, addrs.Verify(pk)) + require.Error(t, addrs.Verify(pk)) return } - assert.NoError(t, addrs.Verify(pk)) + require.NoError(t, addrs.Verify(pk)) }) } } @@ -199,7 +202,7 @@ func TestSkycoinAddress_Verify(t *testing.T) { func Test_skyPubKeyFromBytes(t *testing.T) { pubkeyFromHex := func(s string) []byte { b, err := hex.DecodeString(s) - assert.NoError(t, err) + require.NoError(t, err) return b } tests := []struct { @@ -215,18 +218,18 @@ func Test_skyPubKeyFromBytes(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := skyPubKeyFromBytes(pubkeyFromHex(tt.pubkHex)) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) return } - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, pubkeyFromHex(tt.pubkHex), got.Bytes()) assert.Equal(t, tt.pubkHex, got.pubkey.Hex()) - assert.NoError(t, got.Verify()) + require.NoError(t, got.Verify()) assert.False(t, got.Null()) spk, err := toSkycoinPubKey(got) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, spk.Hex(), tt.pubkHex) - assert.NoError(t, spk.Verify()) + require.NoError(t, spk.Verify()) assert.False(t, spk.Null()) }) } @@ -235,7 +238,7 @@ func Test_skyPubKeyFromBytes(t *testing.T) { func Test_skySecKeyFromBytes(t *testing.T) { pubkeyFromHex := func(s string) []byte { b, err := hex.DecodeString(s) - assert.NoError(t, err) + require.NoError(t, err) return b } tests := []struct { @@ -251,19 +254,64 @@ func Test_skySecKeyFromBytes(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := skySecKeyFromBytes(pubkeyFromHex(tt.pubkHex)) if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) return } - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, pubkeyFromHex(tt.pubkHex), got.Bytes()) assert.Equal(t, tt.pubkHex, got.seckey.Hex()) - assert.NoError(t, got.Verify()) + require.NoError(t, got.Verify()) assert.False(t, got.Null()) spk, err := toSkycoinSecKey(got) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, spk.Hex(), tt.pubkHex) - assert.NoError(t, spk.Verify()) + require.NoError(t, spk.Verify()) assert.False(t, spk.Null()) }) } } + +func TestAddressFromString(t *testing.T) { + type args struct { + addrs string + coinTicket string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + {name: "valid1", args: args{ + addrs: "2kvLEyXwAYvHfJuFCkjnYNRTUfHPyWgVwKt", + coinTicket: "SKY", + }, want: "2kvLEyXwAYvHfJuFCkjnYNRTUfHPyWgVwKt", wantErr: false}, + {name: "invalid_ticket", args: args{ + addrs: "2kvLEyXwAYvHfJuFCkjnYNRTUfHPyWgVwKt", + coinTicket: "", + }, want: "", wantErr: true}, + {name: "invalid_address", args: args{ + addrs: "2LEyXwAYvHfJuFCkjnYNRTUfHPyWgVwKt", + coinTicket: "SKY", + }, want: "", wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := util.AddressFromString(tt.args.addrs, tt.args.coinTicket) + if tt.wantErr { + require.Error(t, err) + switch tt.name { + case "invalid_ticket": + assert.Equal(t, err, errors.New("coinTicket not match")) + case "invalid_address": + assert.Equal(t, err, cipher.ErrAddressInvalidLength) + } + + } else { + assert.NotNil(t, got) + assert.Equal(t, tt.want, got.String()) + } + + }) + } +} diff --git a/src/coin/skycoin/models/wallet_test.go b/src/coin/skycoin/models/wallet_test.go index 385fb0a2..f3367b59 100644 --- a/src/coin/skycoin/models/wallet_test.go +++ b/src/coin/skycoin/models/wallet_test.go @@ -1227,7 +1227,7 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { startAddress2 := testutil.MakeAddress() destinationAddress := testutil.MakeAddress() - changeAddress := (testutil.MakeAddress()).String() + changeAddress := testutil.MakeAddress() sky := 500 hash := testutil.RandSHA256(t) @@ -1239,11 +1239,11 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { }, } fromAddr := []*SkycoinAddress{ - &SkycoinAddress{ - address: startAddress1.String(), + { + address: startAddress1, }, - &SkycoinAddress{ - address: startAddress2.String(), + { + address: startAddress2, }, } chgAddr := &SkycoinAddress{ @@ -1254,6 +1254,7 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { opt1.SetValue("BurnFactor", "0.5") opt1.SetValue("CoinHoursSelectionType", "auto") + changeAddrString := changeAddress.String() req1 := api.CreateTransactionRequest{ IgnoreUnconfirmed: false, HoursSelection: api.HoursSelection{ @@ -1261,9 +1262,9 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { Mode: "share", ShareFactor: "0.5", }, - ChangeAddress: &changeAddress, + ChangeAddress: &changeAddrString, To: []api.Receiver{ - api.Receiver{ + { Address: destinationAddress.String(), Coins: strconv.Itoa(sky), }, @@ -1280,9 +1281,9 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { HoursSelection: api.HoursSelection{ Type: "manual", }, - ChangeAddress: &changeAddress, + ChangeAddress: &changeAddrString, To: []api.Receiver{ - api.Receiver{ + { Address: destinationAddress.String(), Coins: strconv.Itoa(sky), Hours: "250", @@ -1306,7 +1307,7 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { bc := makeSkycoinBlockchain() wlt := &LocalWallet{} - //Testing Hours selection to auto + // Testing Hours selection to auto from := []core.WalletAddress{makeSimpleWalletAddress(wlt, fromAddr[0]), makeSimpleWalletAddress(wlt, fromAddr[1])} to := []core.TransactionOutput{toAddr} txnResult, err := bc.SendFromAddress(from, to, chgAddr, opt1) @@ -1317,7 +1318,7 @@ func TestSkycoinBlockchainSendFromAddress(t *testing.T) { require.Equal(t, util.FormatCoins(uint64(sky), 10), util.FormatCoins(uint64(val), 10)) require.Equal(t, ctxnR.Transaction.TxID, txnResult.GetId()) - //Testing Hours selection to manual + // Testing Hours selection to manual from = []core.WalletAddress{makeSimpleWalletAddress(wlt, fromAddr[0]), makeSimpleWalletAddress(wlt, fromAddr[1])} to = []core.TransactionOutput{toAddr} txnResult, err = bc.SendFromAddress(from, to, chgAddr, opt2) @@ -1335,8 +1336,8 @@ func TestSkycoinBlockchainSpend(t *testing.T) { hash := testutil.RandSHA256(t) sky := 500 - //chgAddr := - changeAddr := testutil.MakeAddress().String() + // chgAddr := + changeAddr := testutil.MakeAddress() chgAddr := &SkycoinAddress{ address: changeAddr, poolSection: "", @@ -1389,7 +1390,7 @@ func TestSkycoinBlockchainSpend(t *testing.T) { opt1 := NewTransferOptions() opt1.SetValue("BurnFactor", "0.5") opt1.SetValue("CoinHoursSelectionType", "auto") - + changeAddrString := changeAddr.String() req1 := api.CreateTransactionRequest{ UxOuts: uxOutsStr, IgnoreUnconfirmed: false, @@ -1404,7 +1405,7 @@ func TestSkycoinBlockchainSpend(t *testing.T) { Mode: "share", ShareFactor: "0.5", }, - ChangeAddress: &changeAddr, + ChangeAddress: &changeAddrString, } opt2 := NewTransferOptions() @@ -1417,7 +1418,7 @@ func TestSkycoinBlockchainSpend(t *testing.T) { HoursSelection: api.HoursSelection{ Type: "manual", }, - ChangeAddress: &changeAddr, + ChangeAddress: &changeAddrString, To: []api.Receiver{ api.Receiver{ Address: destinationAddress.String(), @@ -1443,7 +1444,7 @@ func TestSkycoinBlockchainSpend(t *testing.T) { bc := makeSkycoinBlockchain() to := []core.TransactionOutput{toAddr} - //Testing Hours selection auto + // Testing Hours selection auto txnR, err := bc.Spend(wltOuts, to, chgAddr, opt1) require.NoError(t, err) require.NotNil(t, txnR) @@ -1452,7 +1453,7 @@ func TestSkycoinBlockchainSpend(t *testing.T) { require.NoError(t, err) require.Equal(t, util.FormatCoins(uint64(sky), 10), util.FormatCoins(uint64(val), 10)) - //Testing Hours selection manual + // Testing Hours selection manual txnR2, err := bc.Spend(wltOuts, to, chgAddr, opt2) require.NoError(t, err) require.NotNil(t, txnR2) @@ -1498,7 +1499,7 @@ func TestSkycoinSignServiceSign(t *testing.T) { } - //SkycoinCreatedTransaction + // SkycoinCreatedTransaction sigs := txn.Sigs txn.Sigs = []cipher.Sig{} apiCreTxn, err := api.NewCreatedTransaction(&txn, ins) @@ -1514,9 +1515,9 @@ func TestSkycoinSignServiceSign(t *testing.T) { require.NotNil(t, signedTxn) err = signedTxn.VerifySigned() require.NoError(t, err) - //require.Equal(t, txn.Hash().String(), signedTxn.GetId()) + // require.Equal(t, txn.Hash().String(), signedTxn.GetId()) - //SkycoinUninjectedTransaction + // SkycoinUninjectedTransaction txn.Sigs = []cipher.Sig{} skyUninTxn := SkycoinUninjectedTransaction{ txn: &txn, diff --git a/src/util/util.go b/src/util/util.go index f93c9936..a426c858 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -2,9 +2,9 @@ package util import ( "errors" - "github.com/fibercrypto/fibercryptowallet/src/util/logging" "github.com/fibercrypto/fibercryptowallet/src/core" local "github.com/fibercrypto/fibercryptowallet/src/main" + "github.com/fibercrypto/fibercryptowallet/src/util/logging" "strconv" )