qvm_start_daemon.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import functools
  21. import os
  22. import signal
  23. import tempfile
  24. import unittest.mock
  25. import re
  26. import asyncio
  27. import asynctest
  28. import qubesadmin.tests
  29. import qubesadmin.tools.qvm_start_daemon
  30. from qubesadmin.tools.qvm_start_daemon import GUI_DAEMON_OPTIONS
  31. import qubesadmin.vm
  32. class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
  33. def setUp(self):
  34. super(TC_00_qvm_start_gui, self).setUp()
  35. self.launcher = \
  36. qubesadmin.tools.qvm_start_daemon.DAEMONLauncher(self.app)
  37. @unittest.mock.patch('subprocess.check_output')
  38. def test_000_kde_args(self, proc_mock):
  39. self.app.expected_calls[
  40. ('dom0', 'admin.vm.List', None, None)] = \
  41. b'0\x00test-vm class=AppVM state=Running\n'
  42. self.app.expected_calls[
  43. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  44. b'0\x00default=False type=label red'
  45. proc_mock.side_effect = [
  46. b'KWIN_RUNNING = 0x1\n',
  47. b'access control enabled, only authorized clients can connect\n'
  48. b'SI:localuser:root\n'
  49. b'SI:localuser:' + os.environ['USER'].encode() + b'\n',
  50. ]
  51. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  52. self.launcher.kde = True
  53. self.assertEqual(args, ['-T', '-p',
  54. '_KDE_NET_WM_COLOR_SCHEME=s:' +
  55. os.path.expanduser(
  56. '~/.local/share/qubes-kde/red.colors')])
  57. self.assertAllCalled()
  58. def setup_common_args(self):
  59. self.app.expected_calls[
  60. ('dom0', 'admin.vm.List', None, None)] = \
  61. b'0\x00test-vm class=AppVM state=Running\n' \
  62. b'gui-vm class=AppVM state=Running'
  63. self.app.expected_calls[
  64. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  65. b'0\x00default=False type=label red'
  66. self.app.expected_calls[
  67. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  68. b'0\x00default=False type=bool False'
  69. self.app.expected_calls[
  70. ('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  71. b'0\x00default=False type=vm gui-vm'
  72. self.app.expected_calls[
  73. ('dom0', 'admin.label.Get', 'red', None)] = \
  74. b'0\x000xff0000'
  75. self.app.expected_calls[
  76. ('dom0', 'admin.label.Index', 'red', None)] = \
  77. b'0\x001'
  78. self.app.expected_calls[
  79. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  80. 'rpc-clipboard', None)] = \
  81. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  82. self.app.expected_calls[
  83. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  84. b'0\x00default=99 type=int 99'
  85. for name, _kind in GUI_DAEMON_OPTIONS:
  86. self.app.expected_calls[
  87. ('test-vm', 'admin.vm.feature.Get',
  88. 'gui-' + name.replace('_', '-'), None)] = \
  89. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  90. self.app.expected_calls[
  91. ('gui-vm', 'admin.vm.feature.Get',
  92. 'gui-default-' + name.replace('_', '-'), None)] = \
  93. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  94. def run_common_args(self):
  95. with unittest.mock.patch.object(
  96. self.launcher, 'kde_guid_args') as kde_mock, \
  97. unittest.mock.patch.object(
  98. self.launcher, 'write_guid_config') as write_config_mock:
  99. kde_mock.return_value = []
  100. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  101. self.assertEqual(len(write_config_mock.mock_calls), 1)
  102. config_args = write_config_mock.mock_calls[0][1]
  103. self.assertEqual(config_args[0], '/var/run/qubes/guid-conf.99')
  104. config = config_args[1]
  105. # Strip comments and empty lines
  106. config = re.sub(r'^#.*\n', '', config)
  107. config = re.sub(r'^\n', '', config)
  108. self.assertAllCalled()
  109. return args, config
  110. def test_010_common_args(self):
  111. self.setup_common_args()
  112. args, config = self.run_common_args()
  113. self.assertEqual(args, [
  114. '/usr/bin/qubes-guid', '-N', 'test-vm',
  115. '-c', '0xff0000',
  116. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  117. '-l', '1', '-q',
  118. '-C', '/var/run/qubes/guid-conf.99',
  119. ])
  120. self.assertEqual(config, '''\
  121. global: {
  122. }
  123. ''')
  124. def test_011_common_args_debug(self):
  125. self.setup_common_args()
  126. self.app.expected_calls[
  127. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  128. b'0\x00default=False type=bool True'
  129. args, config = self.run_common_args()
  130. self.assertEqual(args, [
  131. '/usr/bin/qubes-guid', '-N', 'test-vm',
  132. '-c', '0xff0000',
  133. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  134. '-l', '1', '-v', '-v',
  135. '-C', '/var/run/qubes/guid-conf.99',
  136. ])
  137. self.assertEqual(config, '''\
  138. global: {
  139. }
  140. ''')
  141. def test_012_common_args_rpc_clipboard(self):
  142. self.setup_common_args()
  143. self.app.expected_calls[
  144. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  145. 'rpc-clipboard', None)] = \
  146. b'0\x001'
  147. args, config = self.run_common_args()
  148. self.assertEqual(args, [
  149. '/usr/bin/qubes-guid', '-N', 'test-vm',
  150. '-c', '0xff0000',
  151. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  152. '-l', '1', '-q', '-Q',
  153. '-C', '/var/run/qubes/guid-conf.99',
  154. ])
  155. self.assertEqual(config, '''\
  156. global: {
  157. }
  158. ''')
  159. def test_013_common_args_guid_config(self):
  160. self.setup_common_args()
  161. self.app.expected_calls[
  162. ('test-vm', 'admin.vm.feature.Get',
  163. 'gui-allow-fullscreen', None)] = \
  164. b'0\x001'
  165. # The template will not be asked for this feature
  166. del self.app.expected_calls[
  167. ('gui-vm', 'admin.vm.feature.Get',
  168. 'gui-default-allow-fullscreen', None)]
  169. self.app.expected_calls[
  170. ('gui-vm', 'admin.vm.feature.Get',
  171. 'gui-default-secure-copy-sequence', None)] = \
  172. b'0\x00Ctrl-Alt-Shift-c'
  173. _args, config = self.run_common_args()
  174. self.assertEqual(config, '''\
  175. global: {
  176. allow_fullscreen = true;
  177. secure_copy_sequence = "Ctrl-Alt-Shift-c";
  178. }
  179. ''')
  180. @asynctest.patch('asyncio.create_subprocess_exec')
  181. def test_020_start_gui_for_vm(self, proc_mock):
  182. loop = asyncio.new_event_loop()
  183. asyncio.set_event_loop(loop)
  184. self.addCleanup(loop.close)
  185. self.app.expected_calls[
  186. ('dom0', 'admin.vm.List', None, None)] = \
  187. b'0\x00test-vm class=AppVM state=Running\n'
  188. self.app.expected_calls[
  189. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  190. b'0\x00power_state=Running'
  191. self.app.expected_calls[
  192. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  193. b'0\x00default=False type=int 3000'
  194. self.app.expected_calls[
  195. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  196. b'0\x00default=False type=str pv'
  197. self.app.expected_calls[
  198. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  199. 'no-monitor-layout', None)] = \
  200. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  201. with unittest.mock.patch.object(self.launcher,
  202. 'common_guid_args', lambda vm: []), \
  203. unittest.mock.patch.object(qubesadmin.tools.qvm_start_daemon,
  204. 'get_monitor_layout',
  205. unittest.mock.Mock(
  206. return_value=['1600 900 0 0\n'])):
  207. loop.run_until_complete(self.launcher.start_gui_for_vm(
  208. self.app.domains['test-vm']))
  209. # common arguments dropped for simplicity
  210. proc_mock.assert_called_once_with('-d', '3000')
  211. self.assertAllCalled()
  212. @asynctest.patch('asyncio.create_subprocess_exec')
  213. def test_021_start_gui_for_vm_hvm(self, proc_mock):
  214. loop = asyncio.new_event_loop()
  215. asyncio.set_event_loop(loop)
  216. self.addCleanup(loop.close)
  217. self.app.expected_calls[
  218. ('dom0', 'admin.vm.List', None, None)] = \
  219. b'0\x00test-vm class=AppVM state=Running\n'
  220. self.app.expected_calls[
  221. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  222. b'0\x00power_state=Running'
  223. self.app.expected_calls[
  224. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  225. b'0\x00default=False type=int 3000'
  226. self.app.expected_calls[
  227. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  228. b'0\x00default=False type=int 3001'
  229. self.app.expected_calls[
  230. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  231. b'0\x00default=False type=str hvm'
  232. self.app.expected_calls[
  233. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  234. b'0\x00default=False type=bool False'
  235. self.app.expected_calls[
  236. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  237. 'no-monitor-layout', None)] = \
  238. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  239. with unittest.mock.patch.object(self.launcher,
  240. 'common_guid_args', lambda vm: []), \
  241. unittest.mock.patch.object(qubesadmin.tools.qvm_start_daemon,
  242. 'get_monitor_layout',
  243. unittest.mock.Mock(
  244. return_value=['1600 900 0 0\n'])):
  245. loop.run_until_complete(self.launcher.start_gui_for_vm(
  246. self.app.domains['test-vm']))
  247. # common arguments dropped for simplicity
  248. proc_mock.assert_called_once_with('-d', '3000', '-n')
  249. self.assertAllCalled()
  250. def test_022_start_gui_for_vm_hvm_stubdom(self):
  251. loop = asyncio.new_event_loop()
  252. asyncio.set_event_loop(loop)
  253. self.addCleanup(loop.close)
  254. self.app.expected_calls[
  255. ('dom0', 'admin.vm.List', None, None)] = \
  256. b'0\x00test-vm class=AppVM state=Running\n'
  257. self.app.expected_calls[
  258. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  259. b'0\x00power_state=Running'
  260. self.app.expected_calls[
  261. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  262. b'0\x00default=False type=int 3000'
  263. self.app.expected_calls[
  264. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  265. b'0\x00default=False type=int 3001'
  266. self.app.expected_calls[
  267. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  268. b'0\x00default=False type=str hvm'
  269. self.app.expected_calls[
  270. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  271. b'0\x00default=False type=bool False'
  272. self.app.expected_calls[
  273. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  274. 'no-monitor-layout', None)] = \
  275. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  276. pidfile = tempfile.NamedTemporaryFile()
  277. pidfile.write(b'1234\n')
  278. pidfile.flush()
  279. self.addCleanup(pidfile.close)
  280. patch_proc = asynctest.patch('asyncio.create_subprocess_exec')
  281. patch_monitor_layout = unittest.mock.patch.object(
  282. qubesadmin.tools.qvm_start_daemon,
  283. 'get_monitor_layout',
  284. unittest.mock.Mock(return_value=['1600 900 0 0\n']))
  285. patch_args = unittest.mock.patch.object(self.launcher,
  286. 'common_guid_args',
  287. lambda vm: [])
  288. patch_pidfile = unittest.mock.patch.object(self.launcher,
  289. 'guid_pidfile',
  290. lambda vm: pidfile.name)
  291. try:
  292. mock_proc = patch_proc.start()
  293. patch_args.start()
  294. patch_pidfile.start()
  295. patch_monitor_layout.start()
  296. loop.run_until_complete(self.launcher.start_gui_for_vm(
  297. self.app.domains['test-vm']))
  298. # common arguments dropped for simplicity
  299. mock_proc.assert_called_once_with(
  300. '-d', '3000', '-n', '-K', '1234')
  301. finally:
  302. unittest.mock.patch.stopall()
  303. self.assertAllCalled()
  304. def test_030_start_gui_for_stubdomain(self):
  305. loop = asyncio.new_event_loop()
  306. asyncio.set_event_loop(loop)
  307. self.addCleanup(loop.close)
  308. self.app.expected_calls[
  309. ('dom0', 'admin.vm.List', None, None)] = \
  310. b'0\x00test-vm class=AppVM state=Running\n'
  311. self.app.expected_calls[
  312. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  313. b'0\x00default=False type=int 3000'
  314. self.app.expected_calls[
  315. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  316. b'0\x00default=False type=int 3001'
  317. self.app.expected_calls[
  318. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  319. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  320. self.app.expected_calls[
  321. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  322. None)] = \
  323. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  324. proc_mock = unittest.mock.Mock()
  325. with asynctest.patch('asyncio.create_subprocess_exec',
  326. lambda *args: self.mock_coroutine(proc_mock,
  327. *args)):
  328. with unittest.mock.patch.object(self.launcher,
  329. 'common_guid_args', lambda vm: []):
  330. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  331. self.app.domains['test-vm']))
  332. # common arguments dropped for simplicity
  333. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  334. self.assertAllCalled()
  335. def test_031_start_gui_for_stubdomain_forced(self):
  336. loop = asyncio.new_event_loop()
  337. asyncio.set_event_loop(loop)
  338. self.addCleanup(loop.close)
  339. self.app.expected_calls[
  340. ('dom0', 'admin.vm.List', None, None)] = \
  341. b'0\x00test-vm class=AppVM state=Running\n'
  342. self.app.expected_calls[
  343. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  344. b'0\x00default=False type=int 3000'
  345. self.app.expected_calls[
  346. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  347. b'0\x00default=False type=int 3001'
  348. # self.app.expected_calls[
  349. # ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  350. # b'0\x00'
  351. self.app.expected_calls[
  352. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  353. None)] = \
  354. b'0\x001'
  355. proc_mock = unittest.mock.Mock()
  356. with asynctest.patch('asyncio.create_subprocess_exec',
  357. lambda *args: self.mock_coroutine(proc_mock,
  358. *args)):
  359. with unittest.mock.patch.object(self.launcher,
  360. 'common_guid_args', lambda vm: []):
  361. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  362. self.app.domains['test-vm']))
  363. # common arguments dropped for simplicity
  364. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  365. self.assertAllCalled()
  366. @asyncio.coroutine
  367. def mock_coroutine(self, mock, *args, **kwargs):
  368. mock(*args, **kwargs)
  369. def test_040_start_gui(self):
  370. loop = asyncio.new_event_loop()
  371. asyncio.set_event_loop(loop)
  372. self.addCleanup(loop.close)
  373. self.app.expected_calls[
  374. ('dom0', 'admin.vm.List', None, None)] = \
  375. b'0\x00test-vm class=AppVM state=Running\n' \
  376. b'gui-vm class=AppVM state=Running'
  377. self.app.expected_calls[
  378. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  379. b'0\x00power_state=Running'
  380. self.app.expected_calls[
  381. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  382. b'0\x00True'
  383. self.app.expected_calls[
  384. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  385. 'no-monitor-layout', None)] = \
  386. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  387. self.app.expected_calls[
  388. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  389. b'0\x00default=False type=str hvm'
  390. self.app.expected_calls[
  391. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  392. b'0\x00default=False type=int 3000'
  393. self.app.expected_calls[
  394. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  395. b'0\x00default=False type=int 3001'
  396. self.app.expected_calls[
  397. ('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  398. b'0\x00default=False type=vm gui-vm'
  399. self.app._local_name = 'gui-vm'
  400. vm = self.app.domains['test-vm']
  401. mock_start_vm = unittest.mock.Mock()
  402. mock_start_stubdomain = unittest.mock.Mock()
  403. patch_start_vm = unittest.mock.patch.object(
  404. self.launcher, 'start_gui_for_vm', functools.partial(
  405. self.mock_coroutine, mock_start_vm))
  406. patch_start_stubdomain = unittest.mock.patch.object(
  407. self.launcher, 'start_gui_for_stubdomain', lambda vm_, force:
  408. self.mock_coroutine(mock_start_stubdomain, vm_))
  409. try:
  410. patch_start_vm.start()
  411. patch_start_stubdomain.start()
  412. loop.run_until_complete(self.launcher.start_gui(vm))
  413. mock_start_vm.assert_called_once_with(vm, monitor_layout=None)
  414. mock_start_stubdomain.assert_called_once_with(vm)
  415. finally:
  416. unittest.mock.patch.stopall()
  417. def test_041_start_gui_running(self):
  418. # simulate existing pidfiles, should not start processes
  419. self.skipTest('todo')
  420. def test_042_start_gui_pvh(self):
  421. # PVH - no stubdomain
  422. self.skipTest('todo')
  423. @unittest.mock.patch('subprocess.Popen')
  424. def test_050_get_monitor_layout1(self, proc_mock):
  425. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  426. HDMI1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
  427. 1920x1200 59.95*+
  428. 1920x1080 60.00 50.00 59.94
  429. 1920x1080i 60.00 50.00 59.94
  430. 1600x1200 60.00
  431. 1680x1050 59.88
  432. 1280x1024 60.02
  433. 1440x900 59.90
  434. 1280x960 60.00
  435. 1280x720 60.00 50.00 59.94
  436. 1024x768 60.00
  437. 800x600 60.32
  438. 720x576 50.00
  439. 720x480 60.00 59.94
  440. 720x480i 60.00 59.94
  441. 640x480 60.00 59.94
  442. HDMI2 disconnected (normal left inverted right x axis y axis)
  443. VGA1 disconnected (normal left inverted right x axis y axis)
  444. VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  445. '''.splitlines()
  446. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  447. ['1920 1200 0 0\n'])
  448. @unittest.mock.patch('subprocess.Popen')
  449. def test_051_get_monitor_layout_multiple(self, proc_mock):
  450. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 2880 x 1024, maximum 32767 x 32767
  451. LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis)
  452. VGA1 connected 1280x1024+1600+0 (normal left inverted right x axis y axis)
  453. '''.splitlines()
  454. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  455. ['1600 900 0 0\n', '1280 1024 1600 0\n'])
  456. @unittest.mock.patch('subprocess.Popen')
  457. def test_052_get_monitor_layout_hidpi1(self, proc_mock):
  458. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  459. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x 208mm
  460. 1920x1200 60.00*+
  461. '''.splitlines()
  462. dpi = 150
  463. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  464. ['2560 1920 0 0 {} {}\n'.format(
  465. int(2560 / dpi * 254 / 10),
  466. int(1920 / dpi * 254 / 10))])
  467. @unittest.mock.patch('subprocess.Popen')
  468. def test_052_get_monitor_layout_hidpi2(self, proc_mock):
  469. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  470. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x 174mm
  471. 1920x1200 60.00*+
  472. '''.splitlines()
  473. dpi = 200
  474. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  475. ['2560 1920 0 0 {} {}\n'.format(
  476. int(2560 / dpi * 254 / 10),
  477. int(1920 / dpi * 254 / 10))])
  478. @unittest.mock.patch('subprocess.Popen')
  479. def test_052_get_monitor_layout_hidpi3(self, proc_mock):
  480. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  481. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x 116mm
  482. 1920x1200 60.00*+
  483. '''.splitlines()
  484. dpi = 300
  485. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  486. ['2560 1920 0 0 {} {}\n'.format(
  487. int(2560 / dpi * 254 / 10),
  488. int(1920 / dpi * 254 / 10))])
  489. def test_060_send_monitor_layout(self):
  490. loop = asyncio.new_event_loop()
  491. asyncio.set_event_loop(loop)
  492. self.addCleanup(loop.close)
  493. self.app.expected_calls[
  494. ('dom0', 'admin.vm.List', None, None)] = \
  495. b'0\x00test-vm class=AppVM state=Running\n'
  496. self.app.expected_calls[
  497. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  498. b'0\x00power_state=Running'
  499. self.app.expected_calls[
  500. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  501. 'no-monitor-layout', None)] = \
  502. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  503. vm = self.app.domains['test-vm']
  504. mock_run_service = unittest.mock.Mock(spec={})
  505. patch_run_service = unittest.mock.patch.object(
  506. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  507. mock_run_service)
  508. patch_run_service.start()
  509. self.addCleanup(patch_run_service.stop)
  510. monitor_layout = ['1920 1080 0 0\n']
  511. loop.run_until_complete(self.launcher.send_monitor_layout(
  512. vm, layout=monitor_layout, startup=True))
  513. mock_run_service.assert_called_once_with(
  514. 'qubes.SetMonitorLayout', autostart=False, input=b'1920 1080 0 0\n')
  515. self.assertAllCalled()
  516. def test_061_send_monitor_layout_exclude(self):
  517. loop = asyncio.new_event_loop()
  518. asyncio.set_event_loop(loop)
  519. self.addCleanup(loop.close)
  520. self.app.expected_calls[
  521. ('dom0', 'admin.vm.List', None, None)] = \
  522. b'0\x00test-vm class=AppVM state=Running\n'
  523. self.app.expected_calls[
  524. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  525. 'no-monitor-layout', None)] = \
  526. b'0\x00True'
  527. vm = self.app.domains['test-vm']
  528. mock_run_service = unittest.mock.Mock()
  529. patch_run_service = unittest.mock.patch.object(
  530. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  531. mock_run_service)
  532. patch_run_service.start()
  533. self.addCleanup(patch_run_service.stop)
  534. monitor_layout = ['1920 1080 0 0\n']
  535. loop.run_until_complete(self.launcher.send_monitor_layout(
  536. vm, layout=monitor_layout, startup=True))
  537. self.assertFalse(mock_run_service.called)
  538. self.assertAllCalled()
  539. def test_062_send_monitor_layout_not_running(self):
  540. loop = asyncio.new_event_loop()
  541. asyncio.set_event_loop(loop)
  542. self.addCleanup(loop.close)
  543. self.app.expected_calls[
  544. ('dom0', 'admin.vm.List', None, None)] = \
  545. b'0\x00test-vm class=AppVM state=Halted\n'
  546. self.app.expected_calls[
  547. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  548. b'0\x00power_state=Halted'
  549. self.app.expected_calls[
  550. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  551. 'no-monitor-layout', None)] = \
  552. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  553. vm = self.app.domains['test-vm']
  554. mock_run_service = unittest.mock.Mock()
  555. patch_run_service = unittest.mock.patch.object(
  556. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  557. mock_run_service)
  558. patch_run_service.start()
  559. self.addCleanup(patch_run_service.stop)
  560. monitor_layout = ['1920 1080 0 0\n']
  561. loop.run_until_complete(self.launcher.send_monitor_layout(
  562. vm, layout=monitor_layout, startup=True))
  563. self.assertFalse(mock_run_service.called)
  564. self.assertAllCalled()
  565. def test_063_send_monitor_layout_signal_existing(self):
  566. loop = asyncio.new_event_loop()
  567. asyncio.set_event_loop(loop)
  568. self.addCleanup(loop.close)
  569. self.app.expected_calls[
  570. ('dom0', 'admin.vm.List', None, None)] = \
  571. b'0\x00test-vm class=AppVM state=Running\n'
  572. self.app.expected_calls[
  573. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  574. b'0\x00power_state=Running'
  575. self.app.expected_calls[
  576. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  577. b'0\x00default=False type=int 123'
  578. self.app.expected_calls[
  579. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  580. b'0\x00default=False type=int 124'
  581. self.app.expected_calls[
  582. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  583. 'no-monitor-layout', None)] = \
  584. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  585. vm = self.app.domains['test-vm']
  586. self.addCleanup(unittest.mock.patch.stopall)
  587. with tempfile.NamedTemporaryFile() as pidfile:
  588. pidfile.write(b'1234\n')
  589. pidfile.flush()
  590. patch_guid_pidfile = unittest.mock.patch.object(
  591. self.launcher, 'guid_pidfile')
  592. mock_guid_pidfile = patch_guid_pidfile.start()
  593. mock_guid_pidfile.return_value = pidfile.name
  594. mock_kill = unittest.mock.patch('os.kill').start()
  595. monitor_layout = ['1920 1080 0 0\n']
  596. loop.run_until_complete(self.launcher.send_monitor_layout(
  597. vm, layout=monitor_layout, startup=False))
  598. self.assertEqual(mock_guid_pidfile.mock_calls,
  599. [unittest.mock.call(123),
  600. unittest.mock.call(124)])
  601. self.assertEqual(mock_kill.mock_calls,
  602. [unittest.mock.call(1234, signal.SIGHUP),
  603. unittest.mock.call(1234, signal.SIGHUP)])
  604. self.assertAllCalled()
  605. def test_070_send_monitor_layout_all(self):
  606. loop = asyncio.new_event_loop()
  607. asyncio.set_event_loop(loop)
  608. self.addCleanup(loop.close)
  609. self.app.expected_calls[
  610. ('dom0', 'admin.vm.List', None, None)] = \
  611. b'0\x00test-vm class=AppVM state=Running\n' \
  612. b'test-vm2 class=AppVM state=Running\n' \
  613. b'test-vm3 class=AppVM state=Running\n' \
  614. b'test-vm4 class=AppVM state=Halted\n' \
  615. b'gui-vm class=AppVM state=Running'
  616. self.app.expected_calls[
  617. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  618. b'0\x00power_state=Running'
  619. self.app.expected_calls[
  620. ('test-vm2', 'admin.vm.CurrentState', None, None)] = \
  621. b'0\x00power_state=Running'
  622. self.app.expected_calls[
  623. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  624. b'0\x00power_state=Running'
  625. self.app.expected_calls[
  626. ('test-vm4', 'admin.vm.CurrentState', None, None)] = \
  627. b'0\x00power_state=Halted'
  628. self.app.expected_calls[
  629. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  630. 'gui', None)] = \
  631. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  632. self.app.expected_calls[
  633. ('test-vm2', 'admin.vm.feature.CheckWithTemplate',
  634. 'gui', None)] = \
  635. b'0\x00True'
  636. self.app.expected_calls[
  637. ('test-vm3', 'admin.vm.feature.CheckWithTemplate',
  638. 'gui', None)] = \
  639. b'0\x00'
  640. self.app.expected_calls[
  641. ('gui-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  642. b'0\x00default=True type=vm '
  643. self.app.expected_calls[
  644. ('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  645. b'0\x00default=False type=vm gui-vm'
  646. self.app.expected_calls[
  647. ('test-vm2', 'admin.vm.property.Get', 'guivm', None)] = \
  648. b'0\x00default=False type=vm gui-vm'
  649. self.app.expected_calls[
  650. ('test-vm3', 'admin.vm.property.Get', 'guivm', None)] = \
  651. b'0\x00default=False type=vm gui-vm'
  652. self.app.expected_calls[
  653. ('test-vm4', 'admin.vm.property.Get', 'guivm', None)] = \
  654. b'0\x00default=False type=vm gui-vm'
  655. self.app._local_name = 'gui-vm'
  656. vm = self.app.domains['test-vm']
  657. vm2 = self.app.domains['test-vm2']
  658. self.addCleanup(unittest.mock.patch.stopall)
  659. mock_send_monitor_layout = unittest.mock.Mock()
  660. patch_send_monitor_layout = unittest.mock.patch.object(
  661. self.launcher, 'send_monitor_layout',
  662. functools.partial(self.mock_coroutine, mock_send_monitor_layout))
  663. patch_send_monitor_layout.start()
  664. monitor_layout = ['1920 1080 0 0\n']
  665. mock_get_monior_layout = unittest.mock.patch(
  666. 'qubesadmin.tools.qvm_start_daemon.get_monitor_layout').start()
  667. mock_get_monior_layout.return_value = monitor_layout
  668. self.launcher.send_monitor_layout_all()
  669. loop.stop()
  670. loop.run_forever()
  671. # test-vm3 not called b/c feature 'gui' set to false
  672. # test-vm4 not called b/c not running
  673. self.assertCountEqual(mock_send_monitor_layout.mock_calls,
  674. [unittest.mock.call(vm, monitor_layout),
  675. unittest.mock.call(vm2, monitor_layout)])
  676. mock_get_monior_layout.assert_called_once_with()
  677. self.assertAllCalled()