Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow Unittesting in Agent Asteroid #154

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions tests/asteroid_agent_test.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,73 @@
"""Unit tests for AsteroidAgent."""

from typing import Type, Iterator
from typing import Any, Iterator, Type

import requests_mock
from ostorlab.agent.message import message as m
from pytest_mock import plugin
from requests_mock.adapter import ANY

from agent import asteroid_agent
from agent import definitions
from agent import asteroid_agent, definitions


def testAsteroidAgent_whenExploitCheckDetectVulnz_EmitsVulnerabilityReport(
exploit_instance_with_report: Iterator[Type[definitions.Exploit]],
asteroid_agent_instance: asteroid_agent.AsteroidAgent,
agent_mock: list[m.Message],
scan_message_domain_name: m.Message,
mocker: plugin.MockerFixture,
requests_mock: requests_mock.Mocker,
) -> None:
"""Unit test for agent AsteroidAgent exploits check. case Exploit emits vulnerability report"""

mock_var_bind = mocker.MagicMock()
mock_var_bind.__getitem__.return_value.prettyPrint.return_value = (
"ArubaOS (MODEL: 7005), Version 8.5.0.0"
)
mock_iterator = mocker.MagicMock()
mock_iterator.__next__.return_value = (None, None, None, [mock_var_bind])
mocker.patch("pysnmp.hlapi.getCmd", return_value=mock_iterator)

requests_mock.register_uri(ANY, ANY, status_code=404, text="")

asteroid_agent_instance.process(scan_message_domain_name)

assert len(agent_mock) == 1
assert len(agent_mock) > 0
assert agent_mock[0].selector == "v3.report.vulnerability"


def testAsteroidAgent_whenTooManyRedirects_doesNotCrash(
exploit_instance_with_report: Iterator[Type[definitions.Exploit]],
asteroid_agent_instance: asteroid_agent.AsteroidAgent,
agent_mock: list[m.Message],
mocker: plugin.MockerFixture,
requests_mock: requests_mock.Mocker,
) -> None:
"""Ensure that the agent does not crash when there are too many redirects."""

def response_callback(request: Any, context: Any) -> str:
3asm marked this conversation as resolved.
Show resolved Hide resolved
context.headers = {"Location": request.url}
context.status_code = 302
return ""

requests_mock.register_uri(
ANY,
ANY,
text=response_callback,
)

mock_var_bind = mocker.MagicMock()
mock_var_bind.__getitem__.return_value.prettyPrint.return_value = (
"ArubaOS (MODEL: 7005), Version 8.5.0.0"
)
mock_iterator = mocker.MagicMock()
mock_iterator.__next__.return_value = (None, None, None, [mock_var_bind])
mocker.patch("pysnmp.hlapi.getCmd", return_value=mock_iterator)

msg = m.Message(
selector="v3.asset.link",
data={"url": "https://expediaagents.com", "method": "GET"},
raw=b"\n\x19https://expediaagents.com\x12\x03GET",
data={"url": "https://example.com", "method": "GET"},
raw=b"\n\x19https://example.com\x12\x03GET",
)

asteroid_agent_instance.process(msg)

assert len(agent_mock) == 1
Expand Down
Loading