github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/sw/keygen_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  	"crypto/elliptic"
    21  	"errors"
    22  	"reflect"
    23  	"testing"
    24  
    25  	mocks2 "github.com/hyperledger/fabric/bccsp/mocks"
    26  	"github.com/hyperledger/fabric/bccsp/sw/mocks"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestKeyGen(t *testing.T) {
    31  	expectedOpts := &mocks2.KeyGenOpts{EphemeralValue: true}
    32  	expectetValue := &mocks2.MockKey{}
    33  	expectedErr := errors.New("Expected Error")
    34  
    35  	keyGenerators := make(map[reflect.Type]KeyGenerator)
    36  	keyGenerators[reflect.TypeOf(&mocks2.KeyGenOpts{})] = &mocks.KeyGenerator{
    37  		OptsArg: expectedOpts,
    38  		Value:   expectetValue,
    39  		Err:     expectedErr,
    40  	}
    41  	csp := impl{keyGenerators: keyGenerators}
    42  	value, err := csp.KeyGen(expectedOpts)
    43  	assert.Nil(t, value)
    44  	assert.Contains(t, err.Error(), expectedErr.Error())
    45  
    46  	keyGenerators = make(map[reflect.Type]KeyGenerator)
    47  	keyGenerators[reflect.TypeOf(&mocks2.KeyGenOpts{})] = &mocks.KeyGenerator{
    48  		OptsArg: expectedOpts,
    49  		Value:   expectetValue,
    50  		Err:     nil,
    51  	}
    52  	csp = impl{keyGenerators: keyGenerators}
    53  	value, err = csp.KeyGen(expectedOpts)
    54  	assert.Equal(t, expectetValue, value)
    55  	assert.Nil(t, err)
    56  }
    57  
    58  func TestECDSAKeyGenerator(t *testing.T) {
    59  	kg := &ecdsaKeyGenerator{curve: elliptic.P256()}
    60  
    61  	k, err := kg.KeyGen(nil)
    62  	assert.NoError(t, err)
    63  
    64  	ecdsaK, ok := k.(*ecdsaPrivateKey)
    65  	assert.True(t, ok)
    66  	assert.NotNil(t, ecdsaK.privKey)
    67  	assert.Equal(t, ecdsaK.privKey.Curve, elliptic.P256())
    68  }
    69  
    70  func TestRSAKeyGenerator(t *testing.T) {
    71  	kg := &rsaKeyGenerator{length: 512}
    72  
    73  	k, err := kg.KeyGen(nil)
    74  	assert.NoError(t, err)
    75  
    76  	rsaK, ok := k.(*rsaPrivateKey)
    77  	assert.True(t, ok)
    78  	assert.NotNil(t, rsaK.privKey)
    79  	assert.Equal(t, rsaK.privKey.N.BitLen(), 512)
    80  }
    81  
    82  func TestAESKeyGenerator(t *testing.T) {
    83  	kg := &aesKeyGenerator{length: 32}
    84  
    85  	k, err := kg.KeyGen(nil)
    86  	assert.NoError(t, err)
    87  
    88  	aesK, ok := k.(*aesPrivateKey)
    89  	assert.True(t, ok)
    90  	assert.NotNil(t, aesK.privKey)
    91  	assert.Equal(t, len(aesK.privKey), 32)
    92  }
    93  
    94  func TestAESKeyGeneratorInvalidInputs(t *testing.T) {
    95  	kg := &aesKeyGenerator{length: -1}
    96  
    97  	_, err := kg.KeyGen(nil)
    98  	assert.Error(t, err)
    99  	assert.Contains(t, err.Error(), "Len must be larger than 0")
   100  }
   101  
   102  func TestRSAKeyGeneratorInvalidInputs(t *testing.T) {
   103  	kg := &rsaKeyGenerator{length: -1}
   104  
   105  	_, err := kg.KeyGen(nil)
   106  	assert.Error(t, err)
   107  	assert.Contains(t, err.Error(), "Failed generating RSA -1 key")
   108  }