forked from djui/await
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
133 lines (114 loc) · 3.15 KB
/
http_test.go
File metadata and controls
133 lines (114 loc) · 3.15 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
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/http"
"os"
"testing"
"time"
)
func TestTLSSkipVerify(t *testing.T) {
shutdownServer := setupHttpsServer(t)
defer shutdownServer()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resources, _ := parseResources([]string{
"https://localhost:55372",
"https://localhost:55372#tls=skip-verify",
})
if err := resources[0].Await(ctx); err == nil {
t.Errorf("Should have failed when verifying TLS, but succeeded.")
}
if err := resources[1].Await(ctx); err != nil {
t.Errorf("Should have skipped TLS verification, but didn't: %v", err)
}
}
func setupHttpsServer(t *testing.T) func() {
certFile, keyFile, cleanupCerts := setupTestCertificates(t)
server := &http.Server{
Addr: ":55372",
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprint(res, "Hello: "+req.Host)
})
// The separation of the listening socket from the serving of the http requests
// is to guarantee one can immediately contact the server on this method's return
ln, err := net.Listen("tcp", ":55372")
if err != nil {
t.Errorf("Unable to setup listening socket for webserver: %v", err)
}
go func() {
_ = server.ServeTLS(ln, certFile, keyFile)
}()
return func() {
_ = server.Close()
_ = ln.Close()
cleanupCerts()
}
}
func setupTestCertificates(t *testing.T) (string, string, func()) {
certFile := "testCert.crt"
keyFile := "testCert.key"
cert, key, err := CertWithKeyPair()
if err != nil {
t.Errorf("failed to generate test certificates: %v", err)
}
if err := ioutil.WriteFile(certFile, cert, 0644); err != nil {
t.Errorf("failed to write cert fixture to %s: %v", certFile, err)
}
if err := ioutil.WriteFile(keyFile, key, 0644); err != nil {
t.Errorf("failed to write key fixture to %s: %v", keyFile, err)
}
return certFile, keyFile, func() {
_ = os.Remove(certFile)
_ = os.Remove(keyFile)
}
}
func CertWithKeyPair() ([]byte, []byte, error) {
bits := 2048
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
tpl := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "wronghost"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(2, 0, 0),
BasicConstraintsValid: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
}
derCert, err := x509.CreateCertificate(rand.Reader, &tpl, &tpl, &privateKey.PublicKey, privateKey)
if err != nil {
return nil, nil, err
}
buf := &bytes.Buffer{}
err = pem.Encode(buf, &pem.Block{
Type: "CERTIFICATE",
Bytes: derCert,
})
if err != nil {
return nil, nil, err
}
pemCert := buf.Bytes()
buf = &bytes.Buffer{}
err = pem.Encode(buf, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
})
if err != nil {
return nil, nil, err
}
pemKey := buf.Bytes()
return pemCert, pemKey, nil
}