-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef45dd3
commit 33ee8df
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package nftables | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNetFirstAndLastIP(t *testing.T) { | ||
type args struct { | ||
cidr string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantFirstIP net.IP | ||
wantLastIP net.IP | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test Fake", | ||
args: args{cidr: "fakecidr"}, | ||
wantFirstIP: nil, | ||
wantLastIP: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Test IPV4 1", | ||
args: args{cidr: "10.0.0.0/24"}, | ||
wantFirstIP: net.IP{10, 0, 0, 0}, | ||
wantLastIP: net.IP{10, 0, 0, 255}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test IPV4 2", | ||
args: args{cidr: "10.0.0.20/24"}, | ||
wantFirstIP: net.IP{10, 0, 0, 0}, | ||
wantLastIP: net.IP{10, 0, 0, 255}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test IPV4 2", | ||
args: args{cidr: "10.0.0.0/19"}, | ||
wantFirstIP: net.IP{10, 0, 0, 0}, | ||
wantLastIP: net.IP{10, 0, 31, 255}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test IPV6 1", | ||
args: args{cidr: "ff00::/16"}, | ||
wantFirstIP: net.ParseIP("ff00::"), | ||
wantLastIP: net.ParseIP("ff00:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test IPV6 2", | ||
args: args{cidr: "2001:db8::/62"}, | ||
wantFirstIP: net.ParseIP("2001:db8::"), | ||
wantLastIP: net.ParseIP("2001:db8:0000:0003:ffff:ffff:ffff:ffff"), | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotFirstIP, gotLastIP, err := NetFirstAndLastIP(tt.args.cidr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetFirstAndLastIPFromCIDR() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(gotFirstIP, tt.wantFirstIP) { | ||
t.Errorf("GetFirstAndLastIPFromCIDR() gotFirstIP = %v, want %v", gotFirstIP, tt.wantFirstIP) | ||
} | ||
if !reflect.DeepEqual(gotLastIP, tt.wantLastIP) { | ||
t.Errorf("GetFirstAndLastIPFromCIDR() gotLastIP = %v, want %v", gotLastIP, tt.wantLastIP) | ||
} | ||
}) | ||
} | ||
} |