cert-manager/pkg/util/pki/parse_test.go
James Munnelly 51195e4c5f Update license header and add header to every file
Signed-off-by: James Munnelly <james.munnelly@jetstack.io>
2018-08-13 15:53:37 +01:00

135 lines
3.4 KiB
Go

/*
Copyright 2018 The Jetstack cert-manager contributors.
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 pki
import (
"crypto/ecdsa"
"crypto/rsa"
"encoding/pem"
"strings"
"testing"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
)
func generatePrivateKeyBytes(keyAlgo v1alpha1.KeyAlgorithm, keySize int) ([]byte, error) {
privateKey, err := GeneratePrivateKeyForCertificate(buildCertificateWithKeyParams(keyAlgo, keySize))
if err != nil {
return nil, err
}
return EncodePrivateKey(privateKey)
}
func TestDecodePrivateKeyBytes(t *testing.T) {
type testT struct {
name string
keyBytes []byte
keyAlgo v1alpha1.KeyAlgorithm
expectErr bool
expectErrStr string
}
rsaKeyBytes, err := generatePrivateKeyBytes(v1alpha1.RSAKeyAlgorithm, MinRSAKeySize)
if err != nil {
t.Errorf("error generating key bytes: %s", err)
return
}
ecdsaKeyBytes, err := generatePrivateKeyBytes(v1alpha1.ECDSAKeyAlgorithm, 256)
if err != nil {
t.Errorf("error generating key bytes: %s", err)
return
}
block := &pem.Block{Type: "BLAH BLAH BLAH", Bytes: []byte("blahblahblah")}
blahKeyBytes := pem.EncodeToMemory(block)
invalidKeyBytes := []byte("blah-blah-invalid")
tests := []testT{
{
name: "decode pem encoded rsa private key bytes",
keyBytes: rsaKeyBytes,
keyAlgo: v1alpha1.RSAKeyAlgorithm,
expectErr: false,
},
{
name: "decode pem encoded ecdsa private key bytes",
keyBytes: ecdsaKeyBytes,
keyAlgo: v1alpha1.ECDSAKeyAlgorithm,
expectErr: false,
},
{
name: "fail to decode unknown pem encoded key bytes",
keyBytes: blahKeyBytes,
expectErr: true,
expectErrStr: "unknown private key type",
},
{
name: "fail to decode unknown not pem encoded key bytes",
keyBytes: invalidKeyBytes,
expectErr: true,
expectErrStr: "error decoding private key PEM block",
},
}
testFn := func(test testT) func(*testing.T) {
return func(t *testing.T) {
privateKey, err := DecodePrivateKeyBytes(test.keyBytes)
if test.expectErr {
if err == nil {
t.Error("expected err, but got no error")
return
}
if !strings.Contains(err.Error(), test.expectErrStr) {
t.Errorf("expected err string to match: '%s', got: '%s'", test.expectErrStr, err.Error())
return
}
}
if !test.expectErr {
if err != nil {
t.Errorf("expected no err, but got '%q'", err)
return
}
if test.keyAlgo == v1alpha1.RSAKeyAlgorithm {
_, ok := privateKey.(*rsa.PrivateKey)
if !ok {
t.Errorf("expected rsa private key, but got %T", privateKey)
return
}
}
if test.keyAlgo == v1alpha1.ECDSAKeyAlgorithm {
_, ok := privateKey.(*ecdsa.PrivateKey)
if !ok {
t.Errorf("expected ecdsa private key, but got %T", privateKey)
return
}
}
}
}
}
for _, test := range tests {
t.Run(test.name, testFn(test))
}
}