Skip to content

Commit cbf1a3c

Browse files
committed
[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 <[email protected]>
1 parent 68a1517 commit cbf1a3c

File tree

5 files changed

+288
-32
lines changed

5 files changed

+288
-32
lines changed

bccsp/factory/opts_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
package factory
17+
18+
import (
19+
"testing"
20+
21+
"github.com/docker/docker/pkg/testutil/assert"
22+
)
23+
24+
func TestFactoryOptsFactoryName(t *testing.T) {
25+
assert.Equal(t, DefaultOpts.FactoryName(), "SW")
26+
}

bccsp/factory/pkcs11.go

+38-32
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,52 @@ type FactoryOpts struct {
3636
// Error is returned only if defaultBCCSP cannot be found
3737
func InitFactories(config *FactoryOpts) error {
3838
factoriesInitOnce.Do(func() {
39-
// Take some precautions on default opts
40-
if config == nil {
41-
config = &DefaultOpts
42-
}
39+
setFactories(config)
40+
})
4341

44-
if config.ProviderName == "" {
45-
config.ProviderName = "SW"
46-
}
42+
return factoriesInitError
43+
}
4744

48-
if config.SwOpts == nil {
49-
config.SwOpts = DefaultOpts.SwOpts
50-
}
45+
func setFactories(config *FactoryOpts) error {
46+
// Take some precautions on default opts
47+
if config == nil {
48+
config = &DefaultOpts
49+
}
50+
51+
if config.ProviderName == "" {
52+
config.ProviderName = "SW"
53+
}
5154

52-
// Initialize factories map
53-
bccspMap = make(map[string]bccsp.BCCSP)
55+
if config.SwOpts == nil {
56+
config.SwOpts = DefaultOpts.SwOpts
57+
}
5458

55-
// Software-Based BCCSP
56-
if config.SwOpts != nil {
57-
f := &SWFactory{}
58-
err := initBCCSP(f, config)
59-
if err != nil {
60-
factoriesInitError = fmt.Errorf("[%s]", err)
61-
}
62-
}
59+
// Initialize factories map
60+
bccspMap = make(map[string]bccsp.BCCSP)
6361

64-
// PKCS11-Based BCCSP
65-
if config.Pkcs11Opts != nil {
66-
f := &PKCS11Factory{}
67-
err := initBCCSP(f, config)
68-
if err != nil {
69-
factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
70-
}
62+
// Software-Based BCCSP
63+
if config.SwOpts != nil {
64+
f := &SWFactory{}
65+
err := initBCCSP(f, config)
66+
if err != nil {
67+
factoriesInitError = fmt.Errorf("Failed initializing SW.BCCSP [%s]", err)
7168
}
69+
}
7270

73-
var ok bool
74-
defaultBCCSP, ok = bccspMap[config.ProviderName]
75-
if !ok {
76-
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
71+
// PKCS11-Based BCCSP
72+
if config.Pkcs11Opts != nil {
73+
f := &PKCS11Factory{}
74+
err := initBCCSP(f, config)
75+
if err != nil {
76+
factoriesInitError = fmt.Errorf("Failed initializing PKCS11.BCCSP %s\n[%s]", factoriesInitError, err)
7777
}
78-
})
78+
}
79+
80+
var ok bool
81+
defaultBCCSP, ok = bccspMap[config.ProviderName]
82+
if !ok {
83+
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
84+
}
7985

8086
return factoriesInitError
8187
}

bccsp/factory/pkcs11_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
package factory
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/bccsp/pkcs11"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestInitFactories(t *testing.T) {
27+
err := InitFactories(nil)
28+
assert.NoError(t, err)
29+
}
30+
31+
func TestSetFactories(t *testing.T) {
32+
err := setFactories(nil)
33+
assert.NoError(t, err)
34+
35+
err = setFactories(&FactoryOpts{})
36+
assert.NoError(t, err)
37+
}
38+
39+
func TestSetFactoriesInvalidArgs(t *testing.T) {
40+
err := setFactories(&FactoryOpts{
41+
ProviderName: "SW",
42+
SwOpts: &SwOpts{},
43+
})
44+
assert.Error(t, err)
45+
assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP")
46+
47+
err = setFactories(&FactoryOpts{
48+
ProviderName: "PKCS11",
49+
Pkcs11Opts: &pkcs11.PKCS11Opts{},
50+
})
51+
assert.Error(t, err)
52+
assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP")
53+
}
54+
55+
func TestGetBCCSPFromOpts(t *testing.T) {
56+
opts := &DefaultOpts
57+
opts.SwOpts.FileKeystore = &FileKeystoreOpts{KeyStorePath: os.TempDir()}
58+
opts.SwOpts.Ephemeral = false
59+
csp, err := GetBCCSPFromOpts(opts)
60+
assert.NoError(t, err)
61+
assert.NotNil(t, csp)
62+
63+
csp, err = GetBCCSPFromOpts(&FactoryOpts{
64+
ProviderName: "PKCS11",
65+
Pkcs11Opts: &pkcs11.PKCS11Opts{
66+
SecLevel: 256,
67+
HashFamily: "SHA2",
68+
Ephemeral: true,
69+
},
70+
})
71+
assert.NoError(t, err)
72+
assert.NotNil(t, csp)
73+
}

bccsp/factory/pkcs11factory_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
package factory
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/bccsp/pkcs11"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestPKCS11FactoryName(t *testing.T) {
27+
f := &PKCS11Factory{}
28+
assert.Equal(t, f.Name(), PKCS11BasedFactoryName)
29+
}
30+
31+
func TestPKCS11FactoryGetInvalidArgs(t *testing.T) {
32+
f := &PKCS11Factory{}
33+
34+
_, err := f.Get(nil)
35+
assert.Error(t, err, "Invalid config. It must not be nil.")
36+
37+
_, err = f.Get(&FactoryOpts{})
38+
assert.Error(t, err, "Invalid config. It must not be nil.")
39+
40+
opts := &FactoryOpts{
41+
Pkcs11Opts: &pkcs11.PKCS11Opts{},
42+
}
43+
_, err = f.Get(opts)
44+
assert.Error(t, err, "CSP:500 - Failed initializing configuration at [0,]")
45+
}
46+
47+
func TestPKCS11FactoryGet(t *testing.T) {
48+
f := &PKCS11Factory{}
49+
50+
opts := &FactoryOpts{
51+
Pkcs11Opts: &pkcs11.PKCS11Opts{
52+
SecLevel: 256,
53+
HashFamily: "SHA2",
54+
},
55+
}
56+
csp, err := f.Get(opts)
57+
assert.NoError(t, err)
58+
assert.NotNil(t, csp)
59+
60+
opts = &FactoryOpts{
61+
Pkcs11Opts: &pkcs11.PKCS11Opts{
62+
SecLevel: 256,
63+
HashFamily: "SHA2",
64+
FileKeystore: &pkcs11.FileKeystoreOpts{KeyStorePath: os.TempDir()},
65+
},
66+
}
67+
csp, err = f.Get(opts)
68+
assert.NoError(t, err)
69+
assert.NotNil(t, csp)
70+
71+
opts = &FactoryOpts{
72+
Pkcs11Opts: &pkcs11.PKCS11Opts{
73+
SecLevel: 256,
74+
HashFamily: "SHA2",
75+
Ephemeral: true,
76+
},
77+
}
78+
csp, err = f.Get(opts)
79+
assert.NoError(t, err)
80+
assert.NotNil(t, csp)
81+
}

