github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/blockchain/pseudohsm/image_test.go (about)

     1  package pseudohsm
     2  
     3  import (
     4  	"encoding/hex"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    11  )
    12  
    13  func TestRestore(t *testing.T) {
    14  	dirPath, err := ioutil.TempDir(".", "")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	defer os.RemoveAll(dirPath)
    19  
    20  	hsm, err := New(dirPath)
    21  	if err != nil {
    22  		t.Fatal("create hsm err:", err)
    23  	}
    24  
    25  	testCases := []struct {
    26  		image    string
    27  		wantErr  error
    28  		wantKeys []string
    29  	}{
    30  		{
    31  			image:    "{\"xkeys\":[{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6\"}]}",
    32  			wantErr:  nil,
    33  			wantKeys: []string{"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6"},
    34  		},
    35  		{
    36  			image:    "{\"xkeys\":[{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6\"},{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6\"}]}",
    37  			wantErr:  nil,
    38  			wantKeys: []string{"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6"},
    39  		},
    40  		{
    41  			image:    "{\"xkeys\":[{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb\"}]}",
    42  			wantErr:  ErrXPubFormat,
    43  			wantKeys: []string{"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6"},
    44  		},
    45  		{
    46  			image:    "{\"xkeys\":[{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb5\"}]}",
    47  			wantErr:  ErrDuplicateKeyAlias,
    48  			wantKeys: []string{"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6"},
    49  		},
    50  		{
    51  			image:    "{\"xkeys\":[{\"alias\":\"test4\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6\"},{\"alias\":\"test5\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"45650919af7c61d907d8681ae8a3f8d0\"},\"ciphertext\":\"0d073355814763f3c4d0c049668d362a419af56ff193a1e65f1e867babaf3b8c9e91e539918314e7b78532216093f8bc2dd7712af579f7ff2b95ef0ecb8562a0\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":4096,\"p\":6,\"r\":8,\"salt\":\"d6a5ff1f5ba2d678db428cb2d387a8130f0223c9fe271dfdf02a3edca4608b30\"},\"mac\":\"889920bee3829a8b3aaccb8c6e8aa5f9dda7f986d72efa25c5777c88c1a86c17\"},\"id\":\"868047f4-6613-4abb-902a-4b2a288cd8c7\",\"type\":\"bytom_kd\",\"version\":1,\"xpub\":\"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb7\"}]}",
    52  			wantErr:  nil,
    53  			wantKeys: []string{"a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb6", "a57f2ac07c69a71ec2ec7432c573c8b0680f3b6e4bb3c30baaf845b2685840b133dbe260c332640904f0a523421cf911084e98dd18204e88271fbc1c35f18fb7"},
    54  		},
    55  	}
    56  
    57  	for _, test := range testCases {
    58  		keyImage := &KeyImage{}
    59  		if err := json.Unmarshal([]byte(test.image), keyImage); err != nil {
    60  			t.Fatal("unmarshal json error:", err)
    61  		}
    62  
    63  		if err := hsm.Restore(keyImage); err != test.wantErr {
    64  			t.Errorf("error mismatch: have %v, want %v", err, test.wantErr)
    65  		}
    66  
    67  		if len(hsm.cache.keys()) != len(test.wantKeys) {
    68  			t.Errorf("error key num: have %v, want %v", len(hsm.cache.keys()), len(test.wantKeys))
    69  		}
    70  
    71  		for _, key := range test.wantKeys {
    72  			var xPub chainkd.XPub
    73  			data, _ := hex.DecodeString(key)
    74  			copy(xPub[:], data)
    75  
    76  			if !hsm.cache.hasKey(xPub) {
    77  				t.Errorf("error restore key: can't find key %v", key)
    78  			}
    79  		}
    80  	}
    81  }