github.com/klaytn/klaytn@v1.10.2/cmd/utils/nodecmd/accountcmd_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // go-ethereum is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from cmd/geth/accountcmd_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package nodecmd
    22  
    23  import (
    24  	"path/filepath"
    25  	"runtime"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/cespare/cp"
    30  )
    31  
    32  // These tests are 'smoke tests' for the account related
    33  // subcommands and flags.
    34  //
    35  // For most tests, the test files from package accounts
    36  // are copied into a temporary keystore directory.
    37  
    38  func tmpDatadirWithKeystore(t *testing.T) string {
    39  	datadir := tmpdir(t)
    40  	keystore := filepath.Join(datadir, "keystore")
    41  	source := filepath.Join("..", "..", "..", "accounts", "keystore", "testdata", "keystore")
    42  	if err := cp.CopyAll(keystore, source); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	return datadir
    46  }
    47  
    48  func TestAccountListEmpty(t *testing.T) {
    49  	klay := runKlay(t, "klay-test", "account", "list")
    50  	klay.ExpectExit()
    51  }
    52  
    53  func TestAccountList(t *testing.T) {
    54  	datadir := tmpDatadirWithKeystore(t)
    55  	klay := runKlay(t, "klay-test", "account", "list", "--datadir", datadir)
    56  	defer klay.ExpectExit()
    57  	if runtime.GOOS == "windows" {
    58  		klay.Expect(`
    59  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    60  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
    61  Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
    62  `)
    63  	} else {
    64  		klay.Expect(`
    65  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    66  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
    67  Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz
    68  `)
    69  	}
    70  }
    71  
    72  func TestAccountNew(t *testing.T) {
    73  	klay := runKlay(t, "klay-test", "account", "new", "--lightkdf")
    74  	defer klay.ExpectExit()
    75  	klay.Expect(`
    76  Your new account is locked with a password. Please give a password. Do not forget this password.
    77  !! Unsupported terminal, password will be echoed.
    78  Passphrase: {{.InputLine "foobar"}}
    79  Repeat passphrase: {{.InputLine "foobar"}}
    80  `)
    81  	klay.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`)
    82  }
    83  
    84  func TestAccountNewBadRepeat(t *testing.T) {
    85  	klay := runKlay(t, "klay-test", "account", "new", "--lightkdf")
    86  	defer klay.ExpectExit()
    87  	klay.Expect(`
    88  Your new account is locked with a password. Please give a password. Do not forget this password.
    89  !! Unsupported terminal, password will be echoed.
    90  Passphrase: {{.InputLine "something"}}
    91  Repeat passphrase: {{.InputLine "something else"}}
    92  Fatal: Passphrases do not match
    93  `)
    94  }
    95  
    96  func TestAccountUpdate(t *testing.T) {
    97  	datadir := tmpDatadirWithKeystore(t)
    98  	klay := runKlay(t, "klay-test", "account", "update",
    99  		"--datadir", datadir, "--lightkdf",
   100  		"f466859ead1932d743d622cb74fc058882e8648a")
   101  	defer klay.ExpectExit()
   102  	klay.Expect(`
   103  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   104  !! Unsupported terminal, password will be echoed.
   105  Passphrase: {{.InputLine "foobar"}}
   106  Please give a new password. Do not forget this password.
   107  Passphrase: {{.InputLine "foobar2"}}
   108  Repeat passphrase: {{.InputLine "foobar2"}}
   109  `)
   110  }
   111  
   112  func TestUnlockFlag(t *testing.T) {
   113  	datadir := tmpDatadirWithKeystore(t)
   114  	klay := runKlay(t, "klay-test",
   115  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   116  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   117  		"js", "testdata/empty.js")
   118  	klay.Expect(`
   119  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   120  !! Unsupported terminal, password will be echoed.
   121  Passphrase: {{.InputLine "foobar"}}
   122  `)
   123  	klay.ExpectExit()
   124  
   125  	wantMessages := []string{
   126  		"Unlocked account",
   127  		"0xf466859eAD1932D743d622CB74FC058882E8648A",
   128  	}
   129  	for _, m := range wantMessages {
   130  		if !strings.Contains(klay.StderrText(), m) {
   131  			t.Errorf("stderr text does not contain %q", m)
   132  		}
   133  	}
   134  }
   135  
   136  func TestUnlockFlagWrongPassword(t *testing.T) {
   137  	datadir := tmpDatadirWithKeystore(t)
   138  	klay := runKlay(t, "klay-test",
   139  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   140  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   141  	defer klay.ExpectExit()
   142  	klay.Expect(`
   143  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   144  !! Unsupported terminal, password will be echoed.
   145  Passphrase: {{.InputLine "wrong1"}}
   146  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
   147  Passphrase: {{.InputLine "wrong2"}}
   148  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
   149  Passphrase: {{.InputLine "wrong3"}}
   150  Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
   151  `)
   152  }
   153  
   154  // https://github.com/ethereum/go-ethereum/issues/1785
   155  func TestUnlockFlagMultiIndex(t *testing.T) {
   156  	datadir := tmpDatadirWithKeystore(t)
   157  	klay := runKlay(t, "klay-test",
   158  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   159  		"--unlock", "0,2",
   160  		"js", "testdata/empty.js")
   161  	klay.Expect(`
   162  Unlocking account 0 | Attempt 1/3
   163  !! Unsupported terminal, password will be echoed.
   164  Passphrase: {{.InputLine "foobar"}}
   165  Unlocking account 2 | Attempt 1/3
   166  Passphrase: {{.InputLine "foobar"}}
   167  `)
   168  	klay.ExpectExit()
   169  
   170  	wantMessages := []string{
   171  		"Unlocked account",
   172  		"0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   173  		"0x289d485D9771714CCe91D3393D764E1311907ACc",
   174  	}
   175  	for _, m := range wantMessages {
   176  		if !strings.Contains(klay.StderrText(), m) {
   177  			t.Errorf("stderr text does not contain %q", m)
   178  		}
   179  	}
   180  }
   181  
   182  func TestUnlockFlagPasswordFile(t *testing.T) {
   183  	datadir := tmpDatadirWithKeystore(t)
   184  	klay := runKlay(t, "klay-test",
   185  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   186  		"--password", "testdata/passwords.txt", "--unlock", "0,2",
   187  		"js", "testdata/empty.js")
   188  	klay.ExpectExit()
   189  
   190  	wantMessages := []string{
   191  		"Unlocked account",
   192  		"0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   193  		"0x289d485D9771714CCe91D3393D764E1311907ACc",
   194  	}
   195  	for _, m := range wantMessages {
   196  		if !strings.Contains(klay.StderrText(), m) {
   197  			t.Errorf("stderr text does not contain %q", m)
   198  		}
   199  	}
   200  }
   201  
   202  func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
   203  	datadir := tmpDatadirWithKeystore(t)
   204  	klay := runKlay(t, "klay-test",
   205  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   206  		"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
   207  	defer klay.ExpectExit()
   208  	klay.Expect(`
   209  Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
   210  `)
   211  }
   212  
   213  func TestUnlockFlagAmbiguous(t *testing.T) {
   214  	store := filepath.Join("..", "..", "..", "accounts", "keystore", "testdata", "dupes")
   215  	klay := runKlay(t, "klay-test",
   216  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   217  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   218  		"js", "testdata/empty.js")
   219  	defer klay.ExpectExit()
   220  
   221  	// Helper for the expect template, returns absolute keystore path.
   222  	klay.SetTemplateFunc("keypath", func(file string) string {
   223  		abs, _ := filepath.Abs(filepath.Join(store, file))
   224  		return abs
   225  	})
   226  	klay.Expect(`
   227  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   228  !! Unsupported terminal, password will be echoed.
   229  Passphrase: {{.InputLine "foobar"}}
   230  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   231     keystore://{{keypath "1"}}
   232     keystore://{{keypath "2"}}
   233  Testing your passphrase against all of them...
   234  Your passphrase unlocked keystore://{{keypath "1"}}
   235  In order to avoid this warning, you need to remove the following duplicate key files:
   236     keystore://{{keypath "2"}}
   237  `)
   238  	klay.ExpectExit()
   239  
   240  	wantMessages := []string{
   241  		"Unlocked account",
   242  		"0xf466859eAD1932D743d622CB74FC058882E8648A",
   243  	}
   244  	for _, m := range wantMessages {
   245  		if !strings.Contains(klay.StderrText(), m) {
   246  			t.Errorf("stderr text does not contain %q", m)
   247  		}
   248  	}
   249  }
   250  
   251  func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
   252  	store := filepath.Join("..", "..", "..", "accounts", "keystore", "testdata", "dupes")
   253  	klay := runKlay(t, "klay-test",
   254  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxconnections", "0", "--port", "0",
   255  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   256  	defer klay.ExpectExit()
   257  
   258  	// Helper for the expect template, returns absolute keystore path.
   259  	klay.SetTemplateFunc("keypath", func(file string) string {
   260  		abs, _ := filepath.Abs(filepath.Join(store, file))
   261  		return abs
   262  	})
   263  	klay.Expect(`
   264  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   265  !! Unsupported terminal, password will be echoed.
   266  Passphrase: {{.InputLine "wrong"}}
   267  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   268     keystore://{{keypath "1"}}
   269     keystore://{{keypath "2"}}
   270  Testing your passphrase against all of them...
   271  Fatal: None of the listed files could be unlocked.
   272  `)
   273  	klay.ExpectExit()
   274  }