github.com/aergoio/aergo@v1.3.1/account/accountservice_test.go (about) 1 package account 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "math/big" 7 "testing" 8 9 "github.com/aergoio/aergo-lib/db" 10 "github.com/aergoio/aergo/config" 11 "github.com/aergoio/aergo/state" 12 "github.com/aergoio/aergo/types" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var ( 17 configName = "server" 18 configPaths = []string{".", "./bin/", "$HOME/.aergo/"} 19 configRequired = false 20 21 configFilePath string 22 as *AccountService 23 conf *config.Config 24 ) 25 26 var sdb *state.ChainStateDB 27 28 func initTest() { 29 serverCtx := config.NewServerContext("", "") 30 conf := serverCtx.GetDefaultConfig().(*config.Config) 31 conf.DataDir, _ = ioutil.TempDir("", "test") 32 33 sdb = state.NewChainStateDB() 34 testmode := true 35 sdb.Init(string(db.BadgerImpl), conf.DataDir, nil, testmode) 36 37 as = NewAccountService(conf, sdb) 38 as.testConfig = true 39 as.BeforeStart() 40 } 41 42 func deinitTest() { 43 as.BeforeStop() 44 } 45 46 func TestNewAccountAndGet(t *testing.T) { 47 initTest() 48 defer deinitTest() 49 var testaccounts []*types.Account 50 testsize := 10 51 for i := 0; i < testsize; i++ { 52 passphrase := fmt.Sprintf("test%d", i) 53 account, err := as.createAccount(passphrase) 54 assert.NoError(t, err, "failed to create account") 55 assert.Equalf(t, types.AddressLength, len(account.Address), "wrong address length : %s", account.Address) 56 57 testaccounts = append(testaccounts, account) 58 } 59 getlist := as.getAccounts() 60 var resultlist []*types.Account 61 for _, a := range getlist { 62 for _, t := range testaccounts { 63 if types.EncodeAddress(a.Address) == types.EncodeAddress(t.Address) { 64 resultlist = append(resultlist, t) 65 break 66 } 67 } 68 } 69 assert.Len(t, resultlist, len(testaccounts), "failed to get account") 70 } 71 72 func TestNewAccountAndUnlockLock(t *testing.T) { 73 initTest() 74 defer deinitTest() 75 var testaccounts []*types.Account 76 testsize := 10 77 for i := 0; i < testsize; i++ { 78 passphrase := fmt.Sprintf("test%d", i) 79 account, err := as.createAccount(passphrase) 80 assert.NoError(t, err, "failed to create account") 81 assert.Equalf(t, types.AddressLength, len(account.Address), "wrong address length : %s", account.Address) 82 83 testaccounts = append(testaccounts, account) 84 } 85 for i := 0; i < testsize; i++ { 86 passphrase := fmt.Sprintf("test%d", i) 87 account, err := as.unlockAccount(testaccounts[i].Address, passphrase) 88 if err != nil || account == nil { 89 t.Errorf("failed to unlock account[%d]:%s", i, err) 90 } 91 } 92 for i := 0; i < testsize; i++ { 93 passphrase := fmt.Sprintf("test%d", i) 94 account, err := as.lockAccount(testaccounts[i].Address, passphrase) 95 if err != nil || account == nil { 96 t.Errorf("failed to lock account[%d]:%s", i, err) 97 } 98 } 99 } 100 101 func TestNewAccountAndUnlockFail(t *testing.T) { 102 initTest() 103 defer deinitTest() 104 var testaccounts []*types.Account 105 testsize := 10 106 for i := 0; i < testsize; i++ { 107 passphrase := fmt.Sprintf("test%d", i) 108 account, err := as.createAccount(passphrase) 109 assert.NoError(t, err, "failed to create account") 110 assert.Equalf(t, types.AddressLength, len(account.Address), "wrong address length : %s", account.Address) 111 112 testaccounts = append(testaccounts, account) 113 } 114 for i := 0; i < testsize; i++ { 115 passphrase := fmt.Sprintf("test_Error%d", i) 116 account, err := as.unlockAccount(testaccounts[i].Address, passphrase) 117 if err == nil || account != nil { 118 t.Errorf("should not unlock the account[%d]:%s", i, err) 119 } 120 if err != types.ErrWrongAddressOrPassWord { 121 t.Errorf("should return proper error code expect = %s, return = %s", types.ErrWrongAddressOrPassWord, err) 122 } 123 } 124 } 125 126 func TestNewAccountUnlockSignVerfiy(t *testing.T) { 127 initTest() 128 defer deinitTest() 129 passphrase := "test" 130 account, err := as.createAccount(passphrase) 131 assert.NoError(t, err, "failed to create account") 132 assert.Equalf(t, types.AddressLength, len(account.Address), "wrong address length : %s", account.Address) 133 134 unlockedAccount, err := as.unlockAccount(account.Address, passphrase) 135 if err != nil || unlockedAccount == nil { 136 t.Errorf("failed to unlock account:%s", err) 137 t.FailNow() 138 } 139 tx := &types.Tx{Body: &types.TxBody{Account: account.Address}} 140 err = as.ks.SignTx(tx, nil) 141 assert.NoError(t, err, "failed to sign") 142 assert.NotNil(t, tx.Body.Sign, "failed to sign") 143 144 err = as.ks.VerifyTx(tx) 145 assert.NoError(t, err, "failed to verify") 146 } 147 148 func TestVerfiyFail(t *testing.T) { 149 initTest() 150 defer deinitTest() 151 passphrase := "test" 152 account, err := as.createAccount(passphrase) 153 assert.NoError(t, err, "failed to create account") 154 assert.Equalf(t, types.AddressLength, len(account.Address), "wrong address length : %s", account.Address) 155 156 unlockedAccount, err := as.unlockAccount(account.Address, passphrase) 157 if err != nil || unlockedAccount == nil { 158 t.Errorf("failed to unlock account:%s", err) 159 t.FailNow() 160 } 161 162 tx := &types.Tx{Body: &types.TxBody{Account: account.Address}} 163 err = as.ks.SignTx(tx, nil) 164 assert.NoError(t, err, "failed to sign") 165 assert.NotNil(t, tx.Body.Sign, "failed to sign") 166 167 //edit tx after sign 168 tx.Body.Amount = new(big.Int).SetUint64(0xff).Bytes() 169 err = as.ks.VerifyTx(tx) 170 assert.Error(t, err, types.ErrSignNotMatch, "failed to verfiy") 171 } 172 173 func TestBase58CheckEncoding(t *testing.T) { 174 initTest() 175 defer deinitTest() 176 addr := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33} 177 178 encoded := types.EncodeAddress(addr) 179 expected := "AmJaNDXoPbBRn9XHh9onKbDKuAzj88n5Bzt7KniYA78qUEc5EwBd" 180 assert.Equal(t, expected, encoded, "incorrectly encoded address") 181 182 decoded, _ := types.DecodeAddress(encoded) 183 assert.Equal(t, addr, decoded, "incorrectly decoded address") 184 185 _, err := types.DecodeAddress("AmJaNDXoPbBRn9XHh9onKbDKuAzj88n5Bzt7KniYA78qUEc5EwBA") 186 assert.NotEmpty(t, err, "decoding address with wrong checksum") 187 }