2021-03-16 16:42:15 +00:00
|
|
|
package aghnet
|
2019-02-22 14:59:42 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-31 12:00:47 +00:00
|
|
|
"net"
|
2019-02-22 14:59:42 +00:00
|
|
|
"testing"
|
2021-02-09 16:38:31 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 16:38:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-22 14:59:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetValidNetInterfacesForWeb(t *testing.T) {
|
2020-02-13 15:42:07 +00:00
|
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
2021-03-25 15:03:29 +00:00
|
|
|
require.NoErrorf(t, err, "cannot get net interfaces: %s", err)
|
|
|
|
require.NotEmpty(t, ifaces, "no net interfaces found")
|
2019-02-22 14:59:42 +00:00
|
|
|
for _, iface := range ifaces {
|
2021-03-25 15:03:29 +00:00
|
|
|
require.NotEmptyf(t, iface.Addresses, "no addresses found for %s", iface.Name)
|
2019-02-22 14:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
|
|
|
func TestUnreverseAddr(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
have string
|
|
|
|
want net.IP
|
|
|
|
}{{
|
|
|
|
name: "good_ipv4",
|
|
|
|
have: "1.0.0.127.in-addr.arpa",
|
|
|
|
want: net.IP{127, 0, 0, 1},
|
|
|
|
}, {
|
|
|
|
name: "good_ipv6",
|
|
|
|
have: "4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
|
|
|
|
want: net.ParseIP("::abcd:1234"),
|
|
|
|
}, {
|
|
|
|
name: "good_ipv6_case",
|
|
|
|
have: "4.3.2.1.d.c.B.A.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.iP6.ArPa",
|
|
|
|
want: net.ParseIP("::abcd:1234"),
|
|
|
|
}, {
|
|
|
|
name: "good_ipv4_dot",
|
|
|
|
have: "1.0.0.127.in-addr.arpa.",
|
|
|
|
want: net.IP{127, 0, 0, 1},
|
|
|
|
}, {
|
|
|
|
name: "good_ipv4_case",
|
|
|
|
have: "1.0.0.127.In-Addr.Arpa",
|
|
|
|
want: net.IP{127, 0, 0, 1},
|
|
|
|
}, {
|
|
|
|
name: "wrong_ipv4",
|
|
|
|
have: ".0.0.127.in-addr.arpa",
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "wrong_ipv6",
|
|
|
|
have: ".3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "bad_ipv6_dot",
|
|
|
|
have: "4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0..ip6.arpa",
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "bad_ipv6_space",
|
|
|
|
have: "4.3.2.1.d.c.b. .0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
|
|
|
|
want: nil,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ip := UnreverseAddr(tc.have)
|
|
|
|
assert.True(t, tc.want.Equal(ip))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReverseAddr(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
|
|
|
ip net.IP
|
|
|
|
}{{
|
|
|
|
name: "valid_ipv4",
|
|
|
|
want: "4.3.2.1.in-addr.arpa",
|
|
|
|
ip: net.IP{1, 2, 3, 4},
|
|
|
|
}, {
|
|
|
|
name: "valid_ipv6",
|
|
|
|
want: "1.3.b.5.4.1.8.6.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.7.4.6.0.6.2.ip6.arpa",
|
|
|
|
ip: net.ParseIP("2606:4700:10::6814:5b31"),
|
|
|
|
}, {
|
|
|
|
name: "nil_ip",
|
|
|
|
want: "",
|
|
|
|
ip: nil,
|
|
|
|
}, {
|
|
|
|
name: "unspecified_ipv6",
|
|
|
|
want: "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
|
|
|
|
ip: net.IPv6unspecified,
|
|
|
|
}, {
|
|
|
|
name: "unspecified_ipv4",
|
|
|
|
want: "0.0.0.0.in-addr.arpa",
|
|
|
|
ip: net.IPv4zero,
|
|
|
|
}, {
|
|
|
|
name: "wrong_length_ip",
|
|
|
|
want: "",
|
|
|
|
ip: net.IP{1, 2, 3, 4, 5},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.want, ReverseAddr(tc.ip))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|