forked from viamrobotics/goutils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
net_test.go
99 lines (82 loc) · 2.93 KB
/
net_test.go
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
package utils
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"testing"
"time"
"go.viam.com/test"
"go.viam.com/utils/internal"
)
func TestTryReserveRandomPort(t *testing.T) {
p, err := TryReserveRandomPort()
test.That(t, err, test.ShouldBeNil)
test.That(t, p, test.ShouldBeGreaterThan, 0)
}
func TestGetAllLocalIPv4s(t *testing.T) {
ips, err := GetAllLocalIPv4s()
test.That(t, err, test.ShouldBeNil)
test.That(t, ips, test.ShouldNotBeEmpty)
}
func TestNewPossiblySecureTCPListenerFromFile(t *testing.T) {
t.Run("providing just cert should fail", func(t *testing.T) {
_, _, err := NewPossiblySecureTCPListenerFromFile(0, "cert", "")
test.That(t, err, test.ShouldBeError, ErrInsufficientX509KeyPair)
})
t.Run("providing just key should fail", func(t *testing.T) {
_, _, err := NewPossiblySecureTCPListenerFromFile(0, "", "key")
test.That(t, err, test.ShouldBeError, ErrInsufficientX509KeyPair)
})
t.Run("no cert or key should be insecure", func(t *testing.T) {
listener, secure, err := NewPossiblySecureTCPListenerFromFile(0, "", "")
test.That(t, err, test.ShouldBeNil)
test.That(t, secure, test.ShouldBeFalse)
test.That(t, listener, test.ShouldNotBeNil)
httpServer := &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
httpServer.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
serveDone := make(chan struct{})
go func() {
defer close(serveDone)
httpServer.Serve(listener)
}()
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://%s", listener.Addr().String()))
test.That(t, err, test.ShouldBeNil)
test.That(t, resp.StatusCode, test.ShouldEqual, http.StatusOK)
test.That(t, httpServer.Shutdown(context.Background()), test.ShouldBeNil)
<-serveDone
})
t.Run("with cert and key should be secure", func(t *testing.T) {
listener, secure, err := NewPossiblySecureTCPListenerFromFile(
0,
internal.ResolveFile("testdata/cert.pem"),
internal.ResolveFile("testdata/key.pem"),
)
test.That(t, err, test.ShouldBeNil)
test.That(t, secure, test.ShouldBeTrue)
test.That(t, listener, test.ShouldNotBeNil)
httpServer := &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
httpServer.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
serveDone := make(chan struct{})
go func() {
defer close(serveDone)
httpServer.Serve(listener)
}()
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{Transport: customTransport}
resp, err := client.Get(fmt.Sprintf("https://%s", listener.Addr().String()))
test.That(t, err, test.ShouldBeNil)
test.That(t, resp.StatusCode, test.ShouldEqual, http.StatusOK)
test.That(t, httpServer.Shutdown(context.Background()), test.ShouldBeNil)
<-serveDone
})
}