github.com/aquanetwork/aquachain@v1.7.8/cmd/aquachain/accountcmd_test.go (about)

     1  // Copyright 2016 The aquachain Authors
     2  // This file is part of aquachain.
     3  //
     4  // aquachain 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  // aquachain 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 aquachain. 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("..", "..", "aqua", "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  	aquachain := runAquaChain(t, "account", "list")
    47  	aquachain.ExpectExit()
    48  }
    49  
    50  func TestAccountList(t *testing.T) {
    51  	datadir := tmpDatadirWithKeystore(t)
    52  	aquachain := runAquaChain(t, "account", "list", "--datadir", datadir)
    53  	defer aquachain.ExpectExit()
    54  	if runtime.GOOS == "windows" {
    55  		aquachain.Expect(`
    56  Account #0: {0x7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    57  Account #1: {0xf466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
    58  Account #2: {0x289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
    59  `)
    60  	} else {
    61  		aquachain.Expect(`
    62  Account #0: {0x7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
    63  Account #1: {0xf466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
    64  Account #2: {0x289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz
    65  `)
    66  	}
    67  }
    68  
    69  func TestAccountNew(t *testing.T) {
    70  	aquachain := runAquaChain(t, "account", "new")
    71  	defer aquachain.ExpectExit()
    72  	aquachain.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  Passphrase: {{.InputLine "foobar"}}
    76  Repeat passphrase: {{.InputLine "foobar"}}
    77  `)
    78  	aquachain.ExpectRegexp(`Address: \{0x[0-9a-f]{40}\}\n`)
    79  }
    80  
    81  func TestAccountNewBadRepeat(t *testing.T) {
    82  	aquachain := runAquaChain(t, "account", "new")
    83  	defer aquachain.ExpectExit()
    84  	aquachain.Expect(`
    85  Your new account is locked with a password. Please give a password. Do not forget this password.
    86  !! Unsupported terminal, password will be echoed.
    87  Passphrase: {{.InputLine "something"}}
    88  Repeat passphrase: {{.InputLine "something else"}}
    89  Fatal: Passphrases do not match
    90  `)
    91  }
    92  
    93  func TestAccountUpdate(t *testing.T) {
    94  	datadir := tmpDatadirWithKeystore(t)
    95  	aquachain := runAquaChain(t, "account", "update",
    96  		"--datadir", datadir,
    97  		"f466859ead1932d743d622cb74fc058882e8648a")
    98  	defer aquachain.ExpectExit()
    99  	aquachain.Expect(`
   100  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   101  !! Unsupported terminal, password will be echoed.
   102  Passphrase: {{.InputLine "foobar"}}
   103  Please give a new password. Do not forget this password.
   104  Passphrase: {{.InputLine "foobar2"}}
   105  Repeat passphrase: {{.InputLine "foobar2"}}
   106  `)
   107  }
   108  
   109  func TestWalletImport(t *testing.T) {
   110  	t.Skip()
   111  	aquachain := runAquaChain(t, "wallet", "import", "testdata/guswallet.json")
   112  	defer aquachain.ExpectExit()
   113  	aquachain.Expect(`
   114  !! Unsupported terminal, password will be echoed.
   115  Passphrase: {{.InputLine "foo"}}
   116  Address: {0xd4584b5f6229b7be90727b0fc8c6b91bb427821f}
   117  `)
   118  
   119  	files, err := ioutil.ReadDir(filepath.Join(aquachain.Datadir, "keystore"))
   120  	if len(files) != 1 {
   121  		t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
   122  	}
   123  }
   124  
   125  func TestWalletImportBadPassword(t *testing.T) {
   126  	t.Skip()
   127  	aquachain := runAquaChain(t, "wallet", "import", "testdata/guswallet.json")
   128  	defer aquachain.ExpectExit()
   129  	aquachain.Expect(`
   130  !! Unsupported terminal, password will be echoed.
   131  Passphrase: {{.InputLine "wrong"}}
   132  Fatal: could not decrypt key with given passphrase
   133  `)
   134  }
   135  
   136  func TestUnlockFlag(t *testing.T) {
   137  	datadir := tmpDatadirWithKeystore(t)
   138  	aquachain := runAquaChain(t,
   139  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   140  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   141  		"js", "testdata/empty.js")
   142  	aquachain.Expect(`
   143  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   144  !! Unsupported terminal, password will be echoed.
   145  Passphrase: {{.InputLine "foobar"}}
   146  `)
   147  	aquachain.ExpectExit()
   148  
   149  	wantMessages := []string{
   150  		"Unlocked account",
   151  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   152  	}
   153  	for _, m := range wantMessages {
   154  		if !strings.Contains(aquachain.StderrText(), m) {
   155  			t.Errorf("stderr text does not contain %q", m)
   156  		}
   157  	}
   158  }
   159  
   160  func TestUnlockFlagWrongPassword(t *testing.T) {
   161  	datadir := tmpDatadirWithKeystore(t)
   162  	aquachain := runAquaChain(t,
   163  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   164  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   165  	defer aquachain.ExpectExit()
   166  	aquachain.Expect(`
   167  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   168  !! Unsupported terminal, password will be echoed.
   169  Passphrase: {{.InputLine "wrong1"}}
   170  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
   171  Passphrase: {{.InputLine "wrong2"}}
   172  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
   173  Passphrase: {{.InputLine "wrong3"}}
   174  Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
   175  `)
   176  }
   177  
   178  // https://gitlab.com/aquachain/aquachain/issues/1785
   179  func TestUnlockFlagMultiIndex(t *testing.T) {
   180  	datadir := tmpDatadirWithKeystore(t)
   181  	aquachain := runAquaChain(t,
   182  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   183  		"--unlock", "0,2",
   184  		"js", "testdata/empty.js")
   185  	aquachain.Expect(`
   186  Unlocking account 0 | Attempt 1/3
   187  !! Unsupported terminal, password will be echoed.
   188  Passphrase: {{.InputLine "foobar"}}
   189  Unlocking account 2 | Attempt 1/3
   190  Passphrase: {{.InputLine "foobar"}}
   191  `)
   192  	aquachain.ExpectExit()
   193  
   194  	wantMessages := []string{
   195  		"Unlocked account",
   196  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   197  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   198  	}
   199  	for _, m := range wantMessages {
   200  		if !strings.Contains(aquachain.StderrText(), m) {
   201  			t.Errorf("stderr text does not contain %q", m)
   202  		}
   203  	}
   204  }
   205  
   206  func TestUnlockFlagPasswordFile(t *testing.T) {
   207  	datadir := tmpDatadirWithKeystore(t)
   208  	aquachain := runAquaChain(t,
   209  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   210  		"--password", "testdata/passwords.txt", "--unlock", "0,2",
   211  		"js", "testdata/empty.js")
   212  	aquachain.ExpectExit()
   213  
   214  	wantMessages := []string{
   215  		"Unlocked account",
   216  		"=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
   217  		"=0x289d485D9771714CCe91D3393D764E1311907ACc",
   218  	}
   219  	for _, m := range wantMessages {
   220  		if !strings.Contains(aquachain.StderrText(), m) {
   221  			t.Errorf("stderr text does not contain %q", m)
   222  		}
   223  	}
   224  }
   225  
   226  func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
   227  	datadir := tmpDatadirWithKeystore(t)
   228  	aquachain := runAquaChain(t,
   229  		"--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   230  		"--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
   231  	defer aquachain.ExpectExit()
   232  	aquachain.Expect(`
   233  Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
   234  `)
   235  }
   236  
   237  func TestUnlockFlagAmbiguous(t *testing.T) {
   238  	store := filepath.Join("..", "..", "aqua", "accounts", "keystore", "testdata", "dupes")
   239  	aquachain := runAquaChain(t,
   240  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   241  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
   242  		"js", "testdata/empty.js")
   243  	defer aquachain.ExpectExit()
   244  
   245  	// Helper for the expect template, returns absolute keystore path.
   246  	aquachain.SetTemplateFunc("keypath", func(file string) string {
   247  		abs, _ := filepath.Abs(filepath.Join(store, file))
   248  		return abs
   249  	})
   250  	aquachain.Expect(`
   251  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   252  !! Unsupported terminal, password will be echoed.
   253  Passphrase: {{.InputLine "foobar"}}
   254  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   255     keystore://{{keypath "1"}}
   256     keystore://{{keypath "2"}}
   257  Testing your passphrase against all of them...
   258  Your passphrase unlocked keystore://{{keypath "1"}}
   259  In order to avoid this warning, you need to remove the following duplicate key files:
   260     keystore://{{keypath "2"}}
   261  `)
   262  	aquachain.ExpectExit()
   263  
   264  	wantMessages := []string{
   265  		"Unlocked account",
   266  		"=0xf466859eAD1932D743d622CB74FC058882E8648A",
   267  	}
   268  	for _, m := range wantMessages {
   269  		if !strings.Contains(aquachain.StderrText(), m) {
   270  			t.Errorf("stderr text does not contain %q", m)
   271  		}
   272  	}
   273  }
   274  
   275  func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
   276  	store := filepath.Join("..", "..", "aqua", "accounts", "keystore", "testdata", "dupes")
   277  	aquachain := runAquaChain(t,
   278  		"--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
   279  		"--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
   280  	defer aquachain.ExpectExit()
   281  
   282  	// Helper for the expect template, returns absolute keystore path.
   283  	aquachain.SetTemplateFunc("keypath", func(file string) string {
   284  		abs, _ := filepath.Abs(filepath.Join(store, file))
   285  		return abs
   286  	})
   287  	aquachain.Expect(`
   288  Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
   289  !! Unsupported terminal, password will be echoed.
   290  Passphrase: {{.InputLine "wrong"}}
   291  Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
   292     keystore://{{keypath "1"}}
   293     keystore://{{keypath "2"}}
   294  Testing your passphrase against all of them...
   295  Fatal: None of the listed files could be unlocked.
   296  `)
   297  	aquachain.ExpectExit()
   298  }