|
| 1 | +"""Test ClamAV configuration verification.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import io |
| 5 | +import pathlib |
| 6 | +import tempfile |
| 7 | +from contextlib import redirect_stdout |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from nilrt_snac._configs._clamav_config import _ClamAVConfig |
| 11 | + |
| 12 | + |
| 13 | +class TestClamAVConfig: |
| 14 | + """Test cases for ClamAV configuration verification.""" |
| 15 | + |
| 16 | + def test_configure_not_installed(self): |
| 17 | + """Test configure method when ClamAV is not installed.""" |
| 18 | + config = _ClamAVConfig() |
| 19 | + |
| 20 | + # Mock opkg_helper to return False for all packages |
| 21 | + with patch.object(config._opkg_helper, "is_installed", return_value=False): |
| 22 | + args = argparse.Namespace(dry_run=False) |
| 23 | + |
| 24 | + # This should not raise an exception |
| 25 | + config.configure(args) |
| 26 | + |
| 27 | + def test_verify_not_installed(self): |
| 28 | + """Test verify method when ClamAV is not installed - should pass.""" |
| 29 | + config = _ClamAVConfig() |
| 30 | + |
| 31 | + # Mock opkg_helper to return False for all packages |
| 32 | + with patch.object(config._opkg_helper, "is_installed", return_value=False): |
| 33 | + # Capture stdout to verify the skip message |
| 34 | + captured_output = io.StringIO() |
| 35 | + args = argparse.Namespace() |
| 36 | + |
| 37 | + with redirect_stdout(captured_output): |
| 38 | + result = config.verify(args) |
| 39 | + |
| 40 | + # Verify it returns True for the right reason |
| 41 | + assert result is True |
| 42 | + output = captured_output.getvalue() |
| 43 | + assert "ClamAV is not installed; skipping verification." in output |
| 44 | + |
| 45 | + def test_verify_installed_missing_config_files(self): |
| 46 | + """Test verify when ClamAV is installed but config files missing.""" |
| 47 | + config = _ClamAVConfig() |
| 48 | + |
| 49 | + # Mock opkg_helper to return True for one ClamAV package |
| 50 | + with patch.object(config._opkg_helper, "is_installed") as mock_installed: |
| 51 | + mock_installed.side_effect = lambda pkg: pkg == "clamav" |
| 52 | + |
| 53 | + args = argparse.Namespace() |
| 54 | + result = config.verify(args) |
| 55 | + |
| 56 | + # Should fail because config files don't exist |
| 57 | + assert result is False |
| 58 | + |
| 59 | + def test_verify_installed_with_valid_config(self): |
| 60 | + """Test verify method when ClamAV installed with valid config.""" |
| 61 | + config = _ClamAVConfig() |
| 62 | + |
| 63 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 64 | + # Create temporary config files and database directory |
| 65 | + clamd_config = pathlib.Path(tmpdir) / "clamd.conf" |
| 66 | + freshclam_config = pathlib.Path(tmpdir) / "freshclam.conf" |
| 67 | + virus_db_dir = pathlib.Path(tmpdir) / "db" |
| 68 | + virus_db_dir.mkdir() |
| 69 | + |
| 70 | + # Create non-empty config files |
| 71 | + clamd_config.write_text( |
| 72 | + "# ClamAV daemon config\n" "LogFile /var/log/clamav/clamd.log\n" |
| 73 | + ) |
| 74 | + freshclam_config.write_text( |
| 75 | + "# FreshClam config\n" "UpdateLogFile /var/log/clamav/freshclam.log\n" |
| 76 | + ) |
| 77 | + |
| 78 | + # Create a signature file |
| 79 | + signature_file = virus_db_dir / "main.cvd" |
| 80 | + signature_file.write_bytes(b"fake signature data") |
| 81 | + |
| 82 | + # Update config paths to use temporary files |
| 83 | + config.clamd_config_path = str(clamd_config) |
| 84 | + config.freshclam_config_path = str(freshclam_config) |
| 85 | + config.virus_db_path = str(virus_db_dir) |
| 86 | + |
| 87 | + # Mock opkg_helper to return True for one ClamAV package |
| 88 | + with patch.object(config._opkg_helper, "is_installed") as mock_installed: |
| 89 | + mock_installed.side_effect = lambda pkg: pkg == "clamav" |
| 90 | + |
| 91 | + args = argparse.Namespace() |
| 92 | + result = config.verify(args) |
| 93 | + |
| 94 | + # Should pass because all required files exist and not empty |
| 95 | + assert result is True |
| 96 | + |
| 97 | + def test_verify_installed_empty_config_files(self): |
| 98 | + """Test verify method when ClamAV config files exist but empty.""" |
| 99 | + config = _ClamAVConfig() |
| 100 | + |
| 101 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 102 | + # Create temporary empty config files |
| 103 | + clamd_config = pathlib.Path(tmpdir) / "clamd.conf" |
| 104 | + freshclam_config = pathlib.Path(tmpdir) / "freshclam.conf" |
| 105 | + virus_db_dir = pathlib.Path(tmpdir) / "db" |
| 106 | + virus_db_dir.mkdir() |
| 107 | + |
| 108 | + # Create empty config files |
| 109 | + clamd_config.write_text("") |
| 110 | + freshclam_config.write_text("") |
| 111 | + |
| 112 | + # Create a signature file |
| 113 | + signature_file = virus_db_dir / "main.cvd" |
| 114 | + signature_file.write_bytes(b"fake signature data") |
| 115 | + |
| 116 | + # Update config paths to use temporary files |
| 117 | + config.clamd_config_path = str(clamd_config) |
| 118 | + config.freshclam_config_path = str(freshclam_config) |
| 119 | + config.virus_db_path = str(virus_db_dir) |
| 120 | + |
| 121 | + # Mock opkg_helper to return True for one ClamAV package |
| 122 | + with patch.object(config._opkg_helper, "is_installed") as mock_installed: |
| 123 | + mock_installed.side_effect = lambda pkg: pkg == "clamav" |
| 124 | + |
| 125 | + args = argparse.Namespace() |
| 126 | + result = config.verify(args) |
| 127 | + |
| 128 | + # Should fail because config files are empty |
| 129 | + assert result is False |
| 130 | + |
| 131 | + def test_verify_installed_no_signatures(self): |
| 132 | + """Test verify when ClamAV installed but no signature files exist.""" |
| 133 | + config = _ClamAVConfig() |
| 134 | + |
| 135 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 136 | + # Create temporary config files and database directory |
| 137 | + clamd_config = pathlib.Path(tmpdir) / "clamd.conf" |
| 138 | + freshclam_config = pathlib.Path(tmpdir) / "freshclam.conf" |
| 139 | + virus_db_dir = pathlib.Path(tmpdir) / "db" |
| 140 | + virus_db_dir.mkdir() |
| 141 | + |
| 142 | + # Create non-empty config files |
| 143 | + clamd_config.write_text( |
| 144 | + "# ClamAV daemon config\n" "LogFile /var/log/clamav/clamd.log\n" |
| 145 | + ) |
| 146 | + freshclam_config.write_text( |
| 147 | + "# FreshClam config\n" "UpdateLogFile /var/log/clamav/freshclam.log\n" |
| 148 | + ) |
| 149 | + |
| 150 | + # Don't create any signature files |
| 151 | + |
| 152 | + # Update config paths to use temporary files |
| 153 | + config.clamd_config_path = str(clamd_config) |
| 154 | + config.freshclam_config_path = str(freshclam_config) |
| 155 | + config.virus_db_path = str(virus_db_dir) |
| 156 | + |
| 157 | + # Mock opkg_helper to return True for one ClamAV package |
| 158 | + with patch.object(config._opkg_helper, "is_installed") as mock_installed: |
| 159 | + mock_installed.side_effect = lambda pkg: pkg == "clamav" |
| 160 | + |
| 161 | + args = argparse.Namespace() |
| 162 | + result = config.verify(args) |
| 163 | + |
| 164 | + # Should fail because no signature files exist |
| 165 | + assert result is False |
0 commit comments