go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/certutil/cert_bundle_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package certutil
     9  
    10  import (
    11  	"bytes"
    12  	"testing"
    13  
    14  	. "go.charczuk.com/sdk/assert"
    15  )
    16  
    17  func TestNewCertBundleFromLiterals(t *testing.T) {
    18  	bundle, err := NewCertBundle(KeyPair{
    19  		Cert: string(certLiteral),
    20  		Key:  string(keyLiteral),
    21  	})
    22  	ItsNil(t, err)
    23  	ItsNotNil(t, bundle.PrivateKey)
    24  	ItsNotNil(t, bundle.PublicKey)
    25  	ItsNotEmpty(t, bundle.Certificates)
    26  	ItsNotEmpty(t, bundle.CertificateDERs)
    27  }
    28  
    29  func TestNewCertBundleFromFiles(t *testing.T) {
    30  	bundle, err := NewCertBundle(KeyPair{
    31  		CertPath: "testdata/client.cert.pem",
    32  		KeyPath:  "testdata/client.key.pem",
    33  	})
    34  	ItsNil(t, err)
    35  	ItsNotNil(t, bundle.PrivateKey)
    36  	ItsNotNil(t, bundle.PublicKey)
    37  	ItsNotEmpty(t, bundle.Certificates)
    38  	ItsNotEmpty(t, bundle.CertificateDERs)
    39  }
    40  
    41  func TestCertBundleWriteCertPem(t *testing.T) {
    42  	bundle, err := NewCertBundle(KeyPair{
    43  		CertPath: "testdata/client.cert.pem",
    44  		KeyPath:  "testdata/client.key.pem",
    45  	})
    46  	ItsNil(t, err)
    47  
    48  	buffer := new(bytes.Buffer)
    49  	ItsNil(t, bundle.WriteCertPem(buffer))
    50  	ItsNotEqual(t, 0, buffer.Len())
    51  }
    52  
    53  func TestCertBundleWriteKeyPem(t *testing.T) {
    54  	bundle, err := NewCertBundle(KeyPair{
    55  		CertPath: "testdata/client.cert.pem",
    56  		KeyPath:  "testdata/client.key.pem",
    57  	})
    58  	ItsNil(t, err)
    59  
    60  	buffer := new(bytes.Buffer)
    61  	ItsNil(t, bundle.WriteKeyPem(buffer))
    62  	ItsNotEqual(t, 0, buffer.Len())
    63  }
    64  
    65  func TestCertBundleCommonNames(t *testing.T) {
    66  	bundle, err := NewCertBundle(KeyPair{
    67  		Cert: string(certLiteral),
    68  		Key:  string(keyLiteral),
    69  	})
    70  	ItsNil(t, err)
    71  
    72  	commonNames, err := bundle.CommonNames()
    73  	ItsNil(t, err)
    74  	ItsLen(t, commonNames, 2)
    75  }
    76  
    77  func TestCertBundle_TLSConfig(t *testing.T) {
    78  	bundle, err := NewCertBundle(KeyPair{
    79  		Cert: string(certLiteral),
    80  		Key:  string(keyLiteral),
    81  	})
    82  	ItsNil(t, err)
    83  
    84  	cfg, err := bundle.TLSConfig()
    85  	ItsNil(t, err)
    86  	ItsNotEmpty(t, cfg.Certificates)
    87  	ItsNotNil(t, cfg.RootCAs)
    88  }