github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/geth/accountcmd_test.go (about)

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