github.com/blend/go-sdk@v1.20220411.3/certutil/key_pair_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  	"os"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestKeyPairIsZero(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	assert.True(KeyPair{}.IsZero())
    21  	assert.False(KeyPair{Cert: "foo"}.IsZero())
    22  	assert.False(KeyPair{Key: "foo"}.IsZero())
    23  	assert.False(KeyPair{CertPath: "foo"}.IsZero())
    24  	assert.False(KeyPair{KeyPath: "foo"}.IsZero())
    25  }
    26  
    27  func TestKeyPairCertBytes(t *testing.T) {
    28  	assert := assert.New(t)
    29  
    30  	assert.Equal("foo", MustBytes(KeyPair{Cert: "foo"}.CertBytes()))
    31  	assert.Equal(MustBytes(os.ReadFile("testdata/client.cert.pem")), MustBytes(KeyPair{CertPath: "testdata/client.cert.pem"}.CertBytes()))
    32  }
    33  
    34  func TestKeyPairKeyBytes(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	assert.Equal("foo", MustBytes(KeyPair{Key: "foo"}.KeyBytes()))
    38  	assert.Equal(MustBytes(os.ReadFile("testdata/client.key.pem")), MustBytes(KeyPair{KeyPath: "testdata/client.key.pem"}.KeyBytes()))
    39  }
    40  
    41  func TestKeyPairString(t *testing.T) {
    42  	assert := assert.New(t)
    43  
    44  	assert.Equal("[ cert: <literal>, key: <literal> ]", KeyPair{Cert: "bar", Key: "foo"}.String())
    45  	assert.Equal("[ cert: bar, key: foo ]", KeyPair{CertPath: "bar", KeyPath: "foo"}.String())
    46  }
    47  
    48  func TestKeyPairTLSCertificate(t *testing.T) {
    49  	its := assert.New(t)
    50  
    51  	kp := KeyPair{
    52  		CertPath: "testdata/server.cert.pem",
    53  		KeyPath:  "testdata/server.key.pem",
    54  	}
    55  
    56  	cert, err := kp.TLSCertificate()
    57  	its.Nil(err)
    58  	its.NotNil(cert)
    59  }