2021-09-16 19:12:27 +00:00
|
|
|
package network_test
|
2019-01-11 01:08:22 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-16 00:17:22 +00:00
|
|
|
"net"
|
2019-01-11 01:08:22 +00:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
2019-10-04 22:46:49 +00:00
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2019-01-11 01:08:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExternalIPv4(t *testing.T) {
|
|
|
|
// Regular expression format for IPv4
|
|
|
|
IPv4Format := `\.\d{1,3}\.\d{1,3}\b`
|
2021-09-16 19:12:27 +00:00
|
|
|
test, err := network.ExternalIPv4()
|
2020-07-19 21:08:29 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-11 01:08:22 +00:00
|
|
|
|
|
|
|
valid := regexp.MustCompile(IPv4Format)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, true, valid.MatchString(test))
|
2019-01-11 01:08:22 +00:00
|
|
|
}
|
2020-09-16 00:17:22 +00:00
|
|
|
|
|
|
|
func TestRetrieveIP(t *testing.T) {
|
2021-09-16 19:12:27 +00:00
|
|
|
ip, err := network.ExternalIP()
|
2020-09-16 00:17:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
retIP := net.ParseIP(ip)
|
|
|
|
if retIP.To4() == nil && retIP.To16() == nil {
|
|
|
|
t.Errorf("An invalid IP was retrieved: %s", ip)
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 03:02:20 +00:00
|
|
|
|
|
|
|
func TestSortAddresses(t *testing.T) {
|
|
|
|
testAddresses := []net.IP{
|
|
|
|
{0xff, 0x02, 0xAA, 0, 0x1F, 0, 0, 0, 0, 0, 0x02, 0x2E, 0, 0, 0x36, 0x45},
|
|
|
|
{0xff, 0x02, 0xAA, 0, 0x1F, 0, 0x2E, 0, 0, 0x36, 0x45, 0, 0, 0, 0, 0x02},
|
|
|
|
{0xAA, 0x11, 0x33, 0x19},
|
|
|
|
{0x01, 0xBF, 0x33, 0x10},
|
|
|
|
{0x03, 0x89, 0x33, 0x13},
|
|
|
|
}
|
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
sortedAddrs := network.SortAddresses(testAddresses)
|
2020-09-17 03:02:20 +00:00
|
|
|
assert.Equal(t, true, sortedAddrs[0].To4() != nil, "expected ipv4 address")
|
|
|
|
assert.Equal(t, true, sortedAddrs[1].To4() != nil, "expected ipv4 address")
|
|
|
|
assert.Equal(t, true, sortedAddrs[2].To4() != nil, "expected ipv4 address")
|
|
|
|
assert.Equal(t, true, sortedAddrs[3].To16() != nil && sortedAddrs[3].To4() == nil, "expected ipv6 address")
|
|
|
|
assert.Equal(t, true, sortedAddrs[4].To16() != nil && sortedAddrs[4].To4() == nil, "expected ipv6 address")
|
|
|
|
}
|