1 package cmd
2
3 import (
4 "regexp"
5 "strings"
6 "testing"
7
8 "github.com/letsencrypt/boulder/metrics"
9 "github.com/letsencrypt/boulder/test"
10 )
11
12 func TestDBConfigURL(t *testing.T) {
13 tests := []struct {
14 conf DBConfig
15 expected string
16 }{
17 {
18
19 conf: DBConfig{DBConnectFile: "testdata/test_dburl"},
20 expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
21 },
22 {
23
24 conf: DBConfig{DBConnectFile: "testdata/test_dburl_newline"},
25 expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
26 },
27 }
28
29 for _, tc := range tests {
30 url, err := tc.conf.URL()
31 test.AssertNotError(t, err, "Failed calling URL() on DBConfig")
32 test.AssertEquals(t, url, tc.expected)
33 }
34 }
35
36 func TestPasswordConfig(t *testing.T) {
37 tests := []struct {
38 pc PasswordConfig
39 expected string
40 }{
41 {pc: PasswordConfig{}, expected: ""},
42 {pc: PasswordConfig{PasswordFile: "testdata/test_secret"}, expected: "secret"},
43 }
44
45 for _, tc := range tests {
46 password, err := tc.pc.Pass()
47 test.AssertNotError(t, err, "Failed to retrieve password")
48 test.AssertEquals(t, password, tc.expected)
49 }
50 }
51
52 func TestTLSConfigLoad(t *testing.T) {
53 null := "/dev/null"
54 nonExistent := "[nonexistent]"
55 cert := "testdata/cert.pem"
56 key := "testdata/key.pem"
57 caCert := "testdata/minica.pem"
58
59 testCases := []struct {
60 TLSConfig
61 want string
62 }{
63 {TLSConfig{"", null, null}, "nil CertFile in TLSConfig"},
64 {TLSConfig{null, "", null}, "nil KeyFile in TLSConfig"},
65 {TLSConfig{null, null, ""}, "nil CACertFile in TLSConfig"},
66 {TLSConfig{nonExistent, key, caCert}, "loading key pair.*no such file or directory"},
67 {TLSConfig{cert, nonExistent, caCert}, "loading key pair.*no such file or directory"},
68 {TLSConfig{cert, key, nonExistent}, "reading CA cert from.*no such file or directory"},
69 {TLSConfig{null, key, caCert}, "loading key pair.*failed to find any PEM data"},
70 {TLSConfig{cert, null, caCert}, "loading key pair.*failed to find any PEM data"},
71 {TLSConfig{cert, key, null}, "parsing CA certs"},
72 }
73 for _, tc := range testCases {
74 var title [3]string
75 if tc.CertFile == "" {
76 title[0] = "nil"
77 } else {
78 title[0] = tc.CertFile
79 }
80 if tc.KeyFile == "" {
81 title[1] = "nil"
82 } else {
83 title[1] = tc.KeyFile
84 }
85 if tc.CACertFile == "" {
86 title[2] = "nil"
87 } else {
88 title[2] = tc.CACertFile
89 }
90 t.Run(strings.Join(title[:], "_"), func(t *testing.T) {
91 _, err := tc.TLSConfig.Load(metrics.NoopRegisterer)
92 if err == nil {
93 t.Errorf("got no error")
94 }
95 if matched, _ := regexp.MatchString(tc.want, err.Error()); !matched {
96 t.Errorf("got error %q, wanted %q", err, tc.want)
97 }
98 })
99 }
100 }
101
View as plain text