2015-01-25 03:07:20 +02:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-02-20 12:36:50 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
2015-02-23 12:28:20 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2015-02-25 18:29:23 +02:00
|
|
|
"time"
|
2015-01-25 03:07:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountManager(t *testing.T) {
|
2015-02-24 19:03:10 +02:00
|
|
|
ks := crypto.NewKeyStorePlain(ethutil.DefaultDataDir() + "/testaccounts")
|
2015-02-25 18:29:23 +02:00
|
|
|
am := NewAccountManager(ks, 100)
|
2015-01-25 03:07:20 +02:00
|
|
|
pass := "" // not used but required by API
|
|
|
|
a1, err := am.NewAccount(pass)
|
2015-02-20 12:36:50 +02:00
|
|
|
toSign := randentropy.GetEntropyCSPRNG(32)
|
2015-02-25 18:29:23 +02:00
|
|
|
_, err = am.SignLocked(a1, pass, toSign)
|
2015-01-25 03:07:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-24 19:03:10 +02:00
|
|
|
|
2015-02-25 18:29:23 +02:00
|
|
|
// Cleanup
|
2015-02-25 19:40:59 +02:00
|
|
|
time.Sleep(time.Millisecond * 150) // wait for locking
|
2015-02-25 18:29:23 +02: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 19:03:10 +02: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 03:07:20 +02:00
|
|
|
}
|