github.com/core-coin/go-core/v2@v2.1.9/accounts/keystore/passphrase_test.go (about) 1 // Copyright 2016 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package keystore 18 19 import ( 20 "io/ioutil" 21 "testing" 22 23 "github.com/core-coin/go-core/v2/common" 24 ) 25 26 const ( 27 veryLightScryptN = 2 28 veryLightScryptP = 1 29 ) 30 31 // Tests that a json key file can be decrypted and encrypted in multiple rounds. 32 func TestKeyEncryptDecrypt(t *testing.T) { 33 keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json") 34 if err != nil { 35 t.Fatal(err) 36 } 37 password := "foobar" 38 address, err := common.HexToAddress("cb45f23d9ab6aefb2c22dfff511e29703435a3b50f50") 39 if err != nil { 40 t.Error(err) 41 } 42 // Do a few rounds of decryption and encryption 43 for i := 0; i < 3; i++ { 44 // Try a bad password first 45 if _, err := DecryptKey(keyjson, password+"bad"); err == nil { 46 t.Errorf("test %d: json key decrypted with bad password", i) 47 } 48 // Decrypt with the correct password 49 key, err := DecryptKey(keyjson, password) 50 if err != nil { 51 t.Fatalf("test %d: json key failed to decrypt: %v", i, err) 52 } 53 if key.Address != address { 54 t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address) 55 } 56 // Recrypt with a new password and start over 57 password += "foobar2" 58 if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil { 59 t.Errorf("test %d: failed to recrypt key %v", i, err) 60 } 61 } 62 }