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