github.com/lzy4123/fabric@v2.1.1+incompatible/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 "bytes" 21 "crypto" 22 "errors" 23 "hash" 24 "reflect" 25 26 "github.com/hyperledger/fabric/bccsp" 27 ) 28 29 type MockBCCSP struct { 30 SignArgKey bccsp.Key 31 SignDigestArg []byte 32 SignOptsArg bccsp.SignerOpts 33 34 SignValue []byte 35 SignErr error 36 37 VerifyValue bool 38 VerifyErr error 39 40 ExpectedSig []byte 41 42 KeyImportValue bccsp.Key 43 KeyImportErr error 44 45 EncryptError error 46 DecryptError error 47 48 HashVal []byte 49 HashErr error 50 } 51 52 func (*MockBCCSP) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) { 53 panic("Not yet implemented") 54 } 55 56 func (*MockBCCSP) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (bccsp.Key, error) { 57 panic("Not yet implemented") 58 } 59 60 func (m *MockBCCSP) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 61 return m.KeyImportValue, m.KeyImportErr 62 } 63 64 func (*MockBCCSP) GetKey(ski []byte) (bccsp.Key, error) { 65 panic("Not yet implemented") 66 } 67 68 func (m *MockBCCSP) Hash(msg []byte, opts bccsp.HashOpts) ([]byte, error) { 69 return m.HashVal, m.HashErr 70 } 71 72 func (*MockBCCSP) GetHash(opts bccsp.HashOpts) (hash.Hash, error) { 73 panic("Not yet implemented") 74 } 75 76 func (b *MockBCCSP) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) ([]byte, error) { 77 if !reflect.DeepEqual(b.SignArgKey, k) { 78 return nil, errors.New("invalid key") 79 } 80 if !reflect.DeepEqual(b.SignDigestArg, digest) { 81 return nil, errors.New("invalid digest") 82 } 83 if !reflect.DeepEqual(b.SignOptsArg, opts) { 84 return nil, errors.New("invalid opts") 85 } 86 87 return b.SignValue, b.SignErr 88 } 89 90 func (b *MockBCCSP) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (bool, error) { 91 // we want to mock a success 92 if b.VerifyValue { 93 return b.VerifyValue, nil 94 } 95 96 // we want to mock a failure because of an error 97 if b.VerifyErr != nil { 98 return b.VerifyValue, b.VerifyErr 99 } 100 101 // in neither case, compare the signature with the expected one 102 return bytes.Equal(b.ExpectedSig, signature), nil 103 } 104 105 func (m *MockBCCSP) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) ([]byte, error) { 106 if m.EncryptError == nil { 107 return plaintext, nil 108 } else { 109 return nil, m.EncryptError 110 } 111 } 112 113 func (m *MockBCCSP) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) ([]byte, error) { 114 if m.DecryptError == nil { 115 return ciphertext, nil 116 } else { 117 return nil, m.DecryptError 118 } 119 } 120 121 type MockKey struct { 122 BytesValue []byte 123 BytesErr error 124 Symm bool 125 PK bccsp.Key 126 PKErr error 127 Pvt bool 128 } 129 130 func (m *MockKey) Bytes() ([]byte, error) { 131 return m.BytesValue, m.BytesErr 132 } 133 134 func (*MockKey) SKI() []byte { 135 panic("Not yet implemented") 136 } 137 138 func (m *MockKey) Symmetric() bool { 139 return m.Symm 140 } 141 142 func (m *MockKey) Private() bool { 143 return m.Pvt 144 } 145 146 func (m *MockKey) PublicKey() (bccsp.Key, error) { 147 return m.PK, m.PKErr 148 } 149 150 type SignerOpts struct { 151 HashFuncValue crypto.Hash 152 } 153 154 func (o *SignerOpts) HashFunc() crypto.Hash { 155 return o.HashFuncValue 156 } 157 158 type KeyGenOpts struct { 159 EphemeralValue bool 160 } 161 162 func (*KeyGenOpts) Algorithm() string { 163 return "Mock KeyGenOpts" 164 } 165 166 func (o *KeyGenOpts) Ephemeral() bool { 167 return o.EphemeralValue 168 } 169 170 type KeyStore struct { 171 GetKeyValue bccsp.Key 172 GetKeyErr error 173 StoreKeyErr error 174 } 175 176 func (*KeyStore) ReadOnly() bool { 177 panic("Not yet implemented") 178 } 179 180 func (ks *KeyStore) GetKey(ski []byte) (bccsp.Key, error) { 181 return ks.GetKeyValue, ks.GetKeyErr 182 } 183 184 func (ks *KeyStore) StoreKey(k bccsp.Key) error { 185 return ks.StoreKeyErr 186 } 187 188 type KeyImportOpts struct{} 189 190 func (*KeyImportOpts) Algorithm() string { 191 return "Mock KeyImportOpts" 192 } 193 194 func (*KeyImportOpts) Ephemeral() bool { 195 panic("Not yet implemented") 196 } 197 198 type EncrypterOpts struct{} 199 type DecrypterOpts struct{} 200 201 type HashOpts struct{} 202 203 func (HashOpts) Algorithm() string { 204 return "Mock HashOpts" 205 } 206 207 type KeyDerivOpts struct { 208 EphemeralValue bool 209 } 210 211 func (*KeyDerivOpts) Algorithm() string { 212 return "Mock KeyDerivOpts" 213 } 214 215 func (o *KeyDerivOpts) Ephemeral() bool { 216 return o.EphemeralValue 217 }