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