bccsp/factory/swfactory_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
package factory
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestSWFactoryName(t *testing.T) {
26+
f := &SWFactory{}
27+
assert.Equal(t, f.Name(), SoftwareBasedFactoryName)
28+
}
29+
30+
func TestSWFactoryGetInvalidArgs(t *testing.T) {
31+
f := &SWFactory{}
32+
33+
_, err := f.Get(nil)
34+
assert.Error(t, err, "Invalid config. It must not be nil.")
35+
36+
_, err = f.Get(&FactoryOpts{})
37+
assert.Error(t, err, "Invalid config. It must not be nil.")
38+
39+
opts := &FactoryOpts{
40+
SwOpts: &SwOpts{},
41+
}
42+
_, err = f.Get(opts)
43+
assert.Error(t, err, "CSP:500 - Failed initializing configuration at [0,]")
44+
}
45+
46+
func TestSWFactoryGet(t *testing.T) {
47+
f := &SWFactory{}
48+
49+
opts := &FactoryOpts{
50+
SwOpts: &SwOpts{
51+
SecLevel: 256,
52+
HashFamily: "SHA2",
53+
},
54+
}
55+
csp, err := f.Get(opts)
56+
assert.NoError(t, err)
57+
assert.NotNil(t, csp)
58+
59+
opts = &FactoryOpts{
60+
SwOpts: &SwOpts{
61+
SecLevel: 256,
62+
HashFamily: "SHA2",
63+
FileKeystore: &FileKeystoreOpts{KeyStorePath: os.TempDir()},
64+
},
65+
}
66+
csp, err = f.Get(opts)
67+
assert.NoError(t, err)
68+
assert.NotNil(t, csp)
69+
70+
}

0 commit comments

Comments
 (0)