|
| 1 | +# Copyright (c) 2024 - 2025, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. |
| 3 | + |
| 4 | +"""Tests for the FakeEmailAnalyzer heuristic.""" |
| 5 | + |
| 6 | + |
| 7 | +from collections.abc import Generator |
| 8 | +from unittest.mock import MagicMock, patch |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult |
| 13 | +from macaron.malware_analyzer.pypi_heuristics.metadata.fake_email import FakeEmailAnalyzer |
| 14 | +from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(name="analyzer") |
| 18 | +def analyzer_fixture() -> FakeEmailAnalyzer: |
| 19 | + """Pytest fixture to create a FakeEmailAnalyzer instance.""" |
| 20 | + return FakeEmailAnalyzer() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(name="pypi_package_json_asset_mock") |
| 24 | +def pypi_package_json_asset_mock_fixture() -> MagicMock: |
| 25 | + """Pytest fixture for a mock PyPIPackageJsonAsset.""" |
| 26 | + mock_asset = MagicMock(spec=PyPIPackageJsonAsset) |
| 27 | + # Default to successful download, tests can override |
| 28 | + mock_asset.download = MagicMock(return_value=True) |
| 29 | + # package_json should be set by each test to simulate different PyPI responses |
| 30 | + mock_asset.package_json = {} |
| 31 | + return mock_asset |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(name="mock_dns_resolve") |
| 35 | +def mock_dns_resolve_fixture() -> Generator[MagicMock]: |
| 36 | + """General purpose mock for dns.resolver.resolve. |
| 37 | +
|
| 38 | + Patches where dns_resolver is imported in the module under test. |
| 39 | + """ |
| 40 | + with patch("macaron.malware_analyzer.pypi_heuristics.metadata.fake_email.dns_resolver.resolve") as mock_resolve: |
| 41 | + # Default behavior: simulate successful MX record lookup. |
| 42 | + mock_mx_record = MagicMock() |
| 43 | + mock_mx_record.exchange = "mail.default-domain.com" |
| 44 | + mock_resolve.return_value = [mock_mx_record] |
| 45 | + yield mock_resolve |
| 46 | + |
| 47 | + |
| 48 | +# Tests for the analyze method |
| 49 | +def test_analyze_download_failure(analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock) -> None: |
| 50 | + """Test the analyzer fails if downloading package JSON fails.""" |
| 51 | + pypi_package_json_asset_mock.download.return_value = False |
| 52 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 53 | + assert result == HeuristicResult.FAIL |
| 54 | + assert "message" in info |
| 55 | + assert isinstance(info["message"], str) |
| 56 | + assert "Failed to download package JSON" in info["message"] |
| 57 | + pypi_package_json_asset_mock.download.assert_called_once_with("") |
| 58 | + |
| 59 | + |
| 60 | +def test_analyze_skip_no_emails_present(analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock) -> None: |
| 61 | + """Test the analyzer skips if no author_email or maintainer_email is present.""" |
| 62 | + pypi_package_json_asset_mock.package_json = {"info": {"author_email": None, "maintainer_email": None}} |
| 63 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 64 | + assert result == HeuristicResult.SKIP |
| 65 | + assert info["message"] == "No maintainers are available" |
| 66 | + |
| 67 | + |
| 68 | +def test_analyze_skip_no_info_key(analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock) -> None: |
| 69 | + """Test the analyzer skips if 'info' key is missing in PyPI data.""" |
| 70 | + pypi_package_json_asset_mock.package_json = {} # No 'info' key |
| 71 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 72 | + assert result == HeuristicResult.SKIP |
| 73 | + assert info["message"] == "No maintainers are available" |
| 74 | + |
| 75 | + |
| 76 | +def test_analyze_fail_empty_author_email(analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock) -> None: |
| 77 | + """Test analyzer fails for empty author_email string (maintainer_email is None).""" |
| 78 | + pypi_package_json_asset_mock.package_json = {"info": {"author_email": "", "maintainer_email": None}} |
| 79 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 80 | + assert result == HeuristicResult.FAIL |
| 81 | + assert info["email"] == "" |
| 82 | + |
| 83 | + |
| 84 | +def test_analyze_pass_only_maintainer_email_valid( |
| 85 | + analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock, mock_dns_resolve: MagicMock |
| 86 | +) -> None: |
| 87 | + """Test analyzer passes when only maintainer_email is present and valid.""" |
| 88 | + mock_mx_record = MagicMock() |
| 89 | + mock_mx_record.exchange = "mail.example.net" |
| 90 | + mock_dns_resolve.return_value = [mock_mx_record] |
| 91 | + |
| 92 | + pypi_package_json_asset_mock.package_json = { |
| 93 | + "info": { "author_email": None, "maintainer_email": "[email protected]"} |
| 94 | + } |
| 95 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 96 | + assert result == HeuristicResult.PASS |
| 97 | + assert info == {} |
| 98 | + mock_dns_resolve.assert_called_once_with("example.net", "MX") |
| 99 | + |
| 100 | + |
| 101 | +def test_analyze_pass_both_emails_valid( |
| 102 | + analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock, mock_dns_resolve: MagicMock |
| 103 | +) -> None: |
| 104 | + """Test the analyzer passes when both emails are present and valid.""" |
| 105 | + |
| 106 | + def side_effect_dns_resolve(domain: str, record_type: str = "MX") -> list[MagicMock]: |
| 107 | + mock_mx = MagicMock() |
| 108 | + domains = { |
| 109 | + "MX": {"example.com", "example.net"}, |
| 110 | + } |
| 111 | + if domain not in domains.get(record_type, set()): |
| 112 | + pytest.fail(f"Unexpected domain for DNS resolve: {domain}") |
| 113 | + mock_mx.exchange = f"mail.{domain}" |
| 114 | + return [mock_mx] |
| 115 | + |
| 116 | + mock_dns_resolve.side_effect = side_effect_dns_resolve |
| 117 | + |
| 118 | + pypi_package_json_asset_mock.package_json = { |
| 119 | + "info": { "author_email": "[email protected]", "maintainer_email": "[email protected]"} |
| 120 | + } |
| 121 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 122 | + assert result == HeuristicResult.PASS |
| 123 | + assert info == {} |
| 124 | + assert mock_dns_resolve.call_count == 2 |
| 125 | + mock_dns_resolve.assert_any_call("example.com", "MX") |
| 126 | + mock_dns_resolve.assert_any_call("example.net", "MX") |
| 127 | + |
| 128 | + |
| 129 | +def test_analyze_fail_author_email_invalid_format( |
| 130 | + analyzer: FakeEmailAnalyzer, pypi_package_json_asset_mock: MagicMock, mock_dns_resolve: MagicMock |
| 131 | +) -> None: |
| 132 | + """Test analyzer fails when author_email has an invalid format.""" |
| 133 | + pypi_package_json_asset_mock.package_json = { |
| 134 | + "info": { "author_email": "bad_email_format", "maintainer_email": "[email protected]"} |
| 135 | + } |
| 136 | + result, info = analyzer.analyze(pypi_package_json_asset_mock) |
| 137 | + assert result == HeuristicResult.FAIL |
| 138 | + assert info["email"] == "bad_email_format" |
| 139 | + mock_dns_resolve.assert_not_called() # Regex check fails before DNS lookup |
| 140 | + |
| 141 | + |
| 142 | +# Tests for the is_valid_email method |
| 143 | +def test_is_valid_email_valid_email_with_mx(analyzer: FakeEmailAnalyzer, mock_dns_resolve: MagicMock) -> None: |
| 144 | + """Test is_valid_email returns True for a valid email with MX records.""" |
| 145 | + mock_mx_record = MagicMock() |
| 146 | + mock_mx_record.exchange = "mail.example.com" |
| 147 | + mock_dns_resolve.return_value = [mock_mx_record] |
| 148 | + assert analyzer. is_valid_email( "[email protected]") is True |
| 149 | + mock_dns_resolve.assert_called_once_with("example.com", "MX") |
| 150 | + |
| 151 | + |
| 152 | +def test_is_valid_email_invalid_format(analyzer: FakeEmailAnalyzer, mock_dns_resolve: MagicMock) -> None: |
| 153 | + """Test is_valid_email method with various invalid email formats.""" |
| 154 | + assert not analyzer.is_valid_email("not_an_email") |
| 155 | + assert not analyzer.is_valid_email("test@") |
| 156 | + assert not analyzer.is_valid_email("@example.com") |
| 157 | + assert not analyzer.is_valid_email("test@example") |
| 158 | + assert not analyzer.is_valid_email("") |
| 159 | + mock_dns_resolve.assert_not_called() |
| 160 | + |
| 161 | + |
| 162 | +def test_is_valid_email_no_mx_records_returned(analyzer: FakeEmailAnalyzer, mock_dns_resolve: MagicMock) -> None: |
| 163 | + """Test is_valid_email returns False if DNS resolve returns no MX records.""" |
| 164 | + mock_dns_resolve.return_value = [] # Simulate no MX records found |
| 165 | + assert analyzer. is_valid_email( "[email protected]") is False |
| 166 | + mock_dns_resolve.assert_called_once_with("no-mx-domain.com", "MX") |
0 commit comments