-
Notifications
You must be signed in to change notification settings - Fork 11
/
request_test.go
53 lines (45 loc) · 1.13 KB
/
request_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
package socks5
import (
"bytes"
"net"
"testing"
)
func TestPackUDPData(t *testing.T) {
data := []byte("udp")
addr := &Address{
Addr: net.IPv4(1, 1, 1, 1).To4(),
ATYPE: IPV4_ADDRESS,
Port: 1080,
}
udpData, err := PackUDPData(addr, data)
if err != nil {
t.Error(err)
}
if len(udpData) != 13 {
t.Errorf("get length: %d, want length: %d", len(udpData), 13)
}
bytes.Equal(udpData, []byte{0x00, 0x00, 0x00, 1, 1, 1, 1, 1, 0x04, 0x38})
}
func TestUnpackUDPData(t *testing.T) {
data := []byte{0x00, 0x00, 0x00, 1, 1, 1, 1, 1, 0x04, 0x38, 'a', 'b', 'c'}
wantAddr := net.IPv4(1, 1, 1, 1)
wantPayload := []byte{'a', 'b', 'c'}
wantATYPE := IPV4_ADDRESS
var wantPort uint16 = 1080
addr, payload, err := UnpackUDPData(data)
if err != nil {
t.Error(err)
}
if !bytes.Equal(payload, wantPayload) {
t.Errorf("want: %v get %v", wantPayload, payload)
}
if !addr.Addr.Equal(wantAddr) {
t.Errorf("want: %s get %s", wantAddr.String(), addr.Addr.String())
}
if wantATYPE != addr.ATYPE {
t.Errorf("want: %#x get %#x", wantATYPE, addr.ATYPE)
}
if addr.Port != wantPort {
t.Errorf("want: %d get %d", wantATYPE, addr.Port)
}
}