github.com/core-coin/go-core/v2@v2.1.9/accounts/keystore/plain_test.go (about)

     1  // Copyright 2014 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-core library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package keystore
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  	"reflect"
    27  	"testing"
    28  
    29  	"github.com/core-coin/go-core/v2/common"
    30  )
    31  
    32  func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
    33  	d, err := ioutil.TempDir("", "gocore-keystore-test")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if encrypted {
    38  		ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
    39  	} else {
    40  		ks = &keyStorePlain{d}
    41  	}
    42  	return d, ks
    43  }
    44  
    45  func TestKeyStorePlain(t *testing.T) {
    46  	dir, ks := tmpKeyStoreIface(t, false)
    47  	defer os.RemoveAll(dir)
    48  
    49  	pass := "" // not used but required by API
    50  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    59  		t.Fatal(err)
    60  	}
    61  	if !reflect.DeepEqual(k1.PrivateKey.PrivateKey(), k2.PrivateKey.PrivateKey()) {
    62  		t.Fatal(err)
    63  	}
    64  }
    65  
    66  func TestKeyStorePassphrase(t *testing.T) {
    67  	dir, ks := tmpKeyStoreIface(t, true)
    68  	defer os.RemoveAll(dir)
    69  
    70  	pass := "foo"
    71  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    80  		t.Fatal(err)
    81  	}
    82  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    83  		t.Fatal(err)
    84  	}
    85  }
    86  
    87  func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    88  	dir, ks := tmpKeyStoreIface(t, true)
    89  	defer os.RemoveAll(dir)
    90  
    91  	pass := "foo"
    92  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
    97  		t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
    98  	}
    99  }
   100  
   101  // Test and utils for the key store tests in the Core JSON tests;
   102  // testdataKeyStoreTests/basic_tests.json
   103  type KeyStoreTestV3 struct {
   104  	Json     encryptedKeyJSONV3
   105  	Password string
   106  	Priv     string
   107  }
   108  
   109  func TestV3_PBKDF2_1(t *testing.T) {
   110  	t.Parallel()
   111  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   112  	testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
   113  }
   114  
   115  var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
   116  
   117  func skipIfSubmoduleMissing(t *testing.T) {
   118  	if !common.FileExist(testsSubmodule) {
   119  		t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
   120  	}
   121  }
   122  
   123  func TestV3_PBKDF2_2(t *testing.T) {
   124  	skipIfSubmoduleMissing(t)
   125  	t.Parallel()
   126  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   127  	testDecryptV3(tests["test1"], t)
   128  }
   129  
   130  func TestV3_PBKDF2_3(t *testing.T) {
   131  	skipIfSubmoduleMissing(t)
   132  	t.Parallel()
   133  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   134  	testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
   135  }
   136  
   137  func TestV3_PBKDF2_4(t *testing.T) {
   138  	skipIfSubmoduleMissing(t)
   139  	t.Parallel()
   140  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   141  	testDecryptV3(tests["evilnonce"], t)
   142  }
   143  
   144  func TestV3_Scrypt_1(t *testing.T) {
   145  	t.Parallel()
   146  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   147  	testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
   148  }
   149  
   150  func TestV3_Scrypt_2(t *testing.T) {
   151  	skipIfSubmoduleMissing(t)
   152  	t.Parallel()
   153  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   154  	testDecryptV3(tests["test2"], t)
   155  }
   156  
   157  func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
   158  	privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	privHex := hex.EncodeToString(privBytes)
   163  	if test.Priv != privHex {
   164  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   165  	}
   166  }
   167  
   168  func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
   169  	tests := make(map[string]KeyStoreTestV3)
   170  	err := common.LoadJSON(file, &tests)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	return tests
   175  }
   176  
   177  func TestV3_31_Byte_Key(t *testing.T) {
   178  	t.Parallel()
   179  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   180  	testDecryptV3(tests["31_byte_key"], t)
   181  }
   182  
   183  func TestV3_30_Byte_Key(t *testing.T) {
   184  	t.Parallel()
   185  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   186  	testDecryptV3(tests["30_byte_key"], t)
   187  }