github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/bccsp_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 bccsp
    18  
    19  import (
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestAESOpts(t *testing.T) {
    28  	test := func(ephemeral bool) {
    29  		for _, opts := range []KeyGenOpts{
    30  			&AES128KeyGenOpts{ephemeral},
    31  			&AES192KeyGenOpts{ephemeral},
    32  			&AES256KeyGenOpts{ephemeral},
    33  		} {
    34  			expectedAlgorithm := reflect.TypeOf(opts).String()[7:13]
    35  			assert.Equal(t, expectedAlgorithm, opts.Algorithm())
    36  			assert.Equal(t, ephemeral, opts.Ephemeral())
    37  		}
    38  	}
    39  	test(true)
    40  	test(false)
    41  
    42  	opts := &AESKeyGenOpts{true}
    43  	assert.Equal(t, "AES", opts.Algorithm())
    44  	assert.True(t, opts.Ephemeral())
    45  	opts.Temporary = false
    46  	assert.False(t, opts.Ephemeral())
    47  }
    48  
    49  func TestECDSAOpts(t *testing.T) {
    50  	test := func(ephemeral bool) {
    51  		for _, opts := range []KeyGenOpts{
    52  			&ECDSAP256KeyGenOpts{ephemeral},
    53  			&ECDSAP384KeyGenOpts{ephemeral},
    54  		} {
    55  			expectedAlgorithm := reflect.TypeOf(opts).String()[7:16]
    56  			assert.Equal(t, expectedAlgorithm, opts.Algorithm())
    57  			assert.Equal(t, ephemeral, opts.Ephemeral())
    58  		}
    59  	}
    60  	test(true)
    61  	test(false)
    62  
    63  	test = func(ephemeral bool) {
    64  		for _, opts := range []KeyGenOpts{
    65  			&ECDSAKeyGenOpts{ephemeral},
    66  			&ECDSAPKIXPublicKeyImportOpts{ephemeral},
    67  			&ECDSAPrivateKeyImportOpts{ephemeral},
    68  			&ECDSAGoPublicKeyImportOpts{ephemeral},
    69  		} {
    70  			assert.Equal(t, "ECDSA", opts.Algorithm())
    71  			assert.Equal(t, ephemeral, opts.Ephemeral())
    72  		}
    73  	}
    74  	test(true)
    75  	test(false)
    76  
    77  	opts := &ECDSAReRandKeyOpts{Temporary: true}
    78  	assert.True(t, opts.Ephemeral())
    79  	opts.Temporary = false
    80  	assert.False(t, opts.Ephemeral())
    81  	assert.Equal(t, "ECDSA_RERAND", opts.Algorithm())
    82  	assert.Empty(t, opts.ExpansionValue())
    83  }
    84  
    85  func TestHashOpts(t *testing.T) {
    86  	for _, ho := range []HashOpts{&SHA256Opts{}, &SHA384Opts{}, &SHA3_256Opts{}, &SHA3_384Opts{}} {
    87  		s := strings.Replace(reflect.TypeOf(ho).String(), "*bccsp.", "", -1)
    88  		algorithm := strings.Replace(s, "Opts", "", -1)
    89  		assert.Equal(t, algorithm, ho.Algorithm())
    90  		ho2, err := GetHashOpt(algorithm)
    91  		assert.NoError(t, err)
    92  		assert.Equal(t, ho.Algorithm(), ho2.Algorithm())
    93  	}
    94  	_, err := GetHashOpt("foo")
    95  	assert.Error(t, err)
    96  	assert.Contains(t, err.Error(), "hash function not recognized")
    97  
    98  	assert.Equal(t, "SHA", (&SHAOpts{}).Algorithm())
    99  }
   100  
   101  func TestHMAC(t *testing.T) {
   102  	opts := &HMACTruncated256AESDeriveKeyOpts{Arg: []byte("arg")}
   103  	assert.False(t, opts.Ephemeral())
   104  	opts.Temporary = true
   105  	assert.True(t, opts.Ephemeral())
   106  	assert.Equal(t, "HMAC_TRUNCATED_256", opts.Algorithm())
   107  	assert.Equal(t, []byte("arg"), opts.Argument())
   108  
   109  	opts2 := &HMACDeriveKeyOpts{Arg: []byte("arg")}
   110  	assert.False(t, opts2.Ephemeral())
   111  	opts2.Temporary = true
   112  	assert.True(t, opts2.Ephemeral())
   113  	assert.Equal(t, "HMAC", opts2.Algorithm())
   114  	assert.Equal(t, []byte("arg"), opts2.Argument())
   115  }
   116  
   117  func TestKeyGenOpts(t *testing.T) {
   118  	expectedAlgorithms := map[reflect.Type]string{
   119  		reflect.TypeOf(&HMACImportKeyOpts{}):       "HMAC",
   120  		reflect.TypeOf(&X509PublicKeyImportOpts{}): "X509Certificate",
   121  		reflect.TypeOf(&AES256ImportKeyOpts{}):     "AES",
   122  	}
   123  	test := func(ephemeral bool) {
   124  		for _, opts := range []KeyGenOpts{
   125  			&HMACImportKeyOpts{ephemeral},
   126  			&X509PublicKeyImportOpts{ephemeral},
   127  			&AES256ImportKeyOpts{ephemeral},
   128  		} {
   129  			expectedAlgorithm := expectedAlgorithms[reflect.TypeOf(opts)]
   130  			assert.Equal(t, expectedAlgorithm, opts.Algorithm())
   131  			assert.Equal(t, ephemeral, opts.Ephemeral())
   132  		}
   133  	}
   134  	test(true)
   135  	test(false)
   136  }