github.com/blend/go-sdk@v1.20220411.3/certutil/pem_utils_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package certutil
     9  
    10  import (
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestParseCertPEM(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	assert := assert.New(t)
    21  
    22  	certs, err := ParseCertPEM(certLiteral)
    23  	assert.Nil(err)
    24  	assert.Len(certs, 2)
    25  	assert.Equal(certLiteralCommonName, certs[0].Subject.CommonName)
    26  
    27  	invalidCert := []byte(strings.Join([]string{
    28  		"-----BEGIN CERTIFICATE-----",
    29  		"nope",
    30  		"-----END CERTIFICATE-----",
    31  		"",
    32  	}, "\n"))
    33  	certs, err = ParseCertPEM(invalidCert)
    34  	assert.Nil(certs)
    35  	expected := "x509: malformed certificate"
    36  	assert.Equal(expected, err.Error())
    37  }
    38  
    39  func TestCommonNamesForCertPair(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	assert := assert.New(t)
    43  
    44  	kp := KeyPair{Cert: string(certLiteral)}
    45  
    46  	certPEM, err := kp.CertBytes()
    47  	assert.Nil(err)
    48  
    49  	commonNames, err := CommonNamesForCertPEM(certPEM)
    50  	assert.Nil(err)
    51  	assert.NotEmpty(commonNames)
    52  	assert.Equal(certLiteralCommonName, commonNames[0])
    53  }