forked from kata-containers/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
342 lines (282 loc) · 6.31 KB
/
proxy_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// A simple proxy that multiplexes a unix socket connection
//
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/signal"
"regexp"
"strings"
"sync"
"syscall"
"testing"
"github.com/hashicorp/yamux"
"github.com/stretchr/testify/assert"
)
func client(proxyAddr, file string) error {
buf := []byte("hello proxy")
conn, err := net.Dial("unix", proxyAddr)
if err != nil {
return err
}
defer conn.Close()
var sum1 string
var expected int64
copyCh := make(chan error)
if file != "" {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
h := md5.New()
expected, err = io.Copy(h, f)
if err != nil {
return err
}
sum1 = fmt.Sprintf("%x", h.Sum(nil))
_, err = f.Seek(0, io.SeekStart)
if err != nil {
return err
}
go func() {
_, err := io.Copy(conn, f)
copyCh <- err
}()
} else {
sum1 = fmt.Sprintf("%x", md5.Sum(buf))
size, err := conn.Write(buf)
if err != nil {
return err
}
expected = int64(size)
copyCh <- nil
}
// read from server
h := md5.New()
var result []byte
for {
if expected >= 1024 {
result = make([]byte, 1024, 1024)
} else if expected > 0 {
result = make([]byte, expected, expected)
} else {
break
}
size, err := conn.Read(result)
if err != nil {
return err
}
_, err = h.Write(result[:size])
if err != nil {
return err
}
expected -= int64(size)
}
sum2 := fmt.Sprintf("%x", h.Sum(nil))
if sum1 != sum2 {
return fmt.Errorf("unmatched checksum on file %s:\norig:\t%s\nnew:\t%s", file, sum1, sum2)
}
return <-copyCh
}
func server(listener net.Listener, closeCh chan bool) error {
// Accept once
conn, err := listener.Accept()
if err != nil {
return err
}
defer conn.Close()
session, err := yamux.Server(conn, nil)
if err != nil {
return err
}
go func() {
<-closeCh
session.Close()
}()
for {
stream, err := session.Accept()
if err != nil {
return err
}
go func() {
io.Copy(stream, stream)
stream.Close()
}()
}
}
func TestUnixAddrParsing(T *testing.T) {
buf := "unix://foo/bar"
addr, err := unixAddr(buf)
if err != nil {
T.Fatalf("failed to parse %s", buf)
}
if addr != "foo/bar" {
T.Fatalf("unexpected parsing result: %s", addr)
}
buf = "unix://////foo/bar"
_, err = unixAddr(buf)
if err != nil {
T.Fatalf("failed to parse %s", buf)
}
buf = "foo/bar"
_, err = unixAddr(buf)
if err != nil {
T.Fatalf("failed to parse %s", buf)
}
buf = "/foo/bar"
_, err = unixAddr(buf)
if err != nil {
T.Fatalf("failed to parse %s", buf)
}
buf = ""
_, err = unixAddr(buf)
if err == nil {
T.Fatal("unexpected success parsing empty string")
}
buf = "vsock://foo/bar"
_, err = unixAddr(buf)
if err == nil {
T.Fatalf("unexpected success parsing %s", buf)
}
}
func TestProxy(t *testing.T) {
muxSock := "/tmp/proxy-mux.sock"
listenSock := "/tmp/proxy-listen.sock"
testDir := "."
os.Remove(muxSock)
os.Remove(listenSock)
// start yamux server
l, err := net.Listen("unix", muxSock)
if err != nil {
t.Fatal(err)
}
closeCh := make(chan bool)
go func() {
server(l, closeCh)
l.Close()
}()
// start proxy
servConn, err := net.Dial("unix", muxSock)
if err != nil {
t.Fatalf("fail to dial channel(%s): %s", muxSock, err)
return
}
defer servConn.Close()
results := make(chan error)
lp, s, err := serve(servConn, "unix", listenSock, results)
if err != nil {
t.Fatal(err)
}
defer func() {
close(closeCh)
s.Close()
}()
// run client tests
files, err := ioutil.ReadDir(testDir)
if err != nil {
lp.Close()
t.Fatal(err)
}
wg := &sync.WaitGroup{}
cliRes := make(chan error)
for _, file := range files {
if file.IsDir() {
continue
}
wg.Add(1)
go func(filename string) {
cliRes <- client(listenSock, filename)
wg.Done()
}(file.Name())
}
go func() {
wg.Wait()
close(cliRes)
}()
for err = range cliRes {
if err != nil {
t.Fatal(err)
}
}
// closing the listener should result in an error in results channel
lp.Close()
err = <-results
assert.NotNil(t, err, "closing listener should result in an error")
}
func TestSetupSigtermNotifier(t *testing.T) {
sigCh := setupNotifier()
assert.NotNil(t, sigCh, "Signal channel should not be nil")
signal.Reset()
}
func TestHandleSigtermSignalNilSignalChannelFailure(t *testing.T) {
err := handleExitSignal(nil, nil, nil)
assert.NotNil(t, err, "Should throw an error because signal channel provided was nil")
}
func TestHandleSigtermSignalWrongSignalFailure(t *testing.T) {
sig := syscall.SIGUSR1
sigCh := make(chan os.Signal, 1)
sigCh <- sig
err := handleExitSignal(sigCh, nil, nil)
assert.NotNil(t, err, "Should throw an error because signal sent was %q", sig.String())
}
func TestHandleSigtermSignalNilConnectionsSuccess(t *testing.T) {
sigCh := make(chan os.Signal, 1)
sigCh <- termSignal
err := handleExitSignal(sigCh, nil, nil)
assert.Nil(t, err, "Should not fail: %v", err)
}
func TestLogger(t *testing.T) {
assert := assert.New(t)
buf := &bytes.Buffer{}
log := logger()
savedLogOut := log.Logger.Out
defer func() {
log.Logger.Out = savedLogOut
}()
// save all output to a buffer
log.Logger.Out = buf
msg := "oh dear!"
log.Error(msg)
line := buf.String()
assert.True(strings.Contains(line, fmt.Sprintf(`msg=%q`, msg)))
assert.True(strings.Contains(line, "name="+proxyName))
assert.True(strings.Contains(line, "source=proxy"))
pidPattern := regexp.MustCompile(`pid=\d+`)
matches := pidPattern.FindAllString(line, -1)
assert.NotNil(matches)
assert.False(strings.Contains(line, "sandbox="))
}
func TestLoggerWithSandbox(t *testing.T) {
assert := assert.New(t)
buf := &bytes.Buffer{}
savedSandbox := sandboxID
sandboxID = "a-sandbox-id"
log := logger()
savedLogOut := log.Logger.Out
defer func() {
log.Logger.Out = savedLogOut
sandboxID = savedSandbox
}()
// save all output to a buffer
log.Logger.Out = buf
msg := "I've got a bad feeling about this!"
log.Error(msg)
line := buf.String()
assert.True(strings.Contains(line, fmt.Sprintf(`msg=%q`, msg)))
assert.True(strings.Contains(line, "name="+proxyName))
assert.True(strings.Contains(line, "source=proxy"))
pidPattern := regexp.MustCompile(`pid=\d+`)
matches := pidPattern.FindAllString(line, -1)
assert.NotNil(matches)
assert.True(strings.Contains(line, "sandbox="+sandboxID))
}