summaryrefslogtreecommitdiff
path: root/src/tests/test_utils.py
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-06-19 22:27:30 +0200
committerChristian Breunig <christian@breunig.cc>2026-06-20 14:12:23 +0200
commit2c7757048169be7db306efbedacd3ae9af4fbb64 (patch)
treed99ca0662167caa3e8e8c946ecb65f0bffcca867 /src/tests/test_utils.py
parent8f3e63d2ac2a29365733fc0e85e8f53e412cb5d1 (diff)
downloadvyos-1x-2c7757048169be7db306efbedacd3ae9af4fbb64.tar.gz
vyos-1x-2c7757048169be7db306efbedacd3ae9af4fbb64.zip
utils: T9003: add list-argument variant of cmd() for safer subprocess execution
A list-argument variant of cmd() for safer subprocess execution named cmdl(). Command must be a list of strings; no shell interpolation is performed, which eliminates a class of command-injection risks present when building commands with f-strings or other string formatting.
Diffstat (limited to 'src/tests/test_utils.py')
-rw-r--r--src/tests/test_utils.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/tests/test_utils.py b/src/tests/test_utils.py
index 5022b24f6..432672f06 100644
--- a/src/tests/test_utils.py
+++ b/src/tests/test_utils.py
@@ -16,6 +16,37 @@ from unittest import TestCase
from unittest.mock import patch
class TestVyOSUtils(TestCase):
+ def test_cmdl_basic(self):
+ from vyos.utils.process import cmdl
+ out = cmdl(['echo', 'hello'])
+ self.assertEqual(out, 'hello')
+
+ def test_cmdl_requires_list(self):
+ from vyos.utils.process import cmdl
+ with self.assertRaises(TypeError):
+ cmdl('echo hello')
+
+ def test_cmdl_sudo_prepends(self):
+ from vyos.utils.process import cmdl
+ with patch('vyos.utils.process.popen') as mock_popen:
+ mock_popen.return_value = ('', 0)
+ cmdl(['id'], sudo=True)
+ args, kwargs = mock_popen.call_args
+ self.assertEqual(args[0][0], 'sudo')
+ self.assertEqual(args[0][1], 'id')
+
+ def test_cmdl_no_shell(self):
+ from vyos.utils.process import cmdl
+ with patch('vyos.utils.process.popen') as mock_popen:
+ mock_popen.return_value = ('', 0)
+ cmdl(['true'])
+ _, kwargs = mock_popen.call_args
+ self.assertEqual(kwargs.get('shell'), False)
+
+ def test_cmdl_raises_on_nonzero(self):
+ from vyos.utils.process import cmdl
+ with self.assertRaises(OSError):
+ cmdl(['false'])
def test_key_mangling(self):
from vyos.utils.dict import mangle_dict_keys
data = {"foo-bar": {"baz-quux": None}}