-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Showing
2 changed files
with
163 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package data | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestIncrementIP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ip string | ||
inc uint | ||
expected string | ||
}{ | ||
// IPv4 tests | ||
{ | ||
name: "IPv4 increment by 1", | ||
ip: "192.168.1.1", | ||
inc: 1, | ||
expected: "192.168.1.2", | ||
}, | ||
{ | ||
name: "IPv4 increment by 255", | ||
ip: "192.168.1.1", | ||
inc: 255, | ||
expected: "192.168.2.0", | ||
}, | ||
{ | ||
name: "IPv4 crosses multiple octets", | ||
ip: "192.168.255.255", | ||
inc: 1, | ||
expected: "192.169.0.0", | ||
}, | ||
// IPv6 tests | ||
{ | ||
name: "IPv6 increment by 1", | ||
ip: "2001:db8::1", | ||
inc: 1, | ||
expected: "2001:db8::2", | ||
}, | ||
{ | ||
name: "IPv6 increment by 255", | ||
ip: "2001:db8::1", | ||
inc: 255, | ||
expected: "2001:db8::100", | ||
}, | ||
{ | ||
name: "IPv6 increment across boundary", | ||
ip: "2001:db8::ffff", | ||
inc: 1, | ||
expected: "2001:db8::1:0", | ||
}, | ||
{ | ||
name: "IPv6 increment zero address", | ||
ip: "::", | ||
inc: 1, | ||
expected: "::1", | ||
}, | ||
{ | ||
name: "IPv6 increment large number", | ||
ip: "2001:db8::", | ||
inc: 65535, | ||
expected: "2001:db8::ffff", | ||
}, | ||
{ | ||
name: "IPv6 with zeros in middle", | ||
ip: "2001:db8:0:0:0:0:0:1", | ||
inc: 1, | ||
expected: "2001:db8::2", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ip := net.ParseIP(tt.ip) | ||
if ip == nil { | ||
t.Fatalf("failed to parse IP: %s", tt.ip) | ||
} | ||
|
||
result := incrementIP(ip, tt.inc) | ||
if result.String() != tt.expected { | ||
|
||
log.Println(len(ip), len(result)) | ||
|
||
t.Errorf("incrementIP(%s, %d) = %s; want %s", | ||
tt.ip, tt.inc, result.String(), tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIncrementIPOverflow(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ip string | ||
inc uint | ||
}{ | ||
{ | ||
name: "IPv4 overflow max IP", | ||
ip: "255.255.255.255", | ||
inc: 1, | ||
}, | ||
{ | ||
name: "IPv6 overflow max IP", | ||
ip: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", | ||
inc: 1, | ||
}, | ||
{ | ||
name: "IPv6 large overflow", | ||
ip: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", | ||
inc: 256, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ip := net.ParseIP(tt.ip) | ||
if ip == nil { | ||
t.Fatalf("failed to parse IP: %s", tt.ip) | ||
} | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Errorf("incrementIP(%s, %d) paniced", | ||
tt.ip, tt.inc) | ||
} | ||
}() | ||
|
||
incrementIP(ip, tt.inc) | ||
}) | ||
} | ||
} |