From 31e87c02b53a4ae79f65e25fcf1994375a708127 Mon Sep 17 00:00:00 2001 From: Dylane Bengono Date: Thu, 12 Sep 2024 15:58:09 +0100 Subject: [PATCH] Refactor Snort test for macOS --- scripts/tests/test.py | 64 ++++++++++++++++++++++++++++++ scripts/tests/test_linux.py | 78 ------------------------------------- scripts/tests/test_macos.py | 36 ----------------- 3 files changed, 64 insertions(+), 114 deletions(-) create mode 100644 scripts/tests/test.py delete mode 100644 scripts/tests/test_linux.py delete mode 100644 scripts/tests/test_macos.py diff --git a/scripts/tests/test.py b/scripts/tests/test.py new file mode 100644 index 0000000..7b76899 --- /dev/null +++ b/scripts/tests/test.py @@ -0,0 +1,64 @@ +import pytest +import testinfra + + + +@pytest.fixture(scope="module") +def install_dependencies(host): + """Install dependencies and run the install script.""" + os = host.system_info.distribution + + if os in ["ubuntu", "debian"]: + host.run("apt-get update") + host.run("apt-get install -y curl gnupg2 iproute2") + + elif os == "alpine": + host.run("apk update") + host.run("apk add curl gnupg2 iproute2") + + else: + pytest.fail("Unsupported OS for dependency installation") + + +@pytest.mark.usefixtures("install_dependencies") +def test_snort_is_installed(host): + """Test if Snort is installed.""" + snort = host.package("snort") + assert snort.is_installed, "Snort should be installed" + + +def test_snort_conf_file_exists(host): + """Test if snort.conf file exists.""" + snort_conf = host.file("/etc/snort/snort.conf") + assert snort_conf.exists, "snort.conf file should exist" + + +def test_snort_interface_configuration(host): + # Retrieve the default network interface + interface = host.run("ip route | grep default | awk '{print $5}'").stdout.strip() + + # Check if the interface is present in the snort.debian.conf file + snort_conf = host.file("/etc/snort/snort.debian.conf") + assert interface in snort_conf.content_string, "Interface should be present in snort.debian.conf" + + + +def test_update_ossec_conf_linux(host): + """Test if ossec.conf is updated on Linux.""" + ossec_conf_path = "/var/ossec/etc/ossec.conf" + expected_content = """ + + + snort-full + /var/log/snort/snort.alert.fast + """ + + ossec_conf = host.file(ossec_conf_path) + assert ( + expected_content.strip() in ossec_conf.content_string.strip() + ), "ossec.conf should be updated on Linux" + + + + + \ No newline at end of file diff --git a/scripts/tests/test_linux.py b/scripts/tests/test_linux.py deleted file mode 100644 index f9be079..0000000 --- a/scripts/tests/test_linux.py +++ /dev/null @@ -1,78 +0,0 @@ -import pytest -import testinfra - -@pytest.fixture(scope="module") -def install_dependencies(host): - """Install dependencies and run the install script.""" - os = host.system_info.distribution - - if os in ["ubuntu", "debian"]: - host.run("apt-get update") - host.run("apt-get install -y curl gnupg2 iproute2") - - elif os == "alpine": - host.run("apk update") - host.run("apk add curl gnupg2 iproute2") - - elif os == "darwin": # macOS - host.run("brew install curl gnupg2 iproute2") - - else: - pytest.fail("Unsupported OS for dependency installation") - - -@pytest.mark.usefixtures("install_dependencies") -def test_snort_is_installed(host): - """Test if Snort is installed.""" - snort = host.package("snort") - assert snort.is_installed, "Snort should be installed" - - -def test_snort_conf_file_exists(host): - """Test if snort configuration file exists.""" - os = host.system_info.distribution - if os == "darwin": # macOS - snort_conf = host.file("/usr/local/etc/snort/snort.lua") - else: - snort_conf = host.file("/etc/snort/snort.conf") - - assert snort_conf.exists, "snort.conf file should exist" - - -def test_snort_interface_configuration(host): - """Test if the network interface is correctly configured in the Snort configuration file.""" - os = host.system_info.distribution - interface = host.run("ip route | grep default | awk '{print $5}'").stdout.strip() - - if os == "darwin": # macOS - snort_conf = host.file("/usr/local/etc/snort/snort.lua") - else: - snort_conf = host.file("/etc/snort/snort.debian.conf") - - assert interface in snort_conf.content_string, "Interface should be present in Snort configuration file" - - -def test_update_ossec_conf_linux(host): - """Test if ossec.conf is updated on Linux.""" - os = host.system_info.distribution - if os == "darwin": # macOS - ossec_conf_path = "/usr/local/etc/ossec.conf" - expected_content = """ - - - snort-full - /usr/local/var/log/snort/snort.alert.fast - """ - else: - ossec_conf_path = "/var/ossec/etc/ossec.conf" - expected_content = """ - - - snort-full - /var/log/snort/snort.alert.fast - """ - - ossec_conf = host.file(ossec_conf_path) - assert ( - expected_content.strip() in ossec_conf.content_string.strip() - ), "ossec.conf should be updated accordingly" diff --git a/scripts/tests/test_macos.py b/scripts/tests/test_macos.py deleted file mode 100644 index 9934353..0000000 --- a/scripts/tests/test_macos.py +++ /dev/null @@ -1,36 +0,0 @@ -import pytest -import testinfra - -@pytest.fixture(scope="module") -def install_dependencies(host): - """Install dependencies for macOS.""" - host.run("brew install curl gnupg2 iproute2") - -@pytest.mark.usefixtures("install_dependencies") -def test_snort_is_installed_mac(host): - """Test if Snort is installed on macOS.""" - snort = host.package("snort") - assert snort.is_installed, "Snort should be installed" - -def test_snort_conf_file_exists_mac(host): - """Test if snort.lua file exists on macOS.""" - snort_conf = host.file("/opt/homebrew/etc/snort/snort.lua") - assert snort_conf.exists, "snort.lua file should exist" - -def test_snort_interface_configuration_mac(host): - """Test if the network interface is correctly configured in the Snort configuration file on macOS.""" - interface = host.run("ip route | grep default | awk '{print $5}'").stdout.strip() - snort_conf = host.file("/opt/homebrew/etc/snort/snort.lua") - assert interface in snort_conf.content_string, "Interface should be present in snort.lua" - -def test_update_ossec_conf_mac(host): - """Test if ossec.conf is correctly updated on macOS.""" - ossec_conf_path = "/opt/homebrew/etc/snort/snort.lua" - expected_content = """ -/var/log/snort/alert_fast.txt - """ - - ossec_conf = host.file(ossec_conf_path) - assert ( - expected_content.strip() in ossec_conf.content_string.strip() - ), "ossec.conf should be updated correctly on macOS"