From cbf1a3cc72b451dd4be98a70c0647c1fc864e7ba Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Wed, 17 May 2017 17:37:52 +0200 Subject: [PATCH] [FAB-3441] bccsp/factory improved test coverage This change-set brings test coverage of the bccsp/factory package to up 85% Change-Id: I6116343d361584258d91528066f09d4b3d716439 Signed-off-by: Angelo De Caro --- bccsp/factory/opts_test.go | 26 +++++++++ bccsp/factory/pkcs11.go | 70 +++++++++++++------------ bccsp/factory/pkcs11_test.go | 73 ++++++++++++++++++++++++++ bccsp/factory/pkcs11factory_test.go | 81 +++++++++++++++++++++++++++++ bccsp/factory/swfactory_test.go | 70 +++++++++++++++++++++++++ 5 files changed, 288 insertions(+), 32 deletions(-) create mode 100644 bccsp/factory/opts_test.go create mode 100644 bccsp/factory/pkcs11_test.go create mode 100644 bccsp/factory/pkcs11factory_test.go create mode 100644 bccsp/factory/swfactory_test.go diff --git a/bccsp/factory/opts_test.go b/bccsp/factory/opts_test.go new file mode 100644 index 00000000000..65ccf07eacc --- /dev/null +++ b/bccsp/factory/opts_test.go @@ -0,0 +1,26 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package factory + +import ( + "testing" + + "github.com/docker/docker/pkg/testutil/assert" +) + +func TestFactoryOptsFactoryName(t *testing.T) { + assert.Equal(t, DefaultOpts.FactoryName(), "SW") +} diff --git a/bccsp/factory/pkcs11.go b/bccsp/factory/pkcs11.go index cafdd3a6f08..b8b8b9546e2 100644 --- a/bccsp/factory/pkcs11.go +++ b/bccsp/factory/pkcs11.go @@ -36,46 +36,52 @@ type FactoryOpts struct { // Error is returned only if defaultBCCSP cannot be found func InitFactories(config *FactoryOpts) error { factoriesInitOnce.Do(func() { - // Take some precautions on default opts - if config == nil { - config = &DefaultOpts - } + setFactories(config) + }) - if config.ProviderName == "" { - config.ProviderName = "SW" - } + return factoriesInitError +} - if config.SwOpts == nil { - config.SwOpts = DefaultOpts.SwOpts - } +func setFactories(config *FactoryOpts) error { + // Take some precautions on default opts + if config == nil { + config = &DefaultOpts + } + + if config.ProviderName == "" { + config.ProviderName = "SW" + } - // Initialize factories map - bccspMap = make(map[string]bccsp.BCCSP) + if config.SwOpts == nil { + config.SwOpts = DefaultOpts.SwOpts + } - // Software-Based BCCSP - if config.SwOpts != nil { - f := &SWFactory{} - err := initBCCSP(f, config) - if err != nil { - factoriesInitError = fmt.Errorf("[%s]", err) - } - } + // Initialize factories map + bccspMap = make(map[string]bccsp.BCCSP) - // PKCS11-Based BCCSP - if config.Pkcs11Opts != nil { - f := &PKCS11Factory{} - err := initBCCSP(f, config) - if err != nil { - factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err) - } + // Software-Based BCCSP + if config.SwOpts != nil { + f := &SWFactory{} + err := initBCCSP(f, config) + if err != nil { + factoriesInitError = fmt.Errorf("Failed initializing SW.BCCSP [%s]", err) } + } - var ok bool - defaultBCCSP, ok = bccspMap[config.ProviderName] - if !ok { - factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName) + // PKCS11-Based BCCSP + if config.Pkcs11Opts != nil { + f := &PKCS11Factory{} + err := initBCCSP(f, config) + if err != nil { + factoriesInitError = fmt.Errorf("Failed initializing PKCS11.BCCSP %s\n[%s]", factoriesInitError, err) } - }) + } + + var ok bool + defaultBCCSP, ok = bccspMap[config.ProviderName] + if !ok { + factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName) + } return factoriesInitError } diff --git a/bccsp/factory/pkcs11_test.go b/bccsp/factory/pkcs11_test.go new file mode 100644 index 00000000000..05ff65b50a0 --- /dev/null +++ b/bccsp/factory/pkcs11_test.go @@ -0,0 +1,73 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package factory + +import ( + "os" + "testing" + + "github.com/hyperledger/fabric/bccsp/pkcs11" + "github.com/stretchr/testify/assert" +) + +func TestInitFactories(t *testing.T) { + err := InitFactories(nil) + assert.NoError(t, err) +} + +func TestSetFactories(t *testing.T) { + err := setFactories(nil) + assert.NoError(t, err) + + err = setFactories(&FactoryOpts{}) + assert.NoError(t, err) +} + +func TestSetFactoriesInvalidArgs(t *testing.T) { + err := setFactories(&FactoryOpts{ + ProviderName: "SW", + SwOpts: &SwOpts{}, + }) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP") + + err = setFactories(&FactoryOpts{ + ProviderName: "PKCS11", + Pkcs11Opts: &pkcs11.PKCS11Opts{}, + }) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP") +} + +func TestGetBCCSPFromOpts(t *testing.T) { + opts := &DefaultOpts + opts.SwOpts.FileKeystore = &FileKeystoreOpts{KeyStorePath: os.TempDir()} + opts.SwOpts.Ephemeral = false + csp, err := GetBCCSPFromOpts(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) + + csp, err = GetBCCSPFromOpts(&FactoryOpts{ + ProviderName: "PKCS11", + Pkcs11Opts: &pkcs11.PKCS11Opts{ + SecLevel: 256, + HashFamily: "SHA2", + Ephemeral: true, + }, + }) + assert.NoError(t, err) + assert.NotNil(t, csp) +} diff --git a/bccsp/factory/pkcs11factory_test.go b/bccsp/factory/pkcs11factory_test.go new file mode 100644 index 00000000000..63a7ddad633 --- /dev/null +++ b/bccsp/factory/pkcs11factory_test.go @@ -0,0 +1,81 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package factory + +import ( + "os" + "testing" + + "github.com/hyperledger/fabric/bccsp/pkcs11" + "github.com/stretchr/testify/assert" +) + +func TestPKCS11FactoryName(t *testing.T) { + f := &PKCS11Factory{} + assert.Equal(t, f.Name(), PKCS11BasedFactoryName) +} + +func TestPKCS11FactoryGetInvalidArgs(t *testing.T) { + f := &PKCS11Factory{} + + _, err := f.Get(nil) + assert.Error(t, err, "Invalid config. It must not be nil.") + + _, err = f.Get(&FactoryOpts{}) + assert.Error(t, err, "Invalid config. It must not be nil.") + + opts := &FactoryOpts{ + Pkcs11Opts: &pkcs11.PKCS11Opts{}, + } + _, err = f.Get(opts) + assert.Error(t, err, "CSP:500 - Failed initializing configuration at [0,]") +} + +func TestPKCS11FactoryGet(t *testing.T) { + f := &PKCS11Factory{} + + opts := &FactoryOpts{ + Pkcs11Opts: &pkcs11.PKCS11Opts{ + SecLevel: 256, + HashFamily: "SHA2", + }, + } + csp, err := f.Get(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) + + opts = &FactoryOpts{ + Pkcs11Opts: &pkcs11.PKCS11Opts{ + SecLevel: 256, + HashFamily: "SHA2", + FileKeystore: &pkcs11.FileKeystoreOpts{KeyStorePath: os.TempDir()}, + }, + } + csp, err = f.Get(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) + + opts = &FactoryOpts{ + Pkcs11Opts: &pkcs11.PKCS11Opts{ + SecLevel: 256, + HashFamily: "SHA2", + Ephemeral: true, + }, + } + csp, err = f.Get(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) +} diff --git a/bccsp/factory/swfactory_test.go b/bccsp/factory/swfactory_test.go new file mode 100644 index 00000000000..31c05d33fe9 --- /dev/null +++ b/bccsp/factory/swfactory_test.go @@ -0,0 +1,70 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package factory + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSWFactoryName(t *testing.T) { + f := &SWFactory{} + assert.Equal(t, f.Name(), SoftwareBasedFactoryName) +} + +func TestSWFactoryGetInvalidArgs(t *testing.T) { + f := &SWFactory{} + + _, err := f.Get(nil) + assert.Error(t, err, "Invalid config. It must not be nil.") + + _, err = f.Get(&FactoryOpts{}) + assert.Error(t, err, "Invalid config. It must not be nil.") + + opts := &FactoryOpts{ + SwOpts: &SwOpts{}, + } + _, err = f.Get(opts) + assert.Error(t, err, "CSP:500 - Failed initializing configuration at [0,]") +} + +func TestSWFactoryGet(t *testing.T) { + f := &SWFactory{} + + opts := &FactoryOpts{ + SwOpts: &SwOpts{ + SecLevel: 256, + HashFamily: "SHA2", + }, + } + csp, err := f.Get(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) + + opts = &FactoryOpts{ + SwOpts: &SwOpts{ + SecLevel: 256, + HashFamily: "SHA2", + FileKeystore: &FileKeystoreOpts{KeyStorePath: os.TempDir()}, + }, + } + csp, err = f.Get(opts) + assert.NoError(t, err) + assert.NotNil(t, csp) + +}