github.com/theQRL/go-zond@v0.2.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 "testing" 25 26 "github.com/theQRL/go-zond/common" 27 ) 28 29 func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { 30 d := t.TempDir() 31 if encrypted { 32 ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true} 33 } else { 34 ks = &keyStorePlain{d} 35 } 36 return d, ks 37 } 38 39 func TestKeyStorePlain(t *testing.T) { 40 _, ks := tmpKeyStoreIface(t, false) 41 42 pass := "" // not used but required by API 43 k1, account, err := storeNewKey(ks, pass) 44 if err != nil { 45 t.Fatal(err) 46 } 47 k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) 48 if err != nil { 49 t.Fatal(err) 50 } 51 if !reflect.DeepEqual(k1.Address, k2.Address) { 52 t.Fatal(err) 53 } 54 if !reflect.DeepEqual(k1.Dilithium.GetSeed(), k2.Dilithium.GetSeed()) { 55 t.Fatal(err) 56 } 57 } 58 59 func TestKeyStorePassphrase(t *testing.T) { 60 _, ks := tmpKeyStoreIface(t, true) 61 62 pass := "foo" 63 k1, account, err := storeNewKey(ks, pass) 64 if err != nil { 65 t.Fatal(err) 66 } 67 k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) 68 if err != nil { 69 t.Fatal(err) 70 } 71 if !reflect.DeepEqual(k1.Address, k2.Address) { 72 t.Fatal(err) 73 } 74 if !reflect.DeepEqual(k1.Dilithium.GetSeed(), k2.Dilithium.GetSeed()) { 75 t.Fatal(err) 76 } 77 } 78 79 func TestKeyStorePassphraseDecryptionFail(t *testing.T) { 80 _, ks := tmpKeyStoreIface(t, true) 81 82 pass := "foo" 83 k1, account, err := storeNewKey(ks, pass) 84 if err != nil { 85 t.Fatal(err) 86 } 87 if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt { 88 t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt) 89 } 90 } 91 92 // Test and utils for the key store tests in the Zond JSON tests; 93 // testdataKeyStoreTests/basic_tests.json 94 type KeyStoreTestV3 struct { 95 Json encryptedKeyJSONV3 96 Password string 97 Priv string 98 } 99 100 func TestV3_PBKDF2_1(t *testing.T) { 101 t.Parallel() 102 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 103 testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t) 104 } 105 106 var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests") 107 108 func skipIfSubmoduleMissing(t *testing.T) { 109 if !common.FileExist(testsSubmodule) { 110 t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule) 111 } 112 } 113 114 func TestV3_PBKDF2_2(t *testing.T) { 115 skipIfSubmoduleMissing(t) 116 t.Parallel() 117 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 118 testDecryptV3(tests["test1"], t) 119 } 120 121 func TestV3_PBKDF2_3(t *testing.T) { 122 skipIfSubmoduleMissing(t) 123 t.Parallel() 124 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 125 testDecryptV3(tests["python_generated_test_with_odd_iv"], t) 126 } 127 128 func TestV3_PBKDF2_4(t *testing.T) { 129 skipIfSubmoduleMissing(t) 130 t.Parallel() 131 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 132 testDecryptV3(tests["evilnonce"], t) 133 } 134 135 func TestV3_Scrypt_1(t *testing.T) { 136 t.Parallel() 137 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 138 testDecryptV3(tests["wikipage_test_vector_scrypt"], t) 139 } 140 141 func TestV3_Scrypt_2(t *testing.T) { 142 skipIfSubmoduleMissing(t) 143 t.Parallel() 144 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 145 testDecryptV3(tests["test2"], t) 146 } 147 148 func testDecryptV3(test KeyStoreTestV3, t *testing.T) { 149 privBytes, _, err := decryptKeyV3(&test.Json, test.Password) 150 if err != nil { 151 t.Fatal(err) 152 } 153 privHex := hex.EncodeToString(privBytes) 154 if test.Priv != privHex { 155 t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) 156 } 157 } 158 159 func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 { 160 tests := make(map[string]KeyStoreTestV3) 161 err := common.LoadJSON(file, &tests) 162 if err != nil { 163 t.Fatal(err) 164 } 165 return tests 166 } 167 168 func TestV3_31_Byte_Key(t *testing.T) { 169 t.Parallel() 170 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 171 testDecryptV3(tests["31_byte_key"], t) 172 } 173 174 func TestV3_30_Byte_Key(t *testing.T) { 175 t.Parallel() 176 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 177 testDecryptV3(tests["30_byte_key"], t) 178 }