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