go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/certutil/key_pair_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 "os" 12 "testing" 13 14 . "go.charczuk.com/sdk/assert" 15 ) 16 17 func mustBytes(b []byte, err error) []byte { 18 if err != nil { 19 panic(err) 20 } 21 return b 22 } 23 24 func TestKeyPairIsZero(t *testing.T) { 25 ItsEqual(t, true, KeyPair{}.IsZero()) 26 ItsEqual(t, false, KeyPair{Cert: "foo"}.IsZero()) 27 ItsEqual(t, false, KeyPair{Key: "foo"}.IsZero()) 28 ItsEqual(t, false, KeyPair{CertPath: "foo"}.IsZero()) 29 ItsEqual(t, false, KeyPair{KeyPath: "foo"}.IsZero()) 30 } 31 32 func TestKeyPairCertBytes(t *testing.T) { 33 ItsEqual(t, "foo", mustBytes(KeyPair{Cert: "foo"}.CertBytes())) 34 ItsEqual(t, mustBytes(os.ReadFile("testdata/client.cert.pem")), mustBytes(KeyPair{CertPath: "testdata/client.cert.pem"}.CertBytes())) 35 } 36 37 func TestKeyPairKeyBytes(t *testing.T) { 38 ItsEqual(t, "foo", mustBytes(KeyPair{Key: "foo"}.KeyBytes())) 39 ItsEqual(t, mustBytes(os.ReadFile("testdata/client.key.pem")), mustBytes(KeyPair{KeyPath: "testdata/client.key.pem"}.KeyBytes())) 40 } 41 42 func TestKeyPairString(t *testing.T) { 43 ItsEqual(t, "[ cert: <literal>, key: <literal> ]", KeyPair{Cert: "bar", Key: "foo"}.String()) 44 ItsEqual(t, "[ cert: bar, key: foo ]", KeyPair{CertPath: "bar", KeyPath: "foo"}.String()) 45 } 46 47 func TestKeyPairTLSCertificate(t *testing.T) { 48 kp := KeyPair{ 49 CertPath: "testdata/server.cert.pem", 50 KeyPath: "testdata/server.key.pem", 51 } 52 53 cert, err := kp.TLSCertificate() 54 ItsNil(t, err) 55 ItsNotNil(t, cert) 56 }