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