"""Tests for :mod:`mgf.vm.core.security.isolation.checks.shares` (MA-A2.3)."""

from __future__ import annotations

from _isolation_xml import domain_with, parsed

from mgf.vm.core.security.isolation.checks.shares import (
    share_no_filesystem,
    share_no_qemu_agent,
    share_no_redirdev,
    share_no_smartcard,
    share_no_spice_clipboard,
)
from mgf.vm.core.security.isolation.context import AnalysisContext
from mgf.vm.core.security.isolation.types import FixKind, IsolationCategory


def _ctx(*body: str) -> AnalysisContext:
    return AnalysisContext(
        domain_xml=parsed(domain_with(*body)),
        vm_name="t",
    )


class TestShareNoFilesystem:
    def test_passes_when_absent(self) -> None:
        f = share_no_filesystem(_ctx())
        assert f.passing is True

    def test_fails_when_present(self) -> None:
        body = (
            "<filesystem type='mount' accessmode='passthrough'>"
            "<source dir='/srv/host-share'/>"
            "<target dir='hostshare'/>"
            "</filesystem>"
        )
        f = share_no_filesystem(_ctx(body))
        assert f.passing is False
        assert f.category is IsolationCategory.BLOCKING
        assert f.fix_kind is FixKind.AUTO_FIX
        assert f.fix_executor == "strip.filesystem"
        assert "/srv/host-share" in str(f.details["sources"])


class TestShareNoQemuAgent:
    def test_passes_when_absent(self) -> None:
        f = share_no_qemu_agent(_ctx())
        assert f.passing is True

    def test_fails_when_qemu_agent_channel_present(self) -> None:
        body = (
            "<channel type='unix'>"
            "<source mode='bind'/>"
            "<target type='virtio' name='org.qemu.guest_agent.0'/>"
            "</channel>"
        )
        f = share_no_qemu_agent(_ctx(body))
        assert f.passing is False
        assert f.fix_executor == "strip.channel.guest_agent"

    def test_other_channels_dont_trip_qemu_check(self) -> None:
        body = (
            "<channel type='spicevmc'><target type='virtio' name='com.redhat.spice.0'/></channel>"
        )
        f = share_no_qemu_agent(_ctx(body))
        assert f.passing is True


class TestShareNoSpiceClipboard:
    def test_passes_when_absent(self) -> None:
        f = share_no_spice_clipboard(_ctx())
        assert f.passing is True

    def test_fails_when_spice_channel_present(self) -> None:
        body = (
            "<channel type='spicevmc'><target type='virtio' name='com.redhat.spice.0'/></channel>"
        )
        f = share_no_spice_clipboard(_ctx(body))
        assert f.passing is False
        assert f.fix_executor == "strip.channel.spice_clipboard"


class TestShareNoRedirdev:
    def test_passes_when_absent(self) -> None:
        f = share_no_redirdev(_ctx())
        assert f.passing is True

    def test_fails_when_present(self) -> None:
        body = "<redirdev bus='usb' type='spicevmc'/>"
        f = share_no_redirdev(_ctx(body))
        assert f.passing is False
        assert f.fix_executor == "strip.redirdev"


class TestShareNoSmartcard:
    def test_passes_when_absent(self) -> None:
        f = share_no_smartcard(_ctx())
        assert f.passing is True

    def test_fails_when_present(self) -> None:
        body = "<smartcard mode='passthrough' type='spicevmc'/>"
        f = share_no_smartcard(_ctx(body))
        assert f.passing is False
        assert f.fix_executor == "strip.smartcard"
