plain_test.go 7.6 KB

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