This repository was archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
79 lines (68 loc) · 1.45 KB
/
proxy_test.go
File metadata and controls
79 lines (68 loc) · 1.45 KB
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
package proxy
import (
"bytes"
"crypto/rand"
"errors"
"io"
"net"
"sync"
"testing"
"golang.org/x/net/nettest"
)
func assertEq(t *testing.T, is, should interface{}) {
t.Helper()
if should != is {
t.Fatalf("Expected value\n%v\nbut got\n%v", should, is)
}
}
// buffer implements io.ReadWriteCloser.
type buffer struct {
*bytes.Buffer
}
func (b *buffer) Close() error {
return nil
}
func TestNettest(t *testing.T) {
mkPipe := func() (c1, c2 net.Conn, stop func(), err error) {
var (
in, out = net.Pipe()
fwd1, fwd2 = net.Pipe()
wg = sync.WaitGroup{}
ch = make(chan error)
)
wg.Add(2)
go TunToVsock(in, fwd1, ch, &wg)
go VsockToTun(fwd2, out, ch, &wg)
return in, out, func() {}, nil
}
nettest.TestConn(t, nettest.MakePipe(mkPipe))
}
func TestAToB(t *testing.T) {
var (
err error
wg sync.WaitGroup
ch = make(chan error)
conn1, conn2 = net.Pipe()
sendBuf = make([]byte, tunMTU*2)
recvBuf = &buffer{
Buffer: new(bytes.Buffer),
}
)
// We only expect to see errors containing io.EOF.
go func() {
for err := range ch {
assertEq(t, errors.Is(err, io.EOF), true)
}
}()
// Fill sendBuf with random data.
_, err = rand.Read(sendBuf)
assertEq(t, err, nil)
wg.Add(2)
go TunToVsock(io.NopCloser(bytes.NewReader(sendBuf)), conn1, ch, &wg)
go VsockToTun(conn2, recvBuf, ch, &wg)
wg.Wait()
assertEq(t, bytes.Equal(
sendBuf,
recvBuf.Bytes(),
), true)
}