github.com/blend/go-sdk@v1.20240719.1/webutil/cert_info_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - 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 webutil
     9  
    10  import (
    11  	"crypto/tls"
    12  	"crypto/x509"
    13  	"crypto/x509/pkix"
    14  	"net/http"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  )
    20  
    21  func TestParseCertInfo(t *testing.T) {
    22  	assert := assert.New(t)
    23  
    24  	// handle the empty cases
    25  	assert.Nil(ParseCertInfo(nil))
    26  	assert.Nil(ParseCertInfo(&http.Response{}))
    27  	assert.Nil(ParseCertInfo(&http.Response{
    28  		TLS: &tls.ConnectionState{},
    29  	}))
    30  
    31  	valid := &http.Response{
    32  		TLS: &tls.ConnectionState{
    33  			PeerCertificates: []*x509.Certificate{
    34  				{
    35  					Issuer: pkix.Name{
    36  						CommonName: "example-string dog",
    37  					},
    38  					DNSNames:  []string{"foo.local"},
    39  					NotAfter:  time.Now().UTC().AddDate(0, 1, 0),
    40  					NotBefore: time.Now().UTC().AddDate(0, -1, 0),
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	info := ParseCertInfo(valid)
    47  	assert.NotNil(info)
    48  	assert.Equal("example-string dog", info.IssuerCommonName)
    49  	assert.Equal([]string{"foo.local"}, info.DNSNames)
    50  	assert.False(info.NotAfter.IsZero())
    51  	assert.False(info.NotBefore.IsZero())
    52  	assert.True(info.NotAfter.After(time.Now().UTC()))
    53  	assert.True(info.NotBefore.Before(time.Now().UTC()))
    54  
    55  	assert.False(info.IsExpired())
    56  	assert.False(info.WillBeExpired(time.Now().UTC()))
    57  }