github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/account/accounts_test.go (about)

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