|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | +import subprocess |
| 5 | +import signal |
| 6 | +from os.path import dirname, abspath |
| 7 | +from time import time |
| 8 | + |
| 9 | +from core.messages import load_messages |
| 10 | + |
| 11 | +messages = load_messages().message_contents |
| 12 | + |
| 13 | + |
| 14 | +def run_container_in_sub_process(command, kill_container_command): |
| 15 | + is_network_traffic_capture_started = False |
| 16 | + parent_directory = str(dirname(dirname(abspath(__file__)))) |
| 17 | + output = str() |
| 18 | + expected_result = False |
| 19 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False, |
| 20 | + cwd=parent_directory) |
| 21 | + start_time = time() |
| 22 | + for c in iter(lambda: process.stdout.read(1), b""): |
| 23 | + if time() - start_time > 300: |
| 24 | + os.kill(process.pid, signal.SIGINT) |
| 25 | + break |
| 26 | + sys.stdout.buffer.write(c) |
| 27 | + output += c.decode("utf-8") |
| 28 | + if messages["network_traffic_capture_start"] in output and is_network_traffic_capture_started is False: |
| 29 | + is_network_traffic_capture_started = True |
| 30 | + os.system(kill_container_command) |
| 31 | + elif is_network_traffic_capture_started is True and "finished." in output: |
| 32 | + expected_result = True |
| 33 | + break |
| 34 | + assert True is expected_result |
| 35 | + |
| 36 | + |
| 37 | +class TestModules(unittest.TestCase): |
| 38 | + |
| 39 | + def test_module_ftp_weak_password(self): |
| 40 | + kill_container_command = "docker kill ohp_ftpserver_weak_password" |
| 41 | + command = ["python3", "ohp.py", "-m", "ftp/weak_password"] |
| 42 | + run_container_in_sub_process(command, kill_container_command) |
| 43 | + |
| 44 | + def test_module_ftp_strong_password(self): |
| 45 | + kill_container_command = "docker kill ohp_ftpserver_strong_password" |
| 46 | + command = ["python3", "ohp.py", "-m", "ftp/strong_password"] |
| 47 | + run_container_in_sub_process(command, kill_container_command) |
| 48 | + |
| 49 | + def test_module_http_basic_auth_strong_password(self): |
| 50 | + kill_container_command = "docker kill ohp_httpserver_basic_auth_strong_password" |
| 51 | + command = ["python3", "ohp.py", "-m", "http/basic_auth_strong_password"] |
| 52 | + run_container_in_sub_process(command, kill_container_command) |
| 53 | + |
| 54 | + def test_module_http_basic_auth_weak_password(self): |
| 55 | + kill_container_command = "docker kill ohp_httpserver_basic_auth_weak_password" |
| 56 | + command = ["python3", "ohp.py", "-m", "http/basic_auth_weak_password"] |
| 57 | + run_container_in_sub_process(command, kill_container_command) |
| 58 | + |
| 59 | + def test_module_http_ics_veeder_root_guardian_ast(self): |
| 60 | + kill_container_command = "docker kill ohp_icsserver_veeder_root_guardian_ast" |
| 61 | + command = ["python3", "ohp.py", "-m", "ics/veeder_root_guardian_ast"] |
| 62 | + run_container_in_sub_process(command, kill_container_command) |
| 63 | + |
| 64 | + def test_module_smtp_strong_password(self): |
| 65 | + kill_container_command = "docker kill ohp_smtpserver_strong_password" |
| 66 | + command = ["python3", "ohp.py", "-m", "smtp/strong_password"] |
| 67 | + run_container_in_sub_process(command, kill_container_command) |
| 68 | + |
| 69 | + def test_module_ssh_weak_password(self): |
| 70 | + kill_container_command = "docker kill ohp_sshserver_weak_password" |
| 71 | + command = ["python3", "ohp.py", "-m", "ssh/weak_password"] |
| 72 | + run_container_in_sub_process(command, kill_container_command) |
| 73 | + |
| 74 | + def test_module_ssh_strong_password(self): |
| 75 | + kill_container_command = "docker kill ohp_sshserver_strong_password" |
| 76 | + command = ["python3", "ohp.py", "-m", "ssh/strong_password"] |
| 77 | + run_container_in_sub_process(command, kill_container_command) |
0 commit comments