2015-01-25 01:07:20 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-02-20 10:36:50 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
2015-02-23 10:28:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2015-02-25 16:29:23 +00:00
|
|
|
"time"
|
2015-01-25 01:07:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountManager(t *testing.T) {
|
2015-02-24 17:03:10 +00:00
|
|
|
ks := crypto.NewKeyStorePlain(ethutil.DefaultDataDir() + "/testaccounts")
|
2015-02-25 16:29:23 +00:00
|
|
|
am := NewAccountManager(ks, 100)
|
2015-01-25 01:07:20 +00:00
|
|
|
pass := "" // not used but required by API
|
|
|
|
a1, err := am.NewAccount(pass)
|
2015-02-20 10:36:50 +00:00
|
|
|
toSign := randentropy.GetEntropyCSPRNG(32)
|
2015-02-25 16:29:23 +00:00
|
|
|
_, err = am.SignLocked(a1, pass, toSign)
|
2015-01-25 01:07:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-24 17:03:10 +00:00
|
|
|
|
2015-02-25 16:29:23 +00:00
|
|
|
// Cleanup
|
2015-02-25 17:40:59 +00:00
|
|
|
time.Sleep(time.Millisecond * 150) // wait for locking
|
2015-02-25 16:29:23 +00:00
|
|
|
|
|
|
|
accounts, err := am.Accounts()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, account := range accounts {
|
|
|
|
err := am.DeleteAccount(account.Address, pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountManagerLocking(t *testing.T) {
|
|
|
|
ks := crypto.NewKeyStorePassphrase(ethutil.DefaultDataDir() + "/testaccounts")
|
|
|
|
am := NewAccountManager(ks, 200)
|
|
|
|
pass := "foo"
|
|
|
|
a1, err := am.NewAccount(pass)
|
|
|
|
toSign := randentropy.GetEntropyCSPRNG(32)
|
|
|
|
|
|
|
|
// Signing without passphrase fails because account is locked
|
|
|
|
_, err = am.Sign(a1, toSign)
|
|
|
|
if err != ErrLocked {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signing with passphrase works
|
|
|
|
_, err = am.SignLocked(a1, pass, toSign)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signing without passphrase works because account is temp unlocked
|
|
|
|
_, err = am.Sign(a1, toSign)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signing without passphrase fails after automatic locking
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(250))
|
|
|
|
|
|
|
|
_, err = am.Sign(a1, toSign)
|
|
|
|
if err != ErrLocked {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:03:10 +00:00
|
|
|
// Cleanup
|
|
|
|
accounts, err := am.Accounts()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, account := range accounts {
|
|
|
|
err := am.DeleteAccount(account.Address, pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|