github.com/codingfuture/orig-energi3@v0.8.4/accounts/keystore/plain_test.go (about)

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