summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_service_router-advert.py
blob: 6dbb6add4827ed11862668689428cf13e794f08d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
#
# Copyright (C) 2019-2022 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import unittest

from vyos.configsession import ConfigSessionError
from base_vyostest_shim import VyOSUnitTestSHIM

from vyos.utils.file import read_file
from vyos.utils.process import process_named_running

PROCESS_NAME = 'radvd'
RADVD_CONF = '/run/radvd/radvd.conf'

interface = 'eth1'
base_path = ['service', 'router-advert', 'interface', interface]
address_base = ['interfaces', 'ethernet', interface, 'address']
prefix = '::/64'

def get_config_value(key):
    tmp = read_file(RADVD_CONF)
    tmp = re.findall(r'\n?{}\s+(.*)'.format(key), tmp)
    return tmp[0].split()[0].replace(';','')

class TestServiceRADVD(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestServiceRADVD, cls).setUpClass()

        # ensure we can also run this test on a live system - so lets clean
        # out the current configuration :)
        cls.cli_delete(cls, ['service', 'router-advert'])

        cls.cli_set(cls, address_base + ['2001:db8::1/64'])

    @classmethod
    def tearDownClass(cls):
        cls.cli_delete(cls, address_base)
        super(TestServiceRADVD, cls).tearDownClass()

    def tearDown(self):
        # Check for running process
        self.assertTrue(process_named_running(PROCESS_NAME))

        self.cli_delete(base_path)
        self.cli_commit()

        # Check for no longer running process
        self.assertFalse(process_named_running(PROCESS_NAME))

    def test_common(self):
        self.cli_set(base_path + ['prefix', prefix, 'no-on-link-flag'])
        self.cli_set(base_path + ['prefix', prefix, 'no-autonomous-flag'])
        self.cli_set(base_path + ['prefix', prefix, 'valid-lifetime', 'infinity'])
        self.cli_set(base_path + ['other-config-flag'])

        # commit changes
        self.cli_commit()

        # verify values
        tmp = get_config_value('interface')
        self.assertEqual(tmp, interface)

        tmp = get_config_value('prefix')
        self.assertEqual(tmp, prefix)

        tmp = get_config_value('AdvOtherConfigFlag')
        self.assertEqual(tmp, 'on')

        # this is a default value
        tmp = get_config_value('AdvRetransTimer')
        self.assertEqual(tmp, '0')

        # this is a default value
        tmp = get_config_value('AdvCurHopLimit')
        self.assertEqual(tmp, '64')

        # this is a default value
        tmp = get_config_value('AdvDefaultPreference')
        self.assertEqual(tmp, 'medium')

        tmp = get_config_value('AdvAutonomous')
        self.assertEqual(tmp, 'off')

        # this is a default value
        tmp = get_config_value('AdvValidLifetime')
        self.assertEqual(tmp, 'infinity')

        # this is a default value
        tmp = get_config_value('AdvPreferredLifetime')
        self.assertEqual(tmp, '14400')

        tmp = get_config_value('AdvOnLink')
        self.assertEqual(tmp, 'off')

        tmp = get_config_value('DeprecatePrefix')
        self.assertEqual(tmp, 'off')

        tmp = get_config_value('DecrementLifetimes')
        self.assertEqual(tmp, 'off')

    def test_dns(self):
        nameserver = ['2001:db8::1', '2001:db8::2']
        dnssl = ['vyos.net', 'vyos.io']
        ns_lifetime = '599'

        self.cli_set(base_path + ['prefix', prefix, 'valid-lifetime', 'infinity'])
        self.cli_set(base_path + ['other-config-flag'])

        for ns in nameserver:
            self.cli_set(base_path + ['name-server', ns])
        for sl in dnssl:
            self.cli_set(base_path + ['dnssl', sl])

        self.cli_set(base_path + ['name-server-lifetime', ns_lifetime])
        # The value, if not 0, must be at least interval max (defaults to 600).
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        ns_lifetime = '600'
        self.cli_set(base_path + ['name-server-lifetime', ns_lifetime])

        # commit changes
        self.cli_commit()

        config = read_file(RADVD_CONF)

        tmp = 'RDNSS ' + ' '.join(nameserver) + ' {'
        self.assertIn(tmp, config)

        tmp = f'AdvRDNSSLifetime {ns_lifetime};'
        self.assertIn(tmp, config)

        tmp = 'DNSSL ' + ' '.join(dnssl) + ' {'
        self.assertIn(tmp, config)

    def test_deprecate_prefix(self):
        self.cli_set(base_path + ['prefix', prefix, 'valid-lifetime', 'infinity'])
        self.cli_set(base_path + ['prefix', prefix, 'deprecate-prefix'])
        self.cli_set(base_path + ['prefix', prefix, 'decrement-lifetime'])

        # commit changes
        self.cli_commit()

        tmp = get_config_value('DeprecatePrefix')
        self.assertEqual(tmp, 'on')

        tmp = get_config_value('DecrementLifetimes')
        self.assertEqual(tmp, 'on')

    def test_route(self):
        route = '2001:db8:1000::/64'

        self.cli_set(base_path + ['prefix', prefix])
        self.cli_set(base_path + ['route', route])

        # commit changes
        self.cli_commit()

        config = read_file(RADVD_CONF)

        tmp = f'route {route}' + ' {'
        self.assertIn(tmp, config)

        self.assertIn('AdvRouteLifetime 1800;', config)
        self.assertIn('AdvRoutePreference medium;', config)
        self.assertIn('RemoveRoute on;', config)

    def test_rasrcaddress(self):
        ra_src = ['fe80::1', 'fe80::2']

        self.cli_set(base_path + ['prefix', prefix])
        for src in ra_src:
            self.cli_set(base_path + ['source-address', src])

        # commit changes
        self.cli_commit()

        config = read_file(RADVD_CONF)
        self.assertIn('AdvRASrcAddress {', config)
        for src in ra_src:
            self.assertIn(f'        {src};', config)

    def test_nat64prefix(self):
        nat64prefix = '64:ff9b::/96'
        nat64prefix_invalid = '64:ff9b::/44'

        self.cli_set(base_path + ['nat64prefix', nat64prefix])

        # and another invalid prefix
        # Invalid NAT64 prefix length for "2001:db8::/34", can only be one of:
        # /32, /40, /48, /56, /64, /96
        self.cli_set(base_path + ['nat64prefix', nat64prefix_invalid])
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_delete(base_path + ['nat64prefix', nat64prefix_invalid])

        # NAT64 valid-lifetime must not be smaller then "interval max"
        self.cli_set(base_path + ['nat64prefix', nat64prefix, 'valid-lifetime', '500'])
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_delete(base_path + ['nat64prefix', nat64prefix, 'valid-lifetime'])

        # commit changes
        self.cli_commit()

        config = read_file(RADVD_CONF)

        tmp = f'nat64prefix {nat64prefix}' + ' {'
        self.assertIn(tmp, config)
        self.assertIn('AdvValidLifetime 65528;', config) # default

    def test_advsendadvert_advintervalopt(self):
        ra_src = ['fe80::1', 'fe80::2']

        self.cli_set(base_path + ['prefix', prefix])
        self.cli_set(base_path + ['no-send-advert'])
        # commit changes
        self.cli_commit()

        # Verify generated configuration
        config = read_file(RADVD_CONF)
        tmp = get_config_value('AdvSendAdvert')
        self.assertEqual(tmp, 'off')

        tmp = get_config_value('AdvIntervalOpt')
        self.assertEqual(tmp, 'on')

        self.cli_set(base_path + ['no-send-interval'])
        # commit changes
        self.cli_commit()

        # Verify generated configuration
        config = read_file(RADVD_CONF)
        tmp = get_config_value('AdvSendAdvert')
        self.assertEqual(tmp, 'off')

        tmp = get_config_value('AdvIntervalOpt')
        self.assertEqual(tmp, 'off')


if __name__ == '__main__':
    unittest.main(verbosity=2)