github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/cmd/gttc/accountcmd_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "github.com/TTCECO/gttc/common/hexutil" 21 "io/ioutil" 22 "path/filepath" 23 "runtime" 24 "strings" 25 "testing" 26 27 "github.com/cespare/cp" 28 ) 29 30 // These tests are 'smoke tests' for the account related 31 // subcommands and flags. 32 // 33 // For most tests, the test files from package accounts 34 // are copied into a temporary keystore directory. 35 36 func tmpDatadirWithKeystore(t *testing.T) string { 37 datadir := tmpdir(t) 38 keystore := filepath.Join(datadir, "keystore") 39 source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore") 40 if err := cp.CopyAll(keystore, source); err != nil { 41 t.Fatal(err) 42 } 43 return datadir 44 } 45 46 func TestAccountListEmpty(t *testing.T) { 47 geth := runGeth(t, "account", "list") 48 geth.ExpectExit() 49 } 50 51 func TestAccountList(t *testing.T) { 52 datadir := tmpDatadirWithKeystore(t) 53 geth := runGeth(t, "account", "list", "--datadir", datadir) 54 defer geth.ExpectExit() 55 if runtime.GOOS == "windows" { 56 geth.Expect(` 57 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 58 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa 59 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz 60 `) 61 } else { 62 geth.Expect(` 63 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 64 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa 65 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz 66 `) 67 } 68 } 69 70 func TestAccountNew(t *testing.T) { 71 geth := runGeth(t, "account", "new", "--lightkdf") 72 defer geth.ExpectExit() 73 geth.Expect(` 74 Your new account is locked with a password. Please give a password. Do not forget this password. 75 !! Unsupported terminal, password will be echoed. 76 Passphrase: {{.InputLine "foobar"}} 77 Repeat passphrase: {{.InputLine "foobar"}} 78 `) 79 geth.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`) 80 } 81 82 func TestAccountNewBadRepeat(t *testing.T) { 83 geth := runGeth(t, "account", "new", "--lightkdf") 84 defer geth.ExpectExit() 85 geth.Expect(` 86 Your new account is locked with a password. Please give a password. Do not forget this password. 87 !! Unsupported terminal, password will be echoed. 88 Passphrase: {{.InputLine "something"}} 89 Repeat passphrase: {{.InputLine "something else"}} 90 Fatal: Passphrases do not match 91 `) 92 } 93 94 func TestAccountUpdate(t *testing.T) { 95 datadir := tmpDatadirWithKeystore(t) 96 geth := runGeth(t, "account", "update", 97 "--datadir", datadir, "--lightkdf", 98 "f466859ead1932d743d622cb74fc058882e8648a") 99 defer geth.ExpectExit() 100 geth.Expect(` 101 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 102 !! Unsupported terminal, password will be echoed. 103 Passphrase: {{.InputLine "foobar"}} 104 Please give a new password. Do not forget this password. 105 Passphrase: {{.InputLine "foobar2"}} 106 Repeat passphrase: {{.InputLine "foobar2"}} 107 `) 108 } 109 110 func TestWalletImport(t *testing.T) { 111 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 112 defer geth.ExpectExit() 113 geth.Expect(` 114 !! Unsupported terminal, password will be echoed. 115 Passphrase: {{.InputLine "foo"}} 116 Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f} 117 `) 118 119 files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore")) 120 if len(files) != 1 { 121 t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) 122 } 123 } 124 125 func TestWalletImportBadPassword(t *testing.T) { 126 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 127 defer geth.ExpectExit() 128 geth.Expect(` 129 !! Unsupported terminal, password will be echoed. 130 Passphrase: {{.InputLine "wrong"}} 131 Fatal: could not decrypt key with given passphrase 132 `) 133 } 134 135 func TestUnlockFlag(t *testing.T) { 136 datadir := tmpDatadirWithKeystore(t) 137 geth := runGeth(t, 138 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 139 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 140 "js", "testdata/empty.js") 141 geth.Expect(` 142 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 143 !! Unsupported terminal, password will be echoed. 144 Passphrase: {{.InputLine "foobar"}} 145 `) 146 geth.ExpectExit() 147 148 wantMessages := []string{ 149 "Unlocked account", 150 "=" + hexutil.CustomHexPrefix + "f466859eAD1932D743d622CB74FC058882E8648A", 151 } 152 for _, m := range wantMessages { 153 if !strings.Contains(geth.StderrText(), m) { 154 t.Errorf("stderr text does not contain %q", m) 155 } 156 } 157 } 158 159 func TestUnlockFlagWrongPassword(t *testing.T) { 160 datadir := tmpDatadirWithKeystore(t) 161 geth := runGeth(t, 162 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 163 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 164 defer geth.ExpectExit() 165 geth.Expect(` 166 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 167 !! Unsupported terminal, password will be echoed. 168 Passphrase: {{.InputLine "wrong1"}} 169 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 170 Passphrase: {{.InputLine "wrong2"}} 171 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 172 Passphrase: {{.InputLine "wrong3"}} 173 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase) 174 `) 175 } 176 177 // https://github.com/TTCECO/gttc/issues/1785 178 func TestUnlockFlagMultiIndex(t *testing.T) { 179 datadir := tmpDatadirWithKeystore(t) 180 geth := runGeth(t, 181 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 182 "--unlock", "0,2", 183 "js", "testdata/empty.js") 184 geth.Expect(` 185 Unlocking account 0 | Attempt 1/3 186 !! Unsupported terminal, password will be echoed. 187 Passphrase: {{.InputLine "foobar"}} 188 Unlocking account 2 | Attempt 1/3 189 Passphrase: {{.InputLine "foobar"}} 190 `) 191 geth.ExpectExit() 192 193 wantMessages := []string{ 194 "Unlocked account", 195 "=" + hexutil.CustomHexPrefix + "7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 196 "=" + hexutil.CustomHexPrefix + "289d485D9771714CCe91D3393D764E1311907ACc", 197 } 198 for _, m := range wantMessages { 199 if !strings.Contains(geth.StderrText(), m) { 200 t.Errorf("stderr text does not contain %q", m) 201 } 202 } 203 } 204 205 func TestUnlockFlagPasswordFile(t *testing.T) { 206 datadir := tmpDatadirWithKeystore(t) 207 geth := runGeth(t, 208 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 209 "--password", "testdata/passwords.txt", "--unlock", "0,2", 210 "js", "testdata/empty.js") 211 geth.ExpectExit() 212 213 wantMessages := []string{ 214 "Unlocked account", 215 "=" + hexutil.CustomHexPrefix + "7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 216 "=" + hexutil.CustomHexPrefix + "289d485D9771714CCe91D3393D764E1311907ACc", 217 } 218 for _, m := range wantMessages { 219 if !strings.Contains(geth.StderrText(), m) { 220 t.Errorf("stderr text does not contain %q", m) 221 } 222 } 223 } 224 225 func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 226 datadir := tmpDatadirWithKeystore(t) 227 geth := runGeth(t, 228 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 229 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 230 defer geth.ExpectExit() 231 geth.Expect(` 232 Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase) 233 `) 234 } 235 236 func TestUnlockFlagAmbiguous(t *testing.T) { 237 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 238 geth := runGeth(t, 239 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 240 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 241 "js", "testdata/empty.js") 242 defer geth.ExpectExit() 243 244 // Helper for the expect template, returns absolute keystore path. 245 geth.SetTemplateFunc("keypath", func(file string) string { 246 abs, _ := filepath.Abs(filepath.Join(store, file)) 247 return abs 248 }) 249 geth.Expect(` 250 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 251 !! Unsupported terminal, password will be echoed. 252 Passphrase: {{.InputLine "foobar"}} 253 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 254 keystore://{{keypath "1"}} 255 keystore://{{keypath "2"}} 256 Testing your passphrase against all of them... 257 Your passphrase unlocked keystore://{{keypath "1"}} 258 In order to avoid this warning, you need to remove the following duplicate key files: 259 keystore://{{keypath "2"}} 260 `) 261 geth.ExpectExit() 262 263 wantMessages := []string{ 264 "Unlocked account", 265 "=" + hexutil.CustomHexPrefix + "f466859eAD1932D743d622CB74FC058882E8648A", 266 } 267 for _, m := range wantMessages { 268 if !strings.Contains(geth.StderrText(), m) { 269 t.Errorf("stderr text does not contain %q", m) 270 } 271 } 272 } 273 274 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 275 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 276 geth := runGeth(t, 277 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 278 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 279 defer geth.ExpectExit() 280 281 // Helper for the expect template, returns absolute keystore path. 282 geth.SetTemplateFunc("keypath", func(file string) string { 283 abs, _ := filepath.Abs(filepath.Join(store, file)) 284 return abs 285 }) 286 geth.Expect(` 287 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 288 !! Unsupported terminal, password will be echoed. 289 Passphrase: {{.InputLine "wrong"}} 290 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 291 keystore://{{keypath "1"}} 292 keystore://{{keypath "2"}} 293 Testing your passphrase against all of them... 294 Fatal: None of the listed files could be unlocked. 295 `) 296 geth.ExpectExit() 297 }