qvm_start_daemon.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. loop.run_until_complete(self.launcher.start_gui_for_vm(
  204. self.app.domains['test-vm']))
  205. # common arguments dropped for simplicity
  206. proc_mock.assert_called_once_with('-d', '3000')
  207. self.assertAllCalled()
  208. @asynctest.patch('asyncio.create_subprocess_exec')
  209. def test_021_start_gui_for_vm_hvm(self, proc_mock):
  210. loop = asyncio.new_event_loop()
  211. asyncio.set_event_loop(loop)
  212. self.addCleanup(loop.close)
  213. self.app.expected_calls[
  214. ('dom0', 'admin.vm.List', None, None)] = \
  215. b'0\x00test-vm class=AppVM state=Running\n'
  216. self.app.expected_calls[
  217. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  218. b'0\x00power_state=Running'
  219. self.app.expected_calls[
  220. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  221. b'0\x00default=False type=int 3000'
  222. self.app.expected_calls[
  223. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  224. b'0\x00default=False type=int 3001'
  225. self.app.expected_calls[
  226. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  227. b'0\x00default=False type=str hvm'
  228. self.app.expected_calls[
  229. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  230. b'0\x00default=False type=bool False'
  231. self.app.expected_calls[
  232. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  233. 'no-monitor-layout', None)] = \
  234. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  235. with unittest.mock.patch.object(self.launcher,
  236. 'common_guid_args', lambda vm: []):
  237. loop.run_until_complete(self.launcher.start_gui_for_vm(
  238. self.app.domains['test-vm']))
  239. # common arguments dropped for simplicity
  240. proc_mock.assert_called_once_with('-d', '3000', '-n')
  241. self.assertAllCalled()
  242. def test_022_start_gui_for_vm_hvm_stubdom(self):
  243. loop = asyncio.new_event_loop()
  244. asyncio.set_event_loop(loop)
  245. self.addCleanup(loop.close)
  246. self.app.expected_calls[
  247. ('dom0', 'admin.vm.List', None, None)] = \
  248. b'0\x00test-vm class=AppVM state=Running\n'
  249. self.app.expected_calls[
  250. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  251. b'0\x00power_state=Running'
  252. self.app.expected_calls[
  253. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  254. b'0\x00default=False type=int 3000'
  255. self.app.expected_calls[
  256. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  257. b'0\x00default=False type=int 3001'
  258. self.app.expected_calls[
  259. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  260. b'0\x00default=False type=str hvm'
  261. self.app.expected_calls[
  262. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  263. b'0\x00default=False type=bool False'
  264. self.app.expected_calls[
  265. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  266. 'no-monitor-layout', None)] = \
  267. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  268. pidfile = tempfile.NamedTemporaryFile()
  269. pidfile.write(b'1234\n')
  270. pidfile.flush()
  271. self.addCleanup(pidfile.close)
  272. patch_proc = asynctest.patch('asyncio.create_subprocess_exec')
  273. patch_args = unittest.mock.patch.object(self.launcher,
  274. 'common_guid_args',
  275. lambda vm: [])
  276. patch_pidfile = unittest.mock.patch.object(self.launcher,
  277. 'guid_pidfile',
  278. lambda vm: pidfile.name)
  279. try:
  280. mock_proc = patch_proc.start()
  281. patch_args.start()
  282. patch_pidfile.start()
  283. loop.run_until_complete(self.launcher.start_gui_for_vm(
  284. self.app.domains['test-vm']))
  285. # common arguments dropped for simplicity
  286. mock_proc.assert_called_once_with(
  287. '-d', '3000', '-n', '-K', '1234')
  288. finally:
  289. unittest.mock.patch.stopall()
  290. self.assertAllCalled()
  291. def test_030_start_gui_for_stubdomain(self):
  292. loop = asyncio.new_event_loop()
  293. asyncio.set_event_loop(loop)
  294. self.addCleanup(loop.close)
  295. self.app.expected_calls[
  296. ('dom0', 'admin.vm.List', None, None)] = \
  297. b'0\x00test-vm class=AppVM state=Running\n'
  298. self.app.expected_calls[
  299. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  300. b'0\x00default=False type=int 3000'
  301. self.app.expected_calls[
  302. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  303. b'0\x00default=False type=int 3001'
  304. self.app.expected_calls[
  305. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  306. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  307. self.app.expected_calls[
  308. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  309. None)] = \
  310. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  311. proc_mock = unittest.mock.Mock()
  312. with asynctest.patch('asyncio.create_subprocess_exec',
  313. lambda *args: self.mock_coroutine(proc_mock,
  314. *args)):
  315. with unittest.mock.patch.object(self.launcher,
  316. 'common_guid_args', lambda vm: []):
  317. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  318. self.app.domains['test-vm']))
  319. # common arguments dropped for simplicity
  320. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  321. self.assertAllCalled()
  322. def test_031_start_gui_for_stubdomain_forced(self):
  323. loop = asyncio.new_event_loop()
  324. asyncio.set_event_loop(loop)
  325. self.addCleanup(loop.close)
  326. self.app.expected_calls[
  327. ('dom0', 'admin.vm.List', None, None)] = \
  328. b'0\x00test-vm class=AppVM state=Running\n'
  329. self.app.expected_calls[
  330. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  331. b'0\x00default=False type=int 3000'
  332. self.app.expected_calls[
  333. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  334. b'0\x00default=False type=int 3001'
  335. # self.app.expected_calls[
  336. # ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  337. # b'0\x00'
  338. self.app.expected_calls[
  339. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  340. None)] = \
  341. b'0\x001'
  342. proc_mock = unittest.mock.Mock()
  343. with asynctest.patch('asyncio.create_subprocess_exec',
  344. lambda *args: self.mock_coroutine(proc_mock,
  345. *args)):
  346. with unittest.mock.patch.object(self.launcher,
  347. 'common_guid_args', lambda vm: []):
  348. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  349. self.app.domains['test-vm']))
  350. # common arguments dropped for simplicity
  351. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  352. self.assertAllCalled()
  353. @asyncio.coroutine
  354. def mock_coroutine(self, mock, *args, **kwargs):
  355. mock(*args, **kwargs)
  356. def test_040_start_gui(self):
  357. loop = asyncio.new_event_loop()
  358. asyncio.set_event_loop(loop)
  359. self.addCleanup(loop.close)
  360. self.app.expected_calls[
  361. ('dom0', 'admin.vm.List', None, None)] = \
  362. b'0\x00test-vm class=AppVM state=Running\n' \
  363. b'gui-vm class=AppVM state=Running'
  364. self.app.expected_calls[
  365. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  366. b'0\x00power_state=Running'
  367. self.app.expected_calls[
  368. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  369. b'0\x00True'
  370. self.app.expected_calls[
  371. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  372. 'no-monitor-layout', None)] = \
  373. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  374. self.app.expected_calls[
  375. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  376. b'0\x00default=False type=str hvm'
  377. self.app.expected_calls[
  378. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  379. b'0\x00default=False type=int 3000'
  380. self.app.expected_calls[
  381. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  382. b'0\x00default=False type=int 3001'
  383. self.app.expected_calls[
  384. ('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  385. b'0\x00default=False type=vm gui-vm'
  386. self.app._local_name = 'gui-vm'
  387. vm = self.app.domains['test-vm']
  388. mock_start_vm = unittest.mock.Mock()
  389. mock_start_stubdomain = unittest.mock.Mock()
  390. patch_start_vm = unittest.mock.patch.object(
  391. self.launcher, 'start_gui_for_vm', functools.partial(
  392. self.mock_coroutine, mock_start_vm))
  393. patch_start_stubdomain = unittest.mock.patch.object(
  394. self.launcher, 'start_gui_for_stubdomain', lambda vm_, force:
  395. self.mock_coroutine(mock_start_stubdomain, vm_))
  396. try:
  397. patch_start_vm.start()
  398. patch_start_stubdomain.start()
  399. loop.run_until_complete(self.launcher.start_gui(vm))
  400. mock_start_vm.assert_called_once_with(vm, monitor_layout=None)
  401. mock_start_stubdomain.assert_called_once_with(vm)
  402. finally:
  403. unittest.mock.patch.stopall()
  404. def test_041_start_gui_running(self):
  405. # simulate existing pidfiles, should not start processes
  406. self.skipTest('todo')
  407. def test_042_start_gui_pvh(self):
  408. # PVH - no stubdomain
  409. self.skipTest('todo')
  410. @unittest.mock.patch('subprocess.Popen')
  411. def test_050_get_monitor_layout1(self, proc_mock):
  412. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  413. HDMI1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
  414. 1920x1200 59.95*+
  415. 1920x1080 60.00 50.00 59.94
  416. 1920x1080i 60.00 50.00 59.94
  417. 1600x1200 60.00
  418. 1680x1050 59.88
  419. 1280x1024 60.02
  420. 1440x900 59.90
  421. 1280x960 60.00
  422. 1280x720 60.00 50.00 59.94
  423. 1024x768 60.00
  424. 800x600 60.32
  425. 720x576 50.00
  426. 720x480 60.00 59.94
  427. 720x480i 60.00 59.94
  428. 640x480 60.00 59.94
  429. HDMI2 disconnected (normal left inverted right x axis y axis)
  430. VGA1 disconnected (normal left inverted right x axis y axis)
  431. VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  432. '''.splitlines()
  433. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  434. ['1920 1200 0 0\n'])
  435. @unittest.mock.patch('subprocess.Popen')
  436. def test_051_get_monitor_layout_multiple(self, proc_mock):
  437. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 2880 x 1024, maximum 32767 x 32767
  438. LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis)
  439. VGA1 connected 1280x1024+1600+0 (normal left inverted right x axis y axis)
  440. '''.splitlines()
  441. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  442. ['1600 900 0 0\n', '1280 1024 1600 0\n'])
  443. @unittest.mock.patch('subprocess.Popen')
  444. def test_052_get_monitor_layout_hidpi1(self, proc_mock):
  445. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  446. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x 208mm
  447. 1920x1200 60.00*+
  448. '''.splitlines()
  449. dpi = 150
  450. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  451. ['2560 1920 0 0 {} {}\n'.format(
  452. int(2560 / dpi * 254 / 10),
  453. int(1920 / dpi * 254 / 10))])
  454. @unittest.mock.patch('subprocess.Popen')
  455. def test_052_get_monitor_layout_hidpi2(self, proc_mock):
  456. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  457. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x 174mm
  458. 1920x1200 60.00*+
  459. '''.splitlines()
  460. dpi = 200
  461. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  462. ['2560 1920 0 0 {} {}\n'.format(
  463. int(2560 / dpi * 254 / 10),
  464. int(1920 / dpi * 254 / 10))])
  465. @unittest.mock.patch('subprocess.Popen')
  466. def test_052_get_monitor_layout_hidpi3(self, proc_mock):
  467. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  468. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x 116mm
  469. 1920x1200 60.00*+
  470. '''.splitlines()
  471. dpi = 300
  472. self.assertEqual(qubesadmin.tools.qvm_start_daemon.get_monitor_layout(),
  473. ['2560 1920 0 0 {} {}\n'.format(
  474. int(2560 / dpi * 254 / 10),
  475. int(1920 / dpi * 254 / 10))])
  476. def test_060_send_monitor_layout(self):
  477. loop = asyncio.new_event_loop()
  478. asyncio.set_event_loop(loop)
  479. self.addCleanup(loop.close)
  480. self.app.expected_calls[
  481. ('dom0', 'admin.vm.List', None, None)] = \
  482. b'0\x00test-vm class=AppVM state=Running\n'
  483. self.app.expected_calls[
  484. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  485. b'0\x00power_state=Running'
  486. self.app.expected_calls[
  487. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  488. 'no-monitor-layout', None)] = \
  489. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  490. vm = self.app.domains['test-vm']
  491. mock_run_service = unittest.mock.Mock(spec={})
  492. patch_run_service = unittest.mock.patch.object(
  493. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  494. mock_run_service)
  495. patch_run_service.start()
  496. self.addCleanup(patch_run_service.stop)
  497. monitor_layout = ['1920 1080 0 0\n']
  498. loop.run_until_complete(self.launcher.send_monitor_layout(
  499. vm, layout=monitor_layout, startup=True))
  500. mock_run_service.assert_called_once_with(
  501. 'qubes.SetMonitorLayout', autostart=False, input=b'1920 1080 0 0\n')
  502. self.assertAllCalled()
  503. def test_061_send_monitor_layout_exclude(self):
  504. loop = asyncio.new_event_loop()
  505. asyncio.set_event_loop(loop)
  506. self.addCleanup(loop.close)
  507. self.app.expected_calls[
  508. ('dom0', 'admin.vm.List', None, None)] = \
  509. b'0\x00test-vm class=AppVM state=Running\n'
  510. self.app.expected_calls[
  511. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  512. 'no-monitor-layout', None)] = \
  513. b'0\x00True'
  514. vm = self.app.domains['test-vm']
  515. mock_run_service = unittest.mock.Mock()
  516. patch_run_service = unittest.mock.patch.object(
  517. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  518. mock_run_service)
  519. patch_run_service.start()
  520. self.addCleanup(patch_run_service.stop)
  521. monitor_layout = ['1920 1080 0 0\n']
  522. loop.run_until_complete(self.launcher.send_monitor_layout(
  523. vm, layout=monitor_layout, startup=True))
  524. self.assertFalse(mock_run_service.called)
  525. self.assertAllCalled()
  526. def test_062_send_monitor_layout_not_running(self):
  527. loop = asyncio.new_event_loop()
  528. asyncio.set_event_loop(loop)
  529. self.addCleanup(loop.close)
  530. self.app.expected_calls[
  531. ('dom0', 'admin.vm.List', None, None)] = \
  532. b'0\x00test-vm class=AppVM state=Halted\n'
  533. self.app.expected_calls[
  534. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  535. b'0\x00power_state=Halted'
  536. self.app.expected_calls[
  537. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  538. 'no-monitor-layout', None)] = \
  539. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  540. vm = self.app.domains['test-vm']
  541. mock_run_service = unittest.mock.Mock()
  542. patch_run_service = unittest.mock.patch.object(
  543. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  544. mock_run_service)
  545. patch_run_service.start()
  546. self.addCleanup(patch_run_service.stop)
  547. monitor_layout = ['1920 1080 0 0\n']
  548. loop.run_until_complete(self.launcher.send_monitor_layout(
  549. vm, layout=monitor_layout, startup=True))
  550. self.assertFalse(mock_run_service.called)
  551. self.assertAllCalled()
  552. def test_063_send_monitor_layout_signal_existing(self):
  553. loop = asyncio.new_event_loop()
  554. asyncio.set_event_loop(loop)
  555. self.addCleanup(loop.close)
  556. self.app.expected_calls[
  557. ('dom0', 'admin.vm.List', None, None)] = \
  558. b'0\x00test-vm class=AppVM state=Running\n'
  559. self.app.expected_calls[
  560. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  561. b'0\x00power_state=Running'
  562. self.app.expected_calls[
  563. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  564. b'0\x00default=False type=int 123'
  565. self.app.expected_calls[
  566. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  567. b'0\x00default=False type=int 124'
  568. self.app.expected_calls[
  569. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  570. 'no-monitor-layout', None)] = \
  571. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  572. vm = self.app.domains['test-vm']
  573. self.addCleanup(unittest.mock.patch.stopall)
  574. with tempfile.NamedTemporaryFile() as pidfile:
  575. pidfile.write(b'1234\n')
  576. pidfile.flush()
  577. patch_guid_pidfile = unittest.mock.patch.object(
  578. self.launcher, 'guid_pidfile')
  579. mock_guid_pidfile = patch_guid_pidfile.start()
  580. mock_guid_pidfile.return_value = pidfile.name
  581. mock_kill = unittest.mock.patch('os.kill').start()
  582. monitor_layout = ['1920 1080 0 0\n']
  583. loop.run_until_complete(self.launcher.send_monitor_layout(
  584. vm, layout=monitor_layout, startup=False))
  585. self.assertEqual(mock_guid_pidfile.mock_calls,
  586. [unittest.mock.call(123),
  587. unittest.mock.call(124)])
  588. self.assertEqual(mock_kill.mock_calls,
  589. [unittest.mock.call(1234, signal.SIGHUP),
  590. unittest.mock.call(1234, signal.SIGHUP)])
  591. self.assertAllCalled()
  592. def test_070_send_monitor_layout_all(self):
  593. loop = asyncio.new_event_loop()
  594. asyncio.set_event_loop(loop)
  595. self.addCleanup(loop.close)
  596. self.app.expected_calls[
  597. ('dom0', 'admin.vm.List', None, None)] = \
  598. b'0\x00test-vm class=AppVM state=Running\n' \
  599. b'test-vm2 class=AppVM state=Running\n' \
  600. b'test-vm3 class=AppVM state=Running\n' \
  601. b'test-vm4 class=AppVM state=Halted\n' \
  602. b'gui-vm class=AppVM state=Running'
  603. self.app.expected_calls[
  604. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  605. b'0\x00power_state=Running'
  606. self.app.expected_calls[
  607. ('test-vm2', 'admin.vm.CurrentState', None, None)] = \
  608. b'0\x00power_state=Running'
  609. self.app.expected_calls[
  610. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  611. b'0\x00power_state=Running'
  612. self.app.expected_calls[
  613. ('test-vm4', 'admin.vm.CurrentState', None, None)] = \
  614. b'0\x00power_state=Halted'
  615. self.app.expected_calls[
  616. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  617. 'gui', None)] = \
  618. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  619. self.app.expected_calls[
  620. ('test-vm2', 'admin.vm.feature.CheckWithTemplate',
  621. 'gui', None)] = \
  622. b'0\x00True'
  623. self.app.expected_calls[
  624. ('test-vm3', 'admin.vm.feature.CheckWithTemplate',
  625. 'gui', None)] = \
  626. b'0\x00'
  627. self.app.expected_calls[
  628. ('gui-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  629. b'0\x00default=True type=vm '
  630. self.app.expected_calls[
  631. ('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
  632. b'0\x00default=False type=vm gui-vm'
  633. self.app.expected_calls[
  634. ('test-vm2', 'admin.vm.property.Get', 'guivm', None)] = \
  635. b'0\x00default=False type=vm gui-vm'
  636. self.app.expected_calls[
  637. ('test-vm3', 'admin.vm.property.Get', 'guivm', None)] = \
  638. b'0\x00default=False type=vm gui-vm'
  639. self.app.expected_calls[
  640. ('test-vm4', 'admin.vm.property.Get', 'guivm', None)] = \
  641. b'0\x00default=False type=vm gui-vm'
  642. self.app._local_name = 'gui-vm'
  643. vm = self.app.domains['test-vm']
  644. vm2 = self.app.domains['test-vm2']
  645. self.addCleanup(unittest.mock.patch.stopall)
  646. mock_send_monitor_layout = unittest.mock.Mock()
  647. patch_send_monitor_layout = unittest.mock.patch.object(
  648. self.launcher, 'send_monitor_layout',
  649. functools.partial(self.mock_coroutine, mock_send_monitor_layout))
  650. patch_send_monitor_layout.start()
  651. monitor_layout = ['1920 1080 0 0\n']
  652. mock_get_monior_layout = unittest.mock.patch(
  653. 'qubesadmin.tools.qvm_start_daemon.get_monitor_layout').start()
  654. mock_get_monior_layout.return_value = monitor_layout
  655. self.launcher.send_monitor_layout_all()
  656. loop.stop()
  657. loop.run_forever()
  658. # test-vm3 not called b/c feature 'gui' set to false
  659. # test-vm4 not called b/c not running
  660. self.assertCountEqual(mock_send_monitor_layout.mock_calls,
  661. [unittest.mock.call(vm, monitor_layout),
  662. unittest.mock.call(vm2, monitor_layout)])
  663. mock_get_monior_layout.assert_called_once_with()
  664. self.assertAllCalled()