10 dbm "github.com/tendermint/tmlibs/db"
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"
24 func TestCreateAccountWithUppercase(t *testing.T) {
25 m := mockAccountManager(t)
27 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 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 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
47 if account.Alias != strings.TrimSpace(alias) {
48 t.Fatal("created account alias should be lowercase")
51 nilAccount, err := m.FindByAlias(alias)
52 if nilAccount != nil {
53 t.Fatal("expected nil")
56 target, err := m.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
58 t.Fatal("expected Account, but got nil")
62 func TestCreateAccount(t *testing.T) {
63 m := mockAccountManager(t)
64 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
66 testutil.FatalErr(t, err)
69 found, err := m.FindByID(account.ID)
71 t.Errorf("unexpected error %v", err)
73 if !testutil.DeepEqual(account, found) {
74 t.Errorf("expected account %v to be recorded as %v", account, found)
78 func TestCreateAccountReusedAlias(t *testing.T) {
79 m := mockAccountManager(t)
80 m.createTestAccount(t, "test-alias", nil)
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)
88 func TestUpdateAccountAlias(t *testing.T) {
89 oldAlias := "test-alias"
90 newAlias := "my-alias"
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")
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)
103 if err := m.UpdateAccountAlias(account.ID, newAlias); err != nil {
104 t.Errorf("expected account %v alias should be update", account)
107 updatedAccount, err := m.FindByID(account.ID)
109 t.Errorf("unexpected error %v", err)
112 if updatedAccount.Alias != newAlias {
113 t.Fatalf("alias:\ngot: %v\nwant: %v", updatedAccount.Alias, newAlias)
116 if _, err = m.FindByAlias(oldAlias); errors.Root(err) != ErrFindAccount {
117 t.Errorf("expected %s when using a old alias, got %v", ErrFindAccount, err)
121 func TestDeleteAccount(t *testing.T) {
122 m := mockAccountManager(t)
124 account1, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
126 testutil.FatalErr(t, err)
129 account2, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
131 testutil.FatalErr(t, err)
134 found, err := m.FindByID(account1.ID)
136 t.Errorf("expected account %v should be deleted", found)
139 if err = m.DeleteAccount(account2.ID); err != nil {
140 testutil.FatalErr(t, err)
143 found, err = m.FindByID(account2.ID)
145 t.Errorf("expected account %v should be deleted", found)
149 func TestFindByID(t *testing.T) {
150 m := mockAccountManager(t)
151 account := m.createTestAccount(t, "", nil)
153 found, err := m.FindByID(account.ID)
155 testutil.FatalErr(t, err)
158 if !testutil.DeepEqual(account, found) {
159 t.Errorf("expected found account to be %v, instead found %v", account, found)
163 func TestFindByAlias(t *testing.T) {
164 m := mockAccountManager(t)
165 account := m.createTestAccount(t, "some-alias", nil)
167 found, err := m.FindByAlias("some-alias")
169 testutil.FatalErr(t, err)
172 if !testutil.DeepEqual(account, found) {
173 t.Errorf("expected found account to be %v, instead found %v", account, found)
177 func TestGetAccountIndexKey(t *testing.T) {
178 dirPath, err := ioutil.TempDir(".", "TestAccount")
182 defer os.RemoveAll(dirPath)
184 hsm, err := pseudohsm.New(dirPath)
189 xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
194 xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
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")
205 if reflect.DeepEqual(xpubs1, xpubs2) {
206 t.Fatal("GetAccountIndexKey test err")
210 func mockAccountManager(t *testing.T) *Manager {
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)
221 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
223 dirPath, err := ioutil.TempDir(".", "")
227 defer os.RemoveAll(dirPath)
229 testDB := dbm.NewDB("testdb", "memdb", "temp")
230 defer os.RemoveAll("temp")
232 store := leveldb.NewStore(testDB)
233 txPool := protocol.NewTxPool(store)
234 chain, err := protocol.NewChain(store, txPool)
239 return NewManager(testDB, chain)
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)
245 testutil.FatalErr(t, err)