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