github.com/JFJun/bsc@v1.0.0/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 TestAccountImport(t *testing.T) {
    92  	tests := []struct{ key, output string }{
    93  		{
    94  			key:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
    95  			output: "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n",
    96  		},
    97  		{
    98  			key:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1",
    99  			output: "Fatal: Failed to load the private key: invalid character '1' at end of key file\n",
   100  		},
   101  	}
   102  	for _, test := range tests {
   103  		importAccountWithExpect(t, test.key, test.output)
   104  	}
   105  }
   106  
   107  func importAccountWithExpect(t *testing.T, key string, expected string) {
   108  	dir := tmpdir(t)
   109  	keyfile := filepath.Join(dir, "key.prv")
   110  	if err := ioutil.WriteFile(keyfile, []byte(key), 0600); err != nil {
   111  		t.Error(err)
   112  	}
   113  	passwordFile := filepath.Join(dir, "password.txt")
   114  	if err := ioutil.WriteFile(passwordFile, []byte("foobar"), 0600); err != nil {
   115  		t.Error(err)
   116  	}
   117  	geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile)
   118  	defer geth.ExpectExit()
   119  	geth.Expect(expected)
   120  }
   121  
   122  func TestAccountNewBadRepeat(t *testing.T) {
   123  	geth := runGeth(t, "account", "new", "--lightkdf")
   124  	defer geth.ExpectExit()
   125  	geth.Expect(`
   126  Your new account is locked with a password. Please give a password. Do not forget this password.
   127  !! Unsupported terminal, password will be echoed.
   128  Password: {{.InputLine "something"}}
   129  Repeat password: {{.InputLine "something else"}}
   130  Fatal: Passwords do not match
   131  `)
   132  }
   133  
   134  func TestAccountUpdate(t *testing.T) {
   135  	datadir := tmpDatadirWithKeystore(t)
   136  	geth := runGeth(t, "account", "update",
   137  		"--datadir", datadir, "--lightkdf",
   138  		"f466859ead1932d743d622cb74fc058882e8648a")
   139  	defer geth.ExpectExit()
   140  	geth.Expect(`
   141  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   142  !! Unsupported terminal, password will be echoed.
   143  Password: {{.InputLine "foobar"}}
   144  Please give a new password. Do not forget this password.
   145  Password: {{.InputLine "foobar2"}}
   146  Repeat password: {{.InputLine "foobar2"}}
   147  `)
   148  }
   149  
   150  func TestWalletImport(t *testing.T) {
   151  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   152  	defer geth.ExpectExit()
   153  	geth.Expect(`
   154  !! Unsupported terminal, password will be echoed.
   155  Password: {{.InputLine "foo"}}
   156  Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
   157  `)
   158  
   159  	files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
   160  	if len(files) != 1 {
   161  		t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
   162  	}
   163  }
   164  
   165  func TestWalletImportBadPassword(t *testing.T) {
   166  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   167  	defer geth.ExpectExit()
   168  	geth.Expect(`
   169  !! Unsupported terminal, password will be echoed.
   170  Password: {{.InputLine "wrong"}}
   171  Fatal: could not decrypt key with given password
   172  `)
   173  }
   174  
   175  func TestUnlockFlag(t *testing.T) {
   176  	datadir := tmpDatadirWithKeystore(t)
   177  	geth := runGeth(t,
   178  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   179  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   180  		"js", "testdata/empty.js")
   181  	geth.Expect(`
   182  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   183  !! Unsupported terminal, password will be echoed.
   184  Password: {{.InputLine "foobar"}}
   185  `)
   186  	geth.ExpectExit()
   187  
   188  	wantMessages := []string{
   189  		"Unlocked account",
   190  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   191  	}
   192  	for _, m := range wantMessages {
   193  		if !strings.Contains(geth.StderrText(), m) {
   194  			t.Errorf("stderr text does not contain %q", m)
   195  		}
   196  	}
   197  }
   198  
   199  func TestUnlockFlagWrongPassword(t *testing.T) {
   200  	datadir := tmpDatadirWithKeystore(t)
   201  	geth := runGeth(t,
   202  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   203  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   204  	defer geth.ExpectExit()
   205  	geth.Expect(`
   206  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   207  !! Unsupported terminal, password will be echoed.
   208  Password: {{.InputLine "wrong1"}}
   209  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
   210  Password: {{.InputLine "wrong2"}}
   211  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
   212  Password: {{.InputLine "wrong3"}}
   213  Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given password)
   214  `)
   215  }
   216  
   217  // https://github.com/JFJun/bsc/issues/1785
   218  func TestUnlockFlagMultiIndex(t *testing.T) {
   219  	datadir := tmpDatadirWithKeystore(t)
   220  	geth := runGeth(t,
   221  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   222  		"--unlock", "0,2",
   223  		"js", "testdata/empty.js")
   224  	geth.Expect(`
   225  Unlocking account 0 | Attempt 1/3
   226  !! 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  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   236  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   237  	}
   238  	for _, m := range wantMessages {
   239  		if !strings.Contains(geth.StderrText(), m) {
   240  			t.Errorf("stderr text does not contain %q", m)
   241  		}
   242  	}
   243  }
   244  
   245  func TestUnlockFlagPasswordFile(t *testing.T) {
   246  	datadir := tmpDatadirWithKeystore(t)
   247  	geth := runGeth(t,
   248  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   249  		"--password", "testdata/passwords.txt", "--unlock", "0,2",
   250  		"js", "testdata/empty.js")
   251  	geth.ExpectExit()
   252  
   253  	wantMessages := []string{
   254  		"Unlocked account",
   255  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   256  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   257  	}
   258  	for _, m := range wantMessages {
   259  		if !strings.Contains(geth.StderrText(), m) {
   260  			t.Errorf("stderr text does not contain %q", m)
   261  		}
   262  	}
   263  }
   264  
   265  func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
   266  	datadir := tmpDatadirWithKeystore(t)
   267  	geth := runGeth(t,
   268  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   269  		"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
   270  	defer geth.ExpectExit()
   271  	geth.Expect(`
   272  Fatal: Failed to unlock account 0 (could not decrypt key with given password)
   273  `)
   274  }
   275  
   276  func TestUnlockFlagAmbiguous(t *testing.T) {
   277  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   278  	geth := runGeth(t,
   279  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   280  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   281  		"js", "testdata/empty.js")
   282  	defer geth.ExpectExit()
   283  
   284  	// Helper for the expect template, returns absolute keystore path.
   285  	geth.SetTemplateFunc("keypath", func(file string) string {
   286  		abs, _ := filepath.Abs(filepath.Join(store, file))
   287  		return abs
   288  	})
   289  	geth.Expect(`
   290  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   291  !! Unsupported terminal, password will be echoed.
   292  Password: {{.InputLine "foobar"}}
   293  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   294     keystore://{{keypath "1"}}
   295     keystore://{{keypath "2"}}
   296  Testing your password against all of them...
   297  Your password unlocked keystore://{{keypath "1"}}
   298  In order to avoid this warning, you need to remove the following duplicate key files:
   299     keystore://{{keypath "2"}}
   300  `)
   301  	geth.ExpectExit()
   302  
   303  	wantMessages := []string{
   304  		"Unlocked account",
   305  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   306  	}
   307  	for _, m := range wantMessages {
   308  		if !strings.Contains(geth.StderrText(), m) {
   309  			t.Errorf("stderr text does not contain %q", m)
   310  		}
   311  	}
   312  }
   313  
   314  func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
   315  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   316  	geth := runGeth(t,
   317  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   318  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   319  	defer geth.ExpectExit()
   320  
   321  	// Helper for the expect template, returns absolute keystore path.
   322  	geth.SetTemplateFunc("keypath", func(file string) string {
   323  		abs, _ := filepath.Abs(filepath.Join(store, file))
   324  		return abs
   325  	})
   326  	geth.Expect(`
   327  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   328  !! Unsupported terminal, password will be echoed.
   329  Password: {{.InputLine "wrong"}}
   330  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   331     keystore://{{keypath "1"}}
   332     keystore://{{keypath "2"}}
   333  Testing your password against all of them...
   334  Fatal: None of the listed files could be unlocked.
   335  `)
   336  	geth.ExpectExit()
   337  }