123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- // 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"
- "runtime"
- "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")
- source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore")
- if err := cp.CopyAll(keystore, source); err != nil {
- t.Fatal(err)
- }
- // add the necessary files for geth to start with the raft consensus
- geth := filepath.Join(datadir, "geth")
- sourceNodeKey := filepath.Join("testdata", "geth")
- if err := cp.CopyAll(geth, sourceNodeKey); err != nil {
- t.Fatal(err)
- }
- return datadir
- }
- func runMinimalGethWithRaftConsensus(t *testing.T, args ...string) *testgeth {
- argsWithRaft := append([]string{"--raft"}, args...)
- return runMinimalGeth(t, argsWithRaft...)
- }
- func TestAccountListEmpty(t *testing.T) {
- geth := runGeth(t, "account", "list")
- geth.ExpectExit()
- }
- func TestAccountList(t *testing.T) {
- datadir := tmpDatadirWithKeystore(t)
- geth := runGeth(t, "account", "list", "--datadir", datadir)
- defer geth.ExpectExit()
- if runtime.GOOS == "windows" {
- geth.Expect(`
- 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
- `)
- } else {
- geth.Expect(`
- 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
- `)
- }
- }
- func TestAccountNew(t *testing.T) {
- geth := runGeth(t, "account", "new", "--lightkdf")
- defer geth.ExpectExit()
- geth.Expect(`
- Your new account is locked with a password. Please give a password. Do not forget this password.
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foobar"}}
- Repeat password: {{.InputLine "foobar"}}
- 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!
- `)
- }
- func TestAccountImport(t *testing.T) {
- tests := []struct{ name, key, output string }{
- {
- name: "correct account",
- key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
- output: "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n",
- },
- {
- name: "invalid character",
- key: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1",
- output: "Fatal: Failed to load the private key: invalid character '1' at end of key file\n",
- },
- }
- for _, test := range tests {
- test := test
- t.Run(test.name, func(t *testing.T) {
- t.Parallel()
- importAccountWithExpect(t, test.key, test.output)
- })
- }
- }
- 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)
- }
- func TestAccountNewBadRepeat(t *testing.T) {
- geth := runGeth(t, "account", "new", "--lightkdf")
- defer geth.ExpectExit()
- geth.Expect(`
- Your new account is locked with a password. Please give a password. Do not forget this password.
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "something"}}
- Repeat password: {{.InputLine "something else"}}
- Fatal: Passwords do not match
- `)
- }
- func TestAccountUpdate(t *testing.T) {
- datadir := tmpDatadirWithKeystore(t)
- geth := runGeth(t, "account", "update",
- "--datadir", datadir, "--lightkdf",
- "f466859ead1932d743d622cb74fc058882e8648a")
- defer geth.ExpectExit()
- geth.Expect(`
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foobar"}}
- Please give a new password. Do not forget this password.
- Password: {{.InputLine "foobar2"}}
- Repeat password: {{.InputLine "foobar2"}}
- `)
- }
- func TestWalletImport(t *testing.T) {
- geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
- defer geth.ExpectExit()
- geth.Expect(`
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foo"}}
- 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) {
- geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
- defer geth.ExpectExit()
- geth.Expect(`
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "wrong"}}
- Fatal: could not decrypt key with given password
- `)
- }
- func TestUnlockFlag(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- geth := runMinimalGethWithRaftConsensus(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
- geth.Expect(`
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foobar"}}
- `)
- geth.ExpectExit()
- wantMessages := []string{
- "Unlocked account",
- "=0xf466859eAD1932D743d622CB74FC058882E8648A",
- }
- for _, m := range wantMessages {
- if !strings.Contains(geth.StderrText(), m) {
- t.Errorf("stderr text does not contain %q", m)
- }
- }
- }
- func TestGethDoesntStartWithoutConfiguredConsensus(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- datadir := tmpDatadirWithKeystore(t)
- geth := runGeth(t,
- "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0")
- expectedText := "Consensus not specified. Exiting!!"
- // changed to expect regexp because fatalf writes the message to stdout/stderr
- geth.ExpectRegexp(expectedText)
- }
- func TestGethStartsWhenConsensusAndPrivateConfigAreConfigured(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- datadir := tmpDatadirWithKeystore(t)
- geth := runGeth(t,
- "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--raft")
- geth.ExpectExit()
- }
- func TestUnlockFlagWrongPassword(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
- defer geth.ExpectExit()
- geth.Expect(`
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "wrong1"}}
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
- Password: {{.InputLine "wrong2"}}
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
- Password: {{.InputLine "wrong3"}}
- Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given password)
- `)
- }
- // https://github.com/ethereum/go-ethereum/issues/1785
- func TestUnlockFlagMultiIndex(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- geth := runMinimalGethWithRaftConsensus(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "js", "testdata/empty.js")
- geth.Expect(`
- Unlocking account 0 | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foobar"}}
- Unlocking account 2 | Attempt 1/3
- Password: {{.InputLine "foobar"}}
- `)
- geth.ExpectExit()
- wantMessages := []string{
- "Unlocked account",
- "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
- "=0x289d485D9771714CCe91D3393D764E1311907ACc",
- }
- for _, m := range wantMessages {
- if !strings.Contains(geth.StderrText(), m) {
- t.Errorf("stderr text does not contain %q", m)
- }
- }
- }
- func TestUnlockFlagPasswordFile(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- geth := runMinimalGethWithRaftConsensus(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "js", "testdata/empty.js")
- geth.ExpectExit()
- wantMessages := []string{
- "Unlocked account",
- "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
- "=0x289d485D9771714CCe91D3393D764E1311907ACc",
- }
- for _, m := range wantMessages {
- if !strings.Contains(geth.StderrText(), m) {
- t.Errorf("stderr text does not contain %q", m)
- }
- }
- }
- func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password",
- "testdata/wrong-passwords.txt", "--unlock", "0,2")
- defer geth.ExpectExit()
- geth.Expect(`
- Fatal: Failed to unlock account 0 (could not decrypt key with given password)
- `)
- }
- func TestUnlockFlagAmbiguous(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
- geth := runMinimalGethWithRaftConsensus(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
- store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
- "js", "testdata/empty.js")
- defer geth.ExpectExit()
- // Helper for the expect template, returns absolute keystore path.
- geth.SetTemplateFunc("keypath", func(file string) string {
- abs, _ := filepath.Abs(filepath.Join(store, file))
- return abs
- })
- geth.Expect(`
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "foobar"}}
- Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
- keystore://{{keypath "1"}}
- keystore://{{keypath "2"}}
- Testing your password against all of them...
- Your password unlocked keystore://{{keypath "1"}}
- In order to avoid this warning, you need to remove the following duplicate key files:
- keystore://{{keypath "2"}}
- `)
- geth.ExpectExit()
- wantMessages := []string{
- "Unlocked account",
- "=0xf466859eAD1932D743d622CB74FC058882E8648A",
- }
- for _, m := range wantMessages {
- if !strings.Contains(geth.StderrText(), m) {
- t.Errorf("stderr text does not contain %q", m)
- }
- }
- }
- func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
- defer SetResetPrivateConfig("ignore")()
- store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
- geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
- "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
- store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
- defer geth.ExpectExit()
- // Helper for the expect template, returns absolute keystore path.
- geth.SetTemplateFunc("keypath", func(file string) string {
- abs, _ := filepath.Abs(filepath.Join(store, file))
- return abs
- })
- geth.Expect(`
- Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
- !! Unsupported terminal, password will be echoed.
- Password: {{.InputLine "wrong"}}
- Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
- keystore://{{keypath "1"}}
- keystore://{{keypath "2"}}
- Testing your password against all of them...
- Fatal: None of the listed files could be unlocked.
- `)
- geth.ExpectExit()
- }
|