accountcmd_test.go 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"io/ioutil"
	"path/filepath"
22
	"runtime"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	"strings"
	"testing"

	"github.com/cespare/cp"
)

// These tests are 'smoke tests' for the account related
// subcommands and flags.
//
// For most tests, the test files from package accounts
// are copied into a temporary keystore directory.

func tmpDatadirWithKeystore(t *testing.T) string {
	datadir := tmpdir(t)
	keystore := filepath.Join(datadir, "keystore")
38
	source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore")
39 40 41 42 43 44 45
	if err := cp.CopyAll(keystore, source); err != nil {
		t.Fatal(err)
	}
	return datadir
}

func TestAccountListEmpty(t *testing.T) {
46
	geth := runGeth(t, "account", "list")
47
	geth.ExpectExit()
48 49 50 51
}

func TestAccountList(t *testing.T) {
	datadir := tmpDatadirWithKeystore(t)
52
	geth := runGeth(t, "account", "list", "--datadir", datadir)
53
	defer geth.ExpectExit()
54
	if runtime.GOOS == "windows" {
55
		geth.Expect(`
56 57 58
Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
59 60
`)
	} else {
61
		geth.Expect(`
62 63 64
Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
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 72
	defer geth.ExpectExit()
	geth.Expect(`
73 74
Your new account is locked with a password. Please give a password. Do not forget this password.
!! Unsupported terminal, password will be echoed.
75 76
Password: {{.InputLine "foobar"}}
Repeat password: {{.InputLine "foobar"}}
77 78 79 80 81 82 83 84 85 86 87

Your new key was generated
`)
	geth.ExpectRegexp(`
Public address of the key:   0x[0-9a-fA-F]{40}
Path of the secret key file: .*UTC--.+--[0-9a-f]{40}

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
88 89 90
`)
}

91
func TestAccountImport(t *testing.T) {
92
	tests := []struct{ name, key, output string }{
93
		{
94
			name:   "correct account",
95 96 97 98
			key:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			output: "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n",
		},
		{
99
			name:   "invalid character",
100 101 102 103 104
			key:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1",
			output: "Fatal: Failed to load the private key: invalid character '1' at end of key file\n",
		},
	}
	for _, test := range tests {
105
		test := test
106 107 108 109
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			importAccountWithExpect(t, test.key, test.output)
		})
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	}
}

func importAccountWithExpect(t *testing.T, key string, expected string) {
	dir := tmpdir(t)
	keyfile := filepath.Join(dir, "key.prv")
	if err := ioutil.WriteFile(keyfile, []byte(key), 0600); err != nil {
		t.Error(err)
	}
	passwordFile := filepath.Join(dir, "password.txt")
	if err := ioutil.WriteFile(passwordFile, []byte("foobar"), 0600); err != nil {
		t.Error(err)
	}
	geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile)
	defer geth.ExpectExit()
	geth.Expect(expected)
}

128
func TestAccountNewBadRepeat(t *testing.T) {
129
	geth := runGeth(t, "account", "new", "--lightkdf")
130 131
	defer geth.ExpectExit()
	geth.Expect(`
132 133
Your new account is locked with a password. Please give a password. Do not forget this password.
!! Unsupported terminal, password will be echoed.
134 135 136
Password: {{.InputLine "something"}}
Repeat password: {{.InputLine "something else"}}
Fatal: Passwords do not match
137 138 139 140 141
`)
}

func TestAccountUpdate(t *testing.T) {
	datadir := tmpDatadirWithKeystore(t)
142
	geth := runGeth(t, "account", "update",
143
		"--datadir", datadir, "--lightkdf",
144
		"f466859ead1932d743d622cb74fc058882e8648a")
145 146
	defer geth.ExpectExit()
	geth.Expect(`
147 148
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
149
Password: {{.InputLine "foobar"}}
150
Please give a new password. Do not forget this password.
151 152
Password: {{.InputLine "foobar2"}}
Repeat password: {{.InputLine "foobar2"}}
153 154 155 156
`)
}

func TestWalletImport(t *testing.T) {
157
	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
158 159
	defer geth.ExpectExit()
	geth.Expect(`
160
!! Unsupported terminal, password will be echoed.
161
Password: {{.InputLine "foo"}}
162 163 164 165 166 167 168 169 170 171
Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
`)

	files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
	if len(files) != 1 {
		t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
	}
}

func TestWalletImportBadPassword(t *testing.T) {
172
	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
173 174
	defer geth.ExpectExit()
	geth.Expect(`
175
!! Unsupported terminal, password will be echoed.
176 177
Password: {{.InputLine "wrong"}}
Fatal: could not decrypt key with given password
178 179 180 181
`)
}

func TestUnlockFlag(t *testing.T) {
182 183
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
184
	geth.Expect(`
185 186
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
187
Password: {{.InputLine "foobar"}}
188
`)
189
	geth.ExpectExit()
190 191

	wantMessages := []string{
192
		"Unlocked account",
193
		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
194 195
	}
	for _, m := range wantMessages {
196
		if !strings.Contains(geth.StderrText(), m) {
197 198 199 200 201 202
			t.Errorf("stderr text does not contain %q", m)
		}
	}
}

func TestUnlockFlagWrongPassword(t *testing.T) {
203 204 205
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")

206 207
	defer geth.ExpectExit()
	geth.Expect(`
208 209
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
210
Password: {{.InputLine "wrong1"}}
211
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
212
Password: {{.InputLine "wrong2"}}
213
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
214 215
Password: {{.InputLine "wrong3"}}
Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given password)
216 217 218 219 220
`)
}

// https://github.com/ethereum/go-ethereum/issues/1785
func TestUnlockFlagMultiIndex(t *testing.T) {
221 222 223
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "js", "testdata/empty.js")

224
	geth.Expect(`
225 226
Unlocking account 0 | Attempt 1/3
!! Unsupported terminal, password will be echoed.
227
Password: {{.InputLine "foobar"}}
228
Unlocking account 2 | Attempt 1/3
229
Password: {{.InputLine "foobar"}}
230
`)
231
	geth.ExpectExit()
232 233

	wantMessages := []string{
234
		"Unlocked account",
235 236
		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
237 238
	}
	for _, m := range wantMessages {
239
		if !strings.Contains(geth.StderrText(), m) {
240 241 242 243 244 245
			t.Errorf("stderr text does not contain %q", m)
		}
	}
}

func TestUnlockFlagPasswordFile(t *testing.T) {
246 247 248
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "js", "testdata/empty.js")

249
	geth.ExpectExit()
250 251

	wantMessages := []string{
252
		"Unlocked account",
253 254
		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
255 256
	}
	for _, m := range wantMessages {
257
		if !strings.Contains(geth.StderrText(), m) {
258 259 260 261 262 263
			t.Errorf("stderr text does not contain %q", m)
		}
	}
}

func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
264 265 266
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password",
		"testdata/wrong-passwords.txt", "--unlock", "0,2")
267 268
	defer geth.ExpectExit()
	geth.Expect(`
269
Fatal: Failed to unlock account 0 (could not decrypt key with given password)
270 271
`)
}
272 273

func TestUnlockFlagAmbiguous(t *testing.T) {
274
	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
275 276 277
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
		store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
278
		"js", "testdata/empty.js")
279
	defer geth.ExpectExit()
280 281

	// Helper for the expect template, returns absolute keystore path.
282
	geth.SetTemplateFunc("keypath", func(file string) string {
283 284 285
		abs, _ := filepath.Abs(filepath.Join(store, file))
		return abs
	})
286
	geth.Expect(`
287 288
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
289
Password: {{.InputLine "foobar"}}
290
Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
291 292
   keystore://{{keypath "1"}}
   keystore://{{keypath "2"}}
293 294
Testing your password against all of them...
Your password unlocked keystore://{{keypath "1"}}
295
In order to avoid this warning, you need to remove the following duplicate key files:
296
   keystore://{{keypath "2"}}
297
`)
298
	geth.ExpectExit()
299 300

	wantMessages := []string{
301
		"Unlocked account",
302
		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
303 304
	}
	for _, m := range wantMessages {
305
		if !strings.Contains(geth.StderrText(), m) {
306 307 308 309 310 311
			t.Errorf("stderr text does not contain %q", m)
		}
	}
}

func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
312
	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
313 314 315 316
	geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
		store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")

317
	defer geth.ExpectExit()
318 319

	// Helper for the expect template, returns absolute keystore path.
320
	geth.SetTemplateFunc("keypath", func(file string) string {
321 322 323
		abs, _ := filepath.Abs(filepath.Join(store, file))
		return abs
	})
324
	geth.Expect(`
325 326
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
327
Password: {{.InputLine "wrong"}}
328
Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
329 330
   keystore://{{keypath "1"}}
   keystore://{{keypath "2"}}
331
Testing your password against all of them...
332 333
Fatal: None of the listed files could be unlocked.
`)
334
	geth.ExpectExit()
335
}