github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/mocks/mocks.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mocks
    18  
    19  import (
    20  	"crypto"
    21  	"errors"
    22  	"hash"
    23  	"reflect"
    24  
    25  	"github.com/hyperledger/fabric/bccsp"
    26  )
    27  
    28  type MockBCCSP struct {
    29  	SignArgKey    bccsp.Key
    30  	SignDigestArg []byte
    31  	SignOptsArg   bccsp.SignerOpts
    32  
    33  	SignValue []byte
    34  	SignErr   error
    35  
    36  	VerifyValue bool
    37  	VerifyErr   error
    38  }
    39  
    40  func (*MockBCCSP) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
    41  	panic("Not yet implemented")
    42  }
    43  
    44  func (*MockBCCSP) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
    45  	panic("Not yet implemented")
    46  }
    47  
    48  func (*MockBCCSP) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
    49  	panic("Not yet implemented")
    50  }
    51  
    52  func (*MockBCCSP) GetKey(ski []byte) (k bccsp.Key, err error) {
    53  	panic("Not yet implemented")
    54  }
    55  
    56  func (*MockBCCSP) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) {
    57  	panic("Not yet implemented")
    58  }
    59  
    60  func (*MockBCCSP) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
    61  	panic("Not yet implemented")
    62  }
    63  
    64  func (b *MockBCCSP) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
    65  	if !reflect.DeepEqual(b.SignArgKey, k) {
    66  		return nil, errors.New("invalid key")
    67  	}
    68  	if !reflect.DeepEqual(b.SignDigestArg, digest) {
    69  		return nil, errors.New("invalid digest")
    70  	}
    71  	if !reflect.DeepEqual(b.SignOptsArg, opts) {
    72  		return nil, errors.New("invalid opts")
    73  	}
    74  
    75  	return b.SignValue, b.SignErr
    76  }
    77  
    78  func (b *MockBCCSP) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
    79  	return b.VerifyValue, b.VerifyErr
    80  }
    81  
    82  func (*MockBCCSP) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
    83  	panic("Not yet implemented")
    84  }
    85  
    86  func (*MockBCCSP) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
    87  	panic("Not yet implemented")
    88  }
    89  
    90  type MockKey struct {
    91  	BytesValue []byte
    92  	BytesErr   error
    93  	Symm       bool
    94  	PK         bccsp.Key
    95  	PKErr      error
    96  }
    97  
    98  func (m *MockKey) Bytes() ([]byte, error) {
    99  	return m.BytesValue, m.BytesErr
   100  }
   101  
   102  func (*MockKey) SKI() []byte {
   103  	panic("Not yet implemented")
   104  }
   105  
   106  func (m *MockKey) Symmetric() bool {
   107  	return m.Symm
   108  }
   109  
   110  func (*MockKey) Private() bool {
   111  	panic("Not yet implemented")
   112  }
   113  
   114  func (m *MockKey) PublicKey() (bccsp.Key, error) {
   115  	return m.PK, m.PKErr
   116  }
   117  
   118  type SignerOpts struct {
   119  	HashFuncValue crypto.Hash
   120  }
   121  
   122  func (o *SignerOpts) HashFunc() crypto.Hash {
   123  	return o.HashFuncValue
   124  }
   125  
   126  type KeyGenOpts struct {
   127  	EphemeralValue bool
   128  }
   129  
   130  func (*KeyGenOpts) Algorithm() string {
   131  	return "Mock KeyGenOpts"
   132  }
   133  
   134  func (o *KeyGenOpts) Ephemeral() bool {
   135  	return o.EphemeralValue
   136  }
   137  
   138  type KeyStore struct {
   139  	GetKeyValue bccsp.Key
   140  	GetKeyErr   error
   141  	StoreKeyErr error
   142  }
   143  
   144  func (*KeyStore) ReadOnly() bool {
   145  	panic("Not yet implemented")
   146  }
   147  
   148  func (ks *KeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
   149  	return ks.GetKeyValue, ks.GetKeyErr
   150  }
   151  
   152  func (ks *KeyStore) StoreKey(k bccsp.Key) (err error) {
   153  	return ks.StoreKeyErr
   154  }
   155  
   156  type KeyImportOpts struct{}
   157  
   158  func (*KeyImportOpts) Algorithm() string {
   159  	return "Mock KeyImportOpts"
   160  }
   161  
   162  func (*KeyImportOpts) Ephemeral() bool {
   163  	panic("Not yet implemented")
   164  }
   165  
   166  type EncrypterOpts struct{}
   167  
   168  type HashOpts struct{}
   169  
   170  func (HashOpts) Algorithm() string {
   171  	return "Mock HashOpts"
   172  }
   173  
   174  type KeyDerivOpts struct {
   175  	EphemeralValue bool
   176  }
   177  
   178  func (*KeyDerivOpts) Algorithm() string {
   179  	return "Mock KeyDerivOpts"
   180  }
   181  
   182  func (o *KeyDerivOpts) Ephemeral() bool {
   183  	return o.EphemeralValue
   184  }