~bigbes/shroud

ref: 3394139df40339ebdae50f853d922c14fca77c35 shroud/internal/awgserver/detect_test.go -rw-r--r-- 1.8 KiB
3394139d — Eugene Blikh docs: update README with VLESS+REALITY, CI/CD site deployment a month 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
package awgserver

import (
	"encoding/binary"
	"testing"
)

func testConfig() *Config {
	return &Config{
		S1: 10, H1: HeaderRange{Min: 1000, Max: 2000},
		S2: 10, H2: HeaderRange{Min: 3000, Max: 4000},
		S3: 10, H3: HeaderRange{Min: 5000, Max: 6000},
		S4: 10, H4: HeaderRange{Min: 7000, Max: 8000},
	}
}

func TestIsAWGPacket_Initiation(t *testing.T) {
	cfg := testConfig()

	// Build a packet: S1(10) padding + 148 bytes = 158 total.
	pkt := make([]byte, 10+messageInitiationSize)
	binary.LittleEndian.PutUint32(pkt[10:], 1500) // H1 value in range

	if !isAWGPacket(pkt, cfg) {
		t.Error("expected initiation packet to match")
	}

	// Wrong size — should not match initiation (and other checks also won't match).
	wrongSize := make([]byte, 10+messageInitiationSize+1)
	binary.LittleEndian.PutUint32(wrongSize[10:], 1500)
	if isAWGPacket(wrongSize, cfg) {
		t.Error("wrong size should not match")
	}

	// H1 out of range.
	pktOutOfRange := make([]byte, 10+messageInitiationSize)
	binary.LittleEndian.PutUint32(pktOutOfRange[10:], 999)
	if isAWGPacket(pktOutOfRange, cfg) {
		t.Error("out-of-range H1 should not match")
	}
}

func TestIsAWGPacket_Transport(t *testing.T) {
	cfg := testConfig()
	cfg.S4 = 5
	cfg.H4 = HeaderRange{Min: 500, Max: 600}

	// Transport: size >= S4+16.
	pkt := make([]byte, 5+messageTransportHeaderSize+100)
	binary.LittleEndian.PutUint32(pkt[5:], 550)

	if !isAWGPacket(pkt, cfg) {
		t.Error("expected transport packet to match")
	}

	// Too small.
	small := make([]byte, 5+messageTransportHeaderSize-1)
	if isAWGPacket(small, cfg) {
		t.Error("too-small packet should not match transport")
	}
}

func TestIsAWGPacket_NonAWG(t *testing.T) {
	cfg := testConfig()

	// Random packet that doesn't match any message type.
	pkt := make([]byte, 200)
	if isAWGPacket(pkt, cfg) {
		t.Error("random packet should not match")
	}
}