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