github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/geth/accountcmd_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package main 19 20 import ( 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 Password: {{.InputLine "foobar"}} 77 Repeat password: {{.InputLine "foobar"}} 78 79 Your new key was generated 80 `) 81 geth.ExpectRegexp(` 82 Public address of the key: 0x[0-9a-fA-F]{40} 83 Path of the secret key file: .*UTC--.+--[0-9a-f]{40} 84 85 - You can share your public address with anyone. Others need it to interact with you. 86 - You must NEVER share the secret key with anyone! The key controls access to your funds! 87 - You must BACKUP your key file! Without the key, it's impossible to access account funds! 88 - You must REMEMBER your password! Without the password, it's impossible to decrypt the key! 89 `) 90 } 91 92 func TestAccountNewBadRepeat(t *testing.T) { 93 geth := runGeth(t, "account", "new", "--lightkdf") 94 defer geth.ExpectExit() 95 geth.Expect(` 96 Your new account is locked with a password. Please give a password. Do not forget this password. 97 !! Unsupported terminal, password will be echoed. 98 Password: {{.InputLine "something"}} 99 Repeat password: {{.InputLine "something else"}} 100 Fatal: Passwords do not match 101 `) 102 } 103 104 func TestAccountUpdate(t *testing.T) { 105 datadir := tmpDatadirWithKeystore(t) 106 geth := runGeth(t, "account", "update", 107 "--datadir", datadir, "--lightkdf", 108 "f466859ead1932d743d622cb74fc058882e8648a") 109 defer geth.ExpectExit() 110 geth.Expect(` 111 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 112 !! Unsupported terminal, password will be echoed. 113 Password: {{.InputLine "foobar"}} 114 Please give a new password. Do not forget this password. 115 Password: {{.InputLine "foobar2"}} 116 Repeat password: {{.InputLine "foobar2"}} 117 `) 118 } 119 120 func TestWalletImport(t *testing.T) { 121 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 122 defer geth.ExpectExit() 123 geth.Expect(` 124 !! Unsupported terminal, password will be echoed. 125 Password: {{.InputLine "foo"}} 126 Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f} 127 `) 128 129 files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore")) 130 if len(files) != 1 { 131 t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) 132 } 133 } 134 135 func TestWalletImportBadPassword(t *testing.T) { 136 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 137 defer geth.ExpectExit() 138 geth.Expect(` 139 !! Unsupported terminal, password will be echoed. 140 Password: {{.InputLine "wrong"}} 141 Fatal: could not decrypt key with given password 142 `) 143 } 144 145 func TestUnlockFlag(t *testing.T) { 146 datadir := tmpDatadirWithKeystore(t) 147 geth := runGeth(t, 148 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 149 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 150 "js", "testdata/empty.js") 151 geth.Expect(` 152 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 153 !! Unsupported terminal, password will be echoed. 154 Password: {{.InputLine "foobar"}} 155 `) 156 geth.ExpectExit() 157 158 wantMessages := []string{ 159 "Unlocked account", 160 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 161 } 162 for _, m := range wantMessages { 163 if !strings.Contains(geth.StderrText(), m) { 164 t.Errorf("stderr text does not contain %q", m) 165 } 166 } 167 } 168 169 func TestUnlockFlagWrongPassword(t *testing.T) { 170 datadir := tmpDatadirWithKeystore(t) 171 geth := runGeth(t, 172 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 173 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 174 defer geth.ExpectExit() 175 geth.Expect(` 176 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 177 !! Unsupported terminal, password will be echoed. 178 Password: {{.InputLine "wrong1"}} 179 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 180 Password: {{.InputLine "wrong2"}} 181 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 182 Password: {{.InputLine "wrong3"}} 183 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given password) 184 `) 185 } 186 187 // https://github.com/AigarNetwork/aigar/issues/1785 188 func TestUnlockFlagMultiIndex(t *testing.T) { 189 datadir := tmpDatadirWithKeystore(t) 190 geth := runGeth(t, 191 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 192 "--unlock", "0,2", 193 "js", "testdata/empty.js") 194 geth.Expect(` 195 Unlocking account 0 | Attempt 1/3 196 !! Unsupported terminal, password will be echoed. 197 Password: {{.InputLine "foobar"}} 198 Unlocking account 2 | Attempt 1/3 199 Password: {{.InputLine "foobar"}} 200 `) 201 geth.ExpectExit() 202 203 wantMessages := []string{ 204 "Unlocked account", 205 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 206 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 207 } 208 for _, m := range wantMessages { 209 if !strings.Contains(geth.StderrText(), m) { 210 t.Errorf("stderr text does not contain %q", m) 211 } 212 } 213 } 214 215 func TestUnlockFlagPasswordFile(t *testing.T) { 216 datadir := tmpDatadirWithKeystore(t) 217 geth := runGeth(t, 218 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 219 "--password", "testdata/passwords.txt", "--unlock", "0,2", 220 "js", "testdata/empty.js") 221 geth.ExpectExit() 222 223 wantMessages := []string{ 224 "Unlocked account", 225 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 226 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 227 } 228 for _, m := range wantMessages { 229 if !strings.Contains(geth.StderrText(), m) { 230 t.Errorf("stderr text does not contain %q", m) 231 } 232 } 233 } 234 235 func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 236 datadir := tmpDatadirWithKeystore(t) 237 geth := runGeth(t, 238 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 239 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 240 defer geth.ExpectExit() 241 geth.Expect(` 242 Fatal: Failed to unlock account 0 (could not decrypt key with given password) 243 `) 244 } 245 246 func TestUnlockFlagAmbiguous(t *testing.T) { 247 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 248 geth := runGeth(t, 249 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 250 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 251 "js", "testdata/empty.js") 252 defer geth.ExpectExit() 253 254 // Helper for the expect template, returns absolute keystore path. 255 geth.SetTemplateFunc("keypath", func(file string) string { 256 abs, _ := filepath.Abs(filepath.Join(store, file)) 257 return abs 258 }) 259 geth.Expect(` 260 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 261 !! Unsupported terminal, password will be echoed. 262 Password: {{.InputLine "foobar"}} 263 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 264 keystore://{{keypath "1"}} 265 keystore://{{keypath "2"}} 266 Testing your password against all of them... 267 Your password unlocked keystore://{{keypath "1"}} 268 In order to avoid this warning, you need to remove the following duplicate key files: 269 keystore://{{keypath "2"}} 270 `) 271 geth.ExpectExit() 272 273 wantMessages := []string{ 274 "Unlocked account", 275 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 276 } 277 for _, m := range wantMessages { 278 if !strings.Contains(geth.StderrText(), m) { 279 t.Errorf("stderr text does not contain %q", m) 280 } 281 } 282 } 283 284 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 285 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 286 geth := runGeth(t, 287 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 288 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 289 defer geth.ExpectExit() 290 291 // Helper for the expect template, returns absolute keystore path. 292 geth.SetTemplateFunc("keypath", func(file string) string { 293 abs, _ := filepath.Abs(filepath.Join(store, file)) 294 return abs 295 }) 296 geth.Expect(` 297 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 298 !! Unsupported terminal, password will be echoed. 299 Password: {{.InputLine "wrong"}} 300 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 301 keystore://{{keypath "1"}} 302 keystore://{{keypath "2"}} 303 Testing your password against all of them... 304 Fatal: None of the listed files could be unlocked. 305 `) 306 geth.ExpectExit() 307 }