github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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", "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") 47 geth.expectExit() 48 } 49 50 func TestAccountList(t *testing.T) { 51 datadir := tmpDatadirWithKeystore(t) 52 geth := runGeth(t, "--datadir", datadir, "account") 53 defer geth.expectExit() 54 if runtime.GOOS == "windows" { 55 geth.expect(` 56 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} {{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 57 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} {{.Datadir}}\keystore\aaa 58 Account #2: {289d485d9771714cce91d3393d764e1311907acc} {{.Datadir}}\keystore\zzz 59 `) 60 } else { 61 geth.expect(` 62 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} {{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 63 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} {{.Datadir}}/keystore/aaa 64 Account #2: {289d485d9771714cce91d3393d764e1311907acc} {{.Datadir}}/keystore/zzz 65 `) 66 } 67 } 68 69 func TestAccountNew(t *testing.T) { 70 geth := runGeth(t, "--lightkdf", "account", "new") 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, "--lightkdf", "account", "new") 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, 96 "--datadir", datadir, "--lightkdf", 97 "account", "update", "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, "--lightkdf", "wallet", "import", "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, "--lightkdf", "wallet", "import", "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", "--dev", 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 f466859ead1932d743d622cb74fc058882e8648a", 149 } 150 for _, m := range wantMessages { 151 if !strings.Contains(geth.stderrText(), m) { 152 t.Errorf("stderr text does not contain %q", m) 153 } 154 } 155 } 156 157 func TestUnlockFlagWrongPassword(t *testing.T) { 158 datadir := tmpDatadirWithKeystore(t) 159 geth := runGeth(t, 160 "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev", 161 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 162 defer geth.expectExit() 163 geth.expect(` 164 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 165 !! Unsupported terminal, password will be echoed. 166 Passphrase: {{.InputLine "wrong1"}} 167 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 168 Passphrase: {{.InputLine "wrong2"}} 169 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 170 Passphrase: {{.InputLine "wrong3"}} 171 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase) 172 `) 173 } 174 175 // https://github.com/ethereum/go-ethereum/issues/1785 176 func TestUnlockFlagMultiIndex(t *testing.T) { 177 datadir := tmpDatadirWithKeystore(t) 178 geth := runGeth(t, 179 "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev", 180 "--unlock", "0,2", 181 "js", "testdata/empty.js") 182 geth.expect(` 183 Unlocking account 0 | Attempt 1/3 184 !! Unsupported terminal, password will be echoed. 185 Passphrase: {{.InputLine "foobar"}} 186 Unlocking account 2 | Attempt 1/3 187 Passphrase: {{.InputLine "foobar"}} 188 `) 189 geth.expectExit() 190 191 wantMessages := []string{ 192 "Unlocked account 7ef5a6135f1fd6a02593eedc869c6d41d934aef8", 193 "Unlocked account 289d485d9771714cce91d3393d764e1311907acc", 194 } 195 for _, m := range wantMessages { 196 if !strings.Contains(geth.stderrText(), m) { 197 t.Errorf("stderr text does not contain %q", m) 198 } 199 } 200 } 201 202 func TestUnlockFlagPasswordFile(t *testing.T) { 203 datadir := tmpDatadirWithKeystore(t) 204 geth := runGeth(t, 205 "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev", 206 "--password", "testdata/passwords.txt", "--unlock", "0,2", 207 "js", "testdata/empty.js") 208 geth.expectExit() 209 210 wantMessages := []string{ 211 "Unlocked account 7ef5a6135f1fd6a02593eedc869c6d41d934aef8", 212 "Unlocked account 289d485d9771714cce91d3393d764e1311907acc", 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 TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 222 datadir := tmpDatadirWithKeystore(t) 223 geth := runGeth(t, 224 "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev", 225 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 226 defer geth.expectExit() 227 geth.expect(` 228 Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase) 229 `) 230 } 231 232 func TestUnlockFlagAmbiguous(t *testing.T) { 233 store := filepath.Join("..", "..", "accounts", "testdata", "dupes") 234 geth := runGeth(t, 235 "--keystore", store, "--nat", "none", "--nodiscover", "--dev", 236 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 237 "js", "testdata/empty.js") 238 defer geth.expectExit() 239 240 // Helper for the expect template, returns absolute keystore path. 241 geth.setTemplateFunc("keypath", func(file string) string { 242 abs, _ := filepath.Abs(filepath.Join(store, file)) 243 return abs 244 }) 245 geth.expect(` 246 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 247 !! Unsupported terminal, password will be echoed. 248 Passphrase: {{.InputLine "foobar"}} 249 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 250 {{keypath "1"}} 251 {{keypath "2"}} 252 Testing your passphrase against all of them... 253 Your passphrase unlocked {{keypath "1"}} 254 In order to avoid this warning, you need to remove the following duplicate key files: 255 {{keypath "2"}} 256 `) 257 geth.expectExit() 258 259 wantMessages := []string{ 260 "Unlocked account f466859ead1932d743d622cb74fc058882e8648a", 261 } 262 for _, m := range wantMessages { 263 if !strings.Contains(geth.stderrText(), m) { 264 t.Errorf("stderr text does not contain %q", m) 265 } 266 } 267 } 268 269 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 270 store := filepath.Join("..", "..", "accounts", "testdata", "dupes") 271 geth := runGeth(t, 272 "--keystore", store, "--nat", "none", "--nodiscover", "--dev", 273 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 274 defer geth.expectExit() 275 276 // Helper for the expect template, returns absolute keystore path. 277 geth.setTemplateFunc("keypath", func(file string) string { 278 abs, _ := filepath.Abs(filepath.Join(store, file)) 279 return abs 280 }) 281 geth.expect(` 282 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 283 !! Unsupported terminal, password will be echoed. 284 Passphrase: {{.InputLine "wrong"}} 285 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 286 {{keypath "1"}} 287 {{keypath "2"}} 288 Testing your passphrase against all of them... 289 Fatal: None of the listed files could be unlocked. 290 `) 291 geth.expectExit() 292 }