mirror of
https://github.com/Such-Software/adaptive-multi-protocol-browser.git
synced 2026-10-18 15:01:26 +02:00
Keep transports in one browser window
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ampbrowser.broker import BrokerRouteError, open_brokered_url
|
||||
from ampbrowser.config import AppConfig
|
||||
from ampbrowser.transports import TransportStatus
|
||||
|
||||
|
||||
class BrokerTest(unittest.TestCase):
|
||||
def test_broker_launches_matching_route_without_shared_profile(self) -> None:
|
||||
status = TransportStatus(
|
||||
transport="tor",
|
||||
installed=True,
|
||||
running=True,
|
||||
endpoint="socks5://127.0.0.1:9050",
|
||||
adoptable=True,
|
||||
manage_supported=True,
|
||||
note="Tor SOCKS proxy",
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
with patch("ampbrowser.plan.inspect_transport", return_value=status):
|
||||
with patch("ampbrowser.broker.launch_open_plan", side_effect=lambda plan, **_: plan) as launch:
|
||||
result = open_brokered_url(
|
||||
"http://example.onion/",
|
||||
expected_transport="tor",
|
||||
consent=False,
|
||||
config=AppConfig(transport_modes={}),
|
||||
root=Path(tmp),
|
||||
)
|
||||
|
||||
self.assertEqual("ready", result.status)
|
||||
self.assertEqual(".ampb/profiles/tor", result.profile_path)
|
||||
self.assertFalse(result.launch_spec.broker)
|
||||
launch.assert_called_once()
|
||||
|
||||
def test_broker_rejects_claimed_transport_mismatch(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
with self.assertRaises(BrokerRouteError):
|
||||
open_brokered_url(
|
||||
"http://example.b32.i2p/",
|
||||
expected_transport="tor",
|
||||
consent=False,
|
||||
config=AppConfig(transport_modes={}),
|
||||
root=Path(tmp),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -15,28 +15,28 @@ class FixtureCheckTest(unittest.TestCase):
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"schema": "ampg.fixture-manifest.v1",
|
||||
"schema": "ampg.fixture-manifest.v2",
|
||||
"site": {"id": "wownero", "domain": "wownero.org"},
|
||||
"fixtures": [
|
||||
{
|
||||
"protocol": "clearnet",
|
||||
"url": "https://wownero.org/",
|
||||
"checks": {"transport": "clearnet", "profile": "clearnet"},
|
||||
"checks": {"transport": "clearnet", "context": "clearnet", "isolation": "transport-context"},
|
||||
},
|
||||
{
|
||||
"protocol": "tor",
|
||||
"url": "http://wownero.onion/",
|
||||
"checks": {
|
||||
"transport": "tor",
|
||||
"profile": "tor",
|
||||
"isolation": "transport-profile",
|
||||
"context": "tor",
|
||||
"isolation": "transport-context",
|
||||
},
|
||||
},
|
||||
{
|
||||
"protocol": "ipfs",
|
||||
"url": "ipfs://bafyexample",
|
||||
"route": {"match": "/snapshot/*", "fixture_path": "/snapshot/"},
|
||||
"checks": {"transport": "ipfs", "profile": "ipfs"},
|
||||
"checks": {"transport": "ipfs", "context": "ipfs", "isolation": "transport-context"},
|
||||
"interaction": {"tier": "static"},
|
||||
},
|
||||
],
|
||||
@@ -49,7 +49,7 @@ class FixtureCheckTest(unittest.TestCase):
|
||||
|
||||
self.assertTrue(result.ok)
|
||||
self.assertEqual(3, len(result.checks))
|
||||
self.assertEqual("transport-profile", result.checks[1].actual_isolation)
|
||||
self.assertEqual("transport-context", result.checks[1].actual_isolation)
|
||||
self.assertEqual("/snapshot/*", result.checks[2].route_match)
|
||||
self.assertEqual("/snapshot/", result.checks[2].fixture_path)
|
||||
|
||||
@@ -115,13 +115,13 @@ def _check_one(transport: str, url: str, interaction: dict) -> object:
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"schema": "ampg.fixture-manifest.v1",
|
||||
"schema": "ampg.fixture-manifest.v2",
|
||||
"site": {"id": "example", "domain": "example.test"},
|
||||
"fixtures": [
|
||||
{
|
||||
"protocol": transport,
|
||||
"url": url,
|
||||
"checks": {"transport": transport, "profile": transport},
|
||||
"checks": {"transport": transport, "context": transport, "isolation": "transport-context"},
|
||||
"interaction": interaction,
|
||||
}
|
||||
],
|
||||
|
||||
+18
-5
@@ -141,7 +141,7 @@ class PrepareOpenTest(unittest.TestCase):
|
||||
self.assertTrue(user_js.exists())
|
||||
self.assertIn('user_pref("network.proxy.type", 0);', user_js.read_text(encoding="utf-8"))
|
||||
|
||||
def test_broker_open_writes_direct_profile_and_handoff_extension(self) -> None:
|
||||
def test_broker_open_writes_single_window_container_router(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
runtime = root / "firefox"
|
||||
@@ -153,6 +153,7 @@ class PrepareOpenTest(unittest.TestCase):
|
||||
self.assertIsNotNone(plan.launch_spec)
|
||||
self.assertTrue(plan.launch_spec.broker)
|
||||
self.assertIn('user_pref("network.proxy.type", 0);', plan.launch_spec.prefs)
|
||||
self.assertIn('user_pref("media.peerconnection.enabled", false);', plan.launch_spec.prefs)
|
||||
|
||||
planned_helper = RouteHelperLaunch("planned", endpoint="http://127.0.0.1:44001/", token="test-token")
|
||||
helper = RouteHelperLaunch("started", endpoint="http://127.0.0.1:44001/", token="test-token", pid=4321)
|
||||
@@ -185,17 +186,29 @@ class PrepareOpenTest(unittest.TestCase):
|
||||
background = (extension / "background.js").read_text(encoding="utf-8")
|
||||
setup = (extension / "setup.html").read_text(encoding="utf-8")
|
||||
self.assertIn("webRequestBlocking", manifest)
|
||||
self.assertIn("http://*.onion/*", manifest)
|
||||
self.assertIn("wss://*.i2p/*", manifest)
|
||||
self.assertIn("contextualIdentities", manifest)
|
||||
self.assertIn('"proxy"', manifest)
|
||||
self.assertIn("<all_urls>", manifest)
|
||||
self.assertIn("cancel: true", background)
|
||||
self.assertIn("helper('open'", background)
|
||||
self.assertIn("browser.proxy.onRequest", background)
|
||||
self.assertIn("cookieStoreId", background)
|
||||
self.assertIn("browser.tabs.create", background)
|
||||
self.assertIn("proxyDNS: true", background)
|
||||
self.assertIn("'<torS0X>0'", background)
|
||||
self.assertIn("connectionIsolationKey", background)
|
||||
self.assertIn("blockedProxy", background)
|
||||
self.assertIn("isHelperRequest", background)
|
||||
self.assertIn("details.tabId < 0", background)
|
||||
self.assertIn("helper('status'", background)
|
||||
self.assertIn("helper('ensure'", background)
|
||||
self.assertNotIn("helper('open'", background)
|
||||
self.assertIn("install_command", background)
|
||||
self.assertIn("http://127.0.0.1:", background)
|
||||
self.assertNotIn("__AMPB_ROUTE_HELPER_URL__", background)
|
||||
self.assertNotIn("__AMPB_ROUTE_HELPER_TOKEN__", background)
|
||||
self.assertIn("Set Up Transport", setup)
|
||||
self.assertIn("install-command", setup)
|
||||
self.assertTrue((extension / "handoff.html").exists())
|
||||
self.assertFalse((extension / "handoff.html").exists())
|
||||
|
||||
def test_broker_entry_rejects_alternate_transport_url(self) -> None:
|
||||
plan = prepare_open("http://example.onion/", broker=True)
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ampbrowser.config import AppConfig
|
||||
from ampbrowser.launch import prepare_open
|
||||
from ampbrowser.route_helper import handle_helper_message
|
||||
from ampbrowser.transport_manager import ManagedTransportResult
|
||||
from ampbrowser.transports import TransportStatus
|
||||
|
||||
|
||||
class RouteHelperTest(unittest.TestCase):
|
||||
@@ -84,41 +81,7 @@ class RouteHelperTest(unittest.TestCase):
|
||||
self.assertFalse(response["ok"])
|
||||
self.assertEqual("unsupported-transport", response["status"])
|
||||
|
||||
def test_open_hands_route_to_isolated_transport_profile(self) -> None:
|
||||
status = TransportStatus(
|
||||
transport="tor",
|
||||
installed=True,
|
||||
running=True,
|
||||
endpoint="socks5://127.0.0.1:9050",
|
||||
adoptable=True,
|
||||
manage_supported=True,
|
||||
note="Tor SOCKS proxy",
|
||||
)
|
||||
with patch("ampbrowser.plan.inspect_transport", return_value=status):
|
||||
plan = prepare_open("http://example.onion/")
|
||||
launched = replace(plan, status="launched", dry_run=False, browser_pid=4321)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
with patch("ampbrowser.route_helper.open_brokered_url", return_value=launched) as broker_open:
|
||||
response = handle_helper_message(
|
||||
{
|
||||
"action": "open",
|
||||
"transport": "tor",
|
||||
"url": "http://example.onion/",
|
||||
"consent": True,
|
||||
},
|
||||
root=Path(tmp),
|
||||
config=AppConfig(transport_modes={}),
|
||||
)
|
||||
|
||||
self.assertTrue(response["ok"])
|
||||
self.assertTrue(response["launched"])
|
||||
self.assertEqual("tor", response["profile"])
|
||||
self.assertEqual(".ampb/profiles/tor", response["profile_path"])
|
||||
self.assertEqual(4321, response["browser_pid"])
|
||||
broker_open.assert_called_once()
|
||||
|
||||
def test_open_rejects_transport_url_mismatch(self) -> None:
|
||||
def test_open_action_is_not_exposed(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
response = handle_helper_message(
|
||||
{
|
||||
@@ -131,7 +94,7 @@ class RouteHelperTest(unittest.TestCase):
|
||||
)
|
||||
|
||||
self.assertFalse(response["ok"])
|
||||
self.assertEqual("route-mismatch", response["status"])
|
||||
self.assertEqual("unsupported-action", response["status"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user