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 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"
26 func TestCreateAccountWithUppercase(t *testing.T) {
27 m := mockAccountManager(t)
29 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
35 if account.Alias != strings.ToLower(alias) {
36 t.Fatal("created account alias should be lowercase")
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)
49 if account.Alias != strings.TrimSpace(alias) {
50 t.Fatal("created account alias should be lowercase")
53 nilAccount, err := m.FindByAlias(alias)
54 if nilAccount != nil {
55 t.Fatal("expected nil")
58 target, err := m.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
60 t.Fatal("expected Account, but got nil")
64 func TestCreateAccount(t *testing.T) {
65 m := mockAccountManager(t)
66 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
68 testutil.FatalErr(t, err)
71 found, err := m.FindByID(account.ID)
73 t.Errorf("unexpected error %v", err)
75 if !testutil.DeepEqual(account, found) {
76 t.Errorf("expected account %v to be recorded as %v", account, found)
80 func TestCreateAccountReusedAlias(t *testing.T) {
81 m := mockAccountManager(t)
82 m.createTestAccount(t, "test-alias", nil)
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)
90 func TestUpdateAccountAlias(t *testing.T) {
91 oldAlias := "test-alias"
92 newAlias := "my-alias"
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")
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)
105 if err := m.UpdateAccountAlias(account.ID, newAlias); err != nil {
106 t.Errorf("expected account %v alias should be update", account)
109 updatedAccount, err := m.FindByID(account.ID)
111 t.Errorf("unexpected error %v", err)
114 if updatedAccount.Alias != newAlias {
115 t.Fatalf("alias:\ngot: %v\nwant: %v", updatedAccount.Alias, newAlias)
118 if _, err = m.FindByAlias(oldAlias); errors.Root(err) != ErrFindAccount {
119 t.Errorf("expected %s when using a old alias, got %v", ErrFindAccount, err)
123 func TestDeleteAccount(t *testing.T) {
124 m := mockAccountManager(t)
126 account1, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
128 testutil.FatalErr(t, err)
131 account2, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
133 testutil.FatalErr(t, err)
136 found, err := m.FindByID(account1.ID)
138 t.Errorf("expected account %v should be deleted", found)
141 if err = m.DeleteAccount(account2.ID); err != nil {
142 testutil.FatalErr(t, err)
145 found, err = m.FindByID(account2.ID)
147 t.Errorf("expected account %v should be deleted", found)
151 func TestFindByID(t *testing.T) {
152 m := mockAccountManager(t)
153 account := m.createTestAccount(t, "", nil)
155 found, err := m.FindByID(account.ID)
157 testutil.FatalErr(t, err)
160 if !testutil.DeepEqual(account, found) {
161 t.Errorf("expected found account to be %v, instead found %v", account, found)
165 func TestFindByAlias(t *testing.T) {
166 m := mockAccountManager(t)
167 account := m.createTestAccount(t, "some-alias", nil)
169 found, err := m.FindByAlias("some-alias")
171 testutil.FatalErr(t, err)
174 if !testutil.DeepEqual(account, found) {
175 t.Errorf("expected found account to be %v, instead found %v", account, found)
179 func TestGetAccountIndexKey(t *testing.T) {
180 dirPath, err := ioutil.TempDir(".", "TestAccount")
184 defer os.RemoveAll(dirPath)
186 hsm, err := pseudohsm.New(dirPath)
191 xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
196 xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
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")
207 if reflect.DeepEqual(xpubs1, xpubs2) {
208 t.Fatal("GetAccountIndexKey test err")
212 func mockAccountManager(t *testing.T) *Manager {
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)
223 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
225 dirPath, err := ioutil.TempDir(".", "")
229 defer os.RemoveAll(dirPath)
231 testDB := dbm.NewDB("testdb", "memdb", "temp")
232 defer os.RemoveAll("temp")
234 store := leveldb.NewStore(testDB)
235 txPool := protocol.NewTxPool(store)
236 var engine engine.Engine
237 switch config.CommonConfig.Consensus.Type {
241 chain, err := protocol.NewChain(store, txPool, engine)
246 return NewManager(testDB, chain)
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)
252 testutil.FatalErr(t, err)