github.com/klaytn/klaytn@v1.10.2/node/sc/bridge_account_test.go (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package sc 18 19 import ( 20 "io/ioutil" 21 "math" 22 "math/big" 23 "os" 24 "path" 25 "testing" 26 "time" 27 28 "github.com/klaytn/klaytn/accounts/keystore" 29 "github.com/klaytn/klaytn/storage/database" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 // TestBridgeAccountLockUnlock checks the lock/unlock functionality. 34 func TestBridgeAccountLockUnlock(t *testing.T) { 35 tempDir, err := ioutil.TempDir(os.TempDir(), "sc") 36 assert.NoError(t, err) 37 defer func() { 38 if err := os.RemoveAll(tempDir); err != nil { 39 t.Fatalf("fail to delete file %v", err) 40 } 41 }() 42 43 // Config Bridge Account Manager 44 config := &SCConfig{} 45 config.DataDir = tempDir 46 bAcc, err := NewBridgeAccounts(nil, config.DataDir, database.NewDBManager(&database.DBConfig{DBType: database.MemoryDB}), DefaultBridgeTxGasLimit, DefaultBridgeTxGasLimit) 47 assert.NoError(t, err) 48 assert.Equal(t, true, bAcc.cAccount.IsUnlockedAccount()) 49 assert.Equal(t, true, bAcc.pAccount.IsUnlockedAccount()) 50 51 pPwdFilePath := path.Join(tempDir, "parent_bridge_account", bAcc.pAccount.address.String()) 52 pPwd, err := ioutil.ReadFile(pPwdFilePath) 53 pPwdStr := string(pPwd) 54 assert.NoError(t, err) 55 56 cPwdFilePath := path.Join(tempDir, "child_bridge_account", bAcc.cAccount.address.String()) 57 cPwd, err := ioutil.ReadFile(cPwdFilePath) 58 cPwdStr := string(cPwd) 59 assert.NoError(t, err) 60 61 lockAccountsWithCheck := func(t *testing.T, bAcc *BridgeAccounts) { 62 { 63 err := bAcc.cAccount.LockAccount() 64 assert.NoError(t, err) 65 assert.Equal(t, false, bAcc.cAccount.IsUnlockedAccount()) 66 } 67 { 68 err := bAcc.pAccount.LockAccount() 69 assert.NoError(t, err) 70 assert.Equal(t, false, bAcc.pAccount.IsUnlockedAccount()) 71 } 72 } 73 74 unlockParentAccountWithCheck := func(t *testing.T, bAcc *BridgeAccounts, passwd string, duration *uint64, expectedErr error) { 75 expectedIsUnLock := false 76 if expectedErr == nil { 77 expectedIsUnLock = true 78 } 79 80 err := bAcc.pAccount.UnLockAccount(passwd, duration) 81 assert.Equal(t, expectedErr, err) 82 83 time.Sleep(time.Second) 84 85 if duration == nil || *duration == 0 { 86 assert.Equal(t, expectedIsUnLock, bAcc.pAccount.IsUnlockedAccount()) 87 return 88 } 89 90 time.Sleep(time.Duration(*duration) * time.Second) 91 assert.Equal(t, false, bAcc.pAccount.IsUnlockedAccount()) 92 } 93 94 unlockChildAccountWithCheck := func(t *testing.T, bAcc *BridgeAccounts, passwd string, duration *uint64, expectedErr error) { 95 expectedIsUnLock := false 96 if expectedErr == nil { 97 expectedIsUnLock = true 98 } 99 100 err := bAcc.cAccount.UnLockAccount(passwd, duration) 101 assert.Equal(t, expectedErr, err) 102 103 time.Sleep(time.Second) 104 105 if duration == nil || *duration == 0 { 106 assert.Equal(t, expectedIsUnLock, bAcc.cAccount.IsUnlockedAccount()) 107 return 108 } 109 110 time.Sleep(time.Duration(*duration) * time.Second) 111 assert.Equal(t, false, bAcc.cAccount.IsUnlockedAccount()) 112 } 113 114 // Double time Lock Account 115 lockAccountsWithCheck(t, bAcc) 116 lockAccountsWithCheck(t, bAcc) 117 118 // Fail to UnLock Account with invalid timeout 119 lockAccountsWithCheck(t, bAcc) 120 duration := uint64(time.Duration(math.MaxInt64)/time.Second) + 1 121 unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, errUnlockDurationTooLarge) 122 unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, errUnlockDurationTooLarge) 123 124 // Fail to UnLock Account with wrong password 125 lockAccountsWithCheck(t, bAcc) 126 duration = uint64(0) 127 unlockParentAccountWithCheck(t, bAcc, pPwdStr[:3], &duration, keystore.ErrDecrypt) 128 unlockChildAccountWithCheck(t, bAcc, cPwdStr[:3], &duration, keystore.ErrDecrypt) 129 130 // Succeed to UnLock Account 131 lockAccountsWithCheck(t, bAcc) 132 duration = uint64(0) 133 unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, nil) 134 unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, nil) 135 136 // Succeed to UnLock Account with timeout 137 lockAccountsWithCheck(t, bAcc) 138 duration = uint64(5) 139 unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, nil) 140 unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, nil) 141 142 // Fail to UnLock Account with wrong password 143 lockAccountsWithCheck(t, bAcc) 144 duration = uint64(5) 145 unlockParentAccountWithCheck(t, bAcc, pPwdStr[:3], &duration, keystore.ErrDecrypt) 146 unlockChildAccountWithCheck(t, bAcc, cPwdStr[:3], &duration, keystore.ErrDecrypt) 147 148 // Succeed to UnLock Account with nil timeout 149 lockAccountsWithCheck(t, bAcc) 150 unlockParentAccountWithCheck(t, bAcc, pPwdStr, nil, nil) 151 unlockChildAccountWithCheck(t, bAcc, cPwdStr, nil, nil) 152 } 153 154 // TestBridgeAccountInformation checks if the information result is right or not. 155 func TestBridgeAccountInformation(t *testing.T) { 156 tempDir, err := ioutil.TempDir(os.TempDir(), "sc") 157 assert.NoError(t, err) 158 defer func() { 159 if err := os.RemoveAll(tempDir); err != nil { 160 t.Fatalf("fail to delete file %v", err) 161 } 162 }() 163 164 // Config Bridge Account Manager 165 config := &SCConfig{} 166 config.DataDir = tempDir 167 bAcc, err := NewBridgeAccounts(nil, config.DataDir, database.NewDBManager(&database.DBConfig{DBType: database.MemoryDB}), DefaultBridgeTxGasLimit, DefaultBridgeTxGasLimit) 168 assert.NoError(t, err) 169 assert.Equal(t, true, bAcc.cAccount.IsUnlockedAccount()) 170 assert.Equal(t, true, bAcc.pAccount.IsUnlockedAccount()) 171 172 bAcc.pAccount.gasPrice = big.NewInt(100) 173 bAcc.pAccount.nonce = 10 174 bAcc.pAccount.chainID = big.NewInt(200) 175 bAcc.pAccount.isNonceSynced = true 176 err = bAcc.pAccount.LockAccount() 177 assert.NoError(t, err) 178 179 res := bAcc.GetBridgeOperators() 180 assert.Equal(t, 2, len(res)) 181 182 pRes := res["parentOperator"].(map[string]interface{}) 183 cRes := res["childOperator"].(map[string]interface{}) 184 assert.Equal(t, 6, len(pRes)) 185 assert.Equal(t, 6, len(cRes)) 186 187 assert.Equal(t, pRes["address"], bAcc.pAccount.address) 188 assert.Equal(t, pRes["nonce"], bAcc.pAccount.nonce) 189 assert.Equal(t, pRes["chainID"].(*big.Int).String(), bAcc.pAccount.chainID.String()) 190 assert.Equal(t, pRes["gasPrice"].(*big.Int).String(), bAcc.pAccount.gasPrice.String()) 191 assert.Equal(t, pRes["isNonceSynced"], bAcc.pAccount.isNonceSynced) 192 assert.Equal(t, pRes["isUnlocked"], bAcc.pAccount.IsUnlockedAccount()) 193 194 assert.Equal(t, cRes["address"], bAcc.cAccount.address) 195 assert.Equal(t, cRes["nonce"], bAcc.cAccount.nonce) 196 assert.Equal(t, cRes["chainID"].(*big.Int).String(), bAcc.cAccount.chainID.String()) 197 assert.Equal(t, cRes["gasPrice"].(*big.Int).String(), bAcc.cAccount.gasPrice.String()) 198 assert.Equal(t, cRes["isNonceSynced"], bAcc.cAccount.isNonceSynced) 199 assert.Equal(t, cRes["isUnlocked"], bAcc.cAccount.IsUnlockedAccount()) 200 }