github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/keystore/keystore_passphrase_test.go (about)

     1  package keystore
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/quickchainproject/quickchain/common"
     8  )
     9  
    10  const (
    11  	veryLightScryptN = 2
    12  	veryLightScryptP = 1
    13  )
    14  
    15  // Tests that a json key file can be decrypted and encrypted in multiple rounds.
    16  func TestKeyEncryptDecrypt(t *testing.T) {
    17  	keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	password := ""
    22  	address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8")
    23  
    24  	// Do a few rounds of decryption and encryption
    25  	for i := 0; i < 3; i++ {
    26  		// Try a bad password first
    27  		if _, err := DecryptKey(keyjson, password+"bad"); err == nil {
    28  			t.Errorf("test %d: json key decrypted with bad password", i)
    29  		}
    30  		// Decrypt with the correct password
    31  		key, err := DecryptKey(keyjson, password)
    32  		if err != nil {
    33  			t.Fatalf("test %d: json key failed to decrypt: %v", i, err)
    34  		}
    35  		if key.Address != address {
    36  			t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address)
    37  		}
    38  		// Recrypt with a new password and start over
    39  		password += "new data appended"
    40  		if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil {
    41  			t.Errorf("test %d: failed to recrypt key %v", i, err)
    42  		}
    43  	}
    44  }