Use go:embed for easier to read tests

This commit is contained in:
Joshua Casey 2023-06-07 18:08:23 -05:00
parent 52b0cf43ca
commit f8ce2af08c

View File

@ -7,10 +7,10 @@ import (
"crypto" "crypto"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
_ "embed"
"fmt" "fmt"
"io" "io"
"net" "net"
"os"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -20,60 +20,66 @@ import (
"go.pinniped.dev/internal/testutil" "go.pinniped.dev/internal/testutil"
) )
func loadFromFiles(t *testing.T, certPath string, keyPath string) (*CA, error) { var (
t.Helper() //go:embed testdata/empty
empty string
certPEM, err := os.ReadFile(certPath) //go:embed testdata/invalid
require.NoError(t, err) invalid string
//go:embed testdata/multiple.crt
keyPEM, err := os.ReadFile(keyPath) multiple string
require.NoError(t, err) //go:embed testdata/test.crt
testCert string
ca, err := Load(string(certPEM), string(keyPEM)) //go:embed testdata/test.key
return ca, err testKey string
} //go:embed testdata/test2.key
testKey2 string
)
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
certPath string cert string
keyPath string key string
wantErr string wantErr string
test []byte
}{ }{
{ {
name: "empty key", name: "empty key",
certPath: "./testdata/test.crt", cert: testCert,
keyPath: "./testdata/empty", key: empty,
wantErr: "could not load CA: tls: failed to find any PEM data in key input", wantErr: "could not load CA: tls: failed to find any PEM data in key input",
}, },
{ {
name: "invalid key", name: "invalid key",
certPath: "./testdata/test.crt", cert: testCert,
keyPath: "./testdata/invalid", key: invalid,
wantErr: "could not load CA: tls: failed to find any PEM data in key input", wantErr: "could not load CA: tls: failed to find any PEM data in key input",
}, },
{ {
name: "mismatched cert and key", name: "mismatched cert and key",
certPath: "./testdata/test.crt", cert: testCert,
keyPath: "./testdata/test2.key", key: testKey2,
wantErr: "could not load CA: tls: private key does not match public key", wantErr: "could not load CA: tls: private key does not match public key",
}, },
{ {
name: "multiple certs", name: "multiple certs",
certPath: "./testdata/multiple.crt", cert: multiple,
keyPath: "./testdata/test.key", key: testKey,
wantErr: "invalid CA certificate: expected a single certificate, found 2 certificates", wantErr: "invalid CA certificate: expected a single certificate, found 2 certificates",
}, },
{ {
name: "success", name: "success",
certPath: "./testdata/test.crt", cert: testCert,
keyPath: "./testdata/test.key", key: testKey,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ca, err := loadFromFiles(t, tt.certPath, tt.keyPath) t.Parallel()
ca, err := Load(tt.cert, tt.key)
if tt.wantErr != "" { if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr) require.EqualError(t, err, tt.wantErr)
return return
@ -226,7 +232,7 @@ func TestIssue(t *testing.T) {
now := time.Date(2020, 7, 10, 12, 41, 12, 1234, time.UTC) now := time.Date(2020, 7, 10, 12, 41, 12, 1234, time.UTC)
realCA, err := loadFromFiles(t, "./testdata/test.crt", "./testdata/test.key") realCA, err := Load(testCert, testKey)
require.NoError(t, err) require.NoError(t, err)
tests := []struct { tests := []struct {