github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/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  	dbm "github.com/bytom/bytom/database/leveldb"
    15  	"github.com/bytom/bytom/errors"
    16  	"github.com/bytom/bytom/event"
    17  	"github.com/bytom/bytom/protocol"
    18  	"github.com/bytom/bytom/testutil"
    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  	account1, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
   121  	if err != nil {
   122  		testutil.FatalErr(t, err)
   123  	}
   124  
   125  	account2, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
   126  	if err != nil {
   127  		testutil.FatalErr(t, err)
   128  	}
   129  
   130  	if err = m.DeleteAccount(account2.ID); err != nil {
   131  		testutil.FatalErr(t, err)
   132  	}
   133  
   134  	if found, err := m.FindByID(account1.ID); err != nil {
   135  		t.Errorf("expected account %v should not be deleted", found)
   136  	}
   137  
   138  	if found, err := m.FindByID(account2.ID); err == nil {
   139  		t.Errorf("expected account %v should be deleted", found)
   140  	}
   141  }
   142  
   143  func TestFindByID(t *testing.T) {
   144  	m := mockAccountManager(t)
   145  	account := m.createTestAccount(t, "", nil)
   146  
   147  	found, err := m.FindByID(account.ID)
   148  	if err != nil {
   149  		testutil.FatalErr(t, err)
   150  	}
   151  
   152  	if !testutil.DeepEqual(account, found) {
   153  		t.Errorf("expected found account to be %v, instead found %v", account, found)
   154  	}
   155  }
   156  
   157  func TestFindByAlias(t *testing.T) {
   158  	m := mockAccountManager(t)
   159  	account := m.createTestAccount(t, "some-alias", nil)
   160  
   161  	found, err := m.FindByAlias("some-alias")
   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 TestGetAccountIndexKey(t *testing.T) {
   172  	dirPath, err := ioutil.TempDir(".", "TestAccount")
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  	defer os.RemoveAll(dirPath)
   177  
   178  	hsm, err := pseudohsm.New(dirPath)
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  
   183  	xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
   194  	xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
   195  	if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
   196  		t.Fatal("GetAccountIndexKey test err")
   197  	}
   198  
   199  	if reflect.DeepEqual(xpubs1, xpubs2) {
   200  		t.Fatal("GetAccountIndexKey test err")
   201  	}
   202  }
   203  
   204  func mockAccountManager(t *testing.T) *Manager {
   205  	dirPath, err := ioutil.TempDir(".", "")
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  	defer os.RemoveAll(dirPath)
   210  
   211  	testDB := dbm.NewDB("testdb", "leveldb", dirPath)
   212  	dispatcher := event.NewDispatcher()
   213  
   214  	store := database.NewStore(testDB)
   215  	txPool := protocol.NewTxPool(store, dispatcher)
   216  	chain, err := protocol.NewChain(store, txPool, dispatcher)
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	return NewManager(testDB, chain)
   222  }
   223  
   224  func (m *Manager) createTestAccount(t testing.TB, alias string, tags map[string]interface{}) *Account {
   225  	account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
   226  	if err != nil {
   227  		testutil.FatalErr(t, err)
   228  	}
   229  
   230  	return account
   231  
   232  }