One keyboard and mouse for a Mac and a Windows PC over the local network, in either direction.
"""The Mac's controller and the owner (WIRE.md sections 4 and 5) with the links faked: where input
goes, what is sent to the machine it leaves and the one it takes, the pointer and the alerts each
way home, the key table on the way out, and the clipboard. The links themselves are
core/tests/test_link.py's; the two ends together are test_mac_loopback.py's."""
import logging
import os
import sys
import threading
import time
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from bridge import KVMController
from core import protocol
from bridge_fakes import PAIRED_TOKEN, FakeClipboard, FakeClock, FakeQuartz, make_config, quiet_logger, settle
from fake_link import CAPS, OTHER_ID, PEER_ID, FakeLink, bring_up
class Base(unittest.TestCase):
def setUp(self):
FakeQuartz.reset_cursor_spies()
self.clock = FakeClock()
self.clipboard = FakeClipboard("hello")
self.controller = KVMController(
make_config(PAIRED_TOKEN), logger=quiet_logger(), quartz=FakeQuartz, clock=self.clock,
clipboard=self.clipboard, link_factory=FakeLink, desktop_bounds=lambda: (0, 0, 1920, 1080),
)
self.alerts = []
self.controller.on_user_alert = lambda title, message: self.alerts.append(message)
self.crossings = []
self.controller.on_crossing = lambda kind, step: self.crossings.append((kind, step))
self.link = self.controller._primary_link
self.own = protocol.id_text(self.controller.identity()["id"])
self.peer = protocol.id_text(PEER_ID)
def up(self, **fields):
return bring_up(self.controller, **fields)
def focus(self, index=0):
focuses = [m for m in self.link.posted if m["type"] == protocol.MSG_FOCUS]
return focuses[index]["data"]
class AckStallRecoveryTests(Base):
def test_a_heartbeat_only_peer_stall_ends_the_visit_and_releases_the_cursor(self):
self.up()
self.assertTrue(self.controller.set_redirecting(True))
with self.assertLogs(self.controller.logger, level="WARNING") as said:
self.link.callbacks["state"](self.link, False, "Office PC stopped acknowledging input")
self.assertFalse(self.controller.redirecting)
self.assertFalse(self.controller.owner.away)
self.assertIn(True, FakeQuartz.associate_calls)
self.assertTrue(any("stopped acknowledging input" in line for line in said.output), said.output)
def test_a_peer_switch_is_logged_as_the_move_source(self):
self.up()
with self.controller.book.lock:
peers = self.controller.book.data["peers"]
peers.append({**peers[0], "id": protocol.id_text(OTHER_ID), "token": "second", "send": True})
self.controller._sync_links()
other = self.controller.links["second"]
other.up(self.controller, ident=OTHER_ID, name="Laptop", platform="macos")
self.assertTrue(self.controller.set_redirecting(True))
self.link.arrives(self.controller, protocol.accept_msg(1))
with self.assertLogs(self.controller.logger, level="INFO") as said:
self.link.arrives(self.controller, protocol.switch_v6(1, OTHER_ID))
other.arrives(self.controller, protocol.accept_msg(self.controller.owner.route))
self.assertTrue(any("move started by peer request" in line for line in said.output), said.output)
class HardwareHookTests(unittest.TestCase):
def controller(self, **kwargs):
return KVMController(
make_config(PAIRED_TOKEN), logger=quiet_logger(), quartz=FakeQuartz, clock=FakeClock(),
clipboard=FakeClipboard("hello"), link_factory=FakeLink, desktop_bounds=lambda: (0, 0, 1920, 1080), **kwargs,
)
def test_each_link_is_handed_the_controllers_hardware_hook(self):
hook = lambda host: "aa:bb:cc:dd:ee:ff"
self.assertIs(self.controller(hardware=hook)._primary_link.hardware, hook)
def test_no_hook_leaves_the_link_without_one(self):
self.assertIsNone(self.controller()._primary_link.hardware)
def test_each_link_reads_a_large_frame_only_while_the_owner_expects_its_clipboard(self):
controller = self.controller()
link = controller._primary_link
link.peer_id = "B"
now = controller.owner._clock()
self.assertFalse(link.large(link, now))
controller.owner._let_go["B"] = now
self.assertTrue(link.large(link, now))
class RemovedLinksTests(Base):
def test_a_link_for_a_machine_that_went_is_stopped_without_waiting_for_it(self):
# Stopping joins the link's thread, which can be in a connect: never on the main thread.
self.controller.book.data["peers"][0]["send"] = False
self.controller._sync_links()
self.assertTrue(self.link.stopped)
self.assertFalse(self.link.stop_waited)
def test_the_primary_link_moves_to_the_first_machine_still_in_use(self):
self.controller.book.data["peers"][0]["in_use"] = False
self.controller.book.data["peers"].append({**self.controller.book.peers()[0], "token": "second", "in_use": True})
self.controller._sync_links()
self.assertEqual(self.controller._primary_token, "second")
self.assertIsNotNone(self.controller._primary_link)
def test_a_link_that_goes_down_after_its_machine_went_cannot_bring_input_back_or_report_up(self):
self.up()
self.controller.book.data["peers"][0]["send"] = False
self.controller._sync_links()
self.controller._link_up(self.link, {"id": PEER_ID, "name": "Late", "accepts": True, "caps": CAPS})
self.assertNotIn(self.peer, self.controller._peers_up)
def test_a_link_whose_machine_goes_as_it_comes_up_is_not_left_registered(self):
controller, link, peer = self.controller, self.link, self.peer
class RemovesTheMachine(dict):
# The machine goes between the check that its link is still wanted and the registration.
def __setitem__(self, key, value):
super().__setitem__(key, value)
controller.book.data["peers"][0]["send"] = False
controller._sync_links()
controller._up_names = RemovesTheMachine()
link._live = True
link.peer_id = peer
controller._link_up(link, {"id": PEER_ID, "name": "Late", "accepts": True, "caps": CAPS})
self.assertNotIn(peer, controller._peers_up)
self.assertNotIn(peer, controller.owner._links)
def test_stopping_the_controller_still_waits_for_its_links(self):
self.controller.stop()
self.assertTrue(self.link.stop_waited)
class TakingAndLeavingTests(Base):
def test_a_take_sends_the_focus_then_the_clipboard(self):
self.up()
self.assertTrue(self.controller.set_redirecting(True, edge="left", offset=0.4))
settle(self.controller)
take = self.focus()
self.assertEqual((take["route"], take["target"], take["edge"], take["offset"]), (1, self.peer, "left", 0.4))
self.assertEqual(take["resistance_px"], int(self.controller.crossing.resistance_px))
self.assertEqual(take["reach"], [])
kinds = [m["type"] for m in self.link.posted if m["type"] in (protocol.MSG_FOCUS, protocol.MSG_CLIPBOARD)]
self.assertEqual(kinds, [protocol.MSG_FOCUS, protocol.MSG_CLIPBOARD])
self.assertEqual(self.link.posted[-1]["data"]["text"], "hello")
self.assertTrue(self.controller.redirecting)
self.assertEqual(FakeQuartz.associate_calls, [False])
def test_the_shortcut_takes_with_no_position(self):
self.up()
self.controller.set_redirecting(True)
self.assertNotIn("edge", self.focus())
def test_input_follows_the_take_to_the_link(self):
self.up()
self.controller.set_redirecting(True)
self.controller._process_outbound({"type": protocol.MSG_MOUSEMOVE, "data": {"dx": 3, "dy": 4}})
self.assertEqual(self.link.sent, [{"type": protocol.MSG_MOUSEMOVE, "data": {"dx": 3, "dy": 4}}])
def test_going_home_releases_what_was_pressed_then_lets_go(self):
self.up()
self.controller.set_redirecting(True)
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "a", "us": "a"}})
self.link.posted.clear()
self.link.sent.clear()
self.assertTrue(self.controller.set_redirecting(False))
self.assertEqual(self.link.sent, [{"type": protocol.MSG_KEYUP, "data": {"key": "a", "us": "a"}}])
let_go = self.focus()
self.assertEqual((let_go["route"], let_go["target"]), (2, self.own))
self.assertFalse(self.controller.redirecting)
self.assertEqual(FakeQuartz.associate_calls[-1], True)
self.assertEqual([kind for kind, _ in self.crossings], ["home"])
def test_nothing_is_taken_when_the_peer_does_not_accept_this_machine(self):
self.up(accepts=False)
self.assertFalse(self.controller.set_redirecting(True))
self.assertFalse(self.controller.redirecting)
self.assertIn("does not accept input from this Mac", self.alerts[-1])
def test_nothing_is_taken_while_the_link_is_down(self):
self.assertFalse(self.controller.set_redirecting(True))
self.assertTrue(self.alerts)
def test_sending_off_refuses_the_take(self):
self.up()
self.controller.cfg.send_to_windows = False
self.assertFalse(self.controller.set_redirecting(True))
self.assertEqual(self.link.posted.count(None), 0)
self.assertEqual(self.focus_count(), 0)
def focus_count(self):
return len([m for m in self.link.posted if m["type"] == protocol.MSG_FOCUS])
class ModifierNamesTests(Base):
def send(self, key, **link):
self.up(**link)
self.controller.set_redirecting(True)
self.link.sent.clear()
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": key}})
return self.link.sent[-1]["data"]["key"] if self.link.sent else None
def test_command_goes_to_a_pc_as_control_and_control_as_the_windows_key(self):
self.assertEqual(self.send("cmd"), "ctrl")
self.link.sent.clear()
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "ctrl"}})
self.assertEqual(self.link.sent[-1]["data"]["key"], "cmd")
def test_a_mac_peer_gets_every_key_as_itself(self):
self.assertEqual(self.send("cmd", platform="macos"), "cmd")
def test_the_positional_style_keeps_each_key_in_place(self):
self.controller.cfg.key_map = {"alt": "alt", "alt_r": "alt", "ctrl": "ctrl", "ctrl_r": "ctrl", "cmd": "cmd", "cmd_r": "cmd"}
self.assertEqual(self.send("cmd"), "cmd")
def test_a_media_key_is_dropped_for_a_peer_that_does_not_act_on_it(self):
self.assertIsNone(self.send("volume_up", caps=("clipboard",)))
def test_a_media_key_goes_to_a_peer_that_does(self):
self.assertEqual(self.send("volume_up"), "volume_up")
class _Gesture:
def __init__(self, magnification=0.0, delta_x=0.0):
self.magnification, self.deltaX, self.deltaY, self.phase = magnification, delta_x, 0.0, 0
def _wait(predicate, timeout=2.0):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if predicate():
return True
time.sleep(0.005)
return predicate()
class SlowPasteboardTests(Base):
"""Reading the pasteboard can take seconds (a large image, or data an app promised lazily), and
the event tap takes the owner's lock, so the read never holds it; input that follows a take
still reaches the machine after the take's clipboard (WIRE.md section 5)."""
def setUp(self):
super().setUp()
self.up()
self.order = []
post, send = self.link.post, self.link.send_input
self.link.post = lambda message: self.order.append(message["type"]) or post(message)
self.link.send_input = lambda message: self.order.append(message["type"]) or send(message)
self.reading, self.release = threading.Event(), threading.Event()
# One pasteboard, as clipboard_mac's lock makes it: a write waits for a read in progress.
board = threading.Lock()
write = self.clipboard.set_contents
def slow():
with board:
self.reading.set()
self.release.wait(5)
return self.clipboard.text, None
def written(text, image):
with board:
return write(text, image)
self.clipboard.get_contents = slow
self.clipboard.set_contents = written
self.addCleanup(self.release.set)
def test_a_clipboard_written_here_during_the_read_holds_no_lock_the_event_tap_takes(self):
from core import owner as owner_module
self.take()
writer = threading.Thread(target=self.controller._step, daemon=True,
args=(lambda owner: [owner_module.SetClipboard(protocol.clipboard_msg("from there"))],))
writer.start()
writer.join(1)
self.assertFalse(writer.is_alive(), "the write waited for the read")
self.assertTrue(self.controller._owner_lock.acquire(timeout=1), "the owner's lock is held through the write")
self.controller._owner_lock.release()
self.release.set()
settle(self.controller)
self.assertEqual(self.clipboard.set_calls, [("from there", None)])
def take(self):
taker = threading.Thread(target=self.controller.set_redirecting, args=(True,), daemon=True)
taker.start()
self.assertTrue(self.reading.wait(2), "the take never read the pasteboard")
taker.join(1)
self.assertFalse(taker.is_alive(), "the take waited for the pasteboard")
def test_the_read_holds_no_lock_the_event_tap_takes(self):
self.take()
self.assertTrue(self.controller._owner_lock.acquire(timeout=1), "the owner's lock is held through the read")
self.controller._owner_lock.release()
def test_input_after_the_take_waits_behind_the_clipboard(self):
self.take()
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}})
self.assertNotIn(protocol.MSG_KEYDOWN, self.order)
self.release.set()
self.assertTrue(_wait(lambda: protocol.MSG_KEYDOWN in self.order), self.order)
self.assertLess(self.order.index(protocol.MSG_CLIPBOARD), self.order.index(protocol.MSG_KEYDOWN))
def test_a_read_asked_for_again_waits_its_turn_behind_a_write_decided_before_it(self):
from core import owner as owner_module
self.take()
arrived = protocol.clipboard_msg("from there")
key = {"type": protocol.MSG_KEYDOWN, "data": {"key": "v"}}
decide = lambda actions: threading.Thread(target=self.controller._step, args=(lambda owner: actions,), daemon=True)
for step in (decide([owner_module.SetClipboard(arrived)]),
decide([owner_module.SendClipboard(self.peer), owner_module.Send(self.peer, key)])):
step.start()
step.join(1)
self.release.set()
settle(self.controller)
clipboards = [m["data"]["text"] for m in self.link.posted if m["type"] == protocol.MSG_CLIPBOARD]
self.assertEqual(clipboards, ["hello", "from there"])
self.assertEqual(self.order[-3:], [protocol.MSG_CLIPBOARD, protocol.MSG_CLIPBOARD, protocol.MSG_KEYDOWN])
def test_a_link_lost_during_the_read_drops_what_waited_and_a_retake_sends_the_clipboard_after_its_focus(self):
self.take()
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}})
self.link.down(self.controller)
self.assertFalse(self.controller.redirecting)
self.up()
self.order.clear()
threading.Thread(target=self.controller.set_redirecting, args=(True,), daemon=True).start()
self.assertTrue(_wait(lambda: protocol.MSG_FOCUS in self.order), self.order)
self.release.set()
settle(self.controller)
self.assertEqual(self.order, [protocol.MSG_FOCUS, protocol.MSG_CLIPBOARD])
def test_stopping_during_the_read_skips_the_clipboard_and_still_lets_the_machine_go(self):
self.take()
threading.Timer(0.2, self.release.set).start()
self.controller.stop()
self.assertNotIn(protocol.MSG_CLIPBOARD, self.order)
let_go = [m for m in self.link.posted if m["type"] == protocol.MSG_FOCUS][-1]["data"]
self.assertEqual(let_go["target"], self.own)
def test_pointer_moves_made_during_the_read_go_one_by_one_as_made(self):
# Never summed: the far machine's edge feels each move where its pointer is (return_edge).
self.take()
for dx, dy in ((3, 1), (4, -2), (5, 0)):
self.controller._process_outbound({"type": protocol.MSG_MOUSEMOVE, "data": {"dx": dx, "dy": dy}})
self.release.set()
settle(self.controller)
moves = [m["data"] for m in self.link.sent if m["type"] == protocol.MSG_MOUSEMOVE]
self.assertEqual(moves, [{"dx": 3, "dy": 1}, {"dx": 4, "dy": -2}, {"dx": 5, "dy": 0}])
def test_input_flows_straight_through_once_the_clipboard_is_sent(self):
self.take()
self.release.set()
self.assertTrue(_wait(lambda: protocol.MSG_CLIPBOARD in self.order))
self.assertTrue(_wait(lambda: not self.controller._clip_wait))
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}})
self.assertEqual(self.order[-1], protocol.MSG_KEYDOWN)
class GestureShapeTests(Base):
"""A pinch or a page swipe goes as the chord the machine it reaches answers to, decided when it
is sent to that machine, not when it was captured."""
def gesture(self, event_type, view, **link):
self.up(**link)
self.controller.set_redirecting(True)
self.link.sent.clear()
self.controller.handle_overlay_gesture(event_type, view)
while not self.controller.outbound.empty():
self.controller._process_outbound(self.controller.outbound.get_nowait())
return [(m["type"], m["data"].get("key")) for m in self.link.sent]
def test_a_page_swipe_to_a_mac_is_command_bracket(self):
self.assertEqual(self.gesture(31, _Gesture(delta_x=1), platform="macos"), [
(protocol.MSG_KEYDOWN, "cmd"), (protocol.MSG_KEYDOWN, "["), (protocol.MSG_KEYUP, "["), (protocol.MSG_KEYUP, "cmd")])
def test_a_page_swipe_to_a_pc_is_alt_arrow(self):
self.assertEqual(self.gesture(31, _Gesture(delta_x=1)), [
(protocol.MSG_KEYDOWN, "alt"), (protocol.MSG_KEYDOWN, "left"), (protocol.MSG_KEYUP, "left"), (protocol.MSG_KEYUP, "alt")])
def test_a_pinch_to_a_mac_is_command_plus_and_never_a_wheel(self):
sent = self.gesture(30, _Gesture(magnification=0.06), platform="macos")
self.assertEqual(sent[:2], [(protocol.MSG_KEYDOWN, "cmd"), (protocol.MSG_KEYDOWN, "=")])
self.assertNotIn(protocol.MSG_SCROLL, [kind for kind, _ in sent])
def test_a_pinch_to_a_pc_is_control_wheel(self):
sent = self.gesture(30, _Gesture(magnification=0.06))
self.assertEqual([kind for kind, _ in sent], [protocol.MSG_KEYDOWN, protocol.MSG_SCROLL, protocol.MSG_KEYUP])
self.assertEqual(sent[0][1], "ctrl")
class GestureThroughAHandOverTests(Base):
"""A gesture made while the PC hands the input on to a Mac is held with the rest of the input and
goes to the Mac as the Mac's chord: the chord is chosen when it is sent, not when it was made."""
def setUp(self):
super().setUp()
self.up()
with self.controller.book.lock:
peers = self.controller.book.data["peers"]
peers.append({**peers[0], "id": protocol.id_text(OTHER_ID), "token": "second", "send": True})
self.controller._sync_links()
self.mac = self.controller.links["second"]
self.mac.up(self.controller, ident=OTHER_ID, name="Laptop", platform="macos")
self.controller.set_redirecting(True)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.link.arrives(self.controller, protocol.switch_v6(1, OTHER_ID))
self.link.sent.clear()
def pinch(self):
self.controller.handle_overlay_gesture(30, _Gesture(magnification=0.06))
while not self.controller.outbound.empty():
self.controller._process_outbound(self.controller.outbound.get_nowait())
def test_the_pinch_waits_for_the_mac_and_reaches_it_as_command_plus(self):
self.pinch()
self.assertEqual(self.mac.sent, [])
self.mac.arrives(self.controller, protocol.accept_msg(self.controller.owner.route))
settle(self.controller)
self.assertEqual([(m["type"], m["data"].get("key")) for m in self.mac.sent], [
(protocol.MSG_KEYDOWN, "cmd"), (protocol.MSG_KEYDOWN, "="), (protocol.MSG_KEYUP, "="), (protocol.MSG_KEYUP, "cmd")])
self.assertEqual(self.link.sent, [])
class AbandonedHandOverTests(Base):
"""A hand-over given up before its answer closes the link to the machine asked, whose accept may
still be on the way (WIRE.md section 5); the owner's tests cover every way it is given up."""
def test_the_shortcut_home_while_the_next_machine_is_asked_closes_its_link(self):
self.up()
with self.controller.book.lock:
peers = self.controller.book.data["peers"]
peers.append({**peers[0], "id": protocol.id_text(OTHER_ID), "token": "second", "send": True})
self.controller._sync_links()
mac = self.controller.links["second"]
mac.up(self.controller, ident=OTHER_ID, name="Laptop", platform="macos")
self.controller.set_redirecting(True)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.link.arrives(self.controller, protocol.switch_v6(1, OTHER_ID))
self.assertEqual(mac.dropped, [])
self.controller.set_redirecting(False)
settle(self.controller)
self.assertEqual(len(mac.dropped), 1)
self.assertEqual(self.link.dropped, [])
self.assertFalse(self.controller.redirecting)
class ChordCutShortTests(Base):
def test_a_link_whose_queue_fills_mid_chord_is_dropped_rather_than_left_holding_a_modifier(self):
self.up(platform="macos")
self.controller.set_redirecting(True)
self.link.sent.clear()
send = self.link.send_input
self.link.send_input = lambda message: not self.link.sent and send(message)
self.controller.handle_overlay_gesture(31, _Gesture(delta_x=1))
while not self.controller.outbound.empty():
self.controller._process_outbound(self.controller.outbound.get_nowait())
self.assertEqual([m["data"]["key"] for m in self.link.sent], ["cmd"])
self.assertEqual(self.link.dropped, ["The outbound queue filled"])
class ComingHomeTests(Base):
def setUp(self):
super().setUp()
self.up()
self.controller.set_redirecting(True, edge="left", offset=0.5)
self.link.posted.clear()
def arrives(self, message):
self.link.arrives(self.controller, message)
def test_a_takeover_comes_home_and_names_the_new_driver(self):
peer = dict(self.controller.book.data["peers"][0], id=protocol.id_text(OTHER_ID),
token="second-peer", name="Desk-PC")
self.controller.book.data["peers"].append(peer)
message = protocol.switch_v6(1, protocol.read_id(self.own))
message["data"]["taken_over_by"] = protocol.id_text(OTHER_ID)
self.controller.crossing.touching = True
self.controller.crossing.pressure = 10
self.arrives(message)
self.assertFalse(self.controller.redirecting)
self.assertEqual(self.alerts, ["Desk-PC took over Office PC"])
self.assertEqual(FakeQuartz.associate_calls[-1], True)
self.assertFalse(self.controller.pointer._hidden)
self.assertFalse(self.controller.crossing.touching)
self.assertEqual(self.controller.crossing.pressure, 0)
def test_a_takeover_by_an_unknown_machine_uses_the_fallback_name(self):
message = protocol.switch_v6(1, protocol.read_id(self.own))
message["data"]["taken_over_by"] = protocol.id_text(OTHER_ID)
self.arrives(message)
self.assertEqual(self.alerts, ["Another machine took over Office PC"])
def test_the_peer_sending_input_home_lands_the_pointer_where_it_left(self):
self.arrives(protocol.switch_v6(1, protocol.read_id(self.own), "right", 0.25))
self.assertFalse(self.controller.redirecting)
self.assertEqual([kind for kind, _ in self.crossings], ["arrive"])
self.assertEqual(self.crossings[0][1].mac_edge, "right")
self.assertTrue(FakeQuartz.warp_calls)
let_go = self.focus()
self.assertEqual(let_go["target"], self.own)
self.assertEqual(self.alerts, [])
def test_the_log_names_the_edge_input_comes_home_at(self):
with self.assertLogs(self.controller.logger, level="INFO") as said:
self.arrives(protocol.switch_v6(1, protocol.read_id(self.own), "right", 0.25))
moves = [line for line in said.output if "move applied:" in line]
self.assertTrue(moves and "at the right edge" in moves[-1], said.output)
def test_a_send_home_without_a_position_still_returns_input_and_shows_the_arrival(self):
self.arrives(protocol.switch_v6(1, protocol.read_id(self.own)))
self.assertFalse(self.controller.redirecting)
self.assertEqual([(kind, step.mac_edge) for kind, step in self.crossings], [("arrive", None)])
def test_a_refusal_for_the_current_route_sends_input_home_with_the_reason(self):
self.arrives(protocol.refuse_msg(1, "owned"))
self.assertFalse(self.controller.redirecting)
self.assertEqual(self.alerts, ["Can't cross to Office PC: another machine is driving it."])
def test_a_refusal_names_the_other_machine_that_owns_the_responder(self):
peer = dict(self.controller.book.data["peers"][0], id=protocol.id_text(OTHER_ID),
token="second-peer", name="Desk-PC")
self.controller.book.data["peers"].append(peer)
self.arrives(protocol.refuse_msg(1, "owned", other=OTHER_ID))
self.assertEqual(self.alerts, ["Can't cross to Office PC: it is being driven from Desk-PC."])
def test_a_refusal_for_an_old_route_is_ignored(self):
self.arrives(protocol.accept_msg(1))
self.controller.owner.route = 5
self.arrives(protocol.refuse_msg(1, "busy"))
self.assertTrue(self.controller.redirecting)
def test_losing_the_link_brings_input_home(self):
self.link.down(self.controller)
self.assertFalse(self.controller.redirecting)
self.assertEqual(self.alerts, ["Lost the link to Office PC"])
self.assertEqual(FakeQuartz.associate_calls[-1], True)
def test_no_answer_within_a_second_brings_input_home(self):
self.clock.value += 1.2
self.controller._owner_tick()
self.assertFalse(self.controller.redirecting)
self.assertEqual(self.alerts, ["Office PC did not answer"])
def test_an_accept_keeps_input_there(self):
self.arrives(protocol.accept_msg(1))
self.clock.value += 5
self.controller._owner_tick()
self.assertTrue(self.controller.redirecting)
def test_a_stale_switch_is_ignored(self):
self.arrives(protocol.accept_msg(1))
self.controller.owner.route = 6
self.arrives(protocol.switch_v6(5, OTHER_ID))
self.assertTrue(self.controller.redirecting)
class ClipboardTests(Base):
def test_unsendable_clipboards_are_read_once_until_the_next_local_copy(self):
for text, image, caps in ((None, None, CAPS), ("", None, CAPS),
("x" * (protocol.CLIPBOARD_MAX_BYTES + 1), None, CAPS),
(None, b"not a PNG", CAPS), ("unsupported", None, ())):
with self.subTest(text_length=len(text or ""), image=image, caps=caps):
self.setUp()
self.up(caps=caps)
self.clipboard.copy(text)
self.clipboard.image = image
with mock.patch.object(self.clipboard, "get_contents", wraps=self.clipboard.get_contents) as read:
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(self.controller.owner.route))
self.controller.set_redirecting(False)
settle(self.controller)
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(self.controller.owner.route))
self.assertEqual(read.call_count, 1)
self.assertNotIn(protocol.MSG_CLIPBOARD, self.link.types())
self.clipboard.copy("new copy")
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(read.call_count, 2)
if "clipboard" in caps:
self.assertEqual(self.link.posted[-1], protocol.clipboard_msg("new copy"))
def test_a_read_finishing_after_home_never_posts_private_local_text(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.link.posted.clear()
self.clipboard.copy("before home")
def copied_after_home():
self.controller.set_redirecting(False)
self.clipboard.copy("private local text")
return self.clipboard.text, None
with mock.patch.object(self.clipboard, "get_contents", side_effect=copied_after_home) as read:
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(read.call_count, 1)
self.assertNotIn(protocol.MSG_CLIPBOARD, self.link.types())
def test_a_source_retry_stops_after_home_even_if_the_stamp_is_unchanged(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.link.posted.clear()
self.clipboard.copy("before home")
stamp = self.clipboard.stamp
def unavailable_after_home():
self.controller.set_redirecting(False)
return None, None
with mock.patch.object(self.clipboard, "get_contents", side_effect=unavailable_after_home) as read:
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(read.call_count, 1)
self.assertEqual(self.clipboard.stamp, stamp)
self.assertNotIn(protocol.MSG_CLIPBOARD, self.link.types())
def test_copies_while_the_panel_is_hidden_reach_the_active_peer(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
for text in ("first", "日本語 — café 𝄞", "large " * 20000):
self.clock.value += 0.1
self.clipboard.copy(text)
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(self.link.posted[-1], protocol.clipboard_msg(text))
def test_a_transient_read_or_post_failure_recovers_without_another_crossing(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
for failure in ("read_exception", "post", "post_exception"):
with self.subTest(failure=failure):
text = "new local copy: " + failure
self.clipboard.copy(text)
read, post = self.clipboard.get_contents, self.link.post
calls = []
def attempt(*args):
calls.append(True)
if len(calls) == 1:
if failure.endswith("exception"):
raise OSError("clipboard temporarily busy")
return False
return read() if failure.startswith("read") else post(*args)
target, name = (self.clipboard, "get_contents") if failure.startswith("read") else (self.link, "post")
with mock.patch.object(target, name, side_effect=attempt):
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(len(calls), 2)
self.assertEqual(self.link.posted[-1], protocol.clipboard_msg(text))
def test_a_persistent_source_read_failure_is_bounded_and_releases_input(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.clipboard.copy("temporarily unreadable")
with mock.patch.object(self.clipboard, "get_contents", side_effect=OSError("clipboard busy")) as read:
self.controller._owner_tick()
self.controller._step(lambda owner: owner.input({"type": "keydown", "data": {"key": "a"}}))
settle(self.controller)
self.assertEqual(read.call_count, 3)
self.assertEqual(self.link.sent[-1]["type"], protocol.MSG_KEYDOWN)
def test_a_source_retry_never_reads_a_newer_copy_for_the_old_attempt(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.clipboard.copy("first generation")
def changed_while_busy():
self.clipboard.copy("new generation")
return None, None
with mock.patch.object(self.clipboard, "get_contents", side_effect=changed_while_busy) as read:
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(read.call_count, 1)
self.controller._owner_tick()
settle(self.controller)
self.assertEqual(self.link.posted[-1], protocol.clipboard_msg("new generation"))
def test_an_inbound_write_exception_is_retried(self):
write = self.clipboard.set_contents
calls = []
def temporarily_busy(text, image):
calls.append(text)
if len(calls) == 1:
raise OSError("clipboard temporarily busy")
return write(text, image)
with mock.patch.object(self.clipboard, "set_contents", side_effect=temporarily_busy):
self.controller._set_clipboard(protocol.clipboard_msg("remote copy"))
self.assertEqual(calls, ["remote copy", "remote copy"])
self.assertEqual(self.clipboard.text, "remote copy")
def test_a_copy_completed_before_accept_is_forwarded_on_accept(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.clipboard.copy("copy completed after the crossing")
self.link.arrives(self.controller, protocol.accept_msg(1))
settle(self.controller)
self.assertEqual(self.link.posted[-1], protocol.clipboard_msg(self.clipboard.text))
def test_a_failed_read_is_tried_again_on_the_next_crossing(self):
self.up()
read = self.clipboard.get_contents
self.clipboard.get_contents = mock.Mock(side_effect=OSError("clipboard busy"))
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.controller.set_redirecting(False)
self.clipboard.get_contents = read
self.link.posted.clear()
self.controller.set_redirecting(True)
settle(self.controller)
self.assertIn(protocol.clipboard_msg("hello"), self.link.posted)
def test_a_failed_post_is_tried_again_on_the_next_crossing(self):
self.up()
post = self.link.post
self.link.post = lambda message: False if message["type"] == protocol.MSG_CLIPBOARD else post(message)
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.controller.set_redirecting(False)
self.link.post = post
self.link.posted.clear()
self.controller.set_redirecting(True)
settle(self.controller)
self.assertIn(protocol.clipboard_msg("hello"), self.link.posted)
def test_failed_inbound_writes_do_not_swallow_a_local_copy(self):
self.up()
self.controller._note_clipboard()
before = self.controller._clipboard_stamp
self.clipboard.copy("local copy")
self.clipboard.set_contents = lambda text, image: False
self.controller._set_clipboard(protocol.clipboard_msg("remote copy"))
self.assertEqual(self.controller._clipboard_stamp, before)
def test_an_inbound_write_retry_keeps_a_newer_local_copy(self):
self.up()
calls = []
def copied_while_busy(text, image):
calls.append(text)
self.clipboard.copy("newer local copy")
return False
self.clipboard.set_contents = copied_while_busy
self.controller._set_clipboard(protocol.clipboard_msg("remote copy"))
self.assertEqual(calls, ["remote copy"])
self.assertEqual(self.clipboard.text, "newer local copy")
def test_a_copy_during_a_peer_write_is_not_mistaken_for_the_peer_write(self):
self.up()
self.controller._note_clipboard()
write_stamp = self.clipboard.stamp + 1
self.clipboard.write_stamp = lambda: write_stamp
write = self.clipboard.set_contents
def copied_after_write(text, image):
result = write(text, image)
self.clipboard.copy("new local copy")
return result
self.clipboard.set_contents = copied_after_write
self.controller._set_clipboard(protocol.clipboard_msg("remote copy"))
self.assertEqual(self.controller._clipboard_stamp, write_stamp)
self.assertTrue(self.controller._note_clipboard())
def test_a_clipboard_from_the_machine_just_let_go_is_taken_once(self):
self.up()
self.controller.set_redirecting(True)
self.controller.set_redirecting(False)
self.link.arrives(self.controller, protocol.clipboard_msg("from the pc"), began=self.clock.value)
self.assertEqual(self.clipboard.set_calls, [("from the pc", None)])
self.link.arrives(self.controller, protocol.clipboard_msg("again"), began=self.clock.value)
self.assertEqual(len(self.clipboard.set_calls), 1)
def test_a_clipboard_from_a_machine_never_taken_is_dropped(self):
self.up()
self.link.arrives(self.controller, protocol.clipboard_msg("unasked"), began=self.clock.value)
self.assertEqual(self.clipboard.set_calls, [])
def test_a_clipboard_that_began_arriving_too_late_is_dropped(self):
self.up()
self.controller.set_redirecting(True)
self.controller.set_redirecting(False)
self.link.arrives(self.controller, protocol.clipboard_msg("late"), began=self.clock.value + 11)
self.assertEqual(self.clipboard.set_calls, [])
def test_the_clipboard_is_sent_again_only_after_it_changed_here(self):
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.link.arrives(self.controller, protocol.accept_msg(1))
self.controller.set_redirecting(False)
self.link.posted.clear()
self.controller.set_redirecting(True)
settle(self.controller)
self.assertNotIn(protocol.MSG_CLIPBOARD, self.link.types())
self.link.arrives(self.controller, protocol.accept_msg(3))
self.controller.set_redirecting(False)
self.clipboard.copy("something new")
self.link.posted.clear()
self.controller.set_redirecting(True)
settle(self.controller)
self.assertIn(protocol.MSG_CLIPBOARD, self.link.types())
def test_an_oversized_text_is_skipped_but_the_take_still_goes(self):
self.clipboard.text = "x" * (protocol.CLIPBOARD_MAX_BYTES + 1)
self.up()
self.controller.set_redirecting(True)
settle(self.controller)
self.assertNotIn(protocol.MSG_CLIPBOARD, self.link.types())
self.assertEqual(self.focus_count(), 1)
def arrivals(self):
"""What the owner is handed when a clipboard arrives from the machine just let go."""
taken = []
self.controller.owner.clipboard_arrived = lambda peer, began, message: taken.append(message) or []
return taken
def test_a_clipboard_with_a_text_that_is_not_a_string_goes_nowhere(self):
self.up()
taken = self.arrivals()
self.link.arrives(self.controller, {"type": protocol.MSG_CLIPBOARD, "data": {"text": 5}}, began=self.clock.value)
self.assertEqual(taken, [])
def test_the_clipboard_goes_before_input_the_same_move_hands_on(self):
# WIRE.md section 5: on the next machine's accept it gets the clipboard, then the held
# input, in order. The pasteboard is read on its own thread, and what follows it for the
# same machine waits for it.
from core import owner as owner_module
self.up()
self.controller._note_clipboard()
self.controller.owner.go(self.peer)
self.controller.owner.accept(self.peer, {"route": self.controller.owner.route})
order = []
post, send_input = self.link.post, self.link.send_input
self.link.post = lambda message: order.append(message["type"]) or post(message)
self.link.send_input = lambda message: order.append(message["type"]) or send_input(message)
key = {"type": protocol.MSG_KEYDOWN, "data": {"key": "v"}}
self.controller._step(lambda owner: [owner_module.SendClipboard(self.peer), owner_module.Send(self.peer, key)])
self.assertTrue(_wait(lambda: len(order) == 2), order)
self.assertEqual(order, [protocol.MSG_CLIPBOARD, protocol.MSG_KEYDOWN])
def test_a_clipboard_is_handed_on_as_rebuilt_from_what_was_read(self):
self.up()
taken = self.arrivals()
self.link.arrives(self.controller, {"type": protocol.MSG_CLIPBOARD, "data": {"text": "hi", "z": [{}, {}]}, "extra": 1}, began=self.clock.value)
self.assertEqual(taken, [protocol.clipboard_msg("hi", None)])
def focus_count(self):
return len([m for m in self.link.posted if m["type"] == protocol.MSG_FOCUS])
class AnnouncementTests(Base):
def test_the_link_up_announces_the_side_the_settings_and_who_else_is_paired(self):
entry = self.controller.book.peers()[0]
with self.controller.book.lock:
self.controller.book.data["peers"][0].update(side="left", side_set_at=7, id=self.peer)
self.controller.book.data["peers"].append({**entry, "id": protocol.id_text(OTHER_ID), "token": "other", "send": False})
inactive_id = bytes([33]) * 16
self.controller.book.data["peers"].append({**entry, "id": protocol.id_text(inactive_id), "token": "inactive", "in_use": False})
self.controller.announce = lambda: [protocol.settings_msg({"on": False, "set_at": 3, "by": ""})]
self.up()
types = self.link.types()
self.assertIn(protocol.MSG_ARRANGEMENT, types)
self.assertIn(protocol.MSG_SETTINGS, types)
paired = next(m for m in self.link.posted if m["type"] == protocol.MSG_PAIRED)
self.assertEqual(paired["data"]["ids"], [protocol.id_text(OTHER_ID)])
arrangement = next(m for m in self.link.posted if m["type"] == protocol.MSG_ARRANGEMENT)
self.assertEqual((arrangement["data"]["edge"], arrangement["data"]["set_at"]), ("left", 7))
def test_the_available_pairing_ids_skip_machines_not_in_use(self):
entry = self.controller.book.peers()[0]
self.controller.book.data["peers"][0]["id"] = self.peer
inactive_id = protocol.id_text(bytes([33]) * 16)
self.controller.book.data["peers"].append({**entry, "id": inactive_id, "token": "inactive", "in_use": False})
self.assertEqual(self.controller._pairing_ids(), (self.peer,))
def test_settings_are_not_announced_to_a_peer_that_does_not_keep_them(self):
self.controller.announce = lambda: [protocol.settings_msg({"on": False, "set_at": 3, "by": ""})]
self.up(caps=("clipboard",))
self.assertNotIn(protocol.MSG_SETTINGS, self.link.types())
def test_an_arrangement_from_the_peer_reaches_the_app_as_this_macs_edge(self):
seen = []
self.controller.on_arrangement = lambda *args: seen.append(args)
with self.controller.book.lock:
self.controller.book.data["peers"][0]["id"] = self.peer
self.up()
self.link.arrives(self.controller, protocol.arrangement_v6("right", int(time.time()) - 5, PEER_ID))
self.assertEqual(seen, [(self.peer, "right", seen[0][2], self.peer, None)])
def test_a_peers_settings_reach_the_app_with_who_sent_them(self):
seen = []
self.controller.on_settings = lambda data, peer: seen.append((data, peer))
self.up()
self.link.arrives(self.controller, protocol.settings_msg({"on": True, "set_at": 5}))
self.assertEqual(seen, [({"on": True, "set_at": 5}, self.peer)])
def test_settings_go_on_to_every_peer_but_the_one_they_came_from(self):
self.up()
self.controller.send_settings({"on": True, "set_at": 9}, source=self.peer)
self.assertNotIn(protocol.MSG_SETTINGS, self.link.types())
self.controller.send_settings({"on": True, "set_at": 9})
self.assertIn(protocol.MSG_SETTINGS, self.link.types())
def test_an_arrangement_made_here_goes_to_the_machine_it_is_for(self):
self.up()
self.assertTrue(self.controller.send_arrangement(self.peer, "top", 12))
self.assertFalse(self.controller.send_arrangement(protocol.id_text(OTHER_ID), "top", 13))
sent = [m for m in self.link.posted if m["type"] == protocol.MSG_ARRANGEMENT][-1]["data"]
self.assertEqual((sent["edge"], sent["set_at"], sent["by"]), ("top", 12, self.own))
class DrivenTests(Base):
def test_a_driven_mac_sends_its_owner_home_then_takes_once_it_has_let_go(self):
self.up()
homes = []
def send_home():
homes.append(True)
threading.Timer(0.1, lambda: self.controller.set_receiving(False)).start()
return True
self.controller.send_peer_home = send_home
self.controller.set_receiving(True)
self.assertTrue(self.controller.set_redirecting(True))
self.assertEqual(homes, [True])
self.assertFalse(self.controller.redirecting)
deadline = time.monotonic() + 2
while not self.controller.redirecting and time.monotonic() < deadline:
time.sleep(0.01)
self.assertTrue(self.controller.redirecting)
def test_a_driven_mac_whose_owner_cannot_be_reached_does_not_take(self):
self.up()
self.controller.send_peer_home = lambda: False
self.controller.set_receiving(True)
self.assertFalse(self.controller.set_redirecting(True))
def test_a_take_that_won_the_race_sends_this_macs_input_home(self):
self.up()
self.controller.responder = type("Driven", (), {"driven": True})()
self.assertFalse(self.controller.set_redirecting(True))
self.assertFalse(self.controller.redirecting)
self.assertEqual(self.focus_count(), 2)
def focus_count(self):
return len([m for m in self.link.posted if m["type"] == protocol.MSG_FOCUS])
class LinkSetTests(Base):
def test_one_link_per_peer_with_send_on(self):
self.assertEqual(len(self.controller.links), 1)
with self.controller.book.lock:
peers = self.controller.book.data["peers"]
peers.append({**peers[0], "id": protocol.id_text(OTHER_ID), "token": "second", "send": False})
self.controller._sync_links()
self.assertEqual(len(self.controller.links), 1)
def test_a_new_token_makes_a_new_link_and_ends_the_old(self):
old = self.link
self.controller.update_config(make_config("a-different-token"))
self.assertTrue(old.stopped)
self.assertIsNot(self.controller._primary_link, old)
def test_removing_the_peer_ends_its_link_and_brings_input_home(self):
self.up()
self.controller.set_redirecting(True)
with self.controller.book.lock:
self.controller.book.data["peers"].clear()
self.controller._sync_links()
self.assertFalse(self.controller.redirecting)
def test_the_connection_status_follows_the_primary_link(self):
self.controller._link_state(self.link, False, "Office PC is unreachable from this machine.")
self.assertEqual(self.controller.connection_status, "Office PC is unreachable from this machine.")
def test_a_locked_peer_is_called_unlocking(self):
self.up()
self.link.peer_locked = True
self.assertTrue(self.controller.windows_locked)
self.assertEqual(self.controller.connection_status, "Unlocking 192.0.2.10…")
def test_the_round_trip_is_the_upper_median_of_the_last_trips_on_the_machine_input_is_on(self):
self.up()
self.assertIsNone(self.controller.round_trip_ms)
self.controller.set_redirecting(True)
self.link.samples = [0.010, 0.030, 0.020, 0.040]
self.assertEqual(self.controller.round_trip_ms, 30)
def test_the_beacons_are_offered_to_every_link(self):
beacons = [{"name": "Office PC", "address": "192.0.2.77"}]
self.controller.discovery = type("Found", (), {"pcs": lambda self: beacons})()
self.controller._follow_the_peers()
self.assertEqual(self.link.followed, [beacons])
def test_a_learnt_address_is_handed_to_the_app(self):
learnt = []
self.controller.on_host_learned = learnt.append
with self.controller.book.lock:
self.controller.book.data["peers"][0]["host"] = "192.0.2.99"
self.up()
self.assertEqual(learnt, ["192.0.2.99"])
self.assertEqual(self.controller.cfg.host, "192.0.2.99")
class PressedAtHomeTests(Base):
"""A key pressed while the input was home is released at home (WIRE.md section 4)."""
def tap(self, kind, keycode=0x00, character="a"):
event = {FakeQuartz.kCGKeyboardEventKeycode: keycode, FakeQuartz.kCGKeyboardEventAutorepeat: 0, "unicode": character}
returned = self.controller._event_tap_callback(None, kind, event, None)
return event, returned
def test_the_release_of_a_key_pressed_at_home_stays_at_home(self):
self.up()
event, returned = self.tap(FakeQuartz.kCGEventKeyDown)
self.assertIs(returned, event)
self.controller.set_redirecting(True)
event, returned = self.tap(FakeQuartz.kCGEventKeyUp)
self.assertIs(returned, event)
self.assertTrue(self.controller.outbound.empty())
def test_a_key_pressed_after_the_input_left_is_sent_and_released_there(self):
self.up()
self.controller.set_redirecting(True)
_, down = self.tap(FakeQuartz.kCGEventKeyDown)
_, up = self.tap(FakeQuartz.kCGEventKeyUp)
self.assertIsNone(down)
self.assertIsNone(up)
self.assertEqual([m["type"] for m in list(self.controller.outbound.queue)], [protocol.MSG_KEYDOWN, protocol.MSG_KEYUP])
def test_a_key_pressed_and_released_at_home_is_not_remembered(self):
self.up()
self.tap(FakeQuartz.kCGEventKeyDown)
self.tap(FakeQuartz.kCGEventKeyUp)
self.controller.set_redirecting(True)
_, down = self.tap(FakeQuartz.kCGEventKeyDown)
_, up = self.tap(FakeQuartz.kCGEventKeyUp)
self.assertIsNone(down)
self.assertIsNone(up)
self.assertEqual(self.controller.outbound.qsize(), 2)
class GestureNamesTests(Base):
def test_a_pinch_reaches_a_pc_as_control_and_scroll_not_as_the_windows_key(self):
from gestures import MAGNIFY_TYPE
from bridge_fakes import FakeGestureEvent
self.up()
self.controller.set_redirecting(True)
self.controller.handle_overlay_gesture(MAGNIFY_TYPE, FakeGestureEvent(magnification=0.05))
while not self.controller.outbound.empty():
self.controller._process_outbound(self.controller.outbound.get_nowait())
keys = [m["data"]["key"] for m in self.link.sent if m["type"] in (protocol.MSG_KEYDOWN, protocol.MSG_KEYUP)]
self.assertEqual(keys, ["ctrl", "ctrl"])
class UnreachableWordsTests(Base):
def test_a_hand_over_refused_as_owned_says_so_rather_than_could_not_be_reached(self):
# Opus review: owned and busy left WHY_TEXT, so an onward refusal fell back to "could not be reached".
from core.owner import Unreachable
self.up()
self.controller._on_unreachable(Unreachable(self.peer, "owned", OTHER_ID))
self.assertEqual(self.alerts, ["Can't cross to Office PC: another machine is driving it."])
class ItsOwnKeyboardTests(Base):
"""beta.4 on 02-10-2026: the Mac's tap kept swallowing every key for nine minutes while its owner
said input was home, and the outbound worker dropped each one without a word. A Mac that thinks
it is redirecting never keeps its keyboard while its input is home."""
def tap(self, kind, keycode=0x00, character="a"):
event = {FakeQuartz.kCGKeyboardEventKeycode: keycode, FakeQuartz.kCGKeyboardEventAutorepeat: 0, "unicode": character}
return event, self.controller._event_tap_callback(None, kind, event, None)
def diverge(self):
"""The state the Mac was found in: redirecting, with the owner at home."""
self.up()
self.controller._redirecting = True
self.assertFalse(self.controller.owner.away)
def test_a_key_while_the_owner_is_home_reaches_this_mac_and_the_state_is_put_right(self):
self.diverge()
event, returned = self.tap(FakeQuartz.kCGEventKeyDown)
self.assertIs(returned, event)
self.assertTrue(_wait(lambda: not self.controller.redirecting), "redirecting was never put right")
self.assertTrue(self.controller.outbound.empty())
# And the keyboard is this Mac's from then on.
event, returned = self.tap(FakeQuartz.kCGEventKeyUp)
self.assertIs(returned, event)
def test_the_tap_never_takes_a_lock_to_put_it_right(self):
self.diverge()
self.assertTrue(self.controller._owner_lock.acquire(timeout=1))
try:
started = time.monotonic()
event, returned = self.tap(FakeQuartz.kCGEventKeyDown)
self.assertIs(returned, event)
self.assertLess(time.monotonic() - started, 0.2, "the tap waited on the owner's lock")
finally:
self.controller._owner_lock.release()
self.assertTrue(_wait(lambda: not self.controller.redirecting))
def test_a_repair_that_finds_a_new_take_by_the_time_it_runs_leaves_it_alone(self):
# Sol's review: a repair queued in the gap of an ordinary return must not send a later take home.
self.diverge()
self.assertTrue(self.controller._owner_lock.acquire(timeout=1))
try:
self.tap(FakeQuartz.kCGEventKeyDown)
# The take that lands before the repair gets the lock.
self.controller.owner.on = self.peer
finally:
self.controller._owner_lock.release()
time.sleep(0.2)
self.assertTrue(self.controller.redirecting)
self.assertEqual(self.controller.owner.on, self.peer)
def test_the_tap_returns_the_key_even_while_logging_is_stuck(self):
self.diverge()
stuck = threading.Event()
class Stuck(logging.Handler):
def emit(self, record):
stuck.wait(3)
handler = Stuck()
self.controller.logger.addHandler(handler)
try:
started = time.monotonic()
event, returned = self.tap(FakeQuartz.kCGEventKeyDown)
self.assertIs(returned, event)
self.assertLess(time.monotonic() - started, 0.2, "the tap waited on the log")
finally:
stuck.set()
self.controller.logger.removeHandler(handler)
self.assertTrue(_wait(lambda: not self.controller.redirecting))
def test_input_that_reaches_the_worker_after_the_owner_came_home_puts_it_right_rather_than_vanishing(self):
self.diverge()
self.controller._process_outbound({"type": protocol.MSG_KEYDOWN, "data": {"key": "a"}})
self.assertTrue(_wait(lambda: not self.controller.redirecting), "the worker dropped the key and left the tap swallowing")
def test_the_disagreement_is_logged_once_with_every_threads_stack(self):
self.diverge()
with self.assertLogs(self.controller.logger, level="ERROR") as said:
self.tap(FakeQuartz.kCGEventKeyDown)
self.controller._redirecting = True
self.tap(FakeQuartz.kCGEventKeyDown)
self.assertTrue(_wait(lambda: not self.controller.redirecting))
disagreements = [line for line in said.output if "redirecting while the owner says input is home" in line]
self.assertEqual(len(disagreements), 1, said.output)
self.assertIn("Thread", disagreements[0])
def test_every_change_of_redirecting_and_every_move_applied_is_logged_with_its_thread(self):
self.up()
with self.assertLogs(self.controller.logger, level="INFO") as said:
self.controller.set_redirecting(True)
self.controller.set_redirecting(False)
here = threading.current_thread().name
changes = [line for line in said.output if "redirecting False -> True" in line or "redirecting True -> False" in line]
moves = [line for line in said.output if "move applied:" in line]
self.assertEqual(len(changes), 2, said.output)
self.assertGreaterEqual(len(moves), 2, said.output)
self.assertTrue(all(here in line for line in changes + moves), said.output)
def test_a_take_racing_a_hand_over_from_the_link_never_leaves_the_two_views_apart(self):
# The 12:29:36 to 12:29:38 race: input moved on by the peer's switch on a link thread while
# this Mac's tap took the same peer again.
self.up()
for _ in range(200):
self.controller.set_redirecting(True)
route = self.controller.owner.route
switch = threading.Thread(target=self.link.arrives, daemon=True, args=(
self.controller, protocol.switch_v6(route, protocol.read_id(self.own), "right", 0.5)))
take = threading.Thread(target=self.controller.set_redirecting, args=(True,), daemon=True)
switch.start()
take.start()
switch.join(2)
take.join(2)
settle(self.controller)
self.assertEqual(self.controller.redirecting, self.controller.owner.away)
self.controller.set_redirecting(False)
self.assertFalse(self.controller.redirecting or self.controller.owner.away)
if __name__ == "__main__":
unittest.main()