-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_test.go
107 lines (100 loc) · 2.3 KB
/
main_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
100
101
102
103
104
105
106
107
package main
import (
"context"
"crypto/tls"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"
oohttp "github.com/ooni/oohttp"
"github.com/ooni/oohttp/example/internal/ja3x"
"github.com/ooni/oohttp/example/internal/utlsx"
)
// tlsDialerRecorder performs TLS dials and records the ALPN.
type tlsDialerRecorder struct {
alpn map[string]int
config *tls.Config
mu sync.Mutex
}
// do is like dialTLSContext but also records the ALPN.
func (d *tlsDialerRecorder) do(ctx context.Context, network string, addr string) (net.Conn, error) {
child := &utlsx.TLSDialer{
Config: d.config,
}
conn, err := child.DialTLSContext(ctx, network, addr)
if err != nil {
return nil, err
}
tconn := conn.(oohttp.TLSConn)
p := tconn.ConnectionState().NegotiatedProtocol
d.mu.Lock()
if d.alpn == nil {
d.alpn = make(map[string]int)
}
d.alpn[p]++
d.mu.Unlock()
return conn, nil
}
func TestWorkAsIntendedWithH2(t *testing.T) {
srvr := ja3x.NewServer("h2")
defer srvr.Close()
d := &tlsDialerRecorder{
alpn: map[string]int{},
config: srvr.ClientConfig(),
mu: sync.Mutex{},
}
clnt := newClient(newTransport(d.do))
resp, err := clnt.Get(srvr.URL())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.ReadAll(resp.Body); err != nil {
log.Fatal(err)
}
d.mu.Lock()
defer d.mu.Unlock()
if count := d.alpn["h2"]; count < 1 {
t.Fatal("did not dial h2")
}
}
func TestWorkAsIntendedWithHTTP11(t *testing.T) {
srvr := ja3x.NewServer("http/1.1")
defer srvr.Close()
d := &tlsDialerRecorder{
alpn: map[string]int{},
config: srvr.ClientConfig(),
mu: sync.Mutex{},
}
clnt := newClient(newTransport(d.do))
resp, err := clnt.Get(srvr.URL())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.ReadAll(resp.Body); err != nil {
log.Fatal(err)
}
d.mu.Lock()
defer d.mu.Unlock()
if count := d.alpn["http/1.1"]; count < 1 {
t.Fatal("did not dial http/1.1")
}
}
func TestWorkAsIntendedWithHTTP(t *testing.T) {
srvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("0xdeadbeef"))
}))
defer srvr.Close()
resp, err := defaultClient.Get(srvr.URL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.ReadAll(resp.Body); err != nil {
log.Fatal(err)
}
}