accounts_test.go 5.9 KB

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