accounts_test.go 4.17 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

17 18 19
package accounts

import (
20 21
	"io/ioutil"
	"os"
22
	"testing"
23 24
	"time"

25
	"github.com/ethereum/go-ethereum/crypto"
26 27
)

28 29
var testSigData = make([]byte, 32)

30 31 32 33 34
func TestSign(t *testing.T) {
	dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
	defer os.RemoveAll(dir)

	am := NewManager(ks)
35 36
	pass := "" // not used but required by API
	a1, err := am.NewAccount(pass)
37
	am.Unlock(a1.Address, "")
38

39
	_, err = am.Sign(a1, testSigData)
40 41 42 43 44
	if err != nil {
		t.Fatal(err)
	}
}

45
func TestTimedUnlock(t *testing.T) {
46
	dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
47 48 49
	defer os.RemoveAll(dir)

	am := NewManager(ks)
50 51 52 53
	pass := "foo"
	a1, err := am.NewAccount(pass)

	// Signing without passphrase fails because account is locked
54
	_, err = am.Sign(a1, testSigData)
55
	if err != ErrLocked {
56
		t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
57 58 59
	}

	// Signing with passphrase works
60
	if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
61 62 63 64
		t.Fatal(err)
	}

	// Signing without passphrase works because account is temp unlocked
65
	_, err = am.Sign(a1, testSigData)
66
	if err != nil {
67
		t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
68 69
	}

70 71
	// Signing fails again after automatic locking
	time.Sleep(150 * time.Millisecond)
72
	_, err = am.Sign(a1, testSigData)
73
	if err != ErrLocked {
74
		t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
75
	}
zelig's avatar
zelig committed
76 77 78
}

func TestOverrideUnlock(t *testing.T) {
79
	dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
zelig's avatar
zelig committed
80 81 82 83 84 85 86 87 88 89 90 91
	defer os.RemoveAll(dir)

	am := NewManager(ks)
	pass := "foo"
	a1, err := am.NewAccount(pass)

	// Unlock indefinitely
	if err = am.Unlock(a1.Address, pass); err != nil {
		t.Fatal(err)
	}

	// Signing without passphrase works because account is temp unlocked
92
	_, err = am.Sign(a1, testSigData)
zelig's avatar
zelig committed
93 94 95 96 97 98 99 100 101 102
	if err != nil {
		t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
	}

	// reset unlock to a shorter period, invalidates the previous unlock
	if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
		t.Fatal(err)
	}

	// Signing without passphrase still works because account is temp unlocked
103
	_, err = am.Sign(a1, testSigData)
zelig's avatar
zelig committed
104 105 106 107 108 109
	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)
110
	_, err = am.Sign(a1, testSigData)
zelig's avatar
zelig committed
111 112 113
	if err != ErrLocked {
		t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
	}
114
}
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
// 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)
	}
131
	end := time.Now().Add(500 * time.Millisecond)
132 133 134 135 136 137 138
	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
		}
139
		time.Sleep(1 * time.Millisecond)
140 141 142
	}
	t.Errorf("Account did not lock within the timeout")
}
zelig's avatar
zelig committed
143 144

func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore) (string, crypto.KeyStore) {
145
	d, err := ioutil.TempDir("", "eth-keystore-test")
146 147 148
	if err != nil {
		t.Fatal(err)
	}
149
	return d, new(d)
150
}