gitlab.com/flarenetwork/coreth@v0.1.1/accounts/keystore/plain_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2014 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package keystore
    28  
    29  import (
    30  	"crypto/rand"
    31  	"encoding/hex"
    32  	"fmt"
    33  	"io/ioutil"
    34  	"os"
    35  	"path/filepath"
    36  	"reflect"
    37  	"strings"
    38  	"testing"
    39  
    40  	"github.com/ethereum/go-ethereum/common"
    41  )
    42  
    43  func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
    44  	d, err := ioutil.TempDir("", "geth-keystore-test")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if encrypted {
    49  		ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
    50  	} else {
    51  		ks = &keyStorePlain{d}
    52  	}
    53  	return d, ks
    54  }
    55  
    56  func TestKeyStorePlain(t *testing.T) {
    57  	dir, ks := tmpKeyStoreIface(t, false)
    58  	defer os.RemoveAll(dir)
    59  
    60  	pass := "" // not used but required by API
    61  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    70  		t.Fatal(err)
    71  	}
    72  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    73  		t.Fatal(err)
    74  	}
    75  }
    76  
    77  func TestKeyStorePassphrase(t *testing.T) {
    78  	dir, ks := tmpKeyStoreIface(t, true)
    79  	defer os.RemoveAll(dir)
    80  
    81  	pass := "foo"
    82  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    91  		t.Fatal(err)
    92  	}
    93  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    94  		t.Fatal(err)
    95  	}
    96  }
    97  
    98  func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    99  	dir, ks := tmpKeyStoreIface(t, true)
   100  	defer os.RemoveAll(dir)
   101  
   102  	pass := "foo"
   103  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
   108  		t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
   109  	}
   110  }
   111  
   112  func TestImportPreSaleKey(t *testing.T) {
   113  	dir, ks := tmpKeyStoreIface(t, true)
   114  	defer os.RemoveAll(dir)
   115  
   116  	// file content of a presale key file generated with:
   117  	// python pyethsaletool.py genwallet
   118  	// with password "foo"
   119  	fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
   120  	pass := "foo"
   121  	account, _, err := importPreSaleKey(ks, []byte(fileContent), pass)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") {
   126  		t.Errorf("imported account has wrong address %x", account.Address)
   127  	}
   128  	if !strings.HasPrefix(account.URL.Path, dir) {
   129  		t.Errorf("imported account file not in keystore directory: %q", account.URL)
   130  	}
   131  }
   132  
   133  // Test and utils for the key store tests in the Ethereum JSON tests;
   134  // testdataKeyStoreTests/basic_tests.json
   135  type KeyStoreTestV3 struct {
   136  	Json     encryptedKeyJSONV3
   137  	Password string
   138  	Priv     string
   139  }
   140  
   141  type KeyStoreTestV1 struct {
   142  	Json     encryptedKeyJSONV1
   143  	Password string
   144  	Priv     string
   145  }
   146  
   147  func TestV3_PBKDF2_1(t *testing.T) {
   148  	t.Parallel()
   149  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   150  	testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
   151  }
   152  
   153  var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
   154  
   155  func skipIfSubmoduleMissing(t *testing.T) {
   156  	if !common.FileExist(testsSubmodule) {
   157  		t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
   158  	}
   159  }
   160  
   161  func TestV3_PBKDF2_2(t *testing.T) {
   162  	skipIfSubmoduleMissing(t)
   163  	t.Parallel()
   164  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   165  	testDecryptV3(tests["test1"], t)
   166  }
   167  
   168  func TestV3_PBKDF2_3(t *testing.T) {
   169  	skipIfSubmoduleMissing(t)
   170  	t.Parallel()
   171  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   172  	testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
   173  }
   174  
   175  func TestV3_PBKDF2_4(t *testing.T) {
   176  	skipIfSubmoduleMissing(t)
   177  	t.Parallel()
   178  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   179  	testDecryptV3(tests["evilnonce"], t)
   180  }
   181  
   182  func TestV3_Scrypt_1(t *testing.T) {
   183  	t.Parallel()
   184  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   185  	testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
   186  }
   187  
   188  func TestV3_Scrypt_2(t *testing.T) {
   189  	skipIfSubmoduleMissing(t)
   190  	t.Parallel()
   191  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   192  	testDecryptV3(tests["test2"], t)
   193  }
   194  
   195  func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
   196  	privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	privHex := hex.EncodeToString(privBytes)
   201  	if test.Priv != privHex {
   202  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   203  	}
   204  }
   205  
   206  func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
   207  	tests := make(map[string]KeyStoreTestV3)
   208  	err := common.LoadJSON(file, &tests)
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  	return tests
   213  }
   214  
   215  func TestKeyForDirectICAP(t *testing.T) {
   216  	t.Parallel()
   217  	key := NewKeyForDirectICAP(rand.Reader)
   218  	if !strings.HasPrefix(key.Address.Hex(), "0x00") {
   219  		t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
   220  	}
   221  }
   222  
   223  func TestV3_31_Byte_Key(t *testing.T) {
   224  	t.Parallel()
   225  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   226  	testDecryptV3(tests["31_byte_key"], t)
   227  }
   228  
   229  func TestV3_30_Byte_Key(t *testing.T) {
   230  	t.Parallel()
   231  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   232  	testDecryptV3(tests["30_byte_key"], t)
   233  }