-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_test.go
More file actions
105 lines (99 loc) · 3.1 KB
/
Copy pathflow_test.go
File metadata and controls
105 lines (99 loc) · 3.1 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
package rhttp
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"net/http"
"testing"
"time"
)
// genUnitTestCert mints a quick self-signed cert+key for tests that just
// need to satisfy "TLSCert is set" — the validation tests below don't
// actually drive a network connection, so anything that parses is fine.
func genUnitTestCert(t *testing.T) (tls.Certificate, *x509.CertPool) {
t.Helper()
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "unit-test"},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
if err != nil {
t.Fatal(err)
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyDER, _ := x509.MarshalECPrivateKey(priv)
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
t.Fatal(err)
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(certPEM)
return cert, pool
}
// TestNewConnectionPoolRejectsMissingCert: a worker cert is mandatory —
// the library refuses to run without one rather than silently dialling
// out as an anonymous client.
func TestNewConnectionPoolRejectsMissingCert(t *testing.T) {
_, pool := genUnitTestCert(t)
_, err := NewConnectionPool(ServerOptions{
Addr: "127.0.0.1:1",
Handler: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}),
CACertPool: pool,
// TLSCert deliberately omitted.
})
if !errors.Is(err, ErrMTLSRequired) {
t.Fatalf("err = %v, want ErrMTLSRequired", err)
}
}
// TestValidateOptionsAcceptsNilCA: a nil CACertPool is allowed — it tells
// rhttp to fall back to the host's system root CAs, which is the right
// behaviour for proxies whose server cert is publicly trusted (LE etc.).
func TestValidateOptionsAcceptsNilCA(t *testing.T) {
cert, _ := genUnitTestCert(t)
opts := ServerOptions{
Addr: "127.0.0.1:1",
Handler: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}),
TLSCert: cert,
}
if err := validateOptions(&opts); err != nil {
t.Fatalf("validateOptions error: %v", err)
}
}
// TestNewConnectionPoolRejectsMissingAddr / MissingHandler: required-fields
// guarded so misconfiguration fails fast at startup rather than producing
// a silent reconnect loop.
func TestNewConnectionPoolRejectsMissingAddr(t *testing.T) {
cert, pool := genUnitTestCert(t)
_, err := NewConnectionPool(ServerOptions{
Handler: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}),
TLSCert: cert,
CACertPool: pool,
})
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestNewConnectionPoolRejectsMissingHandler(t *testing.T) {
cert, pool := genUnitTestCert(t)
_, err := NewConnectionPool(ServerOptions{
Addr: "127.0.0.1:1",
TLSCert: cert,
CACertPool: pool,
})
if err == nil {
t.Fatal("expected error, got nil")
}
}