summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_service_stunnel.py
blob: 3aeffd09ef5547de8b0b59d3d38f29ec474cddf6 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env python3
#
# Copyright (C) 2024 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 base_vyostest_shim import VyOSUnitTestSHIM

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


PROCESS_NAME = 'stunnel'
STUNNEL_CONF = '/run/stunnel/stunnel.conf'
base_path = ['service', 'stunnel']

ca_certificate = """
MIIDnTCCAoWgAwIBAgIUcSMo/zT/GUAyH3uM3Hr3qjCDmMUwDQYJKoZIhvcNAQELBQAwVzELMAkGA1U
EBhMCR0IxEzARBgNVBAgMClNvbWUtU3RhdGUxEjAQBgNVBAcMCVNvbWUtQ2l0eTENMAsGA1UECgwEVn
lPUzEQMA4GA1UEAwwHdnlvcy5pbzAeFw0yNDA2MDQwNjU1MDFaFw0yOTA2MDMwNjU1MDFaMFcxCzAJB
gNVBAYTAkdCMRMwEQYDVQQIDApTb21lLVN0YXRlMRIwEAYDVQQHDAlTb21lLUNpdHkxDTALBgNVBAoM
BFZ5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VH9jGFIdnB4ILYMxuu6bBokzvewKBgBWbjPppjrM46bZ0rwEYCcG0F/k6TKkw4pjyrDR4B0XsrqTjK
0yz0ga7FHe10saeS2cXMqygdkjhWLZ6Zhrp0LAEzhEvdiBYeRH37J9Bvwo2YIHakox4hJCSXNnELs/A
GhUb5YIISNnZnZZeUD/Z0IJXJryjk9eUbhDCgEZRVzeT
"""


def get_config_value(key):
    tmp = read_file(STUNNEL_CONF)
    tmp = re.findall(f'\n?{key}\s+(.*)', tmp)
    return tmp


def read_config():
    config = {'global': {},
              'services': {}
              }
    service_pattern = re.compile(r'\[(.+?)\]')
    key_value_pattern = re.compile(r'(\S+)\s*=\s*(.+)')
    service = None

    for line in read_file(STUNNEL_CONF).split('\n'):
        line.strip()
        if not line or line.startswith(';'):
            continue
        if service_pattern.match(line):
            service = line.strip('[]')
            config['services'][service] = {}
        key_value_match = key_value_pattern.match(line)
        if key_value_match:
            key, value = key_value_match.group(1), key_value_match.group(2)
            if service:
                apply_value(config['services'][service], key, value)
            else:
                apply_value(config['global'], key, value)

    return config


def apply_value(service_config, key, value):
    if service_config.get(key) is None:
        service_config[key] = value
    else:
        if not isinstance(service_config[key], list):
            service_config[key] = [
                service_config[key]]
        else:
            service_config[key].append(value)


class TestServiceStunnel(VyOSUnitTestSHIM.TestCase):
    maxDiff = None
    @classmethod
    def setUpClass(cls):
        super(TestServiceStunnel, cls).setUpClass()

        # ensure we can also run this test on a live system - so lets clean
        # out the current configuration :)
        cls.cli_delete(cls, base_path)
        cls.is_valid_conf = True

    def tearDown(self):
        # Check for running process
        if self.is_valid_conf:
            self.assertTrue(process_named_running(PROCESS_NAME))
        self.is_valid_conf = True
        # delete testing Stunnel config
        self.cli_delete(base_path)
        self.cli_delete(['pki'])
        self.cli_commit()

        # Check for stopped process
        self.assertFalse(process_named_running(PROCESS_NAME))

    def set_pki(self):
        self.cli_set(['pki', 'ca', 'ca-1', 'certificate', ca_certificate.replace('\n','')])
        self.cli_set(['pki', 'ca', 'ca-1', 'private', 'key', ca_private_key.replace('\n','')])
        self.cli_set(['pki', 'certificate', 'srv-1', 'certificate', srv_certificate.replace('\n','')])
        self.cli_set(['pki', 'certificate', 'srv-1', 'private', 'key', srv_private_key.replace('\n','')])
        self.cli_commit()

    def test_01_stunnel_simple_client(self):
        service = 'app1'
        self.cli_set(base_path + ['client', service, 'connect', 'port', '9001'])

        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_set(base_path + ['client', service, 'listen', 'port', '8001'])

        self.cli_commit()
        config = read_config()

        self.assertEqual('/run/stunnel/stunnel.pid', config['global']['pid'])
        self.assertEqual('notice', config['global']['debug'])
        self.assertListEqual([service], list(config['services']))
        self.assertEqual('8001', config['services'][service]['accept'])
        self.assertEqual('9001', config['services'][service]['connect'])
        self.assertEqual('yes', config['services'][service]['client'])

    def test_02_stunnel_simple_server(self):
        service = 'ser1'
        self.set_pki()
        self.cli_set(base_path + ['server', service, 'connect', 'port', '8080'])
        self.cli_set(base_path + ['server', service, 'ssl', 'certificate', 'srv-1'])

        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_set(base_path + ['server', service, 'listen', 'port', '9001'])

        self.cli_commit()
        config = read_config()

        self.assertEqual('/run/stunnel/stunnel.pid', config['global']['pid'])
        self.assertEqual('notice', config['global']['debug'])
        self.assertListEqual([service], list(config['services']))
        self.assertEqual('9001', config['services'][service]['accept'])
        self.assertEqual('8080', config['services'][service]['connect'])
        self.assertIsNone(config['services'][service].get('client'))
        self.assertEqual('/run/stunnel/server-ser1-srv-1.pem', config['services'][service]['cert'])
        self.assertEqual('/run/stunnel/server-ser1-srv-1.pem.key', config['services'][service]['key'])

    def test_03_multy_services(self):
        self.set_pki()
        clients = ['app1', 'app2', 'app3']
        servers = ['serv1', 'serv2', 'serv3']
        port = 80
        for service in clients:
            port += 1
            self.cli_set(base_path + ['client', service, 'listen', 'port', f'{port}'])
            port += 1
            self.cli_set(base_path + ['client', service, 'connect', 'port', f'{port}'])
            if service == 'app2':
                self.cli_set(base_path + ['client', service, 'connect', 'address', f'192.168.0.10'])
                self.cli_set(base_path + ['client', service, 'listen', 'address', '127.0.0.1'])
                self.cli_set(base_path + ['client', service, 'protocol', 'connect'])
                self.cli_set(base_path + ['client', service, 'options', 'authentication', 'basic'])
                self.cli_set(base_path + ['client', service, 'options', 'domain', 'basic.com'])
                self.cli_set(base_path + ['client', service, 'options', 'host', 'address', '127.0.0.1'])
                self.cli_set(base_path + ['client', service, 'options', 'host', 'port', '5000'])
                self.cli_set(base_path + ['client', service, 'options', 'password', 'test_pass'])
                self.cli_set(base_path + ['client', service, 'options', 'username', 'test'])
            if service == 'app3':
                self.cli_set(base_path + ['client', service, 'ssl', 'ca-certificate', 'ca-1'])
                self.cli_set(base_path + ['client', service, 'ssl', 'certificate', 'srv-1'])

        for service in servers:
            port += 1
            self.cli_set(base_path + ['server', service, 'listen', 'port', f'{port}'])
            port += 1
            self.cli_set(base_path + ['server', service, 'connect', 'port', f'{port}'])
            self.cli_set(base_path + ['server', service, 'ssl', 'certificate', 'srv-1'])
            if service == 'serv2':
                self.cli_set(base_path + ['server', service, 'ssl', 'ca-certificate', 'ca-1'])
                self.cli_set(base_path + ['server', service, 'connect', 'address', f'google.com'])
                self.cli_set(base_path + ['server', service, 'listen', 'address', f'127.0.0.1'])
            if service == 'serv3':
                self.cli_set(base_path + ['server', service, 'connect', 'address', f'10.18.105.10'])
                self.cli_set(base_path + ['server', service, 'protocol', 'imap'])

        self.cli_commit()
        config = read_config()

        self.assertEqual('/run/stunnel/stunnel.pid', config['global']['pid'])
        self.assertListEqual(clients + servers, list(config['services']))
        self.assertDictEqual(config['services'], {
            'app1': {
                'client': 'yes',
                'accept': '81',
                'connect': '82'
            },
            'app2': {
                'client': 'yes',
                'accept': '127.0.0.1:83',
                'connect': '192.168.0.10:84',
                'protocol': 'connect',
                'protocolAuthentication': 'basic',
                'protocolDomain': 'basic.com',
                'protocolHost': '127.0.0.1:5000',
                'protocolPassword': 'test_pass',
                'protocolUsername': 'test'
            },
            'app3': {
                'client': 'yes',
                'accept': '85',
                'connect': '86',
                'CApath': '/run/stunnel/ca',
                'CAfile': 'client-app3-ca.pem',
                'cert': '/run/stunnel/client-app3-srv-1.pem',
                'key': '/run/stunnel/client-app3-srv-1.pem.key'
            },
            'serv1': {
                'accept': '87',
                'connect': '88',
                'cert': '/run/stunnel/server-serv1-srv-1.pem',
                'key': '/run/stunnel/server-serv1-srv-1.pem.key'
            },
            'serv2': {
                'accept': '127.0.0.1:89',
                'connect': 'google.com:90',
                'CApath': '/run/stunnel/ca',
                'CAfile': 'server-serv2-ca.pem',
                'cert': '/run/stunnel/server-serv2-srv-1.pem',
                'key': '/run/stunnel/server-serv2-srv-1.pem.key'
            },
            'serv3': {
                'accept': '91',
                'connect': '10.18.105.10:92',
                'protocol': 'imap',
                'cert': '/run/stunnel/server-serv3-srv-1.pem',
                'key': '/run/stunnel/server-serv3-srv-1.pem.key'
            }
        })

    def test_04_cert_problems(self):
        service = 'app1'
        self.cli_set(base_path + ['client', service, 'connect', 'port', '9001'])
        self.cli_set(base_path + ['client', service, 'listen', 'port', '8001'])
        self.cli_set(base_path + ['client', service, 'ssl', 'ca-certificate', 'ca-2'])

        # ca not exist in pki
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_delete(base_path + ['client', service, 'ssl', 'ca-certificate', 'ca-2'])
        self.cli_set(base_path + ['client', service, 'ssl', 'certificate', 'srv-2'])

        # cert not exist in pki
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_delete(base_path)

        self.cli_set(base_path + ['server', service, 'connect', 'port', '8080'])
        self.cli_set(base_path + ['server', service, 'listen', 'port', '9001'])

        # Create server without any cert
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_set(base_path + ['server', service, 'ssl', 'ca-certificate', 'ca-2'])
        # ca not exist in pki
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_delete(base_path + ['server', service, 'ssl', 'ca-certificate', 'ca-2'])
        self.cli_set(base_path + ['server', service, 'ssl', 'certificate', 'srv-2'])
        # cert not exist in pki
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.is_valid_conf = False

    def test_05_psk_auth(self):
        modes = ['client', 'server']
        psk_id_1 = 'psk_id_1'
        psk_secret_1 = '1234567890ABCDEF1234567890ABCDEF'
        psk_id_2 = 'psk_id_2'
        psk_secret_2 = '1234567890ABCDEF1234567890ABCDEA'
        expected_config = {
            'global': {'pid': '/run/stunnel/stunnel.pid',
                       'debug': 'notice'},
            'services': {}}
        port = 8000
        for mode in modes:
            service = f'{mode}-one'
            psk_secrets = f'/run/stunnel/psk/{mode}_{service}.txt'
            expected_config['services'][service] = {
                'PSKsecrets': psk_secrets,
            }
            port += 1
            expected_config['services'][service]['accept'] = f'{port}'
            self.cli_set(base_path + [mode, service, 'listen', 'port', f'{port}'])
            port += 1
            expected_config['services'][service]['connect'] = f'{port}'
            self.cli_set(base_path + [mode, service, 'connect', 'port', f'{port}'])
            self.cli_set(base_path + [mode, service, 'psk', 'smoketest1', 'id', psk_id_1])
            with self.assertRaises(ConfigSessionError):
                self.cli_set(base_path + [mode, service, 'psk', 'smoketest1', 'secret', '123'])
            with self.assertRaises(ConfigSessionError):
                self.cli_set(base_path + [mode, service, 'psk', 'smoketest1', 'secret', '1234567890ABCDEF1234567890ABCDEZ'])
            self.cli_set(base_path + [mode, service, 'psk', 'smoketest1', 'secret', psk_secret_1])
            self.cli_set(base_path + [mode, service, 'psk', 'smoketest2', 'id', psk_id_2])
            self.cli_set(base_path + [mode, service, 'psk', 'smoketest2', 'secret', psk_secret_2])
            if mode != 'server':
                expected_config['services'][service]['client'] = 'yes'

            self.cli_commit()
            config = read_config()

            self.assertDictEqual(expected_config, config)

            self.assertListEqual([f'{psk_id_1}:{psk_secret_1}',
                                  f'{psk_id_2}:{psk_secret_2}'],
                [line for line in read_file(psk_secrets).split('\n')])

    def test_06_socks_proxy(self):
        server_port = '9001'
        client_port = '9000'
        srv_name = 'srv-one'
        cli_name = 'cli-one'
        expected_config = {
            'global': {'pid': '/run/stunnel/stunnel.pid',
                       'debug': 'notice'},
            'services': {
                'cli-one': {
                    'PSKsecrets': f'/run/stunnel/psk/client_{cli_name}.txt',
                    'client': 'yes',
                    'accept': client_port,
                    'connect': server_port
                },
                'srv-one': {
                    'PSKsecrets': f'/run/stunnel/psk/server_{srv_name}.txt',
                    'accept': server_port,
                    'protocol': 'socks'
                }
            }}

        self.cli_set(base_path + ['server', srv_name, 'listen', 'port', server_port])
        self.cli_set(base_path + ['server', srv_name, 'connect', 'port', '9005'])
        self.cli_set(base_path + ['server', srv_name, 'protocol', 'socks'])
        self.cli_set(base_path + ['server', srv_name, 'psk', 'sock_proxy', 'id', cli_name])
        self.cli_set(base_path + ['server', srv_name, 'psk', 'sock_proxy', 'secret', '1234567890ABCDEF1234567890ABCDEF'])

        self.cli_set(base_path + ['client', cli_name, 'listen', 'port', client_port])
        self.cli_set(base_path + ['client', cli_name, 'connect', 'port', server_port])
        self.cli_set(base_path + ['client', cli_name, 'psk', 'sock_proxy', 'id', cli_name])
        self.cli_set(base_path + ['client', cli_name, 'psk', 'sock_proxy', 'secret', '1234567890ABCDEF1234567890ABCDEF'])

        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_delete(base_path + ['server', srv_name, 'connect'])
        self.cli_commit()
        config = read_config()

        self.assertDictEqual(expected_config, config)

    def test_07_available_port(self):
        expected_config = {
            'global': {'pid': '/run/stunnel/stunnel.pid',
                       'debug': 'notice'},
            'services': {
                'app1': {
                    'client': 'yes',
                    'accept': '8001',
                    'connect': '9001'
                },
                'srv1': {
                    'PSKsecrets': f'/run/stunnel/psk/server_srv1.txt',
                    'accept': '127.0.0.1:8002',
                    'connect': '9001'
                }
            }}
        self.cli_set(base_path + ['client', 'app1', 'connect', 'port', '9001'])
        self.cli_set(base_path + ['client', 'app1', 'listen', 'port', '8001'])

        self.cli_set(base_path + ['server', 'srv1', 'connect', 'port', '9001'])
        self.cli_set(base_path + ['server', 'srv1', 'listen', 'address',
                                  '127.0.0.1'])
        self.cli_set(base_path + ['server', 'srv1', 'listen', 'port', '8001'])
        self.cli_set(base_path + ['server', 'srv1', 'psk', 'smoketest1',
                                  'id', 'foo'])
        self.cli_set(base_path + ['server', 'srv1', 'psk', 'smoketest1',
                                  'secret', '1234567890ABCDEF1234567890ABCDEF'])

        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_set(base_path + ['server', 'srv1', 'listen', 'port', '8002'])
        self.cli_commit()

        config = read_config()
        self.assertDictEqual(expected_config, config)

    def test_08_two_endpoints(self):
        expected_config = {
            'global': {'pid': '/run/stunnel/stunnel.pid',
                       'debug': 'notice'},
            'services': {
                'app1': {
                    'client': 'yes',
                    'accept': '8001',
                    'connect': '9001'
                }
            }}

        self.cli_set(base_path + ['client', 'app1', 'listen', 'port', '8001'])
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()
        self.cli_set(base_path + ['client', 'app1', 'connect', 'port', '9001'])
        self.cli_commit()

        config = read_config()
        self.assertDictEqual(expected_config, config)

    def test_09_pki_still_used(self):
        service = 'ser1'
        self.set_pki()
        self.cli_set(base_path + ['server', service, 'connect', 'port', '8080'])
        self.cli_set(base_path + ['server', service, 'listen', 'port', '9001'])
        self.cli_set(base_path + ['server', service, 'ssl', 'certificate', 'srv-1'])
        self.cli_commit()

        self.cli_delete(['pki'])
        with self.assertRaises(ConfigSessionError):
            self.cli_commit()

        self.cli_delete(base_path)
        self.cli_commit()

        self.is_valid_conf = False

    def test_99_protocols(self):
        self.set_pki()
        service = 'one'
        proto_address = 'google.com'
        proto_port = '80'
        modes = ['client', 'server']
        protocols = ['cifs', 'connect', 'imap', 'nntp', 'pgsql', 'pop3',
                     'proxy', 'smtp', 'socks']
        options = ['authentication', 'domain', 'host', 'password', 'username']

        for protocol in protocols:
            for mode in modes:
                expected_config = {
                    'global': {'pid': '/run/stunnel/stunnel.pid',
                               'debug': 'notice'},
                    'services': {'one': {
                        'accept': '8001',
                        'protocol': protocol,
                    }}}
                if not(mode == 'server' and protocol == 'socks'):
                    self.cli_set(base_path + [mode, service, 'connect', 'port', '9001'])
                    expected_config['services']['one']['connect'] = '9001'
                self.cli_set(base_path + [mode, service, 'listen', 'port', '8001'])

                if mode == 'server':
                    expected_config['services'][service]['cert'] = '/run/stunnel/server-one-srv-1.pem'
                    expected_config['services'][service]['key'] = '/run/stunnel/server-one-srv-1.pem.key'
                    self.cli_set(base_path + [mode, service, 'ssl',
                                              'certificate', 'srv-1'])
                else:
                    expected_config['services'][service]['client'] = 'yes'

                # protocols connect and nntp is only supported in client mode.
                if mode == 'server' and protocol in ['connect', 'nntp']:
                    with self.assertRaises(ConfigSessionError):
                        self.cli_set(base_path + [mode, service, 'protocol', protocol])
                        # self.cli_commit()
                else:
                    self.cli_set(base_path + [mode, service, 'protocol', protocol])
                    self.cli_commit()
                    config = read_config()

                    self.assertDictEqual(expected_config, config)

                expected_config['services'][service]['protocolDomain'] = 'valdomain'
                expected_config['services'][service]['protocolPassword'] = 'valpassword'
                expected_config['services'][service]['protocolUsername'] = 'valusername'

                for option in options:
                    if option == 'authentication':
                        expected_config['services'][service]['protocolAuthentication'] = \
                            'basic' if protocol == 'connect' else 'plain'
                        continue

                    if option == 'host' and mode != 'server':
                        expected_config['services'][service]['protocolHost'] = \
                            f'{proto_address}:{proto_port}'
                        self.cli_set(base_path + [mode, service, 'options',
                                         option, 'address', f'{proto_address}'])
                        self.cli_set(base_path + [mode, service, 'options',
                                                  option, 'port', f'{proto_port}'])
                        continue
                    if mode == 'server':
                        with self.assertRaises(ConfigSessionError):
                            self.cli_set(
                                base_path + [mode, service, 'options', option, f'val{option}'])
                    else:
                        self.cli_set(
                            base_path + [mode, service, 'options', option, f'val{option}'])
                # Additional option is only supported in the 'connect' and 'smtp' protocols.
                if mode != 'server':
                    if protocol not in ['connect', 'smtp']:
                        with self.assertRaises(ConfigSessionError):
                            self.cli_commit()
                    else:
                        if protocol == 'smtp':
                            # Protocol smtp does not support options domain and host
                            with self.assertRaises(ConfigSessionError):
                                self.cli_commit()

                            self.cli_delete(
                                base_path + [mode, service, 'options', 'domain'])
                            self.cli_delete(
                                base_path + [mode, service, 'options', 'host'])
                            del expected_config['services'][service]['protocolDomain']
                            del expected_config['services'][service]['protocolHost']

                        self.cli_commit()
                        config = read_config()

                        self.assertDictEqual(expected_config, config)

                self.cli_delete(base_path)
                self.cli_commit()

        self.is_valid_conf = False


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