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

     1  /*
     2  Copyright Gen Digital Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  // Package wrapper contains mocks for kms+crypto wrapper APIs.
     7  package wrapper
     8  
     9  import (
    10  	"github.com/trustbloc/kms-go/doc/jose/jwk"
    11  	"github.com/trustbloc/kms-go/spi/kms"
    12  	wrapperapi "github.com/trustbloc/kms-go/wrapper/api"
    13  )
    14  
    15  // MockKMSCrypto mocks wrapper.KMSCrypto.
    16  type MockKMSCrypto struct {
    17  	CreateVal         *jwk.JWK
    18  	CreateRawKID      string
    19  	CreateRawVal      interface{}
    20  	CreateErr         error
    21  	SignVal           []byte
    22  	SignErr           error
    23  	VerifyErr         error
    24  	FixedKeyCryptoVal *MockFixedKeyCrypto
    25  	FixedKeyCryptoErr error
    26  	EncryptVal        []byte
    27  	EncryptNonce      []byte
    28  	EncryptErr        error
    29  	DecryptVal        []byte
    30  	DecryptErr        error
    31  }
    32  
    33  // Create mock.
    34  func (m *MockKMSCrypto) Create(keyType kms.KeyType) (*jwk.JWK, error) {
    35  	return m.CreateVal, m.CreateErr
    36  }
    37  
    38  // CreateRaw mock.
    39  func (m *MockKMSCrypto) CreateRaw(keyType kms.KeyType) (string, interface{}, error) {
    40  	return m.CreateRawKID, m.CreateRawVal, m.CreateErr
    41  }
    42  
    43  // Sign mock.
    44  func (m *MockKMSCrypto) Sign(msg []byte, pub *jwk.JWK) ([]byte, error) {
    45  	return m.SignVal, m.SignErr
    46  }
    47  
    48  // SignMulti mock.
    49  func (m *MockKMSCrypto) SignMulti(msgs [][]byte, pub *jwk.JWK) ([]byte, error) {
    50  	return m.SignVal, m.SignErr
    51  }
    52  
    53  // Verify mock.
    54  func (m *MockKMSCrypto) Verify(sig, msg []byte, pub *jwk.JWK) error {
    55  	return m.VerifyErr
    56  }
    57  
    58  // Encrypt mock.
    59  func (m *MockKMSCrypto) Encrypt(msg, aad []byte, kid string) (cipher, nonce []byte, err error) {
    60  	return m.EncryptVal, m.EncryptNonce, m.EncryptErr
    61  }
    62  
    63  // Decrypt mock.
    64  func (m *MockKMSCrypto) Decrypt(cipher, aad, nonce []byte, kid string) (msg []byte, err error) {
    65  	return m.DecryptVal, m.DecryptErr
    66  }
    67  
    68  // FixedKeyCrypto mock.
    69  func (m *MockKMSCrypto) FixedKeyCrypto(pub *jwk.JWK) (wrapperapi.FixedKeyCrypto, error) {
    70  	return makeMockFixedKey(m)
    71  }
    72  
    73  // FixedKeySigner mock.
    74  func (m *MockKMSCrypto) FixedKeySigner(pub *jwk.JWK) (wrapperapi.FixedKeySigner, error) {
    75  	return makeMockFixedKey(m)
    76  }
    77  
    78  // FixedMultiSignerGivenKID mock.
    79  func (m *MockKMSCrypto) FixedMultiSignerGivenKID(kid string) (wrapperapi.FixedKeyMultiSigner, error) {
    80  	return makeMockFixedKey(m)
    81  }
    82  
    83  // FixedKeyMultiSigner mock.
    84  func (m *MockKMSCrypto) FixedKeyMultiSigner(pub *jwk.JWK) (wrapperapi.FixedKeyMultiSigner, error) {
    85  	return makeMockFixedKey(m)
    86  }
    87  
    88  func makeMockFixedKey(m *MockKMSCrypto) (*MockFixedKeyCrypto, error) {
    89  	if m.FixedKeyCryptoVal != nil || m.FixedKeyCryptoErr != nil {
    90  		return m.FixedKeyCryptoVal, m.FixedKeyCryptoErr
    91  	}
    92  
    93  	fkc := &MockFixedKeyCrypto{
    94  		SignErr:   m.SignErr,
    95  		VerifyErr: m.VerifyErr,
    96  	}
    97  
    98  	return fkc, nil
    99  }
   100  
   101  // MockFixedKeyCrypto mocks kmscrypto.FixedKeyCrypto.
   102  type MockFixedKeyCrypto struct {
   103  	SignVal   []byte
   104  	SignErr   error
   105  	VerifyErr error
   106  }
   107  
   108  // Sign mock.
   109  func (m *MockFixedKeyCrypto) Sign(msg []byte) ([]byte, error) {
   110  	return m.SignVal, m.SignErr
   111  }
   112  
   113  // SignMulti mock.
   114  func (m *MockFixedKeyCrypto) SignMulti(msgs [][]byte) ([]byte, error) {
   115  	return m.SignVal, m.SignErr
   116  }
   117  
   118  // Verify mock.
   119  func (m *MockFixedKeyCrypto) Verify(sig, msg []byte) error {
   120  	return m.VerifyErr
   121  }
   122  
   123  var _ wrapperapi.KMSCryptoMultiSigner = &MockKMSCrypto{}
   124  
   125  var _ wrapperapi.KMSCrypto = &MockKMSCrypto{}