accounts_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Address, testSigData); err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. func TestSignWithPassphrase(t *testing.T) {
  76. dir, am := tmpManager(t, true)
  77. defer os.RemoveAll(dir)
  78. pass := "passwd"
  79. acc, err := am.NewAccount(pass)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if _, unlocked := am.unlocked[acc.Address]; unlocked {
  84. t.Fatal("expected account to be locked")
  85. }
  86. _, err = am.SignWithPassphrase(acc, pass, testSigData)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if _, unlocked := am.unlocked[acc.Address]; unlocked {
  91. t.Fatal("expected account to be locked")
  92. }
  93. if _, err = am.SignWithPassphrase(acc, "invalid passwd", testSigData); err == nil {
  94. t.Fatal("expected SignHash to fail with invalid password")
  95. }
  96. }
  97. func TestTimedUnlock(t *testing.T) {
  98. dir, am := tmpManager(t, true)
  99. defer os.RemoveAll(dir)
  100. pass := "foo"
  101. a1, err := am.NewAccount(pass)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. // Signing without passphrase fails because account is locked
  106. _, err = am.Sign(a1.Address, testSigData)
  107. if err != ErrLocked {
  108. t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
  109. }
  110. // Signing with passphrase works
  111. if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil {
  112. t.Fatal(err)
  113. }
  114. // Signing without passphrase works because account is temp unlocked
  115. _, err = am.Sign(a1.Address, testSigData)
  116. if err != nil {
  117. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  118. }
  119. // Signing fails again after automatic locking
  120. time.Sleep(250 * time.Millisecond)
  121. _, err = am.Sign(a1.Address, testSigData)
  122. if err != ErrLocked {
  123. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  124. }
  125. }
  126. func TestOverrideUnlock(t *testing.T) {
  127. dir, am := tmpManager(t, false)
  128. defer os.RemoveAll(dir)
  129. pass := "foo"
  130. a1, err := am.NewAccount(pass)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. // Unlock indefinitely.
  135. if err = am.TimedUnlock(a1, pass, 5*time.Minute); err != nil {
  136. t.Fatal(err)
  137. }
  138. // Signing without passphrase works because account is temp unlocked
  139. _, err = am.Sign(a1.Address, testSigData)
  140. if err != nil {
  141. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  142. }
  143. // reset unlock to a shorter period, invalidates the previous unlock
  144. if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil {
  145. t.Fatal(err)
  146. }
  147. // Signing without passphrase still works because account is temp unlocked
  148. _, err = am.Sign(a1.Address, testSigData)
  149. if err != nil {
  150. t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
  151. }
  152. // Signing fails again after automatic locking
  153. time.Sleep(250 * time.Millisecond)
  154. _, err = am.Sign(a1.Address, testSigData)
  155. if err != ErrLocked {
  156. t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
  157. }
  158. }
  159. // This test should fail under -race if signing races the expiration goroutine.
  160. func TestSignRace(t *testing.T) {
  161. dir, am := tmpManager(t, false)
  162. defer os.RemoveAll(dir)
  163. // Create a test account.
  164. a1, err := am.NewAccount("")
  165. if err != nil {
  166. t.Fatal("could not create the test account", err)
  167. }
  168. if err := am.TimedUnlock(a1, "", 15*time.Millisecond); err != nil {
  169. t.Fatal("could not unlock the test account", err)
  170. }
  171. end := time.Now().Add(500 * time.Millisecond)
  172. for time.Now().Before(end) {
  173. if _, err := am.Sign(a1.Address, testSigData); err == ErrLocked {
  174. return
  175. } else if err != nil {
  176. t.Errorf("Sign error: %v", err)
  177. return
  178. }
  179. time.Sleep(1 * time.Millisecond)
  180. }
  181. t.Errorf("Account did not lock within the timeout")
  182. }
  183. func tmpManager(t *testing.T, encrypted bool) (string, *Manager) {
  184. d, err := ioutil.TempDir("", "eth-keystore-test")
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. new := NewPlaintextManager
  189. if encrypted {
  190. new = func(kd string) *Manager { return NewManager(kd, veryLightScryptN, veryLightScryptP) }
  191. }
  192. return d, new(d)
  193. }