accounts_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "runtime"
  21. "strings"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. )
  26. var testSigData = make([]byte, 32)
  27. func TestManager(t *testing.T) {
  28. dir, am := tmpManager(t, true)
  29. defer os.RemoveAll(dir)
  30. a, err := am.NewAccount("foo")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if !strings.HasPrefix(a.File, dir) {
  35. t.Errorf("account file %s doesn't have dir prefix", a.File)
  36. }
  37. stat, err := os.Stat(a.File)
  38. if err != nil {
  39. t.Fatalf("account file %s doesn't exist (%v)", a.File, err)
  40. }
  41. if runtime.GOOS != "windows" && stat.Mode() != 0600 {
  42. t.Fatalf("account file has wrong mode: got %o, want %o", stat.Mode(), 0600)
  43. }
  44. if !am.HasAddress(a.Address) {
  45. t.Errorf("HasAccount(%x) should've returned true", a.Address)
  46. }
  47. if err := am.Update(a, "foo", "bar"); err != nil {
  48. t.Errorf("Update error: %v", err)
  49. }
  50. if err := am.DeleteAccount(a, "bar"); err != nil {
  51. t.Errorf("DeleteAccount error: %v", err)
  52. }
  53. if common.FileExist(a.File) {
  54. t.Errorf("account file %s should be gone after DeleteAccount", a.File)
  55. }
  56. if am.HasAddress(a.Address) {
  57. t.Errorf("HasAccount(%x) should've returned true after DeleteAccount", a.Address)
  58. }
  59. }
  60. func TestSign(t *testing.T) {
  61. dir, am := tmpManager(t, true)
  62. defer os.RemoveAll(dir)
  63. pass := "" // not used but required by API
  64. a1, err := am.NewAccount(pass)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if err := am.Unlock(a1, ""); err != nil {
  69. t.Fatal(err)
  70. }
  71. if _, err := am.Sign(a1, testSigData); err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. func TestTimedUnlock(t *testing.T) {
  76. dir, am := tmpManager(t, true)
  77. defer os.RemoveAll(dir)
  78. pass := "foo"
  79. a1, err := am.NewAccount(pass)
  80. // Signing without passphrase fails because account is locked
  81. _, err = am.Sign(a1, testSigData)
  82. if err != ErrLocked {
  83. t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
  84. }
  85. // Signing with passphrase works
  86. if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil {
  87. t.Fatal(err)
  88. }
  89. // Signing without passphrase works because account is temp unlocked
  90. _, err = am.Sign(a1, testSigData)
  91. if err != nil {
  92. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  93. }
  94. // Signing fails again after automatic locking
  95. time.Sleep(350 * time.Millisecond)
  96. _, err = am.Sign(a1, testSigData)
  97. if err != ErrLocked {
  98. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  99. }
  100. }
  101. func TestOverrideUnlock(t *testing.T) {
  102. dir, am := tmpManager(t, false)
  103. defer os.RemoveAll(dir)
  104. pass := "foo"
  105. a1, err := am.NewAccount(pass)
  106. // Unlock indefinitely
  107. if err = am.Unlock(a1, pass); err != nil {
  108. t.Fatal(err)
  109. }
  110. // Signing without passphrase works because account is temp unlocked
  111. _, err = am.Sign(a1, testSigData)
  112. if err != nil {
  113. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  114. }
  115. // reset unlock to a shorter period, invalidates the previous unlock
  116. if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil {
  117. t.Fatal(err)
  118. }
  119. // Signing without passphrase still works because account is temp unlocked
  120. _, err = am.Sign(a1, testSigData)
  121. if err != nil {
  122. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  123. }
  124. // Signing fails again after automatic locking
  125. time.Sleep(150 * time.Millisecond)
  126. _, err = am.Sign(a1, testSigData)
  127. if err != ErrLocked {
  128. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  129. }
  130. }
  131. // This test should fail under -race if signing races the expiration goroutine.
  132. func TestSignRace(t *testing.T) {
  133. dir, am := tmpManager(t, false)
  134. defer os.RemoveAll(dir)
  135. // Create a test account.
  136. a1, err := am.NewAccount("")
  137. if err != nil {
  138. t.Fatal("could not create the test account", err)
  139. }
  140. if err := am.TimedUnlock(a1, "", 15*time.Millisecond); err != nil {
  141. t.Fatal("could not unlock the test account", err)
  142. }
  143. end := time.Now().Add(500 * time.Millisecond)
  144. for time.Now().Before(end) {
  145. if _, err := am.Sign(a1, testSigData); err == ErrLocked {
  146. return
  147. } else if err != nil {
  148. t.Errorf("Sign error: %v", err)
  149. return
  150. }
  151. time.Sleep(1 * time.Millisecond)
  152. }
  153. t.Errorf("Account did not lock within the timeout")
  154. }
  155. func tmpManager(t *testing.T, encrypted bool) (string, *Manager) {
  156. d, err := ioutil.TempDir("", "eth-keystore-test")
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. new := NewPlaintextManager
  161. if encrypted {
  162. new = func(kd string) *Manager { return NewManager(kd, veryLightScryptN, veryLightScryptP) }
  163. }
  164. return d, new(d)
  165. }