OSDN Git Service

Add confirm tx
[bytom/vapor.git] / account / accounts_test.go
1 package account
2
3 import (
4         "io/ioutil"
5         "os"
6         "reflect"
7         "strings"
8         "testing"
9
10         dbm "github.com/tendermint/tmlibs/db"
11
12         "github.com/vapor/blockchain/pseudohsm"
13         "github.com/vapor/blockchain/signers"
14         "github.com/vapor/common"
15         "github.com/vapor/config"
16         "github.com/vapor/consensus"
17         "github.com/vapor/crypto/ed25519/chainkd"
18         "github.com/vapor/database/leveldb"
19         "github.com/vapor/errors"
20         "github.com/vapor/protocol"
21         "github.com/vapor/testutil"
22 )
23
24 func TestCreateAccountWithUppercase(t *testing.T) {
25         m := mockAccountManager(t)
26         alias := "UPPER"
27         account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
28
29         if err != nil {
30                 t.Fatal(err)
31         }
32
33         if account.Alias != strings.ToLower(alias) {
34                 t.Fatal("created account alias should be lowercase")
35         }
36 }
37
38 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
39         m := mockAccountManager(t)
40         alias := " with space "
41         account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
42
43         if err != nil {
44                 t.Fatal(err)
45         }
46
47         if account.Alias != strings.TrimSpace(alias) {
48                 t.Fatal("created account alias should be lowercase")
49         }
50
51         nilAccount, err := m.FindByAlias(alias)
52         if nilAccount != nil {
53                 t.Fatal("expected nil")
54         }
55
56         target, err := m.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
57         if target == nil {
58                 t.Fatal("expected Account, but got nil")
59         }
60 }
61
62 func TestCreateAccount(t *testing.T) {
63         m := mockAccountManager(t)
64         account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
65         if err != nil {
66                 testutil.FatalErr(t, err)
67         }
68
69         found, err := m.FindByID(account.ID)
70         if err != nil {
71                 t.Errorf("unexpected error %v", err)
72         }
73         if !testutil.DeepEqual(account, found) {
74                 t.Errorf("expected account %v to be recorded as %v", account, found)
75         }
76 }
77
78 func TestCreateAccountReusedAlias(t *testing.T) {
79         m := mockAccountManager(t)
80         m.createTestAccount(t, "test-alias", nil)
81
82         _, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
83         if errors.Root(err) != ErrDuplicateAlias {
84                 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
85         }
86 }
87
88 func TestUpdateAccountAlias(t *testing.T) {
89         oldAlias := "test-alias"
90         newAlias := "my-alias"
91
92         m := mockAccountManager(t)
93         account := m.createTestAccount(t, oldAlias, nil)
94         if err := m.UpdateAccountAlias("testID", newAlias); err == nil {
95                 t.Fatal("expected error when using an invalid account id")
96         }
97
98         err := m.UpdateAccountAlias(account.ID, oldAlias)
99         if errors.Root(err) != ErrDuplicateAlias {
100                 t.Errorf("expected %s when using a duplicate alias, got %v", ErrDuplicateAlias, err)
101         }
102
103         if err := m.UpdateAccountAlias(account.ID, newAlias); err != nil {
104                 t.Errorf("expected account %v alias should be update", account)
105         }
106
107         updatedAccount, err := m.FindByID(account.ID)
108         if err != nil {
109                 t.Errorf("unexpected error %v", err)
110         }
111
112         if updatedAccount.Alias != newAlias {
113                 t.Fatalf("alias:\ngot:  %v\nwant: %v", updatedAccount.Alias, newAlias)
114         }
115
116         if _, err = m.FindByAlias(oldAlias); errors.Root(err) != ErrFindAccount {
117                 t.Errorf("expected %s when using a old alias, got %v", ErrFindAccount, err)
118         }
119 }
120
121 func TestDeleteAccount(t *testing.T) {
122         m := mockAccountManager(t)
123
124         account1, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
125         if err != nil {
126                 testutil.FatalErr(t, err)
127         }
128
129         account2, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
130         if err != nil {
131                 testutil.FatalErr(t, err)
132         }
133
134         found, err := m.FindByID(account1.ID)
135         if err != nil {
136                 t.Errorf("expected account %v should be deleted", found)
137         }
138
139         if err = m.DeleteAccount(account2.ID); err != nil {
140                 testutil.FatalErr(t, err)
141         }
142
143         found, err = m.FindByID(account2.ID)
144         if err != nil {
145                 t.Errorf("expected account %v should be deleted", found)
146         }
147 }
148
149 func TestFindByID(t *testing.T) {
150         m := mockAccountManager(t)
151         account := m.createTestAccount(t, "", nil)
152
153         found, err := m.FindByID(account.ID)
154         if err != nil {
155                 testutil.FatalErr(t, err)
156         }
157
158         if !testutil.DeepEqual(account, found) {
159                 t.Errorf("expected found account to be %v, instead found %v", account, found)
160         }
161 }
162
163 func TestFindByAlias(t *testing.T) {
164         m := mockAccountManager(t)
165         account := m.createTestAccount(t, "some-alias", nil)
166
167         found, err := m.FindByAlias("some-alias")
168         if err != nil {
169                 testutil.FatalErr(t, err)
170         }
171
172         if !testutil.DeepEqual(account, found) {
173                 t.Errorf("expected found account to be %v, instead found %v", account, found)
174         }
175 }
176
177 func TestGetAccountIndexKey(t *testing.T) {
178         dirPath, err := ioutil.TempDir(".", "TestAccount")
179         if err != nil {
180                 t.Fatal(err)
181         }
182         defer os.RemoveAll(dirPath)
183
184         hsm, err := pseudohsm.New(dirPath)
185         if err != nil {
186                 t.Fatal(err)
187         }
188
189         xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
190         if err != nil {
191                 t.Fatal(err)
192         }
193
194         xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
195         if err != nil {
196                 t.Fatal(err)
197         }
198
199         xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
200         xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
201         if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
202                 t.Fatal("GetAccountIndexKey test err")
203         }
204
205         if reflect.DeepEqual(xpubs1, xpubs2) {
206                 t.Fatal("GetAccountIndexKey test err")
207         }
208 }
209
210 func mockAccountManager(t *testing.T) *Manager {
211
212         config.CommonConfig = config.DefaultConfig()
213         consensus.SoloNetParams.Signer = "78673764e0ba91a4c5ba9ec0c8c23c69e3d73bf27970e05e0a977e81e13bde475264d3b177a96646bc0ce517ae7fd63504c183ab6d330dea184331a4cf5912d5"
214         config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
215         config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
216         for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners {
217                 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
218                 if err != nil {
219                         t.Fatal(err)
220                 }
221                 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
222         }
223         dirPath, err := ioutil.TempDir(".", "")
224         if err != nil {
225                 t.Fatal(err)
226         }
227         defer os.RemoveAll(dirPath)
228
229         testDB := dbm.NewDB("testdb", "memdb", "temp")
230         defer os.RemoveAll("temp")
231
232         store := leveldb.NewStore(testDB)
233         txPool := protocol.NewTxPool(store)
234         chain, err := protocol.NewChain(store, txPool)
235         if err != nil {
236                 t.Fatal(err)
237         }
238
239         return NewManager(testDB, chain)
240 }
241
242 func (m *Manager) createTestAccount(t testing.TB, alias string, tags map[string]interface{}) *Account {
243         account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
244         if err != nil {
245                 testutil.FatalErr(t, err)
246         }
247
248         return account
249
250 }