-
Notifications
You must be signed in to change notification settings - Fork 4
/
random_local_ip_test.go
404 lines (357 loc) · 11.3 KB
/
random_local_ip_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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package warc
import (
"net"
"strings"
"testing"
)
// TestGenerateRandomIPv6 tests the generation of a random IPv6 address within a given subnet.
func TestGenerateRandomIPv6(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("2001:db8::"),
Mask: net.CIDRMask(64, 128),
}
ip, err := generateRandomIPv6(baseIPNet)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !baseIPNet.Contains(ip) {
t.Errorf("Generated IP %v is not within base IP network %v", ip, baseIPNet)
}
}
// TestGenerateRandomIPv6InvalidBaseIP tests the function with an invalid base IP.
func TestGenerateRandomIPv6InvalidBaseIP(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("invalidIP"),
Mask: net.CIDRMask(64, 128),
}
_, err := generateRandomIPv6(baseIPNet)
if err == nil {
t.Error("Expected error for invalid base IP, got nil")
}
}
// TestGenerateRandomIPv6InvalidMask tests the function with an invalid mask length.
func TestGenerateRandomIPv6InvalidMask(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("2001:db8::"),
Mask: net.CIDRMask(129, 128),
}
_, err := generateRandomIPv6(baseIPNet)
if err == nil {
t.Error("Expected error for invalid mask length, got nil")
}
baseIPNet = net.IPNet{
IP: net.ParseIP("2001:db8::"),
Mask: net.IPMask([]byte{
0xff, 0xfe, 0xff, 0xff, // Non-contiguous mask (invalid) (2nd byte)
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
}),
}
_, err = generateRandomIPv6(baseIPNet)
if err == nil {
t.Error("Expected error for invalid mask length, got nil")
}
}
func TestGenerateRandomIPv6EmptyMask(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("2001:db8::"),
Mask: net.IPMask([]byte{}), // Empty mask
}
_, err := generateRandomIPv6(baseIPNet)
if err == nil {
t.Error("Expected error for empty mask, got nil")
}
}
// TestGenerateRandomIPv6FullMask tests the function with a /128 mask (no host bits).
func TestGenerateRandomIPv6FullMask(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("2001:db8::1"),
Mask: net.CIDRMask(128, 128),
}
ip, err := generateRandomIPv6(baseIPNet)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !ip.Equal(baseIPNet.IP) {
t.Errorf("Expected IP %v, got %v", baseIPNet.IP, ip)
}
}
// TestGenerateRandomIPv6ZeroMask tests the function with a /0 mask (all host bits).
func TestGenerateRandomIPv6ZeroMask(t *testing.T) {
baseIPNet := net.IPNet{
IP: net.ParseIP("::"),
Mask: net.CIDRMask(0, 128),
}
ip, err := generateRandomIPv6(baseIPNet)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !baseIPNet.Contains(ip) {
t.Errorf("Generated IP %v is not within base IP network %v", ip, baseIPNet)
}
}
// TestGetNextIP tests the round-robin IP selection.
func TestGetNextIP(t *testing.T) {
ip1 := net.ParseIP("192.168.1.1")
ip2 := net.ParseIP("192.168.1.2")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(24, 32)}
ipNet2 := net.IPNet{IP: ip2, Mask: net.CIDRMask(24, 32)}
ipList := []net.IPNet{ipNet1, ipNet2}
availableIPs := &availableIPs{}
availableIPs.IPs.Store(&ipList)
ip := getNextIP(availableIPs)
if !ip.Equal(ip1) {
t.Errorf("Expected %v, got %v", ip1, ip)
}
ip = getNextIP(availableIPs)
if !ip.Equal(ip2) {
t.Errorf("Expected %v, got %v", ip2, ip)
}
ip = getNextIP(availableIPs)
if !ip.Equal(ip1) {
t.Errorf("Expected %v, got %v", ip1, ip)
}
}
// TestGetNextIPEmptyIPs tests the function when no IPs are available.
func TestGetNextIPEmptyIPs(t *testing.T) {
availableIPs := &availableIPs{}
availableIPs.IPs.Store(&[]net.IPNet{})
ip := getNextIP(availableIPs)
if ip != nil {
t.Errorf("Expected nil, got %v", ip)
}
}
// TestGetNextIPAnyIP tests the function with AnyIP set to true for IPv6.
func TestGetNextIPAnyIP(t *testing.T) {
baseIP := net.ParseIP("2001:db8::")
baseIPNet := net.IPNet{IP: baseIP, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{baseIPNet}
availableIPs := &availableIPs{AnyIP: true}
availableIPs.IPs.Store(&ipList)
ip := getNextIP(availableIPs)
if ip == nil {
t.Error("Expected non-nil IP, got nil")
}
if !baseIPNet.Contains(ip) {
t.Errorf("Generated IP %v is not within base IP network %v", ip, baseIPNet)
}
}
// TestGetNextIPAnyIPv6MultipleIPv6 tests the function with multiple IPv6 addresses and AnyIP set to true.
func TestGetNextIPAnyIPMultipleIPv6(t *testing.T) {
baseIP1 := net.ParseIP("2001:db8::")
baseIPNet1 := net.IPNet{IP: baseIP1, Mask: net.CIDRMask(64, 128)}
baseIP2 := net.ParseIP("2001:db9::")
baseIPNet2 := net.IPNet{IP: baseIP2, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{baseIPNet1, baseIPNet2}
availableIPs := &availableIPs{AnyIP: true}
availableIPs.IPs.Store(&ipList)
for i := 0; i < 5; i++ {
ip := getNextIP(availableIPs)
if ip == nil {
t.Error("Expected non-nil IP, got nil")
}
if !baseIPNet1.Contains(ip) && !baseIPNet2.Contains(ip) {
t.Errorf("Generated IP %v is not within base IP network %v or %v", ip, baseIPNet1, baseIPNet2)
}
}
}
// TestTestGetNextIPAnyIPv6Randomness tests the randomness of generated IPv6 addresses.
func TestTestGetNextIPAnyIPv6Randomness(t *testing.T) {
baseIP := net.ParseIP("2001:db8::")
baseIPNet := net.IPNet{IP: baseIP, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{baseIPNet}
availableIPs := &availableIPs{AnyIP: true}
availableIPs.IPs.Store(&ipList)
ip1 := getNextIP(availableIPs)
ip2 := getNextIP(availableIPs)
if ip1.Equal(ip2) {
t.Errorf("Expected different IPs, got %v", ip1)
}
}
// TestGetNextIPHighIndex tests the function with a high index value.
func TestGetNextIPHighIndex(t *testing.T) {
ip1 := net.ParseIP("192.168.1.1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(24, 32)}
ipList := []net.IPNet{ipNet1}
availableIPs := &availableIPs{}
availableIPs.IPs.Store(&ipList)
availableIPs.Index.Store(9999999)
ip := getNextIP(availableIPs)
if !ip.Equal(ip1) {
t.Errorf("Expected %v, got %v", ip1, ip)
}
}
// TestGetLocalAddrIPv4TCP tests local address selection for IPv4 TCP connections.
func TestGetLocalAddrIPv4TCP(t *testing.T) {
IPv4 = &availableIPs{}
ip1 := net.ParseIP("192.168.1.1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(24, 32)}
ipList := []net.IPNet{ipNet1}
IPv4.IPs.Store(&ipList)
addr := getLocalAddr("tcp", "192.168.1.2")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
}
if !tcpAddr.IP.Equal(ip1) {
t.Errorf("Expected IP %v, got %v", ip1, tcpAddr.IP)
}
}
// TestGetLocalAddrIPv6TCP tests local address selection for IPv6 TCP connections.
func TestGetLocalAddrIPv6TCP(t *testing.T) {
IPv6 = &availableIPs{}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)
addr := getLocalAddr("tcp6", "[2001:db8::2]")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
}
if !tcpAddr.IP.Equal(ip1) {
t.Errorf("Expected IP %v, got %v", ip1, tcpAddr.IP)
}
}
func TestGetLocalAddrIPv6TCPAnyIP(t *testing.T) {
IPv6 = &availableIPs{AnyIP: true}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)
addr := getLocalAddr("tcp6", "[2001:db12::20]")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
}
if !ipNet1.Contains(tcpAddr.IP) {
t.Errorf("Expected IP within %v, got %v", ipNet1, tcpAddr.IP)
}
}
// TestGetLocalAddrIPv4UDP tests local address selection for IPv4 UDP connections.
func TestGetLocalAddrIPv4UDP(t *testing.T) {
IPv4 = &availableIPs{}
ip1 := net.ParseIP("192.168.1.1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(24, 32)}
ipList := []net.IPNet{ipNet1}
IPv4.IPs.Store(&ipList)
addr := getLocalAddr("udp", "192.168.1.2")
udpAddr, ok := addr.(*net.UDPAddr)
if !ok {
t.Errorf("Expected *net.UDPAddr, got %T", addr)
}
if !udpAddr.IP.Equal(ip1) {
t.Errorf("Expected IP %v, got %v", ip1, udpAddr.IP)
}
}
// TestGetLocalAddrIPv6UDP tests local address selection for IPv6 UDP connections.
func TestGetLocalAddrIPv6UDP(t *testing.T) {
IPv6 = &availableIPs{}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)
addr := getLocalAddr("udp", "[2001:db8::2]")
udpAddr, ok := addr.(*net.UDPAddr)
if !ok {
t.Errorf("Expected *net.UDPAddr, got %T", addr)
}
if !udpAddr.IP.Equal(ip1) {
t.Errorf("Expected IP %v, got %v", ip1, udpAddr.IP)
}
}
// TestGetLocalAddrInvalidIP tests the function with an invalid destination IP.
func TestGetLocalAddrInvalidIP(t *testing.T) {
addr := getLocalAddr("tcp", "invalidIP")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}
// TestGetLocalAddrUnknownNetwork tests the function with an unknown network type.
func TestGetLocalAddrUnknownNetwork(t *testing.T) {
addr := getLocalAddr("unknown", "192.168.1.2")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}
// TestGetLocalAddrNoPort tests the function with an address missing a port.
func TestGetLocalAddrWithPort(t *testing.T) {
addr := getLocalAddr("tcp", "192.168.1.2:80")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}
// TestGetLocalAddrMalformedAddress tests the function with a malformed address.
func TestGetLocalAddrMalformedAddress(t *testing.T) {
addr := getLocalAddr("tcp", "192.168.1.2::80")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}
// TestAnyIPIPv6IPv4DisabledRealLife tests the function with IPv6 enabled and IPv4 disabled.
func TestAnyIPIPv6IPv4DisabledRealLife(t *testing.T) {
IPv6 = &availableIPs{AnyIP: true}
IPv4 = &availableIPs{}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)
tcpAddr := getLocalAddr("tcp6", "2606:4700:3030::ac43:a86a")
if tcpAddr == nil {
t.Error("Expected non-nil TCP address, got nil")
}
t.Logf("IPv6 TCP address: %v", tcpAddr)
}
// TestGetAvailableIPs is difficult due to its infinite loop and dependency on system interfaces.
func TestGetAvailableIPsAnyIP(t *testing.T) {
if IPv6 == nil {
IPv6 = &availableIPs{
AnyIP: true,
}
}
if IPv4 == nil {
IPv4 = &availableIPs{}
}
// Get all network interfaces
interfaces, err := net.Interfaces()
if err != nil {
}
// Iterate over the interfaces
newIPv4 := make([]net.IPNet, 0)
newIPv6 := make([]net.IPNet, 0)
for _, iface := range interfaces {
if strings.Contains(iface.Name, "docker") {
continue
}
// Get the addresses associated with the interface
addrs, err := iface.Addrs()
if err != nil {
continue
}
// Iterate over the addresses
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
t.Logf("IPNet: %v", ipNet)
ip := ipNet.IP
if ip.IsLoopback() {
continue
}
// Process Global Unicast IPv6 addresses
if ip.IsGlobalUnicast() && ip.To16() != nil && ip.To4() == nil && ip.IsGlobalUnicast() {
newIPv6 = append(newIPv6, *ipNet)
}
// Process Global Unicast IPv4 addresses
if ip.IsGlobalUnicast() && ip.To16() == nil && ip.To4() != nil {
// Add IPv4 addresses to the list
newIPv4 = append(newIPv4, *ipNet)
}
}
}
}
// Add the new addresses to the list
IPv6.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)
tcpv6Addr := getLocalAddr("tcp", "[2001:db8::2]:80")
t.Logf("IPv6 TCP address: %v", tcpv6Addr)
}