accounts_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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. "github.com/ethereum/go-ethereum/crypto/randentropy"
  24. )
  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. toSign := randentropy.GetEntropyCSPRNG(32)
  32. am.Unlock(a1.Address, "")
  33. _, err = am.Sign(a1, toSign)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. }
  38. func TestTimedUnlock(t *testing.T) {
  39. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase)
  40. defer os.RemoveAll(dir)
  41. am := NewManager(ks)
  42. pass := "foo"
  43. a1, err := am.NewAccount(pass)
  44. toSign := randentropy.GetEntropyCSPRNG(32)
  45. // Signing without passphrase fails because account is locked
  46. _, err = am.Sign(a1, toSign)
  47. if err != ErrLocked {
  48. t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
  49. }
  50. // Signing with passphrase works
  51. if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
  52. t.Fatal(err)
  53. }
  54. // Signing without passphrase works because account is temp unlocked
  55. _, err = am.Sign(a1, toSign)
  56. if err != nil {
  57. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  58. }
  59. // Signing fails again after automatic locking
  60. time.Sleep(150 * time.Millisecond)
  61. _, err = am.Sign(a1, toSign)
  62. if err != ErrLocked {
  63. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  64. }
  65. }
  66. func TestOverrideUnlock(t *testing.T) {
  67. dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase)
  68. defer os.RemoveAll(dir)
  69. am := NewManager(ks)
  70. pass := "foo"
  71. a1, err := am.NewAccount(pass)
  72. toSign := randentropy.GetEntropyCSPRNG(32)
  73. // Unlock indefinitely
  74. if err = am.Unlock(a1.Address, pass); err != nil {
  75. t.Fatal(err)
  76. }
  77. // Signing without passphrase works because account is temp unlocked
  78. _, err = am.Sign(a1, toSign)
  79. if err != nil {
  80. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  81. }
  82. // reset unlock to a shorter period, invalidates the previous unlock
  83. if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
  84. t.Fatal(err)
  85. }
  86. // Signing without passphrase still works because account is temp unlocked
  87. _, err = am.Sign(a1, toSign)
  88. if err != nil {
  89. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  90. }
  91. // Signing fails again after automatic locking
  92. time.Sleep(150 * time.Millisecond)
  93. _, err = am.Sign(a1, toSign)
  94. if err != ErrLocked {
  95. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  96. }
  97. }
  98. //
  99. func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore) (string, crypto.KeyStore) {
  100. d, err := ioutil.TempDir("", "eth-keystore-test")
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. return d, new(d)
  105. }