| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
|
| 7 |
import dbus |
| 8 |
import pytest |
| 9 |
import socket |
| 10 |
from typing import List, Dict, Any |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture |
| 14 |
def required_templates(): |
| 15 |
return {"remotedesktop": {}} |
| 16 |
|
| 17 |
|
| 18 |
class TestRemoteDesktop: |
| 19 |
def test_version(self, portals, dbus_con): |
| 20 |
xdp.check_version(dbus_con, "RemoteDesktop", 2) |
| 21 |
|
| 22 |
def test_create_close_session(self, portals, dbus_con): |
| 23 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 24 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 25 |
|
| 26 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 27 |
options = { |
| 28 |
"session_handle_token": "session_token0", |
| 29 |
} |
| 30 |
response = request.call( |
| 31 |
"CreateSession", |
| 32 |
options=options, |
| 33 |
) |
| 34 |
|
| 35 |
assert response |
| 36 |
assert response.response == 0 |
| 37 |
|
| 38 |
session = xdp.Session.from_response(dbus_con, response) |
| 39 |
# Check the impl portal was called with the right args |
| 40 |
method_calls = mock_intf.GetMethodCalls("CreateSession") |
| 41 |
assert len(method_calls) > 0 |
| 42 |
_, args = method_calls[-1] |
| 43 |
assert args[1] == session.handle |
| 44 |
# assert args[2] == "" # appid, not necessary empty |
| 45 |
|
| 46 |
session.close() |
| 47 |
xdp.wait_for(lambda: session.closed) |
| 48 |
|
| 49 |
@pytest.mark.parametrize( |
| 50 |
"token", ("Invalid-Token&", "", "/foo", "something-else", "😄") |
| 51 |
) |
| 52 |
def test_remote_desktop_create_session_invalid(self, portals, dbus_con, token): |
| 53 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 54 |
|
| 55 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 56 |
options = {"session_handle_token": token} |
| 57 |
|
| 58 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 59 |
request.call("CreateSession", options=options) |
| 60 |
|
| 61 |
e = excinfo.value |
| 62 |
assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument" |
| 63 |
assert "Invalid token" in e.get_dbus_message() |
| 64 |
|
| 65 |
@pytest.mark.parametrize( |
| 66 |
"template_params", ({"remotedesktop": {"force-close": 500}},) |
| 67 |
) |
| 68 |
def test_create_session_signal_closed(self, portals, dbus_con): |
| 69 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 70 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 71 |
|
| 72 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 73 |
options = { |
| 74 |
"session_handle_token": "session_token0", |
| 75 |
} |
| 76 |
response = request.call( |
| 77 |
"CreateSession", |
| 78 |
options=options, |
| 79 |
) |
| 80 |
|
| 81 |
assert response |
| 82 |
assert response.response == 0 |
| 83 |
|
| 84 |
session = xdp.Session.from_response(dbus_con, response) |
| 85 |
# Check the impl portal was called with the right args |
| 86 |
method_calls = mock_intf.GetMethodCalls("CreateSession") |
| 87 |
assert len(method_calls) > 0 |
| 88 |
_, args = method_calls[-1] |
| 89 |
assert args[1] == session.handle |
| 90 |
# assert args[2] == "" # appid, not necessary empty |
| 91 |
|
| 92 |
# Now expect the backend to close it |
| 93 |
xdp.wait_for(lambda: session.closed) |
| 94 |
|
| 95 |
def test_connect_to_eis(self, portals, dbus_con): |
| 96 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 97 |
|
| 98 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 99 |
options = { |
| 100 |
"session_handle_token": "session_token0", |
| 101 |
} |
| 102 |
response = request.call( |
| 103 |
"CreateSession", |
| 104 |
options=options, |
| 105 |
) |
| 106 |
|
| 107 |
assert response |
| 108 |
assert response.response == 0 |
| 109 |
|
| 110 |
session = xdp.Session.from_response(dbus_con, response) |
| 111 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 112 |
options = { |
| 113 |
"types": dbus.UInt32(0x3), |
| 114 |
} |
| 115 |
response = request.call( |
| 116 |
"SelectDevices", |
| 117 |
session_handle=session.handle, |
| 118 |
options=options, |
| 119 |
) |
| 120 |
assert response |
| 121 |
assert response.response == 0 |
| 122 |
|
| 123 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 124 |
options = {} |
| 125 |
response = request.call( |
| 126 |
"Start", |
| 127 |
session_handle=session.handle, |
| 128 |
parent_window="", |
| 129 |
options=options, |
| 130 |
) |
| 131 |
assert response |
| 132 |
assert response.response == 0 |
| 133 |
|
| 134 |
fd = remotedesktop_intf.ConnectToEIS( |
| 135 |
session.handle, |
| 136 |
dbus.Dictionary({}, signature="sv"), |
| 137 |
) |
| 138 |
eis_socket = socket.fromfd(fd.take(), socket.AF_UNIX, socket.SOCK_STREAM) |
| 139 |
assert eis_socket.recv(10) == b"HELLO" |
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
"template_params", ({"remotedesktop": {"fail-connect-to-eis": True}},) |
| 143 |
) |
| 144 |
def test_connect_to_eis_fail(self, portals, dbus_con): |
| 145 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 146 |
|
| 147 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 148 |
options = { |
| 149 |
"session_handle_token": "session_token0", |
| 150 |
} |
| 151 |
response = request.call( |
| 152 |
"CreateSession", |
| 153 |
options=options, |
| 154 |
) |
| 155 |
|
| 156 |
assert response |
| 157 |
assert response.response == 0 |
| 158 |
|
| 159 |
session = xdp.Session.from_response(dbus_con, response) |
| 160 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 161 |
options = { |
| 162 |
"types": dbus.UInt32(0x3), |
| 163 |
} |
| 164 |
response = request.call( |
| 165 |
"SelectDevices", |
| 166 |
session_handle=session.handle, |
| 167 |
options=options, |
| 168 |
) |
| 169 |
assert response |
| 170 |
assert response.response == 0 |
| 171 |
|
| 172 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 173 |
options = {} |
| 174 |
response = request.call( |
| 175 |
"Start", |
| 176 |
session_handle=session.handle, |
| 177 |
parent_window="", |
| 178 |
options=options, |
| 179 |
) |
| 180 |
assert response |
| 181 |
assert response.response == 0 |
| 182 |
|
| 183 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 184 |
_ = remotedesktop_intf.ConnectToEIS( |
| 185 |
session.handle, dbus.Dictionary({}, signature="sv") |
| 186 |
) |
| 187 |
assert "Purposely failing ConnectToEIS" in excinfo.value.get_dbus_message() |
| 188 |
|
| 189 |
def test_connect_to_eis_fail_notifies(self, portals, dbus_con): |
| 190 |
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") |
| 191 |
|
| 192 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 193 |
options = { |
| 194 |
"session_handle_token": "session_token0", |
| 195 |
} |
| 196 |
response = request.call( |
| 197 |
"CreateSession", |
| 198 |
options=options, |
| 199 |
) |
| 200 |
|
| 201 |
assert response |
| 202 |
assert response.response == 0 |
| 203 |
|
| 204 |
session = xdp.Session.from_response(dbus_con, response) |
| 205 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 206 |
options = { |
| 207 |
"types": dbus.UInt32(0x3), |
| 208 |
} |
| 209 |
response = request.call( |
| 210 |
"SelectDevices", |
| 211 |
session_handle=session.handle, |
| 212 |
options=options, |
| 213 |
) |
| 214 |
assert response |
| 215 |
assert response.response == 0 |
| 216 |
|
| 217 |
request = xdp.Request(dbus_con, remotedesktop_intf) |
| 218 |
options = {} |
| 219 |
response = request.call( |
| 220 |
"Start", |
| 221 |
session_handle=session.handle, |
| 222 |
parent_window="", |
| 223 |
options=options, |
| 224 |
) |
| 225 |
assert response |
| 226 |
assert response.response == 0 |
| 227 |
|
| 228 |
notifyfuncs: List[Dict[str, Any]] = [ |
| 229 |
{"name": "NotifyPointerMotion", "args": (1, 2)}, |
| 230 |
{"name": "NotifyPointerMotionAbsolute", "args": (0, 1, 2)}, |
| 231 |
{"name": "NotifyPointerButton", "args": (1, 1)}, |
| 232 |
{"name": "NotifyPointerAxis", "args": (1, 1)}, |
| 233 |
{"name": "NotifyPointerAxisDiscrete", "args": (1, 1)}, |
| 234 |
{"name": "NotifyKeyboardKeycode", "args": (1, 1)}, |
| 235 |
{"name": "NotifyKeyboardKeysym", "args": (1, 1)}, |
| 236 |
{"name": "NotifyTouchDown", "args": (0, 0, 1, 1)}, |
| 237 |
{"name": "NotifyTouchMotion", "args": (0, 0, 1, 1)}, |
| 238 |
{"name": "NotifyTouchUp", "args": (0,)}, |
| 239 |
] |
| 240 |
for notifyfunc in notifyfuncs: |
| 241 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 242 |
func = getattr(remotedesktop_intf, notifyfunc["name"]) |
| 243 |
assert func is not None |
| 244 |
func( |
| 245 |
session.handle, |
| 246 |
dbus.Dictionary({}, signature="sv"), |
| 247 |
*notifyfunc["args"], |
| 248 |
) |
| 249 |
# Not the best error message but... |
| 250 |
assert ( |
| 251 |
"Session is not allowed to call Notify" |
| 252 |
in excinfo.value.get_dbus_message() |
| 253 |
) |