Skip to content

Commit 22b2324

Browse files
author
John Harrison
committed
[FAB-3772] Improve coverage for PKCS11 package (2 of 3)
This is the second of three patches to improve the coverage of the bccsp/pkcs11 package. This patch builds on top of https://gerrit.hyperledger.org/r/#/c/9441 Change-Id: I36eafcebd1a859bf548974492151f2e32b9e37c2 Signed-off-by: John Harrison <[email protected]>
1 parent 67e2c09 commit 22b2324

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

bccsp/pkcs11/pkcs11_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,91 @@ import (
2828
"github.com/stretchr/testify/assert"
2929
)
3030

31+
func TestKeyGenFailures(t *testing.T) {
32+
var testOpts bccsp.KeyGenOpts
33+
ki := currentBCCSP
34+
_, err := ki.KeyGen(testOpts)
35+
assert.Error(t, err)
36+
assert.Contains(t, err.Error(), "Invalid Opts parameter. It must not be nil.")
37+
}
38+
39+
func TestLoadLib(t *testing.T) {
40+
// Setup PKCS11 library and provide initial set of values
41+
lib, pin, label := FindPKCS11Lib()
42+
43+
// Test for no specified PKCS11 library
44+
_, _, _, err := loadLib("", pin, label)
45+
assert.Error(t, err)
46+
assert.Contains(t, err.Error(), "No PKCS11 library default")
47+
48+
// Test for invalid PKCS11 library
49+
_, _, _, err = loadLib("badLib", pin, label)
50+
assert.Error(t, err)
51+
assert.Contains(t, err.Error(), "Instantiate failed")
52+
53+
// Test for invalid label
54+
_, _, _, err = loadLib(lib, pin, "badLabel")
55+
assert.Error(t, err)
56+
assert.Contains(t, err.Error(), "Could not find token with label")
57+
58+
// Test for no pin
59+
_, _, _, err = loadLib(lib, "", label)
60+
assert.Error(t, err)
61+
assert.Contains(t, err.Error(), "No PIN set")
62+
}
63+
64+
func TestOIDFromNamedCurve(t *testing.T) {
65+
// Test for valid OID for P224
66+
testOID, boolValue := oidFromNamedCurve(elliptic.P224())
67+
assert.Equal(t, oidNamedCurveP224, testOID, "Did not receive expected OID for elliptic.P224")
68+
assert.Equal(t, true, boolValue, "Did not receive a true value when acquiring OID for elliptic.P224")
69+
70+
// Test for valid OID for P256
71+
testOID, boolValue = oidFromNamedCurve(elliptic.P256())
72+
assert.Equal(t, oidNamedCurveP256, testOID, "Did not receive expected OID for elliptic.P256")
73+
assert.Equal(t, true, boolValue, "Did not receive a true value when acquiring OID for elliptic.P256")
74+
75+
// Test for valid OID for P384
76+
testOID, boolValue = oidFromNamedCurve(elliptic.P384())
77+
assert.Equal(t, oidNamedCurveP384, testOID, "Did not receive expected OID for elliptic.P384")
78+
assert.Equal(t, true, boolValue, "Did not receive a true value when acquiring OID for elliptic.P384")
79+
80+
// Test for valid OID for P521
81+
testOID, boolValue = oidFromNamedCurve(elliptic.P521())
82+
assert.Equal(t, oidNamedCurveP521, testOID, "Did not receive expected OID for elliptic.P521")
83+
assert.Equal(t, true, boolValue, "Did not receive a true value when acquiring OID for elliptic.P521")
84+
85+
var testCurve elliptic.Curve
86+
testOID, boolValue = oidFromNamedCurve(testCurve)
87+
if testOID != nil {
88+
t.Fatal("Expected nil to be returned.")
89+
}
90+
}
91+
92+
func TestNamedCurveFromOID(t *testing.T) {
93+
// Test for valid P224 elliptic curve
94+
namedCurve := namedCurveFromOID(oidNamedCurveP224)
95+
assert.Equal(t, elliptic.P224(), namedCurve, "Did not receive expected named curve for oidNamedCurveP224")
96+
97+
// Test for valid P256 elliptic curve
98+
namedCurve = namedCurveFromOID(oidNamedCurveP256)
99+
assert.Equal(t, elliptic.P256(), namedCurve, "Did not receive expected named curve for oidNamedCurveP256")
100+
101+
// Test for valid P256 elliptic curve
102+
namedCurve = namedCurveFromOID(oidNamedCurveP384)
103+
assert.Equal(t, elliptic.P384(), namedCurve, "Did not receive expected named curve for oidNamedCurveP384")
104+
105+
// Test for valid P521 elliptic curve
106+
namedCurve = namedCurveFromOID(oidNamedCurveP521)
107+
assert.Equal(t, elliptic.P521(), namedCurve, "Did not receive expected named curved for oidNamedCurveP521")
108+
109+
testAsn1Value := asn1.ObjectIdentifier{4, 9, 15, 1}
110+
namedCurve = namedCurveFromOID(testAsn1Value)
111+
if namedCurve != nil {
112+
t.Fatal("Expected nil to be returned.")
113+
}
114+
}
115+
31116
func TestPKCS11GetSession(t *testing.T) {
32117
var sessions []pkcs11.SessionHandle
33118
for i := 0; i < 3*sessionCacheSize; i++ {

0 commit comments

Comments
 (0)