|
| 1 | +from unittest.mock import patch, MagicMock |
| 2 | +import csle_common.constants.constants as constants |
| 3 | +from csle_common.dao.emulation_config.vulnerability_type import VulnType |
| 4 | +from csle_common.controllers.vulnerabilities_controller import VulnerabilitiesController |
| 5 | + |
| 6 | + |
| 7 | +class TestVulnControllerSuite: |
| 8 | + """ |
| 9 | + Test VulnerabilitiesController |
| 10 | + """ |
| 11 | + |
| 12 | + @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") |
| 13 | + @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") |
| 14 | + def test_create_vulns(self, mock_execute_ssh_cmd, mock_connect_admin) -> None: |
| 15 | + """ |
| 16 | + Test method that creates vulnerabilities in an emulation environment |
| 17 | +
|
| 18 | + :param mock_execute_ssh_cmd: mock_execute_ssh_cmd |
| 19 | + :param mock_connect_admin: mock_connect_admin |
| 20 | +
|
| 21 | + :return: None |
| 22 | + """ |
| 23 | + logger = MagicMock() |
| 24 | + credential = MagicMock(username="testuser") |
| 25 | + vuln = MagicMock( |
| 26 | + physical_host_ip="192.168.1.1", |
| 27 | + docker_gw_bridge_ip="192.168.1.2", |
| 28 | + vuln_type=VulnType.PRIVILEGE_ESCALATION, |
| 29 | + cve=constants.EXPLOIT_VULNERABILITES.CVE_2010_0426, |
| 30 | + credentials=[credential], |
| 31 | + ) |
| 32 | + emulation_env_config = MagicMock() |
| 33 | + emulation_env_config.vuln_config.node_vulnerability_configs = [vuln] |
| 34 | + emulation_env_config.connections = {"192.168.1.2": MagicMock()} |
| 35 | + |
| 36 | + physical_server_ip = "192.168.1.1" |
| 37 | + |
| 38 | + mock_execute_ssh_cmd.side_effect = [ |
| 39 | + ("", "", 0), # output of cp /etc/sudoers.bak /etc/sudoers |
| 40 | + ("", "", 0), # output of adding CVE entry to sudoers |
| 41 | + ("", "", 0), # output of chmod 440 /etc/sudoers |
| 42 | + ] |
| 43 | + |
| 44 | + VulnerabilitiesController.create_vulns( |
| 45 | + emulation_env_config, physical_server_ip, logger |
| 46 | + ) |
| 47 | + mock_connect_admin.assert_any_call( |
| 48 | + emulation_env_config=emulation_env_config, ip="192.168.1.2" |
| 49 | + ) |
| 50 | + mock_execute_ssh_cmd.assert_called() |
0 commit comments