summaryrefslogtreecommitdiffhomepage
path: root/netboot/rtnetlink_linux_test.go
blob: 860ea0f167c583e529e407f3b9417a5aeb6edcfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//+build integration

package netboot

import (
	"net"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"golang.org/x/sys/unix"
)

// integration tests that require Linux with a properly working rtnetlink
// interface, and the existence of an "eth0" interface.
// WARNING: these tests may improperly configure your network interfaces and
// routing, so be careful before running them. Privileged access and integration
// build tag required to run them.

var (
	testIfname = "eth0"
)

func TestInit(t *testing.T) {
	var r RTNL
	err := r.init()
	assert.NoError(t, err)
	require.NotNil(t, r.conn)
	r.Close()
}

func TestClose(t *testing.T) {
	var r RTNL
	err := r.init()
	assert.NoError(t, err)
	require.NotNil(t, r.conn)
	r.Close()
	require.Nil(t, r.conn)
}

func TestGetLinkState(t *testing.T) {
	var r RTNL
	defer r.Close()

	iface, err := net.InterfaceByName(testIfname)
	require.NoError(t, err)
	_, err = r.GetLinkState(iface.Index)
	require.NoError(t, err)
}

func TestSetLinkState(t *testing.T) {
	var r RTNL
	defer r.Close()

	iface, err := net.InterfaceByName(testIfname)
	require.NoError(t, err)
	err = r.SetLinkState(iface.Index, true)
	require.NoError(t, err)
}

func Test_getFamily(t *testing.T) {
	require.Equal(t, unix.AF_INET, getFamily(net.IPv4zero))
	require.Equal(t, unix.AF_INET, getFamily(net.IPv4bcast))
	require.Equal(t, unix.AF_INET, getFamily(net.IPv4allrouter))

	require.Equal(t, unix.AF_INET6, getFamily(net.IPv6zero))
	require.Equal(t, unix.AF_INET6, getFamily(net.IPv6loopback))
	require.Equal(t, unix.AF_INET6, getFamily(net.IPv6linklocalallrouters))
}

func TestSetAddr(t *testing.T) {
	var r RTNL
	defer r.Close()
	iface, err := net.InterfaceByName(testIfname)
	require.NoError(t, err)

	a := net.IPNet{IP: net.ParseIP("10.0.123.1"), Mask: net.IPv4Mask(255, 255, 255, 0)}
	err = r.SetAddr(iface.Index, a)
	require.NoError(t, err)
	// TODO implement GetAddr to further validate this, and minimize the effect
	// of concurrent tests that may invalidate this check.
}