github.com/mister-meeseeks/go-ethereum@v1.9.7/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", "keystore", "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", "list")
    47  	geth.ExpectExit()
    48  }
    49  
    50  func TestAccountList(t *testing.T) {
    51  	datadir := tmpDatadirWithKeystore(t)
    52  	geth := runGeth(t, "account", "list", "--datadir", datadir)
    53  	defer geth.ExpectExit()
    54  	if runtime.GOOS == "windows" {
    55  		geth.Expect(`
    56  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    57  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
    58  Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
    59  `)
    60  	} else {
    61  		geth.Expect(`
    62  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    63  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
    64  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  	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  Password: {{.InputLine "foobar"}}
    76  Repeat password: {{.InputLine "foobar"}}
    77  
    78  Your new key was generated
    79  `)
    80  	geth.ExpectRegexp(`
    81  Public address of the key:   0x[0-9a-fA-F]{40}
    82  Path of the secret key file: .*UTC--.+--[0-9a-f]{40}
    83  
    84  - You can share your public address with anyone. Others need it to interact with you.
    85  - You must NEVER share the secret key with anyone! The key controls access to your funds!
    86  - You must BACKUP your key file! Without the key, it's impossible to access account funds!
    87  - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
    88  `)
    89  }
    90  
    91  func TestAccountNewBadRepeat(t *testing.T) {
    92  	geth := runGeth(t, "account", "new", "--lightkdf")
    93  	defer geth.ExpectExit()
    94  	geth.Expect(`
    95  Your new account is locked with a password. Please give a password. Do not forget this password.
    96  !! Unsupported terminal, password will be echoed.
    97  Password: {{.InputLine "something"}}
    98  Repeat password: {{.InputLine "something else"}}
    99  Fatal: Passwords do not match
   100  `)
   101  }
   102  
   103  func TestAccountUpdate(t *testing.T) {
   104  	datadir := tmpDatadirWithKeystore(t)
   105  	geth := runGeth(t, "account", "update",
   106  		"--datadir", datadir, "--lightkdf",
   107  		"f466859ead1932d743d622cb74fc058882e8648a")
   108  	defer geth.ExpectExit()
   109  	geth.Expect(`
   110  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   111  !! Unsupported terminal, password will be echoed.
   112  Password: {{.InputLine "foobar"}}
   113  Please give a new password. Do not forget this password.
   114  Password: {{.InputLine "foobar2"}}
   115  Repeat password: {{.InputLine "foobar2"}}
   116  `)
   117  }
   118  
   119  func TestWalletImport(t *testing.T) {
   120  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   121  	defer geth.ExpectExit()
   122  	geth.Expect(`
   123  !! Unsupported terminal, password will be echoed.
   124  Password: {{.InputLine "foo"}}
   125  Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
   126  `)
   127  
   128  	files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
   129  	if len(files) != 1 {
   130  		t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
   131  	}
   132  }
   133  
   134  func TestWalletImportBadPassword(t *testing.T) {
   135  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   136  	defer geth.ExpectExit()
   137  	geth.Expect(`
   138  !! Unsupported terminal, password will be echoed.
   139  Password: {{.InputLine "wrong"}}
   140  Fatal: could not decrypt key with given password
   141  `)
   142  }
   143  
   144  func TestUnlockFlag(t *testing.T) {
   145  	datadir := tmpDatadirWithKeystore(t)
   146  	geth := runGeth(t,
   147  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   148  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   149  		"js", "testdata/empty.js")
   150  	geth.Expect(`
   151  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   152  !! Unsupported terminal, password will be echoed.
   153  Password: {{.InputLine "foobar"}}
   154  `)
   155  	geth.ExpectExit()
   156  
   157  	wantMessages := []string{
   158  		"Unlocked account",
   159  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   160  	}
   161  	for _, m := range wantMessages {
   162  		if !strings.Contains(geth.StderrText(), m) {
   163  			t.Errorf("stderr text does not contain %q", m)
   164  		}
   165  	}
   166  }
   167  
   168  func TestUnlockFlagWrongPassword(t *testing.T) {
   169  	datadir := tmpDatadirWithKeystore(t)
   170  	geth := runGeth(t,
   171  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   172  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   173  	defer geth.ExpectExit()
   174  	geth.Expect(`
   175  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   176  !! Unsupported terminal, password will be echoed.
   177  Password: {{.InputLine "wrong1"}}
   178  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
   179  Password: {{.InputLine "wrong2"}}
   180  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
   181  Password: {{.InputLine "wrong3"}}
   182  Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given password)
   183  `)
   184  }
   185  
   186  // https://github.com/ethereum/go-ethereum/issues/1785
   187  func TestUnlockFlagMultiIndex(t *testing.T) {
   188  	datadir := tmpDatadirWithKeystore(t)
   189  	geth := runGeth(t,
   190  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   191  		"--unlock", "0,2",
   192  		"js", "testdata/empty.js")
   193  	geth.Expect(`
   194  Unlocking account 0 | Attempt 1/3
   195  !! Unsupported terminal, password will be echoed.
   196  Password: {{.InputLine "foobar"}}
   197  Unlocking account 2 | Attempt 1/3
   198  Password: {{.InputLine "foobar"}}
   199  `)
   200  	geth.ExpectExit()
   201  
   202  	wantMessages := []string{
   203  		"Unlocked account",
   204  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   205  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   206  	}
   207  	for _, m := range wantMessages {
   208  		if !strings.Contains(geth.StderrText(), m) {
   209  			t.Errorf("stderr text does not contain %q", m)
   210  		}
   211  	}
   212  }
   213  
   214  func TestUnlockFlagPasswordFile(t *testing.T) {
   215  	datadir := tmpDatadirWithKeystore(t)
   216  	geth := runGeth(t,
   217  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   218  		"--password", "testdata/passwords.txt", "--unlock", "0,2",
   219  		"js", "testdata/empty.js")
   220  	geth.ExpectExit()
   221  
   222  	wantMessages := []string{
   223  		"Unlocked account",
   224  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   225  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   226  	}
   227  	for _, m := range wantMessages {
   228  		if !strings.Contains(geth.StderrText(), m) {
   229  			t.Errorf("stderr text does not contain %q", m)
   230  		}
   231  	}
   232  }
   233  
   234  func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
   235  	datadir := tmpDatadirWithKeystore(t)
   236  	geth := runGeth(t,
   237  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   238  		"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
   239  	defer geth.ExpectExit()
   240  	geth.Expect(`
   241  Fatal: Failed to unlock account 0 (could not decrypt key with given password)
   242  `)
   243  }
   244  
   245  func TestUnlockFlagAmbiguous(t *testing.T) {
   246  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   247  	geth := runGeth(t,
   248  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   249  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   250  		"js", "testdata/empty.js")
   251  	defer geth.ExpectExit()
   252  
   253  	// Helper for the expect template, returns absolute keystore path.
   254  	geth.SetTemplateFunc("keypath", func(file string) string {
   255  		abs, _ := filepath.Abs(filepath.Join(store, file))
   256  		return abs
   257  	})
   258  	geth.Expect(`
   259  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   260  !! Unsupported terminal, password will be echoed.
   261  Password: {{.InputLine "foobar"}}
   262  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   263     keystore://{{keypath "1"}}
   264     keystore://{{keypath "2"}}
   265  Testing your password against all of them...
   266  Your password unlocked keystore://{{keypath "1"}}
   267  In order to avoid this warning, you need to remove the following duplicate key files:
   268     keystore://{{keypath "2"}}
   269  `)
   270  	geth.ExpectExit()
   271  
   272  	wantMessages := []string{
   273  		"Unlocked account",
   274  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   275  	}
   276  	for _, m := range wantMessages {
   277  		if !strings.Contains(geth.StderrText(), m) {
   278  			t.Errorf("stderr text does not contain %q", m)
   279  		}
   280  	}
   281  }
   282  
   283  func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
   284  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   285  	geth := runGeth(t,
   286  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   287  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   288  	defer geth.ExpectExit()
   289  
   290  	// Helper for the expect template, returns absolute keystore path.
   291  	geth.SetTemplateFunc("keypath", func(file string) string {
   292  		abs, _ := filepath.Abs(filepath.Join(store, file))
   293  		return abs
   294  	})
   295  	geth.Expect(`
   296  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   297  !! Unsupported terminal, password will be echoed.
   298  Password: {{.InputLine "wrong"}}
   299  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   300     keystore://{{keypath "1"}}
   301     keystore://{{keypath "2"}}
   302  Testing your password against all of them...
   303  Fatal: None of the listed files could be unlocked.
   304  `)
   305  	geth.ExpectExit()
   306  }