Skip to content

Commit

Permalink
polish TestChangeAppPass
Browse files Browse the repository at this point in the history
  • Loading branch information
amass01 committed Mar 17, 2021
1 parent 98e0564 commit 326eff0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 36 deletions.
7 changes: 3 additions & 4 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ func (c *Core) loadWallet(dbWallet *db.Wallet) (*xcWallet, error) {

// Construct the unconnected xcWallet.
contractLockedAmt, orderLockedAmt := c.lockedAmounts(assetID)
xcWallet := &xcWallet{
return &xcWallet{
Wallet: w,
connector: dex.NewConnectionMaster(w),
AssetID: assetID,
Expand All @@ -1533,11 +1533,10 @@ func (c *Core) loadWallet(dbWallet *db.Wallet) (*xcWallet, error) {
OrderLocked: orderLockedAmt,
ContractLocked: contractLockedAmt,
},
encPass: dbWallet.EncryptedPW,
address: dbWallet.Address,
dbID: dbWallet.ID(),
}
xcWallet.setEncPW(dbWallet.EncryptedPW)
return xcWallet, nil
}, nil
}

// WalletState returns the *WalletState for the asset ID.
Expand Down
30 changes: 11 additions & 19 deletions client/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4965,7 +4965,6 @@ func TestChangeAppPass(t *testing.T) {
tCore := rig.core
var assetID uint32 = 54322
newTPW := []byte("apppass")
acctEncKey := string(tCore.conns[tDexHost].acct.encKey)

// App Password error
rig.crypter.recryptErr = tErr
Expand All @@ -4977,36 +4976,29 @@ func TestChangeAppPass(t *testing.T) {

wallet, _ := newTWallet(assetID, rig.crypter)
tCore.wallets[assetID] = wallet
asset.Register(assetID, &tDriver{
f: func(wCfg *asset.WalletConfig, logger dex.Logger, net dex.Network) (asset.Wallet, error) {
return wallet.Wallet, nil
},
})
if err = wallet.Connect(); err != nil {
t.Fatal(err)
}
defer wallet.Disconnect()

origPass := string(wallet.encPW())
acctEncKey := make([]byte, len(tCore.conns[tDexHost].acct.encKey))
copy(acctEncKey, tCore.conns[tDexHost].acct.encKey)
origPass := make([]byte, len(wallet.encPW()))
copy(origPass, wallet.encPW())
err = tCore.ChangeAppPass(tPW, newTPW)
if err != nil {
t.Fatal(err)
}

// Ensure db's UpdateAccount func was called & db's account encrypted
// key didn't change.
// Ensure db's UpdateAccount func was called.
if !rig.db.verifyUpdateAccount {
t.Fatal("expected execution of db.UpdateAccount")
}
if string(rig.db.accountInfoPersisted.EncKey) == acctEncKey {
t.Fatalf("expected db account EncKey to change, old: %v new: %v",
acctEncKey, string(rig.db.accountInfoPersisted.EncKey))
// Ensure db's account encrypted key changed.
if bytes.Equal(rig.db.accountInfoPersisted.EncKey, acctEncKey) {
t.Fatalf("expected db account EncKey to change, old: %x new: %x",
acctEncKey, rig.db.accountInfoPersisted.EncKey)
}
// Ensure xcWallet encrypted password updated.
nWallet := tCore.wallets[assetID]
if origPass == string(nWallet.encPW()) {
if bytes.Equal(origPass, wallet.encPW()) {
t.Fatalf("expected xcwallet encPW to change: old %x new %x",
origPass, nWallet.encPW())
origPass, wallet.encPW())
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/core/trade_simnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,8 @@ func (client *tClient) disableWallets() {
// NOTE: this is not reversible, but could be made so with the undo data:
// walletPasses[cid] = make(map[uint32]passes, len(client.core.wallets))
for _, wallet := range client.core.wallets {
wallet.setEncPW([]byte{0})
wallet.mtx.Lock()
wallet.encPass = []byte{0}
wallet.pw = ""
wallet.mtx.Unlock()
}
Expand Down
23 changes: 11 additions & 12 deletions client/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (w *xcWallet) refreshUnlock() (unlockAttempted bool, err error) {
return false, nil // unlocked
}
// Locked backend requires both encrypted and decrypted passwords.
if len(w.encPW()) == 0 {
w.mtx.RLock()
defer w.mtx.RUnlock()
if len(w.encPass) == 0 {
return false, fmt.Errorf("%s wallet reporting as locked but no password"+
" has been set", unbip(w.AssetID))
}
w.mtx.RLock()
defer w.mtx.RUnlock()
if len(w.pw) == 0 {
return false, fmt.Errorf("cannot refresh unlock on a locked %s wallet",
unbip(w.AssetID))
Expand All @@ -100,12 +100,12 @@ func (w *xcWallet) refreshUnlock() (unlockAttempted bool, err error) {
// Lock the wallet. For encrypted wallets (encPW set), this clears the cached
// decrypted password and attempts to lock the wallet backend.
func (w *xcWallet) Lock() error {
if len(w.encPW()) == 0 {
w.mtx.Lock()
defer w.mtx.Unlock()
if len(w.encPass) == 0 {
return nil
}
w.mtx.Lock()
w.pw = ""
w.mtx.Unlock()
return w.Wallet.Lock()
}

Expand All @@ -122,29 +122,28 @@ func (w *xcWallet) unlocked() bool {
// this is true only if the decrypted password is cached. Use this to determine
// if the wallet may be unlocked without user interaction (via refreshUnlock).
func (w *xcWallet) locallyUnlocked() bool {
if len(w.encPW()) == 0 {
return true // unencrypted wallet
}
w.mtx.RLock()
defer w.mtx.RUnlock()
if len(w.encPass) == 0 {
return true // unencrypted wallet
}
return len(w.pw) > 0 // cached password for encrypted wallet
}

// state returns the current WalletState.
func (w *xcWallet) state() *WalletState {
encPW := w.encPW()
w.mtx.RLock()
defer w.mtx.RUnlock()
winfo := w.Info()
return &WalletState{
Symbol: unbip(w.AssetID),
AssetID: w.AssetID,
Open: len(encPW) == 0 || len(w.pw) > 0,
Open: len(w.encPass) == 0 || len(w.pw) > 0,
Running: w.connector.On(),
Balance: w.balance,
Address: w.address,
Units: winfo.Units,
Encrypted: len(encPW) > 0,
Encrypted: len(w.encPass) > 0,
Synced: w.synced,
SyncProgress: w.syncProgress,
}
Expand Down

0 comments on commit 326eff0

Please sign in to comment.