-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
87 lines (80 loc) · 1.67 KB
/
main_test.go
File metadata and controls
87 lines (80 loc) · 1.67 KB
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
package main
import (
"net"
"reflect"
"testing"
)
func TestGenerateIPs(t *testing.T) {
tests := []struct {
cidr string
expected []string
}{
{
cidr: "192.168.1.0/30",
// 192.168.1.0 (network)
// 192.168.1.1
// 192.168.1.2
// 192.168.1.3 (broadcast)
// New logic emits all IPs in range
expected: []string{"192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"},
},
{
cidr: "10.0.0.1/32",
// Single IP
expected: []string{"10.0.0.1"},
},
}
for _, tt := range tests {
ip, ipNet, err := net.ParseCIDR(tt.cidr)
if err != nil {
t.Fatalf("Failed to parse CIDR %s: %v", tt.cidr, err)
}
// Consume channel
var got []string
for ip := range generateIPs(ip, ipNet, 100) {
got = append(got, ip)
}
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("generateIPs(%s) = %v, want %v", tt.cidr, got, tt.expected)
}
}
}
func TestParseInput(t *testing.T) {
tests := []struct {
input string
wantErr bool
expected string // CIDR string representation of the network
}{
{
input: "192.168.1.0/24",
wantErr: false,
expected: "192.168.1.0/24",
},
{
input: "10.0.0.1",
wantErr: false,
expected: "10.0.0.1/32",
},
{
input: "3",
wantErr: false,
expected: "192.168.3.0/24",
},
{
input: "invalid",
wantErr: true,
},
}
for _, tt := range tests {
_, ipNet, err := parseInput(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parseInput(%s) error = %v, wantErr %v", tt.input, err, tt.wantErr)
continue
}
if !tt.wantErr {
if ipNet.String() != tt.expected {
t.Errorf("parseInput(%s) = %v, want %v", tt.input, ipNet.String(), tt.expected)
}
}
}
}