1 package sa
2
3 import (
4 "net"
5 "testing"
6 )
7
8 func TestIncrementIP(t *testing.T) {
9 testCases := []struct {
10 ip string
11 index int
12 expected string
13 }{
14 {"0.0.0.0", 128, "0.0.0.1"},
15 {"0.0.0.255", 128, "0.0.1.0"},
16 {"127.0.0.1", 128, "127.0.0.2"},
17 {"1.2.3.4", 120, "1.2.4.4"},
18 {"::1", 128, "::2"},
19 {"2002:1001:4008::", 128, "2002:1001:4008::1"},
20 {"2002:1001:4008::", 48, "2002:1001:4009::"},
21 {"2002:1001:ffff::", 48, "2002:1002::"},
22 {"ffff:ffff:ffff::", 48, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"},
23 }
24 for _, tc := range testCases {
25 ip := net.ParseIP(tc.ip).To16()
26 actual := incrementIP(ip, tc.index)
27 expectedIP := net.ParseIP(tc.expected)
28 if !actual.Equal(expectedIP) {
29 t.Errorf("Expected incrementIP(%s, %d) to be %s, instead got %s",
30 tc.ip, tc.index, expectedIP, actual.String())
31 }
32 }
33 }
34
35 func TestIPRange(t *testing.T) {
36 testCases := []struct {
37 ip string
38 expectedBegin string
39 expectedEnd string
40 }{
41 {"28.45.45.28", "28.45.45.28", "28.45.45.29"},
42 {"2002:1001:4008::", "2002:1001:4008::", "2002:1001:4009::"},
43 }
44 for _, tc := range testCases {
45 ip := net.ParseIP(tc.ip)
46 expectedBegin := net.ParseIP(tc.expectedBegin)
47 expectedEnd := net.ParseIP(tc.expectedEnd)
48 actualBegin, actualEnd := ipRange(ip)
49 if !expectedBegin.Equal(actualBegin) || !expectedEnd.Equal(actualEnd) {
50 t.Errorf("Expected ipRange(%s) to be (%s, %s), got (%s, %s)",
51 tc.ip, tc.expectedBegin, tc.expectedEnd, actualBegin, actualEnd)
52 }
53 }
54 }
55
View as plain text