mirror of
https://github.com/Such-Software/adaptive-multi-protocol-browser.git
synced 2026-10-17 14:31:46 +02:00
Add transport policy config
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from ampbrowser.config import ConfigError, load_config
|
||||
|
||||
|
||||
class ConfigTest(unittest.TestCase):
|
||||
def test_missing_config_uses_defaults(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
config = load_config(Path(tmp))
|
||||
|
||||
self.assertEqual(".ampb", config.state_dir)
|
||||
self.assertEqual("adopt-or-prompt-manage", config.transport_mode("tor"))
|
||||
self.assertEqual(".ampb/profiles/tor", config.profile_path("tor"))
|
||||
|
||||
def test_loads_transport_modes_and_state_dir(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.toml"
|
||||
path.write_text(
|
||||
"""
|
||||
[browser]
|
||||
state_dir = ".local/ampb"
|
||||
|
||||
[transports.tor]
|
||||
mode = "adopt"
|
||||
|
||||
[transports.i2p]
|
||||
enabled = false
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
config = load_config(Path(tmp), path)
|
||||
|
||||
self.assertEqual(".local/ampb", config.state_dir)
|
||||
self.assertEqual("adopt", config.transport_mode("tor"))
|
||||
self.assertEqual("disabled", config.transport_mode("i2p"))
|
||||
self.assertEqual(".local/ampb/profiles/i2p", config.profile_path("i2p"))
|
||||
|
||||
def test_accepts_adopt_or_manage_alias(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.toml"
|
||||
path.write_text(
|
||||
"""
|
||||
[transports.tor]
|
||||
mode = "adopt-or-manage"
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
config = load_config(Path(tmp), path)
|
||||
|
||||
self.assertEqual("adopt-or-prompt-manage", config.transport_mode("tor"))
|
||||
|
||||
def test_rejects_unknown_mode(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.toml"
|
||||
path.write_text(
|
||||
"""
|
||||
[transports.tor]
|
||||
mode = "surprise"
|
||||
""".strip(),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config(Path(tmp), path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ampbrowser.config import AppConfig
|
||||
from ampbrowser.launch import prepare_open
|
||||
from ampbrowser.transports import TransportStatus
|
||||
|
||||
@@ -54,6 +55,13 @@ class PrepareOpenTest(unittest.TestCase):
|
||||
self.assertTrue(plan.consent_granted)
|
||||
self.assertEqual(("start managed tor", "wait for socks5://127.0.0.1:9050"), plan.setup_steps)
|
||||
|
||||
def test_custom_state_dir_changes_profile_path(self) -> None:
|
||||
config = AppConfig(state_dir=".local/ampb", transport_modes={})
|
||||
|
||||
plan = prepare_open("wownero.org", config=config)
|
||||
|
||||
self.assertEqual(".local/ampb/profiles/clearnet", plan.profile_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ampbrowser.config import AppConfig
|
||||
from ampbrowser.plan import plan_url
|
||||
from ampbrowser.transports import TransportStatus
|
||||
|
||||
@@ -60,6 +61,34 @@ class PlanUrlTest(unittest.TestCase):
|
||||
self.assertTrue(plan.requires_consent)
|
||||
self.assertIn("can start", plan.prompt)
|
||||
|
||||
def test_disabled_transport_is_blocked_by_policy(self) -> None:
|
||||
config = AppConfig(transport_modes={"i2p": "disabled"})
|
||||
|
||||
plan = plan_url("http://example.b32.i2p/", config=config)
|
||||
|
||||
self.assertEqual("disabled", plan.policy_mode)
|
||||
self.assertEqual("blocked by policy: i2p disabled", plan.action)
|
||||
self.assertFalse(plan.requires_consent)
|
||||
|
||||
def test_adopt_only_transport_does_not_prompt_to_start(self) -> None:
|
||||
status = TransportStatus(
|
||||
transport="tor",
|
||||
installed=True,
|
||||
running=False,
|
||||
endpoint="socks5://127.0.0.1:9050",
|
||||
adoptable=False,
|
||||
manage_supported=True,
|
||||
note="Tor SOCKS proxy",
|
||||
)
|
||||
config = AppConfig(transport_modes={"tor": "adopt"})
|
||||
|
||||
with patch("ampbrowser.plan.inspect_transport", return_value=status):
|
||||
plan = plan_url("http://example.onion/", config=config)
|
||||
|
||||
self.assertEqual("adopt", plan.policy_mode)
|
||||
self.assertEqual("blocked by policy: tor mode is adopt", plan.action)
|
||||
self.assertFalse(plan.requires_consent)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user