summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/helper.py3
-rw-r--r--src/tests/test_config_parser.py4
-rw-r--r--src/tests/test_configverify.py38
-rw-r--r--src/tests/test_dict_search.py (renamed from src/tests/test_vyos_dict_search.py)32
-rwxr-xr-xsrc/tests/test_find_device_file.py35
-rw-r--r--src/tests/test_jinja_filters.py69
-rw-r--r--src/tests/test_util.py6
-rw-r--r--src/tests/test_validate.py64
8 files changed, 224 insertions, 27 deletions
diff --git a/src/tests/helper.py b/src/tests/helper.py
index a7e4f201c..f7033148a 100644
--- a/src/tests/helper.py
+++ b/src/tests/helper.py
@@ -13,13 +13,10 @@
#
# 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 sys
import importlib.util
-
def prepare_module(file_path='', module_name=''):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
diff --git a/src/tests/test_config_parser.py b/src/tests/test_config_parser.py
index 5b922e2dd..6e0a071f8 100644
--- a/src/tests/test_config_parser.py
+++ b/src/tests/test_config_parser.py
@@ -15,11 +15,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
-import tempfile
-import unittest
+import vyos.configtree
from unittest import TestCase
-import vyos.configtree
class TestConfigParser(TestCase):
def setUp(self):
diff --git a/src/tests/test_configverify.py b/src/tests/test_configverify.py
new file mode 100644
index 000000000..ad7e053db
--- /dev/null
+++ b/src/tests/test_configverify.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 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/>.
+
+from unittest import TestCase
+from vyos.configverify import verify_diffie_hellman_length
+from vyos.util import cmd
+
+dh_file = '/tmp/dh.pem'
+
+class TestDictSearch(TestCase):
+ def setUp(self):
+ pass
+
+ def test_dh_key_none(self):
+ self.assertFalse(verify_diffie_hellman_length('/tmp/non_existing_file', '1024'))
+
+ def test_dh_key_256(self):
+ key_len = '256'
+ cmd(f'openssl dhparam -out {dh_file} {key_len}')
+ self.assertTrue(verify_diffie_hellman_length(dh_file, key_len))
+
+ def test_dh_key_512(self):
+ key_len = '512'
+ cmd(f'openssl dhparam -out {dh_file} {key_len}')
+ self.assertTrue(verify_diffie_hellman_length(dh_file, key_len))
diff --git a/src/tests/test_vyos_dict_search.py b/src/tests/test_dict_search.py
index ef338d46f..6a0fc74ad 100644
--- a/src/tests/test_vyos_dict_search.py
+++ b/src/tests/test_dict_search.py
@@ -14,10 +14,8 @@
# 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 unittest
from unittest import TestCase
-
-from vyos.util import vyos_dict_search
+from vyos.util import dict_search
data = {
'string': 'fooo',
@@ -31,29 +29,29 @@ class TestDictSearch(TestCase):
pass
def test_non_existing_keys(self):
- """ TestDictSearch: Return False when querying for non-existent key """
- self.assertFalse(vyos_dict_search('non_existing', data))
+ # TestDictSearch: Return False when querying for non-existent key
+ self.assertFalse(dict_search('non_existing', data))
def test_string(self):
- """ TestDictSearch: Return value when querying string """
- self.assertEqual(vyos_dict_search('string', data), data['string'])
+ # TestDictSearch: Return value when querying string
+ self.assertEqual(dict_search('string', data), data['string'])
def test_list(self):
- """ TestDictSearch: Return list items when querying list """
- self.assertEqual(vyos_dict_search('list', data), data['list'])
+ # TestDictSearch: Return list items when querying list
+ self.assertEqual(dict_search('list', data), data['list'])
def test_dict_key_value(self):
- """ TestDictSearch: Return dictionary keys value when value is present """
- self.assertEqual(vyos_dict_search('dict.key_2', data), data['dict']['key_2'])
+ # TestDictSearch: Return dictionary keys value when value is present
+ self.assertEqual(dict_search('dict.key_2', data), data['dict']['key_2'])
def test_nested_dict_key_value(self):
- """ TestDictSearch: Return string value of last key when querying for a nested string """
- self.assertEqual(vyos_dict_search('nested.string', data), data['nested']['string'])
+ # TestDictSearch: Return string value of last key when querying for a nested string
+ self.assertEqual(dict_search('nested.string', data), data['nested']['string'])
def test_nested_dict_key_empty(self):
- """ TestDictSearch: Return False when querying for a nested string whose last key is empty """
- self.assertFalse(vyos_dict_search('nested.empty', data))
+ # TestDictSearch: Return False when querying for a nested string whose last key is empty
+ self.assertFalse(dict_search('nested.empty', data))
def test_nested_list(self):
- """ TestDictSearch: Return list items when querying nested list """
- self.assertEqual(vyos_dict_search('nested.list', data), data['nested']['list'])
+ # TestDictSearch: Return list items when querying nested list
+ self.assertEqual(dict_search('nested.list', data), data['nested']['list'])
diff --git a/src/tests/test_find_device_file.py b/src/tests/test_find_device_file.py
new file mode 100755
index 000000000..43c80dc76
--- /dev/null
+++ b/src/tests/test_find_device_file.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 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/>.
+
+from unittest import TestCase
+from vyos.util import find_device_file
+
+class TestDeviceFile(TestCase):
+ """ used to find USB devices on target """
+ def setUp(self):
+ pass
+
+ def test_null(self):
+ self.assertEqual(find_device_file('null'), '/dev/null')
+
+ def test_zero(self):
+ self.assertEqual(find_device_file('zero'), '/dev/zero')
+
+ def test_input_event(self):
+ self.assertEqual(find_device_file('event0'), '/dev/input/event0')
+
+ def test_non_existing(self):
+ self.assertFalse(find_device_file('vyos'))
diff --git a/src/tests/test_jinja_filters.py b/src/tests/test_jinja_filters.py
new file mode 100644
index 000000000..acd7a5952
--- /dev/null
+++ b/src/tests/test_jinja_filters.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 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/>.
+
+from unittest import TestCase
+
+from ipaddress import ip_network
+from vyos.template import vyos_address_from_cidr
+from vyos.template import vyos_netmask_from_cidr
+from vyos.template import vyos_ipv4
+from vyos.template import vyos_ipv6
+from vyos.template import vyos_first_host_address
+from vyos.template import vyos_last_host_address
+from vyos.template import vyos_inc_ip
+
+class TestTeamplteHelpers(TestCase):
+ def setUp(self):
+ pass
+
+ def test_helpers_from_cidr(self):
+ network_v4 = '192.0.2.0/26'
+ self.assertEqual(vyos_address_from_cidr(network_v4), str(ip_network(network_v4).network_address))
+ self.assertEqual(vyos_netmask_from_cidr(network_v4), str(ip_network(network_v4).netmask))
+
+ def test_helpers_ipv4(self):
+ self.assertTrue(vyos_ipv4('192.0.2.1'))
+ self.assertTrue(vyos_ipv4('192.0.2.0/24'))
+ self.assertTrue(vyos_ipv4('192.0.2.1/32'))
+ self.assertTrue(vyos_ipv4('10.255.1.2'))
+ self.assertTrue(vyos_ipv4('10.255.1.0/24'))
+ self.assertTrue(vyos_ipv4('10.255.1.2/32'))
+ self.assertFalse(vyos_ipv4('2001:db8::'))
+ self.assertFalse(vyos_ipv4('2001:db8::1'))
+ self.assertFalse(vyos_ipv4('2001:db8::/64'))
+
+ def test_helpers_ipv6(self):
+ self.assertFalse(vyos_ipv6('192.0.2.1'))
+ self.assertFalse(vyos_ipv6('192.0.2.0/24'))
+ self.assertFalse(vyos_ipv6('192.0.2.1/32'))
+ self.assertFalse(vyos_ipv6('10.255.1.2'))
+ self.assertFalse(vyos_ipv6('10.255.1.0/24'))
+ self.assertFalse(vyos_ipv6('10.255.1.2/32'))
+ self.assertTrue(vyos_ipv6('2001:db8::'))
+ self.assertTrue(vyos_ipv6('2001:db8::1'))
+ self.assertTrue(vyos_ipv6('2001:db8::1/64'))
+ self.assertTrue(vyos_ipv6('2001:db8::/32'))
+ self.assertTrue(vyos_ipv6('2001:db8::/64'))
+
+ def test_helpers_first_host_address(self):
+ self.assertEqual(vyos_first_host_address('10.0.0.0/24'), '10.0.0.1')
+ self.assertEqual(vyos_first_host_address('10.0.0.128/25'), '10.0.0.129')
+ self.assertEqual(vyos_first_host_address('10.0.0.200/29'), '10.0.0.201')
+
+ self.assertEqual(vyos_first_host_address('2001:db8::/64'), '2001:db8::')
+ self.assertEqual(vyos_first_host_address('2001:db8::/112'), '2001:db8::')
+ self.assertEqual(vyos_first_host_address('2001:db8::10/112'), '2001:db8::10')
+ self.assertEqual(vyos_first_host_address('2001:db8::100/112'), '2001:db8::100')
diff --git a/src/tests/test_util.py b/src/tests/test_util.py
index 09bf947b8..f7405cbde 100644
--- a/src/tests/test_util.py
+++ b/src/tests/test_util.py
@@ -14,10 +14,8 @@
# 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 unittest
from unittest import TestCase
-
-import vyos.util
+from vyos.util import mangle_dict_keys
class TestVyOSUtil(TestCase):
@@ -27,6 +25,6 @@ class TestVyOSUtil(TestCase):
def test_key_mangline(self):
data = {"foo-bar": {"baz-quux": None}}
expected_data = {"foo_bar": {"baz_quux": None}}
- new_data = vyos.util.mangle_dict_keys(data, '-', '_')
+ new_data = mangle_dict_keys(data, '-', '_')
self.assertEqual(new_data, expected_data)
diff --git a/src/tests/test_validate.py b/src/tests/test_validate.py
new file mode 100644
index 000000000..e9fe185ed
--- /dev/null
+++ b/src/tests/test_validate.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 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 vyos.validate
+from unittest import TestCase
+
+class TestVyOSValidate(TestCase):
+ def setUp(self):
+ pass
+
+ def test_is_ip(self):
+ self.assertTrue(vyos.validate.is_ip('192.0.2.1'))
+ self.assertTrue(vyos.validate.is_ip('2001:db8::1'))
+ self.assertFalse(vyos.validate.is_ip('VyOS'))
+
+ def test_is_ipv4(self):
+ self.assertTrue(vyos.validate.is_ipv4('192.0.2.1'))
+ self.assertTrue(vyos.validate.is_ipv4('192.0.2.0/24'))
+ self.assertTrue(vyos.validate.is_ipv4('192.0.2.1/32'))
+
+ self.assertFalse(vyos.validate.is_ipv4('2001:db8::1'))
+ self.assertFalse(vyos.validate.is_ipv4('2001:db8::/64'))
+ self.assertFalse(vyos.validate.is_ipv4('VyOS'))
+
+ def test_is_ipv6(self):
+ self.assertFalse(vyos.validate.is_ipv6('192.0.2.1'))
+ self.assertFalse(vyos.validate.is_ipv6('192.0.2.0/24'))
+ self.assertFalse(vyos.validate.is_ipv6('192.0.2.1/32'))
+ self.assertTrue(vyos.validate.is_ipv6('2001:db8::1'))
+ self.assertTrue(vyos.validate.is_ipv6('2001:db8::/64'))
+ self.assertTrue(vyos.validate.is_ipv6('2001:db8::1/64'))
+ self.assertFalse(vyos.validate.is_ipv6('VyOS'))
+
+ def test_is_ipv6_link_local(self):
+ self.assertFalse(vyos.validate.is_ipv6_link_local('169.254.0.1'))
+ self.assertTrue(vyos.validate.is_ipv6_link_local('fe80::'))
+ self.assertTrue(vyos.validate.is_ipv6_link_local('fe80::affe:1'))
+ self.assertFalse(vyos.validate.is_ipv6_link_local('2001:db8::'))
+ self.assertFalse(vyos.validate.is_ipv6_link_local('VyOS'))
+
+ def test_is_ipv6_link_local(self):
+ self.assertTrue(vyos.validate.is_loopback_addr('127.0.0.1'))
+ self.assertTrue(vyos.validate.is_loopback_addr('127.0.1.1'))
+ self.assertTrue(vyos.validate.is_loopback_addr('127.1.1.1'))
+ self.assertTrue(vyos.validate.is_loopback_addr('::1'))
+
+ self.assertFalse(vyos.validate.is_loopback_addr('::2'))
+ self.assertFalse(vyos.validate.is_loopback_addr('192.0.2.1'))
+
+
+