github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/geth/accountcmd_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:28</date>
    10  //</624342590694625280>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"io/ioutil"
    17  	"path/filepath"
    18  	"runtime"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/cespare/cp"
    23  )
    24  
    25  //这些测试是与帐户相关的“烟雾测试”
    26  //子命令和标志。
    27  //
    28  //
    29  //复制到临时密钥库目录中。
    30  
    31  func tmpDatadirWithKeystore(t *testing.T) string {
    32  	datadir := tmpdir(t)
    33  	keystore := filepath.Join(datadir, "keystore")
    34  	source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore")
    35  	if err := cp.CopyAll(keystore, source); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	return datadir
    39  }
    40  
    41  func TestAccountListEmpty(t *testing.T) {
    42  	geth := runGeth(t, "account", "list")
    43  	geth.ExpectExit()
    44  }
    45  
    46  func TestAccountList(t *testing.T) {
    47  	datadir := tmpDatadirWithKeystore(t)
    48  	geth := runGeth(t, "account", "list", "--datadir", datadir)
    49  	defer geth.ExpectExit()
    50  	if runtime.GOOS == "windows" {
    51  		geth.Expect(`
    52  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://.datadir \keystore\utc--2016-03-22t12-57-55.920751759z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    53  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://.datadir \keystore\aaa
    54  Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://.datadir \keystore\zzz
    55  `)
    56  	} else {
    57  		geth.Expect(`
    58  Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://.datadir/keystore/utc--2016-03-22t12-57-55.920751759z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    59  Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://.datadir/keystore/aaa
    60  Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://.datadir/keystore/zzz
    61  `)
    62  	}
    63  }
    64  
    65  func TestAccountNew(t *testing.T) {
    66  	geth := runGeth(t, "account", "new", "--lightkdf")
    67  	defer geth.ExpectExit()
    68  	geth.Expect(`
    69  Your new account is locked with a password. Please give a password. Do not forget this password.
    70  !! Unsupported terminal, password will be echoed.
    71  Passphrase: {{.InputLine "foobar"}}
    72  Repeat passphrase: {{.InputLine "foobar"}}
    73  `)
    74  	geth.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`)
    75  }
    76  
    77  func TestAccountNewBadRepeat(t *testing.T) {
    78  	geth := runGeth(t, "account", "new", "--lightkdf")
    79  	defer geth.ExpectExit()
    80  	geth.Expect(`
    81  Your new account is locked with a password. Please give a password. Do not forget this password.
    82  !! Unsupported terminal, password will be echoed.
    83  Passphrase: {{.InputLine "something"}}
    84  Repeat passphrase: {{.InputLine "something else"}}
    85  Fatal: Passphrases do not match
    86  `)
    87  }
    88  
    89  func TestAccountUpdate(t *testing.T) {
    90  	datadir := tmpDatadirWithKeystore(t)
    91  	geth := runGeth(t, "account", "update",
    92  		"--datadir", datadir, "--lightkdf",
    93  		"f466859ead1932d743d622cb74fc058882e8648a")
    94  	defer geth.ExpectExit()
    95  	geth.Expect(`
    96  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
    97  !! Unsupported terminal, password will be echoed.
    98  Passphrase: {{.InputLine "foobar"}}
    99  Please give a new password. Do not forget this password.
   100  Passphrase: {{.InputLine "foobar2"}}
   101  Repeat passphrase: {{.InputLine "foobar2"}}
   102  `)
   103  }
   104  
   105  func TestWalletImport(t *testing.T) {
   106  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   107  	defer geth.ExpectExit()
   108  	geth.Expect(`
   109  !! Unsupported terminal, password will be echoed.
   110  Passphrase: {{.InputLine "foo"}}
   111  Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
   112  `)
   113  
   114  	files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
   115  	if len(files) != 1 {
   116  		t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
   117  	}
   118  }
   119  
   120  func TestWalletImportBadPassword(t *testing.T) {
   121  	geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
   122  	defer geth.ExpectExit()
   123  	geth.Expect(`
   124  !! Unsupported terminal, password will be echoed.
   125  Passphrase: {{.InputLine "wrong"}}
   126  Fatal: could not decrypt key with given passphrase
   127  `)
   128  }
   129  
   130  func TestUnlockFlag(t *testing.T) {
   131  	datadir := tmpDatadirWithKeystore(t)
   132  	geth := runGeth(t,
   133  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   134  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   135  		"js", "testdata/empty.js")
   136  	geth.Expect(`
   137  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   138  !! Unsupported terminal, password will be echoed.
   139  Passphrase: {{.InputLine "foobar"}}
   140  `)
   141  	geth.ExpectExit()
   142  
   143  	wantMessages := []string{
   144  		"Unlocked account",
   145  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   146  	}
   147  	for _, m := range wantMessages {
   148  		if !strings.Contains(geth.StderrText(), m) {
   149  			t.Errorf("stderr text does not contain %q", m)
   150  		}
   151  	}
   152  }
   153  
   154  func TestUnlockFlagWrongPassword(t *testing.T) {
   155  	datadir := tmpDatadirWithKeystore(t)
   156  	geth := runGeth(t,
   157  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   158  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   159  	defer geth.ExpectExit()
   160  	geth.Expect(`
   161  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   162  !! Unsupported terminal, password will be echoed.
   163  Passphrase: {{.InputLine "wrong1"}}
   164  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
   165  Passphrase: {{.InputLine "wrong2"}}
   166  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
   167  Passphrase: {{.InputLine "wrong3"}}
   168  Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
   169  `)
   170  }
   171  
   172  //
   173  func TestUnlockFlagMultiIndex(t *testing.T) {
   174  	datadir := tmpDatadirWithKeystore(t)
   175  	geth := runGeth(t,
   176  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   177  		"--unlock", "0,2",
   178  		"js", "testdata/empty.js")
   179  	geth.Expect(`
   180  Unlocking account 0 | Attempt 1/3
   181  !! Unsupported terminal, password will be echoed.
   182  Passphrase: {{.InputLine "foobar"}}
   183  Unlocking account 2 | Attempt 1/3
   184  Passphrase: {{.InputLine "foobar"}}
   185  `)
   186  	geth.ExpectExit()
   187  
   188  	wantMessages := []string{
   189  		"Unlocked account",
   190  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   191  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   192  	}
   193  	for _, m := range wantMessages {
   194  		if !strings.Contains(geth.StderrText(), m) {
   195  			t.Errorf("stderr text does not contain %q", m)
   196  		}
   197  	}
   198  }
   199  
   200  func TestUnlockFlagPasswordFile(t *testing.T) {
   201  	datadir := tmpDatadirWithKeystore(t)
   202  	geth := runGeth(t,
   203  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   204  		"--password", "testdata/passwords.txt", "--unlock", "0,2",
   205  		"js", "testdata/empty.js")
   206  	geth.ExpectExit()
   207  
   208  	wantMessages := []string{
   209  		"Unlocked account",
   210  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   211  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   212  	}
   213  	for _, m := range wantMessages {
   214  		if !strings.Contains(geth.StderrText(), m) {
   215  			t.Errorf("stderr text does not contain %q", m)
   216  		}
   217  	}
   218  }
   219  
   220  func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
   221  	datadir := tmpDatadirWithKeystore(t)
   222  	geth := runGeth(t,
   223  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   224  		"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
   225  	defer geth.ExpectExit()
   226  	geth.Expect(`
   227  Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
   228  `)
   229  }
   230  
   231  func TestUnlockFlagAmbiguous(t *testing.T) {
   232  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   233  	geth := runGeth(t,
   234  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   235  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   236  		"js", "testdata/empty.js")
   237  	defer geth.ExpectExit()
   238  
   239  //Helper对于Expect模板,返回绝对密钥存储路径。
   240  	geth.SetTemplateFunc("keypath", func(file string) string {
   241  		abs, _ := filepath.Abs(filepath.Join(store, file))
   242  		return abs
   243  	})
   244  	geth.Expect(`
   245  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   246  !! Unsupported terminal, password will be echoed.
   247  Passphrase: {{.InputLine "foobar"}}
   248  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   249  keystore://
   250  keystore://
   251  Testing your passphrase against all of them...
   252  Your passphrase unlocked keystore://{{关键路径“1”} }
   253  In order to avoid this warning, you need to remove the following duplicate key files:
   254  keystore://{{关键路径“2”} }
   255  `)
   256  	geth.ExpectExit()
   257  
   258  	wantMessages := []string{
   259  		"Unlocked account",
   260  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   261  	}
   262  	for _, m := range wantMessages {
   263  		if !strings.Contains(geth.StderrText(), m) {
   264  			t.Errorf("stderr text does not contain %q", m)
   265  		}
   266  	}
   267  }
   268  
   269  func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
   270  	store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
   271  	geth := runGeth(t,
   272  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   273  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   274  	defer geth.ExpectExit()
   275  
   276  //Helper对于Expect模板,返回绝对密钥存储路径。
   277  	geth.SetTemplateFunc("keypath", func(file string) string {
   278  		abs, _ := filepath.Abs(filepath.Join(store, file))
   279  		return abs
   280  	})
   281  	geth.Expect(`
   282  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   283  !! Unsupported terminal, password will be echoed.
   284  Passphrase: {{.InputLine "wrong"}}
   285  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   286  keystore://{{关键路径“1”} }
   287  keystore://{{关键路径“2”} }
   288  Testing your passphrase against all of them...
   289  Fatal: None of the listed files could be unlocked.
   290  `)
   291  	geth.ExpectExit()
   292  }
   293