github.com/trustbloc/kms-go@v1.1.2/wrapper/localsuite/multisigner_test.go (about)

     1  /*
     2  Copyright Gen Digital Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package localsuite
     7  
     8  import (
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/go-jose/go-jose/v3"
    13  	"github.com/stretchr/testify/require"
    14  	"github.com/trustbloc/kms-go/doc/jose/jwk"
    15  	mockcrypto "github.com/trustbloc/kms-go/mock/crypto"
    16  	mockkms "github.com/trustbloc/kms-go/mock/kms"
    17  )
    18  
    19  func TestMultiSigner(t *testing.T) {
    20  	var (
    21  		msgs   = [][]byte{[]byte("foo"), []byte("qux")}
    22  		msg    = []byte("foo bar")
    23  		pub    = &jwk.JWK{JSONWebKey: jose.JSONWebKey{KeyID: "foo"}}
    24  		expSig = []byte("signature")
    25  	)
    26  
    27  	t.Run("sign success", func(t *testing.T) {
    28  		ms := newKMSCryptoMultiSigner(&mockkms.KeyManager{}, &mockcrypto.Crypto{
    29  			SignValue:    expSig,
    30  			BBSSignValue: expSig,
    31  		})
    32  
    33  		sig, err := ms.Sign(msg, pub)
    34  		require.NoError(t, err)
    35  		require.Equal(t, expSig, sig)
    36  
    37  		sig, err = ms.SignMulti(msgs, pub)
    38  		require.NoError(t, err)
    39  		require.Equal(t, expSig, sig)
    40  	})
    41  
    42  	t.Run("fixed key success", func(t *testing.T) {
    43  		ms := newKMSCryptoMultiSigner(&mockkms.KeyManager{}, &mockcrypto.Crypto{
    44  			SignValue:    expSig,
    45  			BBSSignValue: expSig,
    46  		})
    47  
    48  		fkms, err := ms.FixedKeyMultiSigner(pub)
    49  		require.NoError(t, err)
    50  		require.NotNil(t, fkms)
    51  
    52  		fkms, err = ms.FixedMultiSignerGivenKID(pub.KeyID)
    53  		require.NoError(t, err)
    54  		require.NotNil(t, fkms)
    55  
    56  		sig, err := fkms.Sign(msg)
    57  		require.NoError(t, err)
    58  		require.Equal(t, expSig, sig)
    59  
    60  		sig, err = fkms.SignMulti(msgs)
    61  		require.NoError(t, err)
    62  		require.Equal(t, expSig, sig)
    63  	})
    64  
    65  	errExpected := errors.New("expected error")
    66  
    67  	t.Run("kms get error", func(t *testing.T) {
    68  		ms := newKMSCryptoMultiSigner(&mockkms.KeyManager{
    69  			GetKeyErr: errExpected,
    70  		}, &mockcrypto.Crypto{})
    71  
    72  		sig, err := ms.Sign(msg, pub)
    73  		require.ErrorIs(t, err, errExpected)
    74  		require.Nil(t, sig)
    75  
    76  		sig, err = ms.SignMulti(msgs, pub)
    77  		require.ErrorIs(t, err, errExpected)
    78  		require.Nil(t, sig)
    79  
    80  		fkms, err := ms.FixedKeyMultiSigner(pub)
    81  		require.ErrorIs(t, err, errExpected)
    82  		require.Nil(t, fkms)
    83  
    84  		fkms, err = ms.FixedMultiSignerGivenKID(pub.KeyID)
    85  		require.ErrorIs(t, err, errExpected)
    86  		require.Nil(t, fkms)
    87  	})
    88  
    89  	t.Run("sign error", func(t *testing.T) {
    90  		ms := newKMSCryptoMultiSigner(
    91  			&mockkms.KeyManager{},
    92  			&mockcrypto.Crypto{
    93  				SignErr:    errExpected,
    94  				BBSSignErr: errExpected,
    95  			},
    96  		)
    97  
    98  		sig, err := ms.Sign(msg, pub)
    99  		require.ErrorIs(t, err, errExpected)
   100  		require.Nil(t, sig)
   101  
   102  		sig, err = ms.SignMulti(msgs, pub)
   103  		require.ErrorIs(t, err, errExpected)
   104  		require.Nil(t, sig)
   105  	})
   106  
   107  	t.Run("fixed key sign error", func(t *testing.T) {
   108  		ms := newKMSCryptoMultiSigner(
   109  			&mockkms.KeyManager{},
   110  			&mockcrypto.Crypto{
   111  				SignErr:    errExpected,
   112  				BBSSignErr: errExpected,
   113  			},
   114  		)
   115  
   116  		fkms, err := ms.FixedKeyMultiSigner(pub)
   117  		require.NoError(t, err)
   118  		require.NotNil(t, fkms)
   119  
   120  		fkms, err = ms.FixedMultiSignerGivenKID(pub.KeyID)
   121  		require.NoError(t, err)
   122  		require.NotNil(t, fkms)
   123  
   124  		sig, err := fkms.Sign(msg)
   125  		require.ErrorIs(t, err, errExpected)
   126  		require.Nil(t, sig)
   127  
   128  		sig, err = fkms.SignMulti(msgs)
   129  		require.ErrorIs(t, err, errExpected)
   130  		require.Nil(t, sig)
   131  	})
   132  }