github.com/blend/go-sdk@v1.20220411.3/certutil/cert_bundle_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  	"bytes"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestNewCertBundleFromLiterals(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	bundle, err := NewCertBundle(KeyPair{
    21  		Cert: string(certLiteral),
    22  		Key:  string(keyLiteral),
    23  	})
    24  
    25  	assert.Nil(err)
    26  	assert.NotNil(bundle.PrivateKey)
    27  	assert.NotNil(bundle.PublicKey)
    28  	assert.NotEmpty(bundle.Certificates)
    29  	assert.NotEmpty(bundle.CertificateDERs)
    30  }
    31  
    32  func TestNewCertBundleFromFiles(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	bundle, err := NewCertBundle(KeyPair{
    36  		CertPath: "testdata/client.cert.pem",
    37  		KeyPath:  "testdata/client.key.pem",
    38  	})
    39  	assert.Nil(err)
    40  	assert.NotNil(bundle.PrivateKey)
    41  	assert.NotNil(bundle.PublicKey)
    42  	assert.NotEmpty(bundle.Certificates)
    43  	assert.NotEmpty(bundle.CertificateDERs)
    44  }
    45  
    46  func TestCertBundleWriteCertPem(t *testing.T) {
    47  	assert := assert.New(t)
    48  
    49  	bundle, err := NewCertBundle(KeyPair{
    50  		CertPath: "testdata/client.cert.pem",
    51  		KeyPath:  "testdata/client.key.pem",
    52  	})
    53  	assert.Nil(err)
    54  
    55  	buffer := new(bytes.Buffer)
    56  	assert.Nil(bundle.WriteCertPem(buffer))
    57  	assert.NotZero(buffer.Len())
    58  }
    59  
    60  func TestCertBundleWriteKeyPem(t *testing.T) {
    61  	assert := assert.New(t)
    62  
    63  	bundle, err := NewCertBundle(KeyPair{
    64  		CertPath: "testdata/client.cert.pem",
    65  		KeyPath:  "testdata/client.key.pem",
    66  	})
    67  	assert.Nil(err)
    68  
    69  	buffer := new(bytes.Buffer)
    70  	assert.Nil(bundle.WriteKeyPem(buffer))
    71  	assert.NotZero(buffer.Len())
    72  }
    73  
    74  func TestCertBundleCommonNames(t *testing.T) {
    75  	assert := assert.New(t)
    76  
    77  	bundle, err := NewCertBundle(KeyPair{
    78  		Cert: string(certLiteral),
    79  		Key:  string(keyLiteral),
    80  	})
    81  	assert.Nil(err)
    82  
    83  	commonNames, err := bundle.CommonNames()
    84  	assert.Nil(err)
    85  	assert.Len(commonNames, 2)
    86  }
    87  
    88  func TestCertBundle_ServerConfig(t *testing.T) {
    89  	assert := assert.New(t)
    90  
    91  	bundle, err := NewCertBundle(KeyPair{
    92  		Cert: string(certLiteral),
    93  		Key:  string(keyLiteral),
    94  	})
    95  	assert.Nil(err)
    96  
    97  	cfg, err := bundle.ServerConfig()
    98  	assert.Nil(err)
    99  	assert.NotEmpty(cfg.Certificates)
   100  	assert.NotNil(cfg.RootCAs)
   101  }