github.com/ethereum/go-ethereum@v1.16.1/cmd/clef/consolecmd_test.go (about)

     1  // Copyright 2022 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  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  // TestImportRaw tests clef --importraw
    28  func TestImportRaw(t *testing.T) {
    29  	t.Parallel()
    30  	keyPath := filepath.Join(t.TempDir(), fmt.Sprintf("%v-tempkey.test", t.Name()))
    31  	os.WriteFile(keyPath, []byte("0102030405060708090a0102030405060708090a0102030405060708090a0102"), 0777)
    32  
    33  	t.Run("happy-path", func(t *testing.T) {
    34  		t.Parallel()
    35  		// Run clef importraw
    36  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "importraw", keyPath)
    37  		clef.input("myverylongpassword").input("myverylongpassword")
    38  		if out := string(clef.Output()); !strings.Contains(out,
    39  			"Key imported:\n  Address 0x9160DC9105f7De5dC5E7f3d97ef11DA47269BdA6") {
    40  			t.Logf("Output\n%v", out)
    41  			t.Error("Failure")
    42  		}
    43  	})
    44  	// tests clef --importraw with mismatched passwords.
    45  	t.Run("pw-mismatch", func(t *testing.T) {
    46  		t.Parallel()
    47  		// Run clef importraw
    48  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "importraw", keyPath)
    49  		clef.input("myverylongpassword1").input("myverylongpassword2").WaitExit()
    50  		if have, want := clef.StderrText(), "Passwords do not match\n"; have != want {
    51  			t.Errorf("have %q, want %q", have, want)
    52  		}
    53  	})
    54  	// tests clef --importraw with a too short password.
    55  	t.Run("short-pw", func(t *testing.T) {
    56  		t.Parallel()
    57  		// Run clef importraw
    58  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "importraw", keyPath)
    59  		clef.input("shorty").input("shorty").WaitExit()
    60  		if have, want := clef.StderrText(),
    61  			"password requirements not met: password too short (<10 characters)\n"; have != want {
    62  			t.Errorf("have %q, want %q", have, want)
    63  		}
    64  	})
    65  }
    66  
    67  // TestListAccounts tests clef --list-accounts
    68  func TestListAccounts(t *testing.T) {
    69  	t.Parallel()
    70  	keyPath := filepath.Join(t.TempDir(), fmt.Sprintf("%v-tempkey.test", t.Name()))
    71  	os.WriteFile(keyPath, []byte("0102030405060708090a0102030405060708090a0102030405060708090a0102"), 0777)
    72  
    73  	t.Run("no-accounts", func(t *testing.T) {
    74  		t.Parallel()
    75  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "list-accounts")
    76  		if out := string(clef.Output()); !strings.Contains(out, "The keystore is empty.") {
    77  			t.Logf("Output\n%v", out)
    78  			t.Error("Failure")
    79  		}
    80  	})
    81  	t.Run("one-account", func(t *testing.T) {
    82  		t.Parallel()
    83  		// First, we need to import
    84  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "importraw", keyPath)
    85  		clef.input("myverylongpassword").input("myverylongpassword").WaitExit()
    86  		// Secondly, do a listing, using the same datadir
    87  		clef = runWithKeystore(t, clef.Datadir, "--suppress-bootwarn", "--lightkdf", "list-accounts")
    88  		if out := string(clef.Output()); !strings.Contains(out, "0x9160DC9105f7De5dC5E7f3d97ef11DA47269BdA6 (keystore:") {
    89  			t.Logf("Output\n%v", out)
    90  			t.Error("Failure")
    91  		}
    92  	})
    93  }
    94  
    95  // TestListWallets tests clef --list-wallets
    96  func TestListWallets(t *testing.T) {
    97  	t.Parallel()
    98  	keyPath := filepath.Join(t.TempDir(), fmt.Sprintf("%v-tempkey.test", t.Name()))
    99  	os.WriteFile(keyPath, []byte("0102030405060708090a0102030405060708090a0102030405060708090a0102"), 0777)
   100  
   101  	t.Run("no-accounts", func(t *testing.T) {
   102  		t.Parallel()
   103  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "list-wallets")
   104  		if out := string(clef.Output()); !strings.Contains(out, "There are no wallets.") {
   105  			t.Logf("Output\n%v", out)
   106  			t.Error("Failure")
   107  		}
   108  	})
   109  	t.Run("one-account", func(t *testing.T) {
   110  		t.Parallel()
   111  		// First, we need to import
   112  		clef := runClef(t, "--suppress-bootwarn", "--lightkdf", "importraw", keyPath)
   113  		clef.input("myverylongpassword").input("myverylongpassword").WaitExit()
   114  		// Secondly, do a listing, using the same datadir
   115  		clef = runWithKeystore(t, clef.Datadir, "--suppress-bootwarn", "--lightkdf", "list-wallets")
   116  		if out := string(clef.Output()); !strings.Contains(out, "Account 0: 0x9160DC9105f7De5dC5E7f3d97ef11DA47269BdA6") {
   117  			t.Logf("Output\n%v", out)
   118  			t.Error("Failure")
   119  		}
   120  	})
   121  }