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