plain_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2014 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. "crypto/rand"
  19. "encoding/hex"
  20. "fmt"
  21. "path/filepath"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
  29. d := t.TempDir()
  30. if encrypted {
  31. ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
  32. } else {
  33. ks = &keyStorePlain{d}
  34. }
  35. return d, ks
  36. }
  37. func TestKeyStorePlain(t *testing.T) {
  38. _, ks := tmpKeyStoreIface(t, false)
  39. pass := "" // not used but required by API
  40. k1, account, err := storeNewKey(ks, rand.Reader, pass)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if !reflect.DeepEqual(k1.Address, k2.Address) {
  49. t.Fatal(err)
  50. }
  51. if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
  52. t.Fatal(err)
  53. }
  54. }
  55. func TestKeyStorePassphrase(t *testing.T) {
  56. _, ks := tmpKeyStoreIface(t, true)
  57. pass := "foo"
  58. k1, account, err := storeNewKey(ks, rand.Reader, pass)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if !reflect.DeepEqual(k1.Address, k2.Address) {
  67. t.Fatal(err)
  68. }
  69. if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
  70. t.Fatal(err)
  71. }
  72. }
  73. func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
  74. _, ks := tmpKeyStoreIface(t, true)
  75. pass := "foo"
  76. k1, account, err := storeNewKey(ks, rand.Reader, pass)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
  81. t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
  82. }
  83. }
  84. func TestImportPreSaleKey(t *testing.T) {
  85. dir, ks := tmpKeyStoreIface(t, true)
  86. // file content of a presale key file generated with:
  87. // python pyethsaletool.py genwallet
  88. // with password "foo"
  89. fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
  90. pass := "foo"
  91. account, _, err := importPreSaleKey(ks, []byte(fileContent), pass)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") {
  96. t.Errorf("imported account has wrong address %x", account.Address)
  97. }
  98. if !strings.HasPrefix(account.URL.Path, dir) {
  99. t.Errorf("imported account file not in keystore directory: %q", account.URL)
  100. }
  101. }
  102. // Test and utils for the key store tests in the Ethereum JSON tests;
  103. // testdataKeyStoreTests/basic_tests.json
  104. type KeyStoreTestV3 struct {
  105. Json encryptedKeyJSONV3
  106. Password string
  107. Priv string
  108. }
  109. type KeyStoreTestV1 struct {
  110. Json encryptedKeyJSONV1
  111. Password string
  112. Priv string
  113. }
  114. func TestV3_PBKDF2_1(t *testing.T) {
  115. t.Parallel()
  116. tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
  117. testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
  118. }
  119. var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
  120. func skipIfSubmoduleMissing(t *testing.T) {
  121. if !common.FileExist(testsSubmodule) {
  122. t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
  123. }
  124. }
  125. func TestV3_PBKDF2_2(t *testing.T) {
  126. skipIfSubmoduleMissing(t)
  127. t.Parallel()
  128. tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
  129. testDecryptV3(tests["test1"], t)
  130. }
  131. func TestV3_PBKDF2_3(t *testing.T) {
  132. skipIfSubmoduleMissing(t)
  133. t.Parallel()
  134. tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
  135. testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
  136. }
  137. func TestV3_PBKDF2_4(t *testing.T) {
  138. skipIfSubmoduleMissing(t)
  139. t.Parallel()
  140. tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
  141. testDecryptV3(tests["evilnonce"], t)
  142. }
  143. func TestV3_Scrypt_1(t *testing.T) {
  144. t.Parallel()
  145. tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
  146. testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
  147. }
  148. func TestV3_Scrypt_2(t *testing.T) {
  149. skipIfSubmoduleMissing(t)
  150. t.Parallel()
  151. tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
  152. testDecryptV3(tests["test2"], t)
  153. }
  154. func TestV1_1(t *testing.T) {
  155. t.Parallel()
  156. tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t)
  157. testDecryptV1(tests["test1"], t)
  158. }
  159. func TestV1_2(t *testing.T) {
  160. t.Parallel()
  161. ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true}
  162. addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
  163. file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
  164. k, err := ks.GetKey(addr, file, "g")
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey))
  169. expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
  170. if privHex != expectedHex {
  171. t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
  172. }
  173. }
  174. func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
  175. privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. privHex := hex.EncodeToString(privBytes)
  180. if test.Priv != privHex {
  181. t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
  182. }
  183. }
  184. func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
  185. privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. privHex := hex.EncodeToString(privBytes)
  190. if test.Priv != privHex {
  191. t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
  192. }
  193. }
  194. func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
  195. tests := make(map[string]KeyStoreTestV3)
  196. err := common.LoadJSON(file, &tests)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. return tests
  201. }
  202. func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
  203. tests := make(map[string]KeyStoreTestV1)
  204. err := common.LoadJSON(file, &tests)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. return tests
  209. }
  210. func TestKeyForDirectICAP(t *testing.T) {
  211. t.Parallel()
  212. key := NewKeyForDirectICAP(rand.Reader)
  213. if !strings.HasPrefix(key.Address.Hex(), "0x00") {
  214. t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
  215. }
  216. }
  217. func TestV3_31_Byte_Key(t *testing.T) {
  218. t.Parallel()
  219. tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
  220. testDecryptV3(tests["31_byte_key"], t)
  221. }
  222. func TestV3_30_Byte_Key(t *testing.T) {
  223. t.Parallel()
  224. tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
  225. testDecryptV3(tests["30_byte_key"], t)
  226. }