forked from kata-containers/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
228 lines (196 loc) · 3.7 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
// A simple proxy that multiplexes a unix socket connection
//
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"sync"
"testing"
"github.com/hashicorp/yamux"
)
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, os.SEEK_SET)
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\n", file, sum1, sum2)
}
return <-copyCh
}
func server(listener net.Listener) 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
}
defer session.Close()
for {
stream, err := session.Accept()
if err != nil {
return err
}
go func() {
io.Copy(stream, stream)
stream.Close()
}()
}
return nil
}
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)
}
go func() {
server(l)
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)
err = serve(servConn, "unix", listenSock, results)
if err != nil {
t.Fatal(err)
}
// run client tests
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatal(err)
}
wg := &sync.WaitGroup{}
for _, file := range files {
if file.IsDir() {
continue
}
wg.Add(1)
go func(filename string) {
results <- client(listenSock, filename)
wg.Done()
}(file.Name())
}
go func() {
wg.Wait()
close(results)
}()
for err = range results {
if err != nil {
t.Fatal(err)
}
}
}