~bigbes/shroud

ref: 3defea82da661a79fbae52bda7225127ce4b13f8 shroud/internal/vless/protocol_test.go -rw-r--r-- 4.6 KiB
3defea82 — Eugene Blikh feat(vless): add scan command for reality targets 2 months ago
                                                                                
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
package vless

import (
	"bytes"
	"encoding/binary"
	"net"
	"testing"
)

func buildRequest(t *testing.T, uuid [16]byte, addons []byte, cmd byte, port uint16, addrType byte, addr []byte) []byte {
	t.Helper()
	var buf bytes.Buffer

	buf.WriteByte(Version) // version
	buf.Write(uuid[:])     // UUID

	if addons == nil {
		buf.WriteByte(0) // addons length
	} else {
		buf.WriteByte(byte(len(addons)))
		buf.Write(addons)
	}

	buf.WriteByte(cmd) // command

	var portBuf [2]byte
	binary.BigEndian.PutUint16(portBuf[:], port)
	buf.Write(portBuf[:]) // port

	buf.WriteByte(addrType) // address type
	buf.Write(addr)         // address

	return buf.Bytes()
}

func TestParseRequest_TCPIPv4(t *testing.T) {
	uuid := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	ip := net.ParseIP("192.168.1.1").To4()
	data := buildRequest(t, uuid, nil, CmdTCP, 8080, AddrIPv4, ip)

	req, err := ParseRequest(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if req.UUID != uuid {
		t.Errorf("UUID mismatch: got %v, want %v", req.UUID, uuid)
	}
	if req.Command != CmdTCP {
		t.Errorf("Command mismatch: got %d, want %d", req.Command, CmdTCP)
	}
	if req.Port != 8080 {
		t.Errorf("Port mismatch: got %d, want 8080", req.Port)
	}
	if req.Address != "192.168.1.1" {
		t.Errorf("Address mismatch: got %q, want %q", req.Address, "192.168.1.1")
	}
	if req.Target() != "192.168.1.1:8080" {
		t.Errorf("Target mismatch: got %q", req.Target())
	}
}

func TestParseRequest_UDPDomain(t *testing.T) {
	uuid := [16]byte{0xaa, 0xbb, 0xcc, 0xdd}
	domain := "example.com"
	addrBuf := append([]byte{byte(len(domain))}, []byte(domain)...)
	data := buildRequest(t, uuid, nil, CmdUDP, 53, AddrDomain, addrBuf)

	req, err := ParseRequest(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if req.Command != CmdUDP {
		t.Errorf("Command mismatch: got %d, want %d", req.Command, CmdUDP)
	}
	if req.Port != 53 {
		t.Errorf("Port mismatch: got %d, want 53", req.Port)
	}
	if req.Address != "example.com" {
		t.Errorf("Address mismatch: got %q, want %q", req.Address, "example.com")
	}
}

func TestParseRequest_TCPIPv6(t *testing.T) {
	uuid := [16]byte{0xff}
	ip := net.ParseIP("2001:db8::1").To16()
	data := buildRequest(t, uuid, nil, CmdTCP, 443, AddrIPv6, ip)

	req, err := ParseRequest(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if req.Address != "2001:db8::1" {
		t.Errorf("Address mismatch: got %q, want %q", req.Address, "2001:db8::1")
	}
}

func TestParseRequest_WithAddons(t *testing.T) {
	uuid := [16]byte{0x01}
	addons := []byte{0x0a, 0x0b, 0x0c}
	ip := net.ParseIP("10.0.0.1").To4()
	data := buildRequest(t, uuid, addons, CmdTCP, 80, AddrIPv4, ip)

	req, err := ParseRequest(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(req.Addons) != 3 {
		t.Errorf("Addons length mismatch: got %d, want 3", len(req.Addons))
	}
	if req.Address != "10.0.0.1" {
		t.Errorf("Address mismatch: got %q", req.Address)
	}
}

func TestParseRequest_BadVersion(t *testing.T) {
	data := []byte{0x01} // version 1, invalid
	_, err := ParseRequest(bytes.NewReader(data))
	if err == nil {
		t.Fatal("expected error for bad version")
	}
}

func TestParseRequest_Truncated(t *testing.T) {
	// Only version + partial UUID.
	data := []byte{0x00, 0x01, 0x02}
	_, err := ParseRequest(bytes.NewReader(data))
	if err == nil {
		t.Fatal("expected error for truncated input")
	}
}

func TestParseRequest_BadCommand(t *testing.T) {
	uuid := [16]byte{}
	ip := net.ParseIP("1.2.3.4").To4()
	data := buildRequest(t, uuid, nil, 0x03, 80, AddrIPv4, ip) // cmd=Mux, unsupported

	_, err := ParseRequest(bytes.NewReader(data))
	if err == nil {
		t.Fatal("expected error for unsupported command")
	}
}

func TestParseRequest_BadAddrType(t *testing.T) {
	uuid := [16]byte{}
	// Build manually with bad address type.
	var buf bytes.Buffer
	buf.WriteByte(Version)
	buf.Write(uuid[:])
	buf.WriteByte(0)    // no addons
	buf.WriteByte(0x01) // TCP
	var portBuf [2]byte
	binary.BigEndian.PutUint16(portBuf[:], 80)
	buf.Write(portBuf[:])
	buf.WriteByte(0xFF) // bad address type

	_, err := ParseRequest(bytes.NewReader(buf.Bytes()))
	if err == nil {
		t.Fatal("expected error for bad address type")
	}
}

func TestWriteResponse(t *testing.T) {
	var buf bytes.Buffer
	if err := WriteResponse(&buf); err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if buf.Len() != 2 {
		t.Fatalf("response length mismatch: got %d, want 2", buf.Len())
	}
	if buf.Bytes()[0] != 0x00 || buf.Bytes()[1] != 0x00 {
		t.Errorf("response bytes mismatch: got %v, want [0x00, 0x00]", buf.Bytes())
	}
}