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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| | #! /usr/bin/env avocado-runner-avocado-classless
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
"""
Test A Simple Socket Transport
ndp.py - Helpers for testing NDP
"""
import dataclasses
import ipaddress
import os
import tempfile
from typing import Iterator
import exeter
from . import cmdsite, ip, unshare, veth
@dataclasses.dataclass
class NdpScenario(exeter.Scenario):
client: cmdsite.CmdSite
ifname: str
network: ip.Net
gateway: ip.Addr
@exeter.scenariotest
def ndp_addr(self) -> None:
# Wait for NDP to do its thing
(addr,) = ip.addr_wait(self.client, self.ifname,
family='inet6', scope='global')
# The SLAAC address is derived from the guest ns MAC, so
# probably won't exactly match the host address (we need
# DHCPv6 for that). It should be in the right network though.
exeter.assert_eq(addr.network, self.network)
@exeter.scenariotest
def ndp_route(self) -> None:
defroutes = ip.routes6(self.client, dst='default')
while not defroutes:
defroutes = ip.routes6(self.client, dst='default')
exeter.assert_eq(len(defroutes), 1)
gw = ipaddress.ip_address(defroutes[0]['gateway'])
exeter.assert_eq(gw, self.gateway)
IFNAME = 'clientif'
NETWORK = ip.TEST_NET6_TASST_A
ipa = ip.IpiAllocator(NETWORK)
(ROUTER_IP6,) = ipa.next_ipis()
def setup_radvd() -> Iterator[NdpScenario]:
router_ifname = 'routerif'
with unshare.unshare('client', '-Un') as client, \
unshare.unshare('router', '-n',
parent=client, privilege=True) as router, \
tempfile.TemporaryDirectory() as tmpdir, \
veth.veth(client, IFNAME, router_ifname, router):
# Configure the simulated router
confpath = os.path.join(tmpdir, 'radvd.conf')
pidfile = os.path.join(tmpdir, 'radvd.pid')
open(confpath, 'w', encoding='UTF-8').write(
f'''
interface {router_ifname} {{
AdvSendAdvert on;
prefix {NETWORK} {{
}};
}};
'''
)
ip.ifup(router, 'lo')
ip.ifup(router, 'routerif', ROUTER_IP6)
# Configure the client
ip.ifup(client, 'lo')
ip.ifup(client, IFNAME)
# Get the router's link-local-address
(router_ll,) = ip.addr_wait(router, router_ifname,
family='inet6', scope='link')
# Run radvd
router.fg('radvd', '-c', '-C', f'{confpath}')
radvd_cmd = ['radvd', '-C', f'{confpath}', '-n',
'-p', f'{pidfile}', '-d', '5']
with router.bg(*radvd_cmd, privilege=True) as radvd:
yield NdpScenario(client=client,
ifname=IFNAME,
network=NETWORK,
gateway=router_ll.ip)
radvd.terminate()
def selftests() -> None:
NdpScenario.test(setup_radvd)
|