github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/sw/sign_test.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 sw
    18  
    19  import (
    20  	"errors"
    21  	"reflect"
    22  	"testing"
    23  
    24  	mocks2 "github.com/hyperledger/fabric/bccsp/mocks"
    25  	"github.com/hyperledger/fabric/bccsp/sw/mocks"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestSign(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	expectedKey := &mocks2.MockKey{}
    33  	expectetDigest := []byte{1, 2, 3, 4}
    34  	expectedOpts := &mocks2.SignerOpts{}
    35  	expectetValue := []byte{0, 1, 2, 3, 4}
    36  	expectedErr := errors.New("Expected Error")
    37  
    38  	signers := make(map[reflect.Type]Signer)
    39  	signers[reflect.TypeOf(&mocks2.MockKey{})] = &mocks.Signer{
    40  		KeyArg:    expectedKey,
    41  		DigestArg: expectetDigest,
    42  		OptsArg:   expectedOpts,
    43  		Value:     expectetValue,
    44  		Err:       nil,
    45  	}
    46  	csp := CSP{Signers: signers}
    47  	value, err := csp.Sign(expectedKey, expectetDigest, expectedOpts)
    48  	assert.Equal(t, expectetValue, value)
    49  	assert.Nil(t, err)
    50  
    51  	signers = make(map[reflect.Type]Signer)
    52  	signers[reflect.TypeOf(&mocks2.MockKey{})] = &mocks.Signer{
    53  		KeyArg:    expectedKey,
    54  		DigestArg: expectetDigest,
    55  		OptsArg:   expectedOpts,
    56  		Value:     nil,
    57  		Err:       expectedErr,
    58  	}
    59  	csp = CSP{Signers: signers}
    60  	value, err = csp.Sign(expectedKey, expectetDigest, expectedOpts)
    61  	assert.Nil(t, value)
    62  	assert.Contains(t, err.Error(), expectedErr.Error())
    63  }