OSDN Git Service

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