accounts_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  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. // GNU Lesser General Public License for more details.
  13. //
  14. // 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. package accounts
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. )
  24. var testSigData = make([]byte, 32)
  25. func TestSign(t *testing.T) {
  26. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
  27. defer os.RemoveAll(dir)
  28. am := NewManager(ks)
  29. pass := "" // not used but required by API
  30. a1, err := am.NewAccount(pass)
  31. am.Unlock(a1.Address, "")
  32. _, err = am.Sign(a1, testSigData)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. func TestTimedUnlock(t *testing.T) {
  38. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
  39. defer os.RemoveAll(dir)
  40. am := NewManager(ks)
  41. pass := "foo"
  42. a1, err := am.NewAccount(pass)
  43. // Signing without passphrase fails because account is locked
  44. _, err = am.Sign(a1, testSigData)
  45. if err != ErrLocked {
  46. t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
  47. }
  48. // Signing with passphrase works
  49. if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
  50. t.Fatal(err)
  51. }
  52. // Signing without passphrase works because account is temp unlocked
  53. _, err = am.Sign(a1, testSigData)
  54. if err != nil {
  55. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  56. }
  57. // Signing fails again after automatic locking
  58. time.Sleep(150 * time.Millisecond)
  59. _, err = am.Sign(a1, testSigData)
  60. if err != ErrLocked {
  61. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  62. }
  63. }
  64. func TestOverrideUnlock(t *testing.T) {
  65. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
  66. defer os.RemoveAll(dir)
  67. am := NewManager(ks)
  68. pass := "foo"
  69. a1, err := am.NewAccount(pass)
  70. // Unlock indefinitely
  71. if err = am.Unlock(a1.Address, pass); err != nil {
  72. t.Fatal(err)
  73. }
  74. // Signing without passphrase works because account is temp unlocked
  75. _, err = am.Sign(a1, testSigData)
  76. if err != nil {
  77. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  78. }
  79. // reset unlock to a shorter period, invalidates the previous unlock
  80. if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
  81. t.Fatal(err)
  82. }
  83. // Signing without passphrase still works because account is temp unlocked
  84. _, err = am.Sign(a1, testSigData)
  85. if err != nil {
  86. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  87. }
  88. // Signing fails again after automatic locking
  89. time.Sleep(150 * time.Millisecond)
  90. _, err = am.Sign(a1, testSigData)
  91. if err != ErrLocked {
  92. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  93. }
  94. }
  95. // This test should fail under -race if signing races the expiration goroutine.
  96. func TestSignRace(t *testing.T) {
  97. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePlain)
  98. defer os.RemoveAll(dir)
  99. // Create a test account.
  100. am := NewManager(ks)
  101. a1, err := am.NewAccount("")
  102. if err != nil {
  103. t.Fatal("could not create the test account", err)
  104. }
  105. if err := am.TimedUnlock(a1.Address, "", 15*time.Millisecond); err != nil {
  106. t.Fatalf("could not unlock the test account", err)
  107. }
  108. end := time.Now().Add(500 * time.Millisecond)
  109. for time.Now().Before(end) {
  110. if _, err := am.Sign(a1, testSigData); err == ErrLocked {
  111. return
  112. } else if err != nil {
  113. t.Errorf("Sign error: %v", err)
  114. return
  115. }
  116. time.Sleep(1 * time.Millisecond)
  117. }
  118. t.Errorf("Account did not lock within the timeout")
  119. }
  120. func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore) (string, crypto.KeyStore) {
  121. d, err := ioutil.TempDir("", "eth-keystore-test")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. return d, new(d)
  126. }