-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttps_test.go
More file actions
212 lines (196 loc) · 5.29 KB
/
Copy pathhttps_test.go
File metadata and controls
212 lines (196 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package libtower
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"math/big"
"net"
"strings"
"testing"
"time"
)
func generateLocalhostCert(t *testing.T) tls.Certificate {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("rsa.GenerateKey: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: time.Now(),
NotAfter: time.Now().Add(1 * time.Hour),
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatalf("CreateCertificate: %v", err)
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
t.Fatalf("X509KeyPair: %v", err)
}
return cert
}
func newLocalTLSServer(t *testing.T) (net.Listener, string, string) {
t.Helper()
cert := generateLocalhostCert(t)
tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}}
ln, err := tls.Listen("tcp", "127.0.0.1:0", tlsCfg)
if err != nil {
t.Fatalf("tls.Listen: %v", err)
}
go func() {
conn, err := ln.Accept()
if err != nil {
return
}
tlsConn := tls.Server(conn, tlsCfg)
tlsConn.Handshake()
tlsConn.Close()
}()
host, port, err := net.SplitHostPort(ln.Addr().String())
if err != nil {
t.Fatalf("SplitHostPort: %v", err)
}
return ln, host, port
}
func TestHTTPSCheckSuccess(t *testing.T) {
ctx := context.TODO()
ln, host, port := newLocalTLSServer(t)
defer ln.Close()
hs := &HTTPS{Host: host, Port: port, Timeout: time.Second, InsecureSkipVerify: true}
ok, notAfter, err := hs.HTTPSCheck(ctx)
if err != nil {
t.Fatalf("HTTPSCheck() error: %v", err)
}
if !ok {
t.Error("HTTPSCheck() = false, want true")
}
if notAfter.IsZero() {
t.Error("NotAfter is zero, want a valid time")
}
if notAfter.Before(time.Now()) {
t.Errorf("NotAfter %v is in the past", notAfter)
}
if hs.Duration <= 0 {
t.Errorf("Duration = %v, want > 0", hs.Duration)
}
}
func TestHTTPSCheckRefused(t *testing.T) {
ctx := context.TODO()
ln, host, port := newLocalTLSServer(t)
ln.Close()
hs := &HTTPS{Host: host, Port: port, Timeout: time.Second}
ok, _, err := hs.HTTPSCheck(ctx)
if err == nil {
t.Fatal("expected connection error, got nil")
}
if ok {
t.Error("HTTPSCheck() = true, want false")
}
}
func TestHTTPSCheckDefaultPort(t *testing.T) {
ctx := context.TODO()
// Port empty — function defaults to "443" and mutation is visible even on error
hs := &HTTPS{Host: "127.0.0.1", Timeout: time.Second}
_, _, err := hs.HTTPSCheck(ctx)
if err == nil {
t.Skip("something running on 127.0.0.1:443 — skipping")
}
if hs.Port != DefaultHTTPSPort {
t.Errorf("Port = %q, want %q", hs.Port, DefaultHTTPSPort)
}
}
func TestHTTPSCheckCertExpiryWarning(t *testing.T) {
ctx := context.TODO()
ln, host, port := newLocalTLSServer(t)
defer ln.Close()
// Set a large warning window — the test cert is valid for 1 hour, so it triggers
hs := &HTTPS{
Host: host,
Port: port,
Timeout: time.Second,
InsecureSkipVerify: true,
WarnIfExpiringWithin: 2 * time.Hour,
}
r := hs.Check(ctx)
if !r.OK {
t.Fatalf("Check() OK = false, want true; err=%v", r.Error)
}
if r.Warning == nil {
t.Fatal("Warning = nil, want non-nil (cert expiring within 2h)")
}
cw, ok := r.Warning.(*CertExpiringWarning)
if !ok {
t.Fatalf("Warning type = %T, want *CertExpiringWarning", r.Warning)
}
if cw.NotAfter.IsZero() {
t.Error("NotAfter is zero")
}
if cw.Remaining <= 0 {
t.Errorf("Remaining = %v, want > 0", cw.Remaining)
}
}
func TestHTTPSCheckCertExpiryNoWarning(t *testing.T) {
ctx := context.TODO()
ln, host, port := newLocalTLSServer(t)
defer ln.Close()
hs := &HTTPS{
Host: host,
Port: port,
Timeout: time.Second,
InsecureSkipVerify: true,
WarnIfExpiringWithin: 0, // disabled
}
r := hs.Check(ctx)
if !r.OK {
t.Fatalf("Check() OK = false, want true; err=%v", r.Error)
}
if r.Warning != nil {
t.Errorf("Warning = %v, want nil (WarnIfExpiringWithin=0)", r.Warning)
}
}
func TestCertExpiringWarningError(t *testing.T) {
w := &CertExpiringWarning{
NotAfter: time.Now().Add(30 * time.Minute),
Remaining: 30 * time.Minute,
}
s := w.Error()
if s == "" {
t.Error("Error() returned empty string")
}
if !strings.Contains(s, "30m") && !strings.Contains(s, "certificate") {
t.Errorf("Error() = %q, want expiry message", s)
}
}
func TestHTTPSCheckHandshakeError(t *testing.T) {
ctx := context.TODO()
// Plain TCP server — TLS handshake will fail, covering rawConn.Close()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
defer ln.Close()
go func() {
conn, _ := ln.Accept()
if conn != nil {
buf := make([]byte, 1024)
conn.Read(buf) // read ClientHello, then close
conn.Close()
}
}()
host, port, _ := net.SplitHostPort(ln.Addr().String())
hs := &HTTPS{Host: host, Port: port, Timeout: time.Second}
ok, _, err := hs.HTTPSCheck(ctx)
if err == nil {
t.Fatal("expected TLS handshake error against plain TCP, got nil")
}
if ok {
t.Error("HTTPSCheck() = true, want false")
}
}