|
1 | 1 | package verify_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
4 | 8 | "os"
|
5 | 9 | "testing"
|
6 | 10 |
|
@@ -87,7 +91,80 @@ func TestVerify(t *testing.T) {
|
87 | 91 |
|
88 | 92 | for _, tC := range testCases {
|
89 | 93 | t.Run(tC.name, func(t *testing.T) {
|
90 |
| - testArgs := []string{execName, verify.CmdVerify, "-dns", tC.dns, "-cafile", tC.cafile, "-certfile", tC.certfile} |
| 94 | + testArgs := []string{ |
| 95 | + execName, verify.CmdVerify, |
| 96 | + "-dns", tC.dns, |
| 97 | + "-cafile", tC.cafile, |
| 98 | + "-certfile", tC.certfile, |
| 99 | + } |
| 100 | + if tC.shouldErr { |
| 101 | + require.Error(t, app.Run(testArgs)) |
| 102 | + } else { |
| 103 | + require.NoError(t, app.Run(testArgs)) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestVerifyURL(t *testing.T) { |
| 110 | + const ( |
| 111 | + // Certificate paths |
| 112 | + serverCert = "../../testdata/server-cert.pem" |
| 113 | + serverKey = "../../testdata/server-key.pem" |
| 114 | + caCert = "../../testdata/ca-cert.pem" |
| 115 | + caCert2 = "../../testdata/ca-cert-2.pem" |
| 116 | + ) |
| 117 | + |
| 118 | + ts := httptest.NewUnstartedServer( |
| 119 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 | + fmt.Fprintln(w, "hello!") |
| 121 | + }), |
| 122 | + ) |
| 123 | + |
| 124 | + cert, err := tls.LoadX509KeyPair(serverCert, serverKey) |
| 125 | + require.NoError(t, err) |
| 126 | + |
| 127 | + ts.TLS = &tls.Config{ |
| 128 | + Certificates: []tls.Certificate{cert}, |
| 129 | + } |
| 130 | + |
| 131 | + ts.StartTLS() |
| 132 | + defer ts.Close() |
| 133 | + |
| 134 | + app := &cli.App{ |
| 135 | + Commands: []*cli.Command{ |
| 136 | + verify.Command(), |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + execName, err := os.Executable() |
| 141 | + require.NoError(t, err) |
| 142 | + |
| 143 | + testCases := []struct { |
| 144 | + name string |
| 145 | + cafile string |
| 146 | + shouldErr bool |
| 147 | + }{ |
| 148 | + { |
| 149 | + name: "valid cert and ca", |
| 150 | + cafile: caCert, |
| 151 | + shouldErr: false, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "bad certificate", |
| 155 | + cafile: caCert2, |
| 156 | + shouldErr: true, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tC := range testCases { |
| 161 | + t.Run(tC.name, func(t *testing.T) { |
| 162 | + testArgs := []string{ |
| 163 | + execName, verify.CmdVerify, |
| 164 | + "--cafile", tC.cafile, |
| 165 | + "--url", ts.URL, |
| 166 | + } |
| 167 | + |
91 | 168 | if tC.shouldErr {
|
92 | 169 | require.Error(t, app.Run(testArgs))
|
93 | 170 | } else {
|
|
0 commit comments