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"
23 func TestCreateAccountWithUppercase(t *testing.T) {
24 m := mockAccountManager(t)
26 xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
27 account, err := m.Create(xpubs, 1, alias, signers.BIP0044)
33 if account.Alias != strings.ToLower(alias) {
34 t.Fatal("created account alias should be lowercase")
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)
48 if account.Alias != strings.TrimSpace(alias) {
49 t.Fatal("created account alias should be lowercase")
52 nilAccount, err := m.FindByAlias(alias)
53 if nilAccount != nil {
54 t.Fatal("expected nil")
57 target, err := m.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
59 t.Fatal("expected Account, but got nil")
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)
68 testutil.FatalErr(t, err)
70 fmt.Println("account is:", account, "account xpub:", account.XPubs)
72 found, err := m.FindByID(account.ID)
74 t.Errorf("unexpected error %v", err)
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)
80 t.Errorf("expected type of account %v to be recorded as %v", reflect.TypeOf(account.XPubs[0]), reflect.TypeOf(found.XPubs[0]))
84 func TestCreateAccountReusedAlias(t *testing.T) {
85 m := mockAccountManager(t)
86 m.createTestAccount(t, "test-alias", nil)
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)
95 func TestUpdateAccountAlias(t *testing.T) {
96 oldAlias := "test-alias"
97 newAlias := "my-alias"
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")
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)
110 if err := m.UpdateAccountAlias(account.ID, newAlias); err != nil {
111 t.Errorf("expected account %v alias should be update", account)
114 updatedAccount, err := m.FindByID(account.ID)
116 t.Errorf("unexpected error %v", err)
119 if updatedAccount.Alias != newAlias {
120 t.Fatalf("alias:\ngot: %v\nwant: %v", updatedAccount.Alias, newAlias)
123 if _, err = m.FindByAlias(oldAlias); errors.Root(err) != ErrFindAccount {
124 t.Errorf("expected %s when using a old alias, got %v", ErrFindAccount, err)
128 func TestDeleteAccount(t *testing.T) {
129 m := mockAccountManager(t)
131 xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
132 account1, err := m.Create(xpubs, 1, "test-alias1", signers.BIP0044)
134 testutil.FatalErr(t, err)
137 account2, err := m.Create(xpubs, 1, "test-alias2", signers.BIP0044)
139 testutil.FatalErr(t, err)
142 found, err := m.FindByID(account1.ID)
144 t.Errorf("expected account %v should be deleted", found)
147 if err = m.DeleteAccount(account2.ID); err != nil {
148 testutil.FatalErr(t, err)
151 found, err = m.FindByID(account2.ID)
153 t.Errorf("expected account %v should be deleted", found)
157 func TestFindByID(t *testing.T) {
158 m := mockAccountManager(t)
159 account := m.createTestAccount(t, "", nil)
161 found, err := m.FindByID(account.ID)
163 testutil.FatalErr(t, err)
166 if !testutil.DeepEqual(account, found) {
167 t.Errorf("expected found account to be %v, instead found %v", account, found)
171 func TestFindByAlias(t *testing.T) {
172 m := mockAccountManager(t)
173 account := m.createTestAccount(t, "some-alias", nil)
175 found, err := m.FindByAlias("some-alias")
177 testutil.FatalErr(t, err)
180 if !testutil.DeepEqual(account, found) {
181 t.Errorf("expected found account to be %v, instead found %v", account, found)
185 func TestGetAccountIndexKey(t *testing.T) {
186 dirPath, err := ioutil.TempDir(".", "TestAccount")
190 defer os.RemoveAll(dirPath)
192 hsm, err := pseudohsm.New(dirPath)
197 xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
202 xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
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")
213 if reflect.DeepEqual(xpubs1, xpubs2) {
214 t.Fatal("GetAccountIndexKey test err")
218 func mockAccountManager(t *testing.T) *Manager {
219 dirPath, err := ioutil.TempDir(".", "")
223 defer os.RemoveAll(dirPath)
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)
235 return NewManager(testDB, chain)
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)
242 testutil.FatalErr(t, err)