github.com/klaytn/klaytn@v1.12.1/accounts/keystore/keystore_plain_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2014 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from accounts/keystore/keystore_plain_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package keystore 22 23 import ( 24 "crypto/rand" 25 "encoding/hex" 26 "fmt" 27 "os" 28 "path/filepath" 29 "reflect" 30 "strings" 31 "testing" 32 33 "github.com/klaytn/klaytn/common" 34 "github.com/klaytn/klaytn/crypto" 35 ) 36 37 func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { 38 d, err := os.MkdirTemp("", "klay-keystore-test") 39 if err != nil { 40 t.Fatal(err) 41 } 42 if encrypted { 43 ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true} 44 } else { 45 ks = &keyStorePlain{d} 46 } 47 return d, ks 48 } 49 50 func TestKeyStorePlain(t *testing.T) { 51 dir, ks := tmpKeyStoreIface(t, false) 52 defer os.RemoveAll(dir) 53 54 pass := "" // not used but required by API 55 k1, account, err := storeNewKey(ks, rand.Reader, pass) 56 if err != nil { 57 t.Fatal(err) 58 } 59 k2, err := ks.GetKey(k1.GetAddress(), account.URL.Path, pass) 60 if err != nil { 61 t.Fatal(err) 62 } 63 if !reflect.DeepEqual(k1.GetAddress(), k2.GetAddress()) { 64 t.Fatal(err) 65 } 66 if !reflect.DeepEqual(k1.GetPrivateKeys(), k2.GetPrivateKeys()) { 67 t.Fatal(err) 68 } 69 } 70 71 func TestKeyStorePassphrase(t *testing.T) { 72 dir, ks := tmpKeyStoreIface(t, true) 73 defer os.RemoveAll(dir) 74 75 pass := "foo" 76 k1, account, err := storeNewKey(ks, rand.Reader, pass) 77 if err != nil { 78 t.Fatal(err) 79 } 80 k2, err := ks.GetKey(k1.GetAddress(), account.URL.Path, pass) 81 if err != nil { 82 t.Fatal(err) 83 } 84 if !reflect.DeepEqual(k1.GetAddress(), k2.GetAddress()) { 85 t.Fatal(err) 86 } 87 if !reflect.DeepEqual(k1.GetPrivateKeys(), k2.GetPrivateKeys()) { 88 t.Fatal(err) 89 } 90 } 91 92 func TestKeyStorePassphraseDecryptionFail(t *testing.T) { 93 dir, ks := tmpKeyStoreIface(t, true) 94 defer os.RemoveAll(dir) 95 96 pass := "foo" 97 k1, account, err := storeNewKey(ks, rand.Reader, pass) 98 if err != nil { 99 t.Fatal(err) 100 } 101 if _, err = ks.GetKey(k1.GetAddress(), account.URL.Path, "bar"); err != ErrDecrypt { 102 t.Fatalf("wrong error for invalid passphrase\ngot %q\nwant %q", err, ErrDecrypt) 103 } 104 } 105 106 // Test and utils for the key store tests in the Klaytn JSON tests; 107 // testdataKeyStoreTests/basic_tests.json 108 type KeyStoreTestV3 struct { 109 Json encryptedKeyJSONV3 110 Password string 111 Priv string 112 } 113 114 type KeyStoreTestV1 struct { 115 Json encryptedKeyJSONV1 116 Password string 117 Priv string 118 } 119 120 func TestV3_PBKDF2_1(t *testing.T) { 121 t.Parallel() 122 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 123 testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t) 124 } 125 126 var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests") 127 128 func skipIfSubmoduleMissing(t *testing.T) { 129 if !common.FileExist(testsSubmodule) { 130 t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule) 131 } 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 141 func TestV3_PBKDF2_3(t *testing.T) { 142 skipIfSubmoduleMissing(t) 143 t.Parallel() 144 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 145 testDecryptV3(tests["python_generated_test_with_odd_iv"], t) 146 } 147 148 func TestV3_PBKDF2_4(t *testing.T) { 149 skipIfSubmoduleMissing(t) 150 t.Parallel() 151 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 152 testDecryptV3(tests["evilnonce"], t) 153 } 154 155 func TestV3_Scrypt_1(t *testing.T) { 156 t.Parallel() 157 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 158 testDecryptV3(tests["wikipage_test_vector_scrypt"], t) 159 } 160 161 func TestV3_Scrypt_2(t *testing.T) { 162 skipIfSubmoduleMissing(t) 163 t.Parallel() 164 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 165 testDecryptV3(tests["test2"], t) 166 } 167 168 func TestV1_1(t *testing.T) { 169 t.Parallel() 170 tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t) 171 testDecryptV1(tests["test1"], t) 172 } 173 174 func TestV1_2(t *testing.T) { 175 t.Parallel() 176 ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true} 177 addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e") 178 file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e" 179 k, err := ks.GetKey(addr, file, "g") 180 if err != nil { 181 t.Fatal(err) 182 } 183 privHex := hex.EncodeToString(crypto.FromECDSA(k.GetPrivateKey())) 184 expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d" 185 if privHex != expectedHex { 186 t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex)) 187 } 188 } 189 190 func testDecryptV3(test KeyStoreTestV3, t *testing.T) { 191 privBytes, _, err := decryptKeyV3(&test.Json, test.Password) 192 if err != nil { 193 t.Fatal(err) 194 } 195 privHex := hex.EncodeToString(privBytes) 196 if test.Priv != privHex { 197 t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) 198 } 199 } 200 201 func testDecryptV1(test KeyStoreTestV1, t *testing.T) { 202 privBytes, _, err := decryptKeyV1(&test.Json, test.Password) 203 if err != nil { 204 t.Fatal(err) 205 } 206 privHex := hex.EncodeToString(privBytes) 207 if test.Priv != privHex { 208 t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) 209 } 210 } 211 212 func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 { 213 tests := make(map[string]KeyStoreTestV3) 214 err := common.LoadJSON(file, &tests) 215 if err != nil { 216 t.Fatal(err) 217 } 218 return tests 219 } 220 221 func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 { 222 tests := make(map[string]KeyStoreTestV1) 223 err := common.LoadJSON(file, &tests) 224 if err != nil { 225 t.Fatal(err) 226 } 227 return tests 228 } 229 230 func TestKeyForDirectICAP(t *testing.T) { 231 t.Parallel() 232 key := NewKeyForDirectICAP(rand.Reader) 233 if !strings.HasPrefix(key.GetAddress().Hex(), "0x00") { 234 t.Errorf("Expected first address byte to be zero, have: %s", key.GetAddress().Hex()) 235 } 236 } 237 238 func TestV3_31_Byte_Key(t *testing.T) { 239 t.Parallel() 240 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 241 testDecryptV3(tests["31_byte_key"], t) 242 } 243 244 func TestV3_30_Byte_Key(t *testing.T) { 245 t.Parallel() 246 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 247 testDecryptV3(tests["30_byte_key"], t) 248 }