aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/key_store_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-03-03 08:15:42 +0800
committerFelix Lange <fjl@twurst.com>2016-04-12 21:58:07 +0800
commita9f26dcd0d14c0cb9f309ebccf81e8f741fc4636 (patch)
tree1a1e269194f3698737b0c1dbaf918c4e8dbae347 /accounts/key_store_test.go
parentef63e9af55fcfe3255dddec3197bd8a807152c66 (diff)
downloaddexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar.gz
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar.bz2
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar.lz
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar.xz
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.tar.zst
dexon-a9f26dcd0d14c0cb9f309ebccf81e8f741fc4636.zip
accounts: cache key addresses
In order to avoid disk thrashing for Accounts and HasAccount, address->key file mappings are now cached in memory. This makes it no longer necessary to keep the key address in the file name. The address of each key is derived from file content instead. There are minor user-visible changes: - "geth account list" now reports key file paths alongside the address. - If multiple keys are present for an address, unlocking by address is not possible. Users are directed to remove the duplicate files instead. Unlocking by index is still possible. - Key files are overwritten written in place when updating the password.
Diffstat (limited to 'accounts/key_store_test.go')
-rw-r--r--accounts/key_store_test.go103
1 files changed, 55 insertions, 48 deletions
diff --git a/accounts/key_store_test.go b/accounts/key_store_test.go
index 62ace3720..01bf1b50a 100644
--- a/accounts/key_store_test.go
+++ b/accounts/key_store_test.go
@@ -17,106 +17,107 @@
package accounts
import (
+ "crypto/rand"
"encoding/hex"
"fmt"
+ "io/ioutil"
+ "os"
"reflect"
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/crypto/randentropy"
)
+func tmpKeyStore(t *testing.T, encrypted bool) (dir string, ks keyStore) {
+ d, err := ioutil.TempDir("", "geth-keystore-test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if encrypted {
+ ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP}
+ } else {
+ ks = &keyStorePlain{d}
+ }
+ return d, ks
+}
+
func TestKeyStorePlain(t *testing.T) {
- ks := newKeyStorePlain(common.DefaultDataDir())
+ dir, ks := tmpKeyStore(t, false)
+ defer os.RemoveAll(dir)
+
pass := "" // not used but required by API
- k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
+ k1, account, err := storeNewKey(ks, rand.Reader, pass)
if err != nil {
t.Fatal(err)
}
-
- k2 := new(Key)
- k2, err = ks.GetKey(k1.Address, pass)
+ k2, err := ks.GetKey(k1.Address, account.File, pass)
if err != nil {
t.Fatal(err)
}
-
if !reflect.DeepEqual(k1.Address, k2.Address) {
t.Fatal(err)
}
-
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
t.Fatal(err)
}
-
- err = ks.DeleteKey(k2.Address, pass)
- if err != nil {
- t.Fatal(err)
- }
}
func TestKeyStorePassphrase(t *testing.T) {
- ks := newKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
+ dir, ks := tmpKeyStore(t, true)
+ defer os.RemoveAll(dir)
+
pass := "foo"
- k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
+ k1, account, err := storeNewKey(ks, rand.Reader, pass)
if err != nil {
t.Fatal(err)
}
- k2 := new(Key)
- k2, err = ks.GetKey(k1.Address, pass)
+ k2, err := ks.GetKey(k1.Address, account.File, pass)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(k1.Address, k2.Address) {
t.Fatal(err)
}
-
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
t.Fatal(err)
}
-
- err = ks.DeleteKey(k2.Address, pass) // also to clean up created files
- if err != nil {
- t.Fatal(err)
- }
}
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
- ks := newKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
+ dir, ks := tmpKeyStore(t, true)
+ defer os.RemoveAll(dir)
+
pass := "foo"
- k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
+ k1, account, err := storeNewKey(ks, rand.Reader, pass)
if err != nil {
t.Fatal(err)
}
-
- _, err = ks.GetKey(k1.Address, "bar") // wrong passphrase
- if err == nil {
- t.Fatal(err)
- }
-
- err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase
- if err == nil {
- t.Fatal(err)
- }
-
- err = ks.DeleteKey(k1.Address, pass) // to clean up
- if err != nil {
- t.Fatal(err)
+ if _, err = ks.GetKey(k1.Address, account.File, "bar"); err == nil {
+ t.Fatal("no error for invalid passphrase")
}
}
func TestImportPreSaleKey(t *testing.T) {
+ dir, ks := tmpKeyStore(t, true)
+ defer os.RemoveAll(dir)
+
// file content of a presale key file generated with:
// python pyethsaletool.py genwallet
// with password "foo"
fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
- ks := newKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
pass := "foo"
- _, err := importPreSaleKey(ks, []byte(fileContent), pass)
+ account, _, err := importPreSaleKey(ks, []byte(fileContent), pass)
if err != nil {
t.Fatal(err)
}
+ if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") {
+ t.Errorf("imported account has wrong address %x", account.Address)
+ }
+ if !strings.HasPrefix(account.File, dir) {
+ t.Errorf("imported account file not in keystore directory: %q", account.File)
+ }
}
// Test and utils for the key store tests in the Ethereum JSON tests;
@@ -134,51 +135,56 @@ type KeyStoreTestV1 struct {
}
func TestV3_PBKDF2_1(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
}
func TestV3_PBKDF2_2(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
testDecryptV3(tests["test1"], t)
}
func TestV3_PBKDF2_3(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
}
func TestV3_PBKDF2_4(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
testDecryptV3(tests["evilnonce"], t)
}
func TestV3_Scrypt_1(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
}
func TestV3_Scrypt_2(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
testDecryptV3(tests["test2"], t)
}
func TestV1_1(t *testing.T) {
+ t.Parallel()
tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t)
testDecryptV1(tests["test1"], t)
}
func TestV1_2(t *testing.T) {
- ks := newKeyStorePassphrase("testdata/v1", LightScryptN, LightScryptP)
+ t.Parallel()
+ ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP}
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
- k, err := ks.GetKey(addr, "g")
+ file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
+ k, err := ks.GetKey(addr, file, "g")
if err != nil {
t.Fatal(err)
}
- if k.Address != addr {
- t.Fatal(fmt.Errorf("Unexpected address: %v, expected %v", k.Address, addr))
- }
-
privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey))
expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
if privHex != expectedHex {
@@ -227,7 +233,8 @@ func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
}
func TestKeyForDirectICAP(t *testing.T) {
- key := NewKeyForDirectICAP(randentropy.Reader)
+ t.Parallel()
+ key := NewKeyForDirectICAP(rand.Reader)
if !strings.HasPrefix(key.Address.Hex(), "0x00") {
t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
}