OSDN Git Service

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