1
16
17 package tlsutil
18
19 import (
20 "path/filepath"
21 "testing"
22 )
23
24 const tlsTestDir = "../../testdata"
25
26 const (
27 testCaCertFile = "rootca.crt"
28 testCertFile = "crt.pem"
29 testKeyFile = "key.pem"
30 )
31
32 func TestClientConfig(t *testing.T) {
33 opts := Options{
34 CaCertFile: testfile(t, testCaCertFile),
35 CertFile: testfile(t, testCertFile),
36 KeyFile: testfile(t, testKeyFile),
37 InsecureSkipVerify: false,
38 }
39
40 cfg, err := ClientConfig(opts)
41 if err != nil {
42 t.Fatalf("error building tls client config: %v", err)
43 }
44
45 if got := len(cfg.Certificates); got != 1 {
46 t.Fatalf("expecting 1 client certificates, got %d", got)
47 }
48 if cfg.InsecureSkipVerify {
49 t.Fatalf("insecure skip verify mismatch, expecting false")
50 }
51 if cfg.RootCAs == nil {
52 t.Fatalf("mismatch tls RootCAs, expecting non-nil")
53 }
54 }
55
56 func testfile(t *testing.T, file string) (path string) {
57 var err error
58 if path, err = filepath.Abs(filepath.Join(tlsTestDir, file)); err != nil {
59 t.Fatalf("error getting absolute path to test file %q: %v", file, err)
60 }
61 return path
62 }
63
64 func TestNewClientTLS(t *testing.T) {
65 certFile := testfile(t, testCertFile)
66 keyFile := testfile(t, testKeyFile)
67 caCertFile := testfile(t, testCaCertFile)
68 insecureSkipTLSverify := false
69
70 cfg, err := NewClientTLS(certFile, keyFile, caCertFile, insecureSkipTLSverify)
71 if err != nil {
72 t.Error(err)
73 }
74
75 if got := len(cfg.Certificates); got != 1 {
76 t.Fatalf("expecting 1 client certificates, got %d", got)
77 }
78 if cfg.InsecureSkipVerify {
79 t.Fatalf("insecure skip verify mismatch, expecting false")
80 }
81 if cfg.RootCAs == nil {
82 t.Fatalf("mismatch tls RootCAs, expecting non-nil")
83 }
84
85 cfg, err = NewClientTLS("", "", caCertFile, insecureSkipTLSverify)
86 if err != nil {
87 t.Error(err)
88 }
89
90 if got := len(cfg.Certificates); got != 0 {
91 t.Fatalf("expecting 0 client certificates, got %d", got)
92 }
93 if cfg.InsecureSkipVerify {
94 t.Fatalf("insecure skip verify mismatch, expecting false")
95 }
96 if cfg.RootCAs == nil {
97 t.Fatalf("mismatch tls RootCAs, expecting non-nil")
98 }
99
100 cfg, err = NewClientTLS(certFile, keyFile, "", insecureSkipTLSverify)
101 if err != nil {
102 t.Error(err)
103 }
104
105 if got := len(cfg.Certificates); got != 1 {
106 t.Fatalf("expecting 1 client certificates, got %d", got)
107 }
108 if cfg.InsecureSkipVerify {
109 t.Fatalf("insecure skip verify mismatch, expecting false")
110 }
111 if cfg.RootCAs != nil {
112 t.Fatalf("mismatch tls RootCAs, expecting nil")
113 }
114 }
115
View as plain text