~bigbes/shroud

ref: 3804bcf465a4eb00d1f03720316d0a176b0b97fb shroud/internal/awgserver/ipalloc_test.go -rw-r--r-- 1.7 KiB
3804bcf4 — Eugene Blikh feat(vless): add VLESS+REALITY transport support 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
package awgserver

import "testing"

func TestIPAllocator_Basic(t *testing.T) {
	alloc, err := NewIPAllocator("10.14.0.0/24")
	if err != nil {
		t.Fatalf("NewIPAllocator() error: %v", err)
	}

	if got := alloc.ServerIP().String(); got != "10.14.0.1" {
		t.Errorf("ServerIP() = %s, want 10.14.0.1", got)
	}

	ip, err := alloc.Allocate(nil)
	if err != nil {
		t.Fatalf("Allocate() error: %v", err)
	}
	if ip != "10.14.0.2/32" {
		t.Errorf("first allocation = %s, want 10.14.0.2/32", ip)
	}
}

func TestIPAllocator_SkipsUsed(t *testing.T) {
	alloc, err := NewIPAllocator("10.14.0.0/24")
	if err != nil {
		t.Fatalf("NewIPAllocator() error: %v", err)
	}

	used := []string{"10.14.0.2/32", "10.14.0.3/32"}
	ip, err := alloc.Allocate(used)
	if err != nil {
		t.Fatalf("Allocate() error: %v", err)
	}
	if ip != "10.14.0.4/32" {
		t.Errorf("allocation = %s, want 10.14.0.4/32", ip)
	}
}

func TestIPAllocator_Exhaustion(t *testing.T) {
	alloc, err := NewIPAllocator("10.14.0.0/30") // Only 4 IPs: .0 (network), .1 (server), .2 (peer), .3 (broadcast)
	if err != nil {
		t.Fatalf("NewIPAllocator() error: %v", err)
	}

	ip, err := alloc.Allocate(nil)
	if err != nil {
		t.Fatalf("first Allocate() error: %v", err)
	}
	if ip != "10.14.0.2/32" {
		t.Errorf("allocation = %s, want 10.14.0.2/32", ip)
	}

	_, err = alloc.Allocate([]string{"10.14.0.2/32"})
	if err == nil {
		t.Errorf("expected error on exhausted subnet, got nil")
	}
}

func TestIPAllocator_16Subnet(t *testing.T) {
	alloc, err := NewIPAllocator("10.14.0.0/16")
	if err != nil {
		t.Fatalf("NewIPAllocator() error: %v", err)
	}

	ip, err := alloc.Allocate(nil)
	if err != nil {
		t.Fatalf("Allocate() error: %v", err)
	}
	if ip != "10.14.0.2/32" {
		t.Errorf("allocation = %s, want 10.14.0.2/32", ip)
	}
}