github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/keystore/keystore_passphrase_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package keystore 13 14 import ( 15 "io/ioutil" 16 "testing" 17 18 "github.com/Sberex/go-sberex/common" 19 ) 20 21 const ( 22 veryLightScryptN = 2 23 veryLightScryptP = 1 24 ) 25 26 // Tests that a json key file can be decrypted and encrypted in multiple rounds. 27 func TestKeyEncryptDecrypt(t *testing.T) { 28 keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json") 29 if err != nil { 30 t.Fatal(err) 31 } 32 password := "" 33 address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8") 34 35 // Do a few rounds of decryption and encryption 36 for i := 0; i < 3; i++ { 37 // Try a bad password first 38 if _, err := DecryptKey(keyjson, password+"bad"); err == nil { 39 t.Errorf("test %d: json key decrypted with bad password", i) 40 } 41 // Decrypt with the correct password 42 key, err := DecryptKey(keyjson, password) 43 if err != nil { 44 t.Fatalf("test %d: json key failed to decrypt: %v", i, err) 45 } 46 if key.Address != address { 47 t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address) 48 } 49 // Recrypt with a new password and start over 50 password += "new data appended" 51 if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil { 52 t.Errorf("test %d: failed to recrypt key %v", i, err) 53 } 54 } 55 }