| 1 |
package x509util |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/pkg/errors" |
| 7 |
"github.com/smallstep/assert" |
| 8 |
"github.com/smallstep/cli/crypto/pemutil" |
| 9 |
) |
| 10 |
|
| 11 |
func TestLoadIdentityFromDisk(t *testing.T) { |
| 12 |
var ( |
| •13 |
testBadCert = "./testdata/badca.crt" |
| 14 |
testCert = "./testdata/ca.crt" |
| 15 |
testNoPasscodeBadKey = "./testdata/noPasscodeBadCa.key" |
| 16 |
noPasscodeKey = "./testdata/noPasscodeCa.key" |
| 17 |
) |
| 18 |
|
| 19 |
tests := map[string]struct { |
| 20 |
crtPath string |
| 21 |
keyPath string |
| 22 |
pass string |
| 23 |
err error |
| 24 |
}{ |
| 25 |
"error parsing x509 certificate": { |
| 26 |
crtPath: testBadCert, |
| 27 |
keyPath: "", |
| 28 |
pass: "", |
| •29 |
err: errors.Errorf("error parsing %s:", |
| 30 |
testBadCert), |
| 31 |
}, |
| 32 |
"error parsing rsa key": { |
| 33 |
crtPath: testCert, |
| 34 |
keyPath: testNoPasscodeBadKey, |
| 35 |
pass: "", |
| 36 |
err: errors.Errorf("error parsing %s: asn1:", testNoPasscodeBadKey), |
| 37 |
}, |
| 38 |
"success": { |
| 39 |
crtPath: testCert, |
| 40 |
keyPath: noPasscodeKey, |
| 41 |
pass: "", |
| 42 |
}, |
| 43 |
} |
| 44 |
|
| 45 |
for name, test := range tests { |
| 46 |
t.Logf("Running test case: %s", name) |
| 47 |
|
| 48 |
var ( |
| 49 |
err error |
| 50 |
i *Identity |
| 51 |
) |
| 52 |
if test.pass == "" { |
| 53 |
i, err = LoadIdentityFromDisk(test.crtPath, test.keyPath) |
| 54 |
} else { |
| 55 |
i, err = LoadIdentityFromDisk(test.crtPath, test.keyPath, |
| 56 |
pemutil.WithPassword([]byte(test.pass))) |
| 57 |
} |
| 58 |
if err != nil { |
| 59 |
if assert.NotNil(t, test.err) { |
| 60 |
assert.HasPrefix(t, err.Error(), test.err.Error()) |
| 61 |
} |
| 62 |
} else { |
| 63 |
assert.FatalError(t, err) |
| 64 |
assert.NotNil(t, i.Crt) |
| 65 |
assert.NotNil(t, i.Key) |
| 66 |
} |
| 67 |
} |
| 68 |
} |