github.com/klaytn/klaytn@v1.10.2/accounts/keystore/keystore_plain_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from accounts/keystore/keystore_plain_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package keystore
    22  
    23  import (
    24  	"crypto/rand"
    25  	"encoding/hex"
    26  	"fmt"
    27  	"io/ioutil"
    28  	"os"
    29  	"path/filepath"
    30  	"reflect"
    31  	"strings"
    32  	"testing"
    33  
    34  	"github.com/klaytn/klaytn/common"
    35  	"github.com/klaytn/klaytn/crypto"
    36  )
    37  
    38  func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
    39  	d, err := ioutil.TempDir("", "klay-keystore-test")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if encrypted {
    44  		ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
    45  	} else {
    46  		ks = &keyStorePlain{d}
    47  	}
    48  	return d, ks
    49  }
    50  
    51  func TestKeyStorePlain(t *testing.T) {
    52  	dir, ks := tmpKeyStoreIface(t, false)
    53  	defer os.RemoveAll(dir)
    54  
    55  	pass := "" // not used but required by API
    56  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	k2, err := ks.GetKey(k1.GetAddress(), account.URL.Path, pass)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	if !reflect.DeepEqual(k1.GetAddress(), k2.GetAddress()) {
    65  		t.Fatal(err)
    66  	}
    67  	if !reflect.DeepEqual(k1.GetPrivateKeys(), k2.GetPrivateKeys()) {
    68  		t.Fatal(err)
    69  	}
    70  }
    71  
    72  func TestKeyStorePassphrase(t *testing.T) {
    73  	dir, ks := tmpKeyStoreIface(t, true)
    74  	defer os.RemoveAll(dir)
    75  
    76  	pass := "foo"
    77  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	k2, err := ks.GetKey(k1.GetAddress(), account.URL.Path, pass)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if !reflect.DeepEqual(k1.GetAddress(), k2.GetAddress()) {
    86  		t.Fatal(err)
    87  	}
    88  	if !reflect.DeepEqual(k1.GetPrivateKeys(), k2.GetPrivateKeys()) {
    89  		t.Fatal(err)
    90  	}
    91  }
    92  
    93  func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    94  	dir, ks := tmpKeyStoreIface(t, true)
    95  	defer os.RemoveAll(dir)
    96  
    97  	pass := "foo"
    98  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if _, err = ks.GetKey(k1.GetAddress(), account.URL.Path, "bar"); err != ErrDecrypt {
   103  		t.Fatalf("wrong error for invalid passphrase\ngot %q\nwant %q", err, ErrDecrypt)
   104  	}
   105  }
   106  
   107  // Test and utils for the key store tests in the Klaytn JSON tests;
   108  // testdataKeyStoreTests/basic_tests.json
   109  type KeyStoreTestV3 struct {
   110  	Json     encryptedKeyJSONV3
   111  	Password string
   112  	Priv     string
   113  }
   114  
   115  type KeyStoreTestV1 struct {
   116  	Json     encryptedKeyJSONV1
   117  	Password string
   118  	Priv     string
   119  }
   120  
   121  func TestV3_PBKDF2_1(t *testing.T) {
   122  	t.Parallel()
   123  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   124  	testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
   125  }
   126  
   127  var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
   128  
   129  func skipIfSubmoduleMissing(t *testing.T) {
   130  	if !common.FileExist(testsSubmodule) {
   131  		t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
   132  	}
   133  }
   134  
   135  func TestV3_PBKDF2_2(t *testing.T) {
   136  	skipIfSubmoduleMissing(t)
   137  	t.Parallel()
   138  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   139  	testDecryptV3(tests["test1"], t)
   140  }
   141  
   142  func TestV3_PBKDF2_3(t *testing.T) {
   143  	skipIfSubmoduleMissing(t)
   144  	t.Parallel()
   145  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   146  	testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
   147  }
   148  
   149  func TestV3_PBKDF2_4(t *testing.T) {
   150  	skipIfSubmoduleMissing(t)
   151  	t.Parallel()
   152  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   153  	testDecryptV3(tests["evilnonce"], t)
   154  }
   155  
   156  func TestV3_Scrypt_1(t *testing.T) {
   157  	t.Parallel()
   158  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   159  	testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
   160  }
   161  
   162  func TestV3_Scrypt_2(t *testing.T) {
   163  	skipIfSubmoduleMissing(t)
   164  	t.Parallel()
   165  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   166  	testDecryptV3(tests["test2"], t)
   167  }
   168  
   169  func TestV1_1(t *testing.T) {
   170  	t.Parallel()
   171  	tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t)
   172  	testDecryptV1(tests["test1"], t)
   173  }
   174  
   175  func TestV1_2(t *testing.T) {
   176  	t.Parallel()
   177  	ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true}
   178  	addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
   179  	file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
   180  	k, err := ks.GetKey(addr, file, "g")
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	privHex := hex.EncodeToString(crypto.FromECDSA(k.GetPrivateKey()))
   185  	expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
   186  	if privHex != expectedHex {
   187  		t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
   188  	}
   189  }
   190  
   191  func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
   192  	privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
   193  	if err != nil {
   194  		t.Fatal(err)
   195  	}
   196  	privHex := hex.EncodeToString(privBytes)
   197  	if test.Priv != privHex {
   198  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   199  	}
   200  }
   201  
   202  func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
   203  	privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	privHex := hex.EncodeToString(privBytes)
   208  	if test.Priv != privHex {
   209  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   210  	}
   211  }
   212  
   213  func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
   214  	tests := make(map[string]KeyStoreTestV3)
   215  	err := common.LoadJSON(file, &tests)
   216  	if err != nil {
   217  		t.Fatal(err)
   218  	}
   219  	return tests
   220  }
   221  
   222  func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
   223  	tests := make(map[string]KeyStoreTestV1)
   224  	err := common.LoadJSON(file, &tests)
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  	return tests
   229  }
   230  
   231  func TestKeyForDirectICAP(t *testing.T) {
   232  	t.Parallel()
   233  	key := NewKeyForDirectICAP(rand.Reader)
   234  	if !strings.HasPrefix(key.GetAddress().Hex(), "0x00") {
   235  		t.Errorf("Expected first address byte to be zero, have: %s", key.GetAddress().Hex())
   236  	}
   237  }
   238  
   239  func TestV3_31_Byte_Key(t *testing.T) {
   240  	t.Parallel()
   241  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   242  	testDecryptV3(tests["31_byte_key"], t)
   243  }
   244  
   245  func TestV3_30_Byte_Key(t *testing.T) {
   246  	t.Parallel()
   247  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   248  	testDecryptV3(tests["30_byte_key"], t)
   249  }