aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/accounts_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-07-20 23:42:14 +0800
committerFelix Lange <fjl@twurst.com>2015-07-21 00:04:23 +0800
commit7662dd9bbbe5ced9aee33a734a754e90fb73d376 (patch)
tree75d1c72bb5af783c0e7dcbbb9e66f4d43e448df3 /accounts/accounts_test.go
parent02c5022742e2bf6d2aadca06a6a1655214ba9d55 (diff)
downloadgo-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar.gz
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar.bz2
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar.lz
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar.xz
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.tar.zst
go-tangerine-7662dd9bbbe5ced9aee33a734a754e90fb73d376.zip
accounts: fix data race when key is locked after the unlock timeout
While here, also improve the docs and speed up the tests. The tests used the scrypt keystore with ridiculous settins and took 20s each.
Diffstat (limited to 'accounts/accounts_test.go')
-rw-r--r--accounts/accounts_test.go52
1 files changed, 37 insertions, 15 deletions
diff --git a/accounts/accounts_test.go b/accounts/accounts_test.go
index 9d09156af..0be8c50bd 100644
--- a/accounts/accounts_test.go
+++ b/accounts/accounts_test.go
@@ -23,9 +23,10 @@ import (
"time"
"github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/crypto/randentropy"
)
+var testSigData = make([]byte, 32)
+
func TestSign(t *testing.T) {
dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
defer os.RemoveAll(dir)
@@ -33,26 +34,24 @@ func TestSign(t *testing.T) {
am := NewManager(ks)
pass := "" // not used but required by API
a1, err := am.NewAccount(pass)
- toSign := randentropy.GetEntropyCSPRNG(32)
am.Unlock(a1.Address, "")
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != nil {
t.Fatal(err)
}
}
func TestTimedUnlock(t *testing.T) {
- dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase)
+ dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
defer os.RemoveAll(dir)
am := NewManager(ks)
pass := "foo"
a1, err := am.NewAccount(pass)
- toSign := randentropy.GetEntropyCSPRNG(32)
// Signing without passphrase fails because account is locked
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != ErrLocked {
t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
}
@@ -63,28 +62,26 @@ func TestTimedUnlock(t *testing.T) {
}
// Signing without passphrase works because account is temp unlocked
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != nil {
t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
}
// Signing fails again after automatic locking
time.Sleep(150 * time.Millisecond)
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != ErrLocked {
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
}
-
}
func TestOverrideUnlock(t *testing.T) {
- dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase)
+ dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
defer os.RemoveAll(dir)
am := NewManager(ks)
pass := "foo"
a1, err := am.NewAccount(pass)
- toSign := randentropy.GetEntropyCSPRNG(32)
// Unlock indefinitely
if err = am.Unlock(a1.Address, pass); err != nil {
@@ -92,7 +89,7 @@ func TestOverrideUnlock(t *testing.T) {
}
// Signing without passphrase works because account is temp unlocked
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != nil {
t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
}
@@ -103,20 +100,45 @@ func TestOverrideUnlock(t *testing.T) {
}
// Signing without passphrase still works because account is temp unlocked
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != nil {
t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
}
// Signing fails again after automatic locking
time.Sleep(150 * time.Millisecond)
- _, err = am.Sign(a1, toSign)
+ _, err = am.Sign(a1, testSigData)
if err != ErrLocked {
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
}
}
-//
+// This test should fail under -race if signing races the expiration goroutine.
+func TestSignRace(t *testing.T) {
+ dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
+ defer os.RemoveAll(dir)
+
+ // Create a test account.
+ am := NewManager(ks)
+ a1, err := am.NewAccount("")
+ if err != nil {
+ t.Fatal("could not create the test account", err)
+ }
+
+ if err := am.TimedUnlock(a1.Address, "", 15*time.Millisecond); err != nil {
+ t.Fatalf("could not unlock the test account", err)
+ }
+ end := time.Now().Add(80 * time.Millisecond)
+ for time.Now().Before(end) {
+ if _, err := am.Sign(a1, testSigData); err == ErrLocked {
+ return
+ } else if err != nil {
+ t.Errorf("Sign error: %v", err)
+ return
+ }
+ }
+ t.Errorf("Account did not lock within the timeout")
+}
func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore) (string, crypto.KeyStore) {
d, err := ioutil.TempDir("", "eth-keystore-test")