Maint: refactor internal/routing
- Split Go files better - Reduce public API for exported errors
This commit is contained in:
98
internal/routing/ip_test.go
Normal file
98
internal/routing/ip_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_IPIsPrivate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
ipString string
|
||||
isPrivate bool
|
||||
}{
|
||||
"loopback 127.0.0.1": {
|
||||
ipString: "127.0.0.1",
|
||||
isPrivate: true,
|
||||
},
|
||||
"loopback 127.0.0.10": {
|
||||
ipString: "127.0.0.10",
|
||||
isPrivate: true,
|
||||
},
|
||||
"loopback ::1": {
|
||||
ipString: "::1",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 10.0.0.1": {
|
||||
ipString: "10.0.0.1",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 10.255.255.255": {
|
||||
ipString: "10.255.255.255",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 172.16.0.1": {
|
||||
ipString: "172.16.0.1",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 172.31.255.255": {
|
||||
ipString: "172.31.255.255",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 192.168.0.0": {
|
||||
ipString: "192.168.0.0",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private 192.168.255.255": {
|
||||
ipString: "192.168.255.255",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private fc00::": {
|
||||
ipString: "fc00::",
|
||||
isPrivate: true,
|
||||
},
|
||||
"private fc00::af": {
|
||||
ipString: "fc00::af",
|
||||
isPrivate: true,
|
||||
},
|
||||
"local unicast 169.254.0.0": {
|
||||
ipString: "169.254.0.0",
|
||||
isPrivate: true,
|
||||
},
|
||||
"local unicast 169.254.255.255": {
|
||||
ipString: "169.254.255.255",
|
||||
isPrivate: true,
|
||||
},
|
||||
"local unicast fe80::": {
|
||||
ipString: "fe80::",
|
||||
isPrivate: true,
|
||||
},
|
||||
"local unicast fe80::ae": {
|
||||
ipString: "fe80::ae",
|
||||
isPrivate: true,
|
||||
},
|
||||
"public IPv4": {
|
||||
ipString: "11.5.6.7",
|
||||
},
|
||||
"public IPv6": {
|
||||
ipString: "af6d::",
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ip := net.ParseIP(testCase.ipString)
|
||||
require.NotNil(t, ip)
|
||||
|
||||
isPrivate := IPIsPrivate(ip)
|
||||
|
||||
assert.Equal(t, testCase.isPrivate, isPrivate)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user