keystore_test.go 6.3 KB

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