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