Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dcff265

Browse files
committedSep 13, 2022
Adding test cases for graceful shutting of modules
1 parent 2600e1a commit dcff265

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed
 

‎.github/workflows/ci.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ jobs:
4242
runs-on: ubuntu-latest
4343
strategy:
4444
matrix:
45-
python-version: [3.9.4, 3.8.9]
45+
python-version: [ 3.9.4, 3.8.9 ]
4646

4747
steps:
48+
4849
- uses: actions/checkout@v2
4950
- name: Set up Python ${{ matrix.python-version }}
5051
uses: actions/setup-python@v2
@@ -71,11 +72,14 @@ jobs:
7172
run: docker logs python-honeypot_ohp_1
7273

7374
- name: Test with pytest
74-
run: docker exec python-honeypot_ohp_1 python3 -m pytest -rpP --reruns 5 --reruns-delay 3
75+
run: docker exec python-honeypot_ohp_1 python3 -m pytest -c tests/pytest.ini -rpP --reruns 5 --reruns-delay 3
7576

7677
- name: Check API server logs
7778
run: docker-compose logs ohp
7879

80+
- name: Run modules test (graceful shutting of modules)
81+
run: sudo python3 -m pytest tests/test_module_shutting.py tests/test_module_killing.py -s
82+
7983
- name: Run modules test
8084
run: sudo python3 ohp.py -m all --test --store-pcap
8185

‎tests/pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --ignore=tests/test_module_killing.py --ignore=tests/test_module_shutting.py

‎tests/test_module_killing.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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)

‎tests/test_module_shutting.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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):
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.kill(process.pid, signal.SIGINT)
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+
command = ["python3", "ohp.py", "-m", "ftp/weak_password"]
41+
run_container_in_sub_process(command)
42+
43+
def test_module_ftp_strong_password(self):
44+
command = ["python3", "ohp.py", "-m", "ftp/strong_password"]
45+
run_container_in_sub_process(command)
46+
47+
def test_module_http_basic_auth_strong_password(self):
48+
command = ["python3", "ohp.py", "-m", "http/basic_auth_strong_password"]
49+
run_container_in_sub_process(command)
50+
51+
def test_module_http_basic_auth_weak_password(self):
52+
command = ["python3", "ohp.py", "-m", "http/basic_auth_weak_password"]
53+
run_container_in_sub_process(command)
54+
55+
def test_module_http_ics_veeder_root_guardian_ast(self):
56+
command = ["python3", "ohp.py", "-m", "ics/veeder_root_guardian_ast"]
57+
run_container_in_sub_process(command)
58+
59+
def test_module_smtp_strong_password(self):
60+
command = ["python3", "ohp.py", "-m", "smtp/strong_password"]
61+
run_container_in_sub_process(command)
62+
63+
def test_module_ssh_weak_password(self):
64+
command = ["python3", "ohp.py", "-m", "ssh/weak_password"]
65+
run_container_in_sub_process(command)
66+
67+
def test_module_ssh_strong_password(self):
68+
command = ["python3", "ohp.py", "-m", "ssh/strong_password"]
69+
run_container_in_sub_process(command)

0 commit comments

Comments
 (0)
Please sign in to comment.