Skip to content

Commit

Permalink
[FAB-3441] bccsp/factory improved test coverage
Browse files Browse the repository at this point in the history
This change-set brings test coverage of
the bccsp/factory package to up 85%

Change-Id: I6116343d361584258d91528066f09d4b3d716439
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed May 17, 2017
1 parent 68a1517 commit cbf1a3c
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 32 deletions.
26 changes: 26 additions & 0 deletions bccsp/factory/opts_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
70 changes: 38 additions & 32 deletions bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
73 changes: 73 additions & 0 deletions bccsp/factory/pkcs11_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
81 changes: 81 additions & 0 deletions bccsp/factory/pkcs11factory_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
70 changes: 70 additions & 0 deletions bccsp/factory/swfactory_test.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit cbf1a3c

Please sign in to comment.