summaryrefslogtreecommitdiff
path: root/pkg/test/hook_test.go
blob: 31e74f75c2393b8e6f77d19c28ca881c312ce37f (plain)
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
package test

import (
	"testing"

	"encoding/hex"
	"strings"
)

func TestUpperHook(t *testing.T) {
	c, s := newClientServer(t)
	defer closeClientServer(c, s)

	c.Exec("add name T listen,addr=-:0 upper loop")

	conn := xDial(t, "tcp", c.Get("tunnel.T.listen"))
	defer conn.Close()

	xWrite(t, conn, xData)

	buf := make([]byte, len(xData))
	xReadFull(t, conn, buf)

	if r := string(buf); r != strings.ToUpper(xData) {
		t.Fatalf("wrong reply: send '%s', recv '%s'", xData, r)
	}
}

func TestHexHook(t *testing.T) {
	c, s := newClientServer(t)
	defer closeClientServer(c, s)

	c.Exec("add name T listen,addr=-:0 hex dial,addr=@[addr]")

	listen := xListen(t, "tcp", "127.0.0.1:0")
	defer listen.Close()

	c.Set("addr", listen.Addr())

	out := xDial(t, "tcp", c.Get("tunnel.T.listen"))
	defer out.Close()

	in := xAccept(t, listen)
	defer in.Close()

	xWrite(t, out, xData)

	buf := make([]byte, 2*len(xData))
	xReadFull(t, in, buf)

	if r := string(buf); r != hex.EncodeToString([]byte(xData)) {
		t.Fatalf("wrong reply: send '%s', recv '%s'", xData, r)
	}
}