@@ -40,6 +40,8 @@ import (
40
40
"github.com/hyperledger/fabric/bccsp/signer"
41
41
"github.com/hyperledger/fabric/bccsp/sw"
42
42
"github.com/hyperledger/fabric/bccsp/utils"
43
+ "github.com/op/go-logging"
44
+ "github.com/stretchr/testify/assert"
43
45
"golang.org/x/crypto/sha3"
44
46
)
45
47
@@ -57,6 +59,9 @@ type testConfig struct {
57
59
}
58
60
59
61
func TestMain (m * testing.M ) {
62
+ // Activate DEBUG level to cover listAttrs function
63
+ logging .SetLevel (logging .DEBUG , "bccsp_p11" )
64
+
60
65
ks , err := sw .NewFileBasedKeyStore (nil , os .TempDir (), false )
61
66
if err != nil {
62
67
fmt .Printf ("Failed initiliazing KeyStore [%s]" , err )
@@ -107,6 +112,61 @@ func TestMain(m *testing.M) {
107
112
os .Exit (0 )
108
113
}
109
114
115
+ func TestNew (t * testing.T ) {
116
+ opts := PKCS11Opts {
117
+ HashFamily : "SHA2" ,
118
+ SecLevel : 256 ,
119
+ SoftVerify : false ,
120
+ Sensitive : true ,
121
+ Library : "lib" ,
122
+ Label : "ForFabric" ,
123
+ Pin : "98765432" ,
124
+ }
125
+
126
+ // Setup PKCS11 library and provide initial set of values
127
+ lib , _ , _ := FindPKCS11Lib ()
128
+ opts .Library = lib
129
+
130
+ // Test for nil keystore
131
+ _ , err := New (opts , nil )
132
+ assert .Error (t , err )
133
+ assert .Contains (t , err .Error (), "Invalid bccsp.KeyStore instance. It must be different from nil." )
134
+
135
+ // Test for invalid PKCS11 loadLib
136
+ opts .Library = ""
137
+ _ , err = New (opts , currentKS )
138
+ assert .Error (t , err )
139
+ assert .Contains (t , err .Error (), "Failed initializing PKCS11 library" )
140
+ }
141
+
142
+ func TestFindPKCS11LibEnvVars (t * testing.T ) {
143
+ const (
144
+ dummy_PKCS11_LIB = "/usr/lib/pkcs11"
145
+ dummy_PKCS11_PIN = "98765432"
146
+ dummy_PKCS11_LABEL = "testing"
147
+ )
148
+
149
+ // Set environment variables used for test and preserve
150
+ // original values for restoration after test completion
151
+ orig_PKCS11_LIB := os .Getenv ("PKCS11_LIB" )
152
+ os .Setenv ("PKCS11_LIB" , dummy_PKCS11_LIB )
153
+
154
+ orig_PKCS11_PIN := os .Getenv ("PKCS11_PIN" )
155
+ os .Setenv ("PKCS11_PIN" , dummy_PKCS11_PIN )
156
+
157
+ orig_PKCS11_LABEL := os .Getenv ("PKCS11_LABEL" )
158
+ os .Setenv ("PKCS11_LABEL" , dummy_PKCS11_LABEL )
159
+
160
+ lib , pin , label := FindPKCS11Lib ()
161
+ assert .EqualValues (t , dummy_PKCS11_LIB , lib , "FindPKCS11Lib did not return expected library" )
162
+ assert .EqualValues (t , dummy_PKCS11_PIN , pin , "FindPKCS11Lib did not return expected pin" )
163
+ assert .EqualValues (t , dummy_PKCS11_LABEL , label , "FindPKCS11Lib did not return expected label" )
164
+
165
+ os .Setenv ("PKCS11_LIB" , orig_PKCS11_LIB )
166
+ os .Setenv ("PKCS11_PIN" , orig_PKCS11_PIN )
167
+ os .Setenv ("PKCS11_LABEL" , orig_PKCS11_LABEL )
168
+ }
169
+
110
170
func TestInvalidNewParameter (t * testing.T ) {
111
171
lib , pin , label := FindPKCS11Lib ()
112
172
opts := PKCS11Opts {
0 commit comments