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