github.com/luckypickle/go-ethereum-vet@v1.14.2/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 TestUnlockFlagWrongPassword(t *testing.T) { 159 datadir := tmpDatadirWithKeystore(t) 160 geth := runGeth(t, 161 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 162 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 163 defer geth.ExpectExit() 164 geth.Expect(` 165 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 166 !! Unsupported terminal, password will be echoed. 167 Passphrase: {{.InputLine "wrong1"}} 168 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 169 Passphrase: {{.InputLine "wrong2"}} 170 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 171 Passphrase: {{.InputLine "wrong3"}} 172 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase) 173 `) 174 } 175 176 // https://github.com/luckypickle/go-ethereum-vet/issues/1785 177 func TestUnlockFlagMultiIndex(t *testing.T) { 178 datadir := tmpDatadirWithKeystore(t) 179 geth := runGeth(t, 180 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 181 "--unlock", "0,2", 182 "js", "testdata/empty.js") 183 geth.Expect(` 184 Unlocking account 0 | Attempt 1/3 185 !! Unsupported terminal, password will be echoed. 186 Passphrase: {{.InputLine "foobar"}} 187 Unlocking account 2 | Attempt 1/3 188 Passphrase: {{.InputLine "foobar"}} 189 `) 190 geth.ExpectExit() 191 192 wantMessages := []string{ 193 "Unlocked account", 194 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 195 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 196 } 197 for _, m := range wantMessages { 198 if !strings.Contains(geth.StderrText(), m) { 199 t.Errorf("stderr text does not contain %q", m) 200 } 201 } 202 } 203 204 func TestUnlockFlagPasswordFile(t *testing.T) { 205 datadir := tmpDatadirWithKeystore(t) 206 geth := runGeth(t, 207 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 208 "--password", "testdata/passwords.txt", "--unlock", "0,2", 209 "js", "testdata/empty.js") 210 geth.ExpectExit() 211 212 wantMessages := []string{ 213 "Unlocked account", 214 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 215 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 216 } 217 for _, m := range wantMessages { 218 if !strings.Contains(geth.StderrText(), m) { 219 t.Errorf("stderr text does not contain %q", m) 220 } 221 } 222 } 223 224 func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 225 datadir := tmpDatadirWithKeystore(t) 226 geth := runGeth(t, 227 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 228 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 229 defer geth.ExpectExit() 230 geth.Expect(` 231 Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase) 232 `) 233 } 234 235 func TestUnlockFlagAmbiguous(t *testing.T) { 236 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 237 geth := runGeth(t, 238 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 239 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 240 "js", "testdata/empty.js") 241 defer geth.ExpectExit() 242 243 // Helper for the expect template, returns absolute keystore path. 244 geth.SetTemplateFunc("keypath", func(file string) string { 245 abs, _ := filepath.Abs(filepath.Join(store, file)) 246 return abs 247 }) 248 geth.Expect(` 249 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 250 !! Unsupported terminal, password will be echoed. 251 Passphrase: {{.InputLine "foobar"}} 252 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 253 keystore://{{keypath "1"}} 254 keystore://{{keypath "2"}} 255 Testing your passphrase against all of them... 256 Your passphrase unlocked keystore://{{keypath "1"}} 257 In order to avoid this warning, you need to remove the following duplicate key files: 258 keystore://{{keypath "2"}} 259 `) 260 geth.ExpectExit() 261 262 wantMessages := []string{ 263 "Unlocked account", 264 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 265 } 266 for _, m := range wantMessages { 267 if !strings.Contains(geth.StderrText(), m) { 268 t.Errorf("stderr text does not contain %q", m) 269 } 270 } 271 } 272 273 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 274 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 275 geth := runGeth(t, 276 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 277 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 278 defer geth.ExpectExit() 279 280 // Helper for the expect template, returns absolute keystore path. 281 geth.SetTemplateFunc("keypath", func(file string) string { 282 abs, _ := filepath.Abs(filepath.Join(store, file)) 283 return abs 284 }) 285 geth.Expect(` 286 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 287 !! Unsupported terminal, password will be echoed. 288 Passphrase: {{.InputLine "wrong"}} 289 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 290 keystore://{{keypath "1"}} 291 keystore://{{keypath "2"}} 292 Testing your passphrase against all of them... 293 Fatal: None of the listed files could be unlocked. 294 `) 295 geth.ExpectExit() 296 }