-
Notifications
You must be signed in to change notification settings - Fork 5
/
serve_test.go
204 lines (151 loc) · 3.93 KB
/
serve_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
// +build !race
package gig
import (
"crypto/tls"
"io"
"net"
"syscall"
"testing"
"time"
"github.com/matryer/is"
)
type errorListener struct {
errs []error
}
func (l *errorListener) Accept() (c net.Conn, err error) {
if len(l.errs) == 0 {
return nil, io.EOF
}
err = l.errs[0]
l.errs = l.errs[1:]
return
}
func (l *errorListener) Close() error {
return nil
}
func (l *errorListener) Addr() net.Addr {
return &FakeAddr{}
}
func TestServe_NetError(t *testing.T) {
is := is.New(t)
ln := &errorListener{[]error{
&net.OpError{
Op: "accept",
Err: syscall.EMFILE,
}}}
g := New()
g.listener = ln
err := g.serve()
is.Equal(io.EOF, err)
}
func TestServe(t *testing.T) {
is := is.New(t)
g := New()
go func() {
_ = g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
_, err = conn.Write([]byte("/test\r\n"))
is.NoErr(err)
buf := make([]byte, 15)
n, err := conn.Read(buf)
is.NoErr(err)
is.Equal("51 Not Found\r\n\x00", string(buf))
is.Equal(14, n)
g.Close()
}
func TestServe_SlowClient_Read(t *testing.T) {
is := is.New(t)
g := New()
g.ReadTimeout = 1 * time.Millisecond
go func() {
_ = g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
time.Sleep(200 * time.Millisecond) // client sleeps before sending request
_, err = conn.Write([]byte("/test\r\n"))
is.True(err != nil)
g.Close()
}
func TestServe_SlowClient_Write(t *testing.T) {
is := is.New(t)
g := New()
g.WriteTimeout = 1 * time.Millisecond
go func() {
err := g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
if err != ErrServerClosed { // Prevent the test to fail after closing the servers
is.NoErr(err)
}
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
_, err = conn.Write([]byte("/test\r\n"))
is.NoErr(err)
conn.Close() // client closes connection before reading response
g.Close()
}
func TestServe_Overflow(t *testing.T) {
is := is.New(t)
g := New()
go func() {
_ = g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
request := make([]byte, 2000)
_, _ = conn.Write(request)
buf := make([]byte, 23)
n, err := conn.Read(buf)
is.NoErr(err)
is.Equal("59 Request too long!\r\n\x00", string(buf))
is.Equal(22, n)
g.Close()
}
func TestServe_NotGemini(t *testing.T) {
is := is.New(t)
g := New()
go func() {
_ = g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
_, err = conn.Write([]byte("http://google.com\r\n"))
is.NoErr(err)
buf := make([]byte, 40)
n, err := conn.Read(buf)
is.NoErr(err)
is.Equal("59 No proxying to non-Gemini content!\r\n\x00", string(buf))
is.Equal(39, n)
g.Close()
}
func TestServe_NotURL(t *testing.T) {
is := is.New(t)
g := New()
go func() {
_ = g.Run("127.0.0.1:0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
}()
time.Sleep(200 * time.Millisecond)
addr := g.listener.Addr().String()
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
is.NoErr(err)
_, err = conn.Write([]byte("::::::\r\n"))
is.NoErr(err)
buf := make([]byte, 24)
n, err := conn.Read(buf)
is.NoErr(err)
is.Equal("59 Error parsing URL!\r\n\x00", string(buf))
is.Equal(23, n)
g.Close()
}