|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2023 VyOS maintainers and contributors |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License version 2 or later as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import unittest |
| 20 | + |
| 21 | +from base_vyostest_shim import VyOSUnitTestSHIM |
| 22 | +from vyos.configsession import ConfigSessionError |
| 23 | +from vyos.utils.process import cmd |
| 24 | +from vyos.utils.dict import dict_search |
| 25 | + |
| 26 | +base_path = ['nat64'] |
| 27 | +src_path = base_path + ['source'] |
| 28 | + |
| 29 | +jool_nat64_config = '/run/jool/instance-100.json' |
| 30 | + |
| 31 | + |
| 32 | +class TestNAT64(VyOSUnitTestSHIM.TestCase): |
| 33 | + @classmethod |
| 34 | + def setUpClass(cls): |
| 35 | + super(TestNAT64, cls).setUpClass() |
| 36 | + |
| 37 | + # ensure we can also run this test on a live system - so lets clean |
| 38 | + # out the current configuration :) |
| 39 | + cls.cli_delete(cls, base_path) |
| 40 | + |
| 41 | + def tearDown(self): |
| 42 | + self.cli_delete(base_path) |
| 43 | + self.cli_commit() |
| 44 | + self.assertFalse(os.path.exists(jool_nat64_config)) |
| 45 | + |
| 46 | + def test_snat64(self): |
| 47 | + rule = '100' |
| 48 | + translation_rule = '10' |
| 49 | + prefix_v6 = '64:ff9b::/96' |
| 50 | + pool = '192.0.2.10' |
| 51 | + pool_port = '1-65535' |
| 52 | + |
| 53 | + self.cli_set(src_path + ['rule', rule, 'source', 'prefix', prefix_v6]) |
| 54 | + self.cli_set( |
| 55 | + src_path |
| 56 | + + ['rule', rule, 'translation', 'pool', translation_rule, 'address', pool] |
| 57 | + ) |
| 58 | + self.cli_set( |
| 59 | + src_path |
| 60 | + + ['rule', rule, 'translation', 'pool', translation_rule, 'port', pool_port] |
| 61 | + ) |
| 62 | + self.cli_commit() |
| 63 | + |
| 64 | + # Load the JSON file |
| 65 | + with open(f'/run/jool/instance-{rule}.json', 'r') as json_file: |
| 66 | + config_data = json.load(json_file) |
| 67 | + |
| 68 | + # Assertions based on the content of the JSON file |
| 69 | + self.assertEqual(config_data['instance'], f'instance-{rule}') |
| 70 | + self.assertEqual(config_data['framework'], 'netfilter') |
| 71 | + self.assertEqual(config_data['global']['pool6'], prefix_v6) |
| 72 | + self.assertTrue(config_data['global']['manually-enabled']) |
| 73 | + |
| 74 | + # Check the pool4 entries |
| 75 | + pool4_entries = config_data.get('pool4', []) |
| 76 | + self.assertIsInstance(pool4_entries, list) |
| 77 | + self.assertGreater(len(pool4_entries), 0) |
| 78 | + |
| 79 | + for entry in pool4_entries: |
| 80 | + self.assertIn('protocol', entry) |
| 81 | + self.assertIn('prefix', entry) |
| 82 | + self.assertIn('port range', entry) |
| 83 | + |
| 84 | + protocol = entry['protocol'] |
| 85 | + prefix = entry['prefix'] |
| 86 | + port_range = entry['port range'] |
| 87 | + |
| 88 | + if protocol == 'ICMP': |
| 89 | + self.assertEqual(prefix, pool) |
| 90 | + self.assertEqual(port_range, pool_port) |
| 91 | + elif protocol == 'UDP': |
| 92 | + self.assertEqual(prefix, pool) |
| 93 | + self.assertEqual(port_range, pool_port) |
| 94 | + elif protocol == 'TCP': |
| 95 | + self.assertEqual(prefix, pool) |
| 96 | + self.assertEqual(port_range, pool_port) |
| 97 | + else: |
| 98 | + self.fail(f'Unexpected protocol: {protocol}') |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main(verbosity=2) |
0 commit comments