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