Path: blob/master/tests/discovery/test_shodan_engine.py
904 views
import socket1import sys2from collections import OrderedDict34import pytest567class TestShodanEngine:8@pytest.mark.asyncio9async def test_shodan_engine_processes_without_work_item_error_and_yields_hostnames(self, monkeypatch, capsys):10# Import inside the test so monkeypatching affects the already-imported module namespace.11import theHarvester.__main__ as main_module1213# Make DNS resolution deterministic and offline.14monkeypatch.setattr(socket, "gethostbyname", lambda _domain: "1.2.3.4", raising=True)1516# Avoid filesystem/sqlite side effects.17class DummyStashManager:18async def do_init(self) -> None:19return None2021async def store_all(self, domain, all, res_type, source) -> None: # noqa: A00222return None2324monkeypatch.setattr(main_module.stash, "StashManager", DummyStashManager, raising=True)2526# Stub Shodan search to avoid network and API key requirements.27class DummySearchShodan:28async def search_ip(self, ip):29return OrderedDict({ip: {"hostnames": ["a.example.com", "b.example.com"]}})3031monkeypatch.setattr(main_module.shodansearch, "SearchShodan", DummySearchShodan, raising=True)3233# Run the CLI path that uses the engine queue/worker (`-b shodan`).34monkeypatch.setattr(sys, "argv", ["theHarvester", "-d", "example.com", "-b", "shodan"], raising=True)3536with pytest.raises(SystemExit) as excinfo:37await main_module.start()38assert excinfo.value.code == 03940out = capsys.readouterr().out41assert 'A error occurred while processing a "work item"' not in out42assert "a.example.com" in out43assert "b.example.com" in out444546