coredns/middleware/tls/tls_test.go
John Belamaric b10a4f9075 Common tls (#474)
* Add common TLS config routines

These routines can be used to load TLS configs based upon the
args in the Corefile.

* Add common routine for Corefile arg handling

Add the NewTLSConfigFromArgs routine so that you can just pass
in the Corefile args and get a tls.Config. This ensures the
parameters are handled consistently across middleware.

* Change to varargs style params

Change to use args ...string instead of []string. Add
documentation of what each call means.
2017-01-10 10:18:34 -05:00

81 lines
1.8 KiB
Go

package tls
import (
"testing"
"path/filepath"
"github.com/miekg/coredns/middleware/test"
)
func getPEMFiles(t *testing.T) (rmFunc func(), cert, key, ca string) {
tempDir, rmFunc, err := test.WritePEMFiles("")
if err != nil {
t.Fatalf("Could not write PEM files: %s", err)
}
cert = filepath.Join(tempDir, "cert.pem")
key = filepath.Join(tempDir, "key.pem")
ca = filepath.Join(tempDir, "ca.pem")
return
}
func TestNewTLSConfig(t *testing.T) {
rmFunc, cert, key, ca := getPEMFiles(t)
defer rmFunc()
_, err := NewTLSConfig(cert, key, ca)
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
}
func TestNewTLSClientConfig(t *testing.T) {
rmFunc, _, _, ca := getPEMFiles(t)
defer rmFunc()
_, err := NewTLSClientConfig(ca)
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
}
func TestNewTLSConfigFromArgs(t *testing.T) {
rmFunc, cert, key, ca := getPEMFiles(t)
defer rmFunc()
_, err := NewTLSConfigFromArgs()
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
c, err := NewTLSConfigFromArgs(ca)
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
if c.RootCAs == nil {
t.Error("RootCAs should not be nil when one arg passed")
}
c, err = NewTLSConfigFromArgs(cert,key)
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
if c.RootCAs != nil {
t.Error("RootCAs should be nil when two args passed")
}
if len(c.Certificates) != 1 {
t.Error("Certificates should have a single entry when two args passed")
}
args := []string{cert,key,ca}
c, err = NewTLSConfigFromArgs(args...)
if err != nil {
t.Errorf("Failed to create TLSConfig: %s", err)
}
if c.RootCAs == nil {
t.Error("RootCAs should not be nil when three args passed")
}
if len(c.Certificates) != 1 {
t.Error("Certificateis should have a single entry when three args passed")
}
}