github.com/hyperledger/aries-framework-go@v0.3.2/pkg/internal/kmssigner/kmssigner_test.go (about)

     1  /*
     2  Copyright Avast Software. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kmssigner
     8  
     9  import (
    10  	"errors"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	kmsapi "github.com/hyperledger/aries-framework-go/pkg/kms"
    16  	mockcrypto "github.com/hyperledger/aries-framework-go/pkg/mock/crypto"
    17  )
    18  
    19  func TestKMSSigner_Alg(t *testing.T) {
    20  	tests := []struct {
    21  		name        string
    22  		kmsKT       kmsapi.KeyType
    23  		expectedAlg string
    24  	}{
    25  		{
    26  			name:        "test ECDSA alg from P256 key type in DER format",
    27  			kmsKT:       kmsapi.ECDSAP256DER,
    28  			expectedAlg: p256Alg,
    29  		},
    30  		{
    31  			name:        "test ECDSA alg from P256 key type in IEEE format",
    32  			kmsKT:       kmsapi.ECDSAP256IEEEP1363,
    33  			expectedAlg: p256Alg,
    34  		},
    35  		{
    36  			name:        "test ECDSA alg from P384 key type in DER format",
    37  			kmsKT:       kmsapi.ECDSAP384DER,
    38  			expectedAlg: p384Alg,
    39  		},
    40  		{
    41  			name:        "test ECDSA alg from P384 key type in IEEE format",
    42  			kmsKT:       kmsapi.ECDSAP384IEEEP1363,
    43  			expectedAlg: p384Alg,
    44  		},
    45  		{
    46  			name:        "test ECDSA alg from P521 key type in DER format",
    47  			kmsKT:       kmsapi.ECDSAP521DER,
    48  			expectedAlg: p521Alg,
    49  		},
    50  		{
    51  			name:        "test ECDSA alg from P521 key type in IEEE format",
    52  			kmsKT:       kmsapi.ECDSAP521IEEEP1363,
    53  			expectedAlg: p521Alg,
    54  		},
    55  		{
    56  			name:        "test EdDSA alg from ed25519 key type",
    57  			kmsKT:       kmsapi.ED25519,
    58  			expectedAlg: edAlg,
    59  		},
    60  		{
    61  			name: "test empty alg from empty key type",
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		tc := tt
    67  
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			signer := KMSSigner{
    70  				KeyType: tc.kmsKT,
    71  			}
    72  
    73  			alg := signer.Alg()
    74  			require.Equal(t, tc.expectedAlg, alg)
    75  		})
    76  	}
    77  }
    78  
    79  func TestKMSSigner_Sign(t *testing.T) {
    80  	signError := errors.New("sign error")
    81  	signResult := []byte("abc")
    82  
    83  	t.Run("sign success", func(t *testing.T) {
    84  		signer := KMSSigner{Crypto: &mockcrypto.Crypto{SignValue: signResult}}
    85  
    86  		res, err := signer.Sign([]byte("1234"))
    87  		require.NoError(t, err)
    88  		require.Equal(t, signResult, res)
    89  	})
    90  
    91  	t.Run("Multi-message sign success", func(t *testing.T) {
    92  		signer := KMSSigner{
    93  			Crypto:   &mockcrypto.Crypto{BBSSignValue: signResult},
    94  			MultiMsg: true,
    95  		}
    96  
    97  		res, err := signer.Sign([]byte("1234\n4321\nabcd"))
    98  		require.NoError(t, err)
    99  		require.Equal(t, signResult, res)
   100  	})
   101  
   102  	t.Run("sign error", func(t *testing.T) {
   103  		signer := KMSSigner{Crypto: &mockcrypto.Crypto{SignErr: signError}}
   104  
   105  		res, err := signer.Sign([]byte("1234"))
   106  		require.Error(t, err)
   107  		require.ErrorIs(t, err, signError)
   108  		require.Empty(t, res)
   109  	})
   110  }