OSDN Git Service

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