-
Notifications
You must be signed in to change notification settings - Fork 36
/
tls_test.go
248 lines (216 loc) · 5.98 KB
/
tls_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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package mint
import (
"fmt"
"net"
"strings"
"testing"
"time"
)
func newLocalListener(t *testing.T) net.Listener {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
ln, err = net.Listen("tcp6", "[::1]:0")
}
if err != nil {
t.Fatal(err)
}
return ln
}
func TestDialTimeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
listener := newLocalListener(t)
addr := listener.Addr().String()
defer listener.Close()
complete := make(chan bool)
defer close(complete)
go func() {
conn, err := listener.Accept()
if err != nil {
t.Error(err)
return
}
<-complete
conn.Close()
}()
dialer := &net.Dialer{
Timeout: 10 * time.Millisecond,
}
var err error
if _, err = DialWithDialer(dialer, "tcp", addr, nil); err == nil {
t.Fatal("DialWithTimeout completed successfully")
}
if !strings.Contains(err.Error(), "timed out") {
t.Errorf("resulting error not a timeout: %s", err)
}
}
func TestDialNonBlocking(t *testing.T) {
config := &Config{NonBlocking: true}
_, err := Dial("tcp", "localhost:1234", config)
assertEquals(t, err.Error(), "dialing not possible in non-blocking mode")
_, err = DialWithDialer(&net.Dialer{}, "tcp", "localhost:1234", config)
assertEquals(t, err.Error(), "dialing not possible in non-blocking mode")
}
func TestListenNonBlocking(t *testing.T) {
config := &Config{
NonBlocking: true,
Certificates: certificates,
}
_, err := Listen("tcp", "localhost:1234", config)
assertEquals(t, err.Error(), "listening not possible in non-blocking mode")
_, err = NewListener(newLocalListener(t), config)
assertEquals(t, err.Error(), "listening not possible in non-blocking mode")
}
// tests that Conn.Read returns (non-zero, io.EOF) instead of
// (non-zero, nil) when a Close (alertCloseNotify) is sitting right
// behind the application data in the buffer.
func DISABLEDTestConnReadNonzeroAndEOF(t *testing.T) {
// This test is racy: it assumes that after a write to a
// localhost TCP connection, the peer TCP connection can
// immediately read it. Because it's racy, we skip this test
// in short mode, and then retry it several times with an
// increasing sleep in between our final write (via srv.Close
// below) and the following read.
if testing.Short() {
t.Skip("skipping in short mode")
}
var err error
for delay := time.Millisecond; delay <= 64*time.Millisecond; delay *= 2 {
if err = testConnReadNonzeroAndEOF(t, delay); err == nil {
return
}
}
t.Error(err)
}
func testConnReadNonzeroAndEOF(t *testing.T, delay time.Duration) error {
ln := newLocalListener(t)
defer ln.Close()
srvCh := make(chan *Conn, 1)
var serr error
go func() {
sconn, err := ln.Accept()
if err != nil {
serr = err
srvCh <- nil
return
}
serverConfig := Config{ServerName: "example.com"}
srv := Server(sconn, &serverConfig)
if alert := srv.Handshake(); alert != AlertNoAlert {
serr = fmt.Errorf("handshake: %v", alert)
srvCh <- nil
return
}
srvCh <- srv
}()
clientConfig := Config{ServerName: "example.com"}
conn, err := Dial("tcp", ln.Addr().String(), &clientConfig)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
srv := <-srvCh
if srv == nil {
return serr
}
buf := make([]byte, 16)
buf = buf[0:6]
// Consume NST.
zeroBuf := []byte{}
conn.Read(zeroBuf)
srv.Write([]byte("foobar"))
n, err := conn.Read(buf)
if n != 6 || err != nil || string(buf) != "foobar" {
return fmt.Errorf("Read = %d, %v, data %q; want 6, nil, foobar", n, err, buf)
}
srv.Write([]byte("foobartoo"))
n, err = conn.Read(buf)
if n != 6 || err != nil || string(buf) != "foobar" {
return fmt.Errorf("Read = %d, %v, data %q; want 6, nil, foobar", n, err, buf)
}
n, err = conn.Read(buf)
if n != 3 || err != nil || string(buf[0:3]) != "too" {
return fmt.Errorf("Read = %d, %v, data %q; want 3, nil, too", n, err, buf)
}
srv.Write([]byte("four"))
n, err = conn.Read(buf)
if n != 4 || err != nil || string(buf[0:4]) != "four" {
return fmt.Errorf("Read = %d, %v, data %q; want 4, nil, foor", n, err, buf)
}
srv.Write([]byte("abcdefgh"))
srv.Close()
time.Sleep(delay)
n, err = conn.Read(buf)
if n != 6 || string(buf) != "abcdef" {
return fmt.Errorf("Read = %d, buf= %q; want 6, abcdef", n, buf)
}
if err != nil {
return fmt.Errorf("First Read error = %v; want nil", err)
}
n, err = conn.Read(buf)
if n != 2 || string(buf[0:2]) != "gh" {
return fmt.Errorf("Read = %d, buf= %q; want 2, gh", n, buf)
}
return nil
}
func TestExchangeData(t *testing.T) {
ln := newLocalListener(t)
defer ln.Close()
srvCh := make(chan *Conn, 1)
var serr error
go func() {
sconn, err := ln.Accept()
if err != nil {
serr = err
srvCh <- nil
return
}
serverConfig := Config{Certificates: certificates}
srv := Server(sconn, &serverConfig)
if alert := srv.Handshake(); alert != AlertNoAlert {
serr = fmt.Errorf("handshake: %v", alert)
srvCh <- nil
return
}
srvCh <- srv
}()
clientConfig := Config{
ServerName: "example.com",
InsecureSkipVerify: true,
}
conn, err := Dial("tcp", ln.Addr().String(), &clientConfig)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
srv := <-srvCh
assertNotNil(t, srv, "Server should have completed handshake")
assertNotError(t, serr, "Server should not have raised an error")
buf := make([]byte, 16)
buf = buf[0:6]
srv.Write([]byte("foobar"))
n, err := conn.Read(buf)
if n != 6 || err != nil || string(buf) != "foobar" {
t.Fatalf("Read = %d, %v, data %q; want 6, nil, foobar", n, err, buf)
return
}
srv.Write([]byte("foobartoo"))
n, err = conn.Read(buf)
if n != 6 || err != nil || string(buf) != "foobar" {
t.Fatalf("Read = %d, %v, data %q; want 6, nil, foobar", n, err, buf)
return
}
n, err = conn.Read(buf)
if n != 3 || err != nil || string(buf[0:3]) != "too" {
t.Fatalf("Read = %d, %v, data %q; want 3, nil, too", n, err, buf)
return
}
srv.Write([]byte("four"))
n, err = conn.Read(buf)
if n != 4 || err != nil || string(buf[0:4]) != "four" {
t.Fatalf("Read = %d, %v, data %q; want 4, nil, four", n, err, buf)
return
}
return
}