github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/legacyconnection/keys_test.go (about)

     1  /*
     2  Copyright Avast Software. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package legacyconnection
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
    15  	"github.com/hyperledger/aries-framework-go/pkg/doc/did"
    16  	"github.com/hyperledger/aries-framework-go/pkg/kms"
    17  	"github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol"
    18  	mockroute "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/mediator"
    19  	mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
    20  )
    21  
    22  func TestCreateNewKeyAndVM(t *testing.T) {
    23  	k := newKMS(t, mockstorage.NewMockStoreProvider())
    24  
    25  	p, err := New(&protocol.MockProvider{
    26  		ServiceMap: map[string]interface{}{
    27  			mediator.Coordination: &mockroute.MockMediatorSvc{},
    28  		},
    29  		CustomKMS: k,
    30  	})
    31  	require.NoError(t, err)
    32  
    33  	t.Run("createNewKeyAndVM success", func(t *testing.T) {
    34  		didDoc := &did.Doc{}
    35  
    36  		p.ctx.keyType = kms.ED25519
    37  		p.ctx.keyAgreementType = kms.X25519ECDHKWType
    38  
    39  		err = p.ctx.createNewKeyAndVM(didDoc)
    40  		require.NoError(t, err)
    41  		require.Equal(t, ed25519VerificationKey2018, didDoc.VerificationMethod[0].Type)
    42  		require.Equal(t, x25519KeyAgreementKey2019, didDoc.KeyAgreement[0].VerificationMethod.Type)
    43  	})
    44  
    45  	t.Run("createNewKeyAndVM invalid keyType export signing key", func(t *testing.T) {
    46  		didDoc := &did.Doc{}
    47  
    48  		p.ctx.keyType = kms.HMACSHA256Tag256Type // invalid signing key
    49  		p.ctx.keyAgreementType = kms.X25519ECDHKWType
    50  
    51  		err = p.ctx.createNewKeyAndVM(didDoc)
    52  		require.EqualError(t, err, "createSigningVM: createAndExportPubKeyBytes: failed to export new public key bytes: "+
    53  			"exportPubKeyBytes: failed to export marshalled key: exportPubKeyBytes: failed to get public keyset "+
    54  			"handle: keyset.Handle: keyset.Handle: keyset contains a non-private key")
    55  		require.Empty(t, didDoc.VerificationMethod)
    56  		require.Empty(t, didDoc.KeyAgreement)
    57  	})
    58  }
    59  
    60  func TestCreateSigningVM(t *testing.T) {
    61  	k := newKMS(t, mockstorage.NewMockStoreProvider())
    62  
    63  	p, err := New(&protocol.MockProvider{
    64  		ServiceMap: map[string]interface{}{
    65  			mediator.Coordination: &mockroute.MockMediatorSvc{},
    66  		},
    67  		CustomKMS: k,
    68  	})
    69  	require.NoError(t, err)
    70  
    71  	t.Run("createSigningVM success", func(t *testing.T) {
    72  		p.ctx.keyType = kms.ED25519
    73  
    74  		svm, err := p.ctx.createSigningVM()
    75  		require.NoError(t, err)
    76  		require.NotEmpty(t, svm)
    77  	})
    78  
    79  	t.Run("createSigningVM with empty vmType", func(t *testing.T) {
    80  		p.ctx.keyType = ""
    81  
    82  		svm, err := p.ctx.createSigningVM()
    83  		require.EqualError(t, err, "createSigningVM: createAndExportPubKeyBytes: failed to create new key: "+
    84  			"failed to create new key, missing key type")
    85  		require.Empty(t, svm)
    86  	})
    87  
    88  	t.Run("createSigningVM with unsupported keyType", func(t *testing.T) {
    89  		p.ctx.keyType = kms.X25519ECDHKW
    90  
    91  		svm, err := p.ctx.createSigningVM()
    92  		require.EqualError(t, err, "createSigningVM: unsupported verification method: 'X25519KeyAgreementKey2019'")
    93  		require.Empty(t, svm)
    94  	})
    95  }
    96  
    97  func TestCreateEncryptionVM(t *testing.T) {
    98  	k := newKMS(t, mockstorage.NewMockStoreProvider())
    99  
   100  	p, err := New(&protocol.MockProvider{
   101  		ServiceMap: map[string]interface{}{
   102  			mediator.Coordination: &mockroute.MockMediatorSvc{},
   103  		},
   104  		CustomKMS: k,
   105  	})
   106  	require.NoError(t, err)
   107  
   108  	t.Run("createEncryptionVM success", func(t *testing.T) {
   109  		p.ctx.keyAgreementType = kms.X25519ECDHKW
   110  
   111  		evm, err := p.ctx.createEncryptionVM()
   112  		require.NoError(t, err)
   113  		require.NotEmpty(t, evm)
   114  	})
   115  
   116  	t.Run("createEncryptionVM with empty keyAgreementType", func(t *testing.T) {
   117  		p.ctx.keyAgreementType = ""
   118  
   119  		evm, err := p.ctx.createEncryptionVM()
   120  		require.EqualError(t, err, "createEncryptionVM: createAndExportPubKeyBytes: failed to create new key: "+
   121  			"failed to create new key, missing key type")
   122  		require.Empty(t, evm)
   123  	})
   124  
   125  	t.Run("createEncryptionVM with unsupported keyType", func(t *testing.T) {
   126  		p.ctx.keyAgreementType = kms.ED25519Type
   127  
   128  		evm, err := p.ctx.createEncryptionVM()
   129  		require.EqualError(t, err, "unsupported verification method for KeyAgreement: 'Ed25519VerificationKey2018'")
   130  		require.Empty(t, evm)
   131  	})
   132  }