diff --git a/tools/computer-test.py b/tools/computer-test.py index 736412cd9e49863691e9686d5c71d5f1b087f2dd..fa812b14d74971cfec2b100306584dea1bd683e8 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -593,6 +593,20 @@ class DesktopBounds(unittest.IsolatedAsyncioTestCase): item.start() self.addCleanup(item.stop) + async def test_unsupported_fallback_text_cannot_partially_execute_a_batch(self): + for text in ['雪', '\x1b']: + actions = [{'type': 'click', 'x': 1, 'y': 1}, {'type': 'text', 'text': text}] + with self.subTest(text=text), self.assertRaisesRegex(ValueError, 'ASCII text only'): + await computer.desktop('fixture', actions, 'linux', False, False) + self.screen.input.assert_not_called() + + async def test_native_text_still_accepts_unicode(self): + action = {'type': 'text', 'text': '雪🦊'} + with patch.object(computer, 'worker') as worker: + result = await computer.desktop('fixture', [action], 'windows', False) + self.assertEqual(result, {'completed': 1}) + worker.assert_called_once_with('fixture', 'windows', {'action': 'input', 'actions': [action], 'screenshot': False}) + async def test_input_without_screenshot_never_encodes_or_requests_another_frame(self): with patch.object(computer.rfb, 'png', side_effect=AssertionError('No capture requested')): result = await computer.desktop('fixture', [{'type': 'click', 'x': 1, 'y': 1}], 'linux', False) diff --git a/tools/vm-computer.py b/tools/vm-computer.py index 5a7317e72151d1d6edffa630ce286e6bcfbad0b2..9de713d0f7ede8047088f2f96e9e46ca601720ed 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -176,6 +176,10 @@ async def desktop(name, actions=None, platform=None, screenshot=True, native_tex async def capture(name, actions, platform, screenshot=True, native_text=True): + if not native_text: + for action in actions or (): + if action['type'] == 'text' and any(not (32 <= ord(char) <= 126 or char in '\n\r\t') for char in action['text']): + raise ValueError('This connection supports ASCII text only. Use key actions for other keyboard input.') target = vms.console_target(name, timeout=remaining(10)) reader, writer = await asyncio.wait_for(asyncio.open_connection('127.0.0.1', target['port']), 10) screen = rfb.Desktop(reader, writer, lambda _: None) @@ -234,11 +238,8 @@ async def capture(name, actions, platform, screenshot=True, native_text=True): with contextlib.suppress(Exception): await pending else: - text = action['text'] - if any(not (32 <= ord(char) <= 126 or char in '\n\r\t') for char in text): - raise ValueError('This connection supports ASCII text only. Use key actions for other keyboard input.') shifted = dict(zip('~!@#$%^&*()_+{}|:"<>?', '`1234567890-=[]\\;\',./')) - for char in text.replace('\r\n', '\n'): + for char in action['text'].replace('\r\n', '\n'): shift = char in shifted base = shifted.get(char, char) key = {'\n': 0xff0d, '\r': 0xff0d, '\t': 0xff09}.get(base, ord(base))