github.com/ethereum/go-ethereum@v1.16.1/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 "os" 21 "path/filepath" 22 "runtime" 23 "testing" 24 25 "github.com/cespare/cp" 26 ) 27 28 // These tests are 'smoke tests' for the account related 29 // subcommands and flags. 30 // 31 // For most tests, the test files from package accounts 32 // are copied into a temporary keystore directory. 33 34 func tmpDatadirWithKeystore(t *testing.T) string { 35 datadir := t.TempDir() 36 keystore := filepath.Join(datadir, "keystore") 37 source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore") 38 if err := cp.CopyAll(keystore, source); err != nil { 39 t.Fatal(err) 40 } 41 return datadir 42 } 43 44 func TestAccountListEmpty(t *testing.T) { 45 t.Parallel() 46 geth := runGeth(t, "account", "list") 47 geth.ExpectExit() 48 } 49 50 func TestAccountList(t *testing.T) { 51 t.Parallel() 52 datadir := tmpDatadirWithKeystore(t) 53 var want = ` 54 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 55 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa 56 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz 57 ` 58 if runtime.GOOS == "windows" { 59 want = ` 60 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 61 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa 62 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz 63 ` 64 } 65 { 66 geth := runGeth(t, "account", "list", "--datadir", datadir) 67 geth.Expect(want) 68 geth.ExpectExit() 69 } 70 { 71 geth := runGeth(t, "--datadir", datadir, "account", "list") 72 geth.Expect(want) 73 geth.ExpectExit() 74 } 75 } 76 77 func TestAccountNew(t *testing.T) { 78 t.Parallel() 79 geth := runGeth(t, "account", "new", "--lightkdf") 80 defer geth.ExpectExit() 81 geth.Expect(` 82 Your new account is locked with a password. Please give a password. Do not forget this password. 83 !! Unsupported terminal, password will be echoed. 84 Password: {{.InputLine "foobar"}} 85 Repeat password: {{.InputLine "foobar"}} 86 87 Your new key was generated 88 `) 89 geth.ExpectRegexp(` 90 Public address of the key: 0x[0-9a-fA-F]{40} 91 Path of the secret key file: .*UTC--.+--[0-9a-f]{40} 92 93 - You can share your public address with anyone. Others need it to interact with you. 94 - You must NEVER share the secret key with anyone! The key controls access to your funds! 95 - You must BACKUP your key file! Without the key, it's impossible to access account funds! 96 - You must REMEMBER your password! Without the password, it's impossible to decrypt the key! 97 `) 98 } 99 100 func TestAccountImport(t *testing.T) { 101 t.Parallel() 102 tests := []struct{ name, key, output string }{ 103 { 104 name: "correct account", 105 key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 106 output: "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n", 107 }, 108 { 109 name: "invalid character", 110 key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1", 111 output: "Fatal: Failed to load the private key: invalid character '1' at end of key file\n", 112 }, 113 } 114 for _, test := range tests { 115 t.Run(test.name, func(t *testing.T) { 116 t.Parallel() 117 importAccountWithExpect(t, test.key, test.output) 118 }) 119 } 120 } 121 122 func TestAccountHelp(t *testing.T) { 123 t.Parallel() 124 geth := runGeth(t, "account", "-h") 125 geth.WaitExit() 126 if have, want := geth.ExitStatus(), 0; have != want { 127 t.Errorf("exit error, have %d want %d", have, want) 128 } 129 130 geth = runGeth(t, "account", "import", "-h") 131 geth.WaitExit() 132 if have, want := geth.ExitStatus(), 0; have != want { 133 t.Errorf("exit error, have %d want %d", have, want) 134 } 135 } 136 137 func importAccountWithExpect(t *testing.T, key string, expected string) { 138 dir := t.TempDir() 139 keyfile := filepath.Join(dir, "key.prv") 140 if err := os.WriteFile(keyfile, []byte(key), 0600); err != nil { 141 t.Error(err) 142 } 143 passwordFile := filepath.Join(dir, "password.txt") 144 if err := os.WriteFile(passwordFile, []byte("foobar"), 0600); err != nil { 145 t.Error(err) 146 } 147 geth := runGeth(t, "--lightkdf", "account", "import", "-password", passwordFile, keyfile) 148 defer geth.ExpectExit() 149 geth.Expect(expected) 150 } 151 152 func TestAccountNewBadRepeat(t *testing.T) { 153 t.Parallel() 154 geth := runGeth(t, "account", "new", "--lightkdf") 155 defer geth.ExpectExit() 156 geth.Expect(` 157 Your new account is locked with a password. Please give a password. Do not forget this password. 158 !! Unsupported terminal, password will be echoed. 159 Password: {{.InputLine "something"}} 160 Repeat password: {{.InputLine "something else"}} 161 Fatal: Passwords do not match 162 `) 163 } 164 165 func TestAccountUpdate(t *testing.T) { 166 t.Parallel() 167 datadir := tmpDatadirWithKeystore(t) 168 geth := runGeth(t, "account", "update", 169 "--datadir", datadir, "--lightkdf", 170 "f466859ead1932d743d622cb74fc058882e8648a") 171 defer geth.ExpectExit() 172 geth.Expect(` 173 Please give a NEW password. Do not forget this password. 174 !! Unsupported terminal, password will be echoed. 175 Password: {{.InputLine "foobar2"}} 176 Repeat password: {{.InputLine "foobar2"}} 177 Please provide the OLD password for account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 178 Password: {{.InputLine "foobar"}} 179 `) 180 } 181 182 func TestWalletImport(t *testing.T) { 183 t.Parallel() 184 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 185 defer geth.ExpectExit() 186 geth.Expect(` 187 !! Unsupported terminal, password will be echoed. 188 Password: {{.InputLine "foo"}} 189 Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f} 190 `) 191 192 files, err := os.ReadDir(filepath.Join(geth.Datadir, "keystore")) 193 if len(files) != 1 { 194 t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) 195 } 196 } 197 198 func TestWalletImportBadPassword(t *testing.T) { 199 t.Parallel() 200 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 201 defer geth.ExpectExit() 202 geth.Expect(` 203 !! Unsupported terminal, password will be echoed. 204 Password: {{.InputLine "wrong"}} 205 Fatal: could not decrypt key with given password 206 `) 207 }