qvm_start_gui.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. # -*- encoding: utf8 -*-
  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 asyncio
  26. import qubesadmin.tests
  27. import qubesadmin.tools.qvm_start_gui
  28. import qubesadmin.vm
  29. class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
  30. def setUp(self):
  31. super(TC_00_qvm_start_gui, self).setUp()
  32. self.launcher = qubesadmin.tools.qvm_start_gui.GUILauncher(self.app)
  33. @unittest.mock.patch('subprocess.check_output')
  34. def test_000_kde_args(self, proc_mock):
  35. self.app.expected_calls[
  36. ('dom0', 'admin.vm.List', None, None)] = \
  37. b'0\x00test-vm class=AppVM state=Running\n'
  38. self.app.expected_calls[
  39. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  40. b'0\x00default=False type=label red'
  41. proc_mock.side_effect = [
  42. b'KWIN_RUNNING = 0x1\n',
  43. b'access control enabled, only authorized clients can connect\n'
  44. b'SI:localuser:root\n'
  45. b'SI:localuser:' + os.environ['USER'].encode() + b'\n',
  46. ]
  47. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  48. self.assertEqual(args, ['-T', '-p',
  49. '_KDE_NET_WM_COLOR_SCHEME=s:' +
  50. os.path.expanduser('~/.local/share/qubes-kde/red.colors')])
  51. self.assertAllCalled()
  52. @unittest.mock.patch('subprocess.check_output')
  53. def test_001_kde_args_none(self, proc_mock):
  54. self.app.expected_calls[
  55. ('dom0', 'admin.vm.List', None, None)] = \
  56. b'0\x00test-vm class=AppVM state=Running\n'
  57. proc_mock.side_effect = [b'']
  58. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  59. self.assertEqual(args, [])
  60. self.assertAllCalled()
  61. def test_010_common_args(self):
  62. self.app.expected_calls[
  63. ('dom0', 'admin.vm.List', None, None)] = \
  64. b'0\x00test-vm class=AppVM state=Running\n'
  65. self.app.expected_calls[
  66. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  67. b'0\x00default=False type=label red'
  68. self.app.expected_calls[
  69. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  70. b'0\x00default=False type=bool False'
  71. self.app.expected_calls[
  72. ('dom0', 'admin.label.Get', 'red', None)] = \
  73. b'0\x000xff0000'
  74. self.app.expected_calls[
  75. ('dom0', 'admin.label.Index', 'red', None)] = \
  76. b'0\x001'
  77. self.app.expected_calls[
  78. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  79. 'rpc-clipboard', None)] = \
  80. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  81. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  82. kde_mock:
  83. kde_mock.return_value = []
  84. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  85. self.assertEqual(args, [
  86. '/usr/bin/qubes-guid', '-N', 'test-vm',
  87. '-c', '0xff0000',
  88. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  89. '-l', '1', '-q'])
  90. self.assertAllCalled()
  91. def test_011_common_args_debug(self):
  92. self.app.expected_calls[
  93. ('dom0', 'admin.vm.List', None, None)] = \
  94. b'0\x00test-vm class=AppVM state=Running\n'
  95. self.app.expected_calls[
  96. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  97. b'0\x00default=False type=label red'
  98. self.app.expected_calls[
  99. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  100. b'0\x00default=False type=bool True'
  101. self.app.expected_calls[
  102. ('dom0', 'admin.label.Get', 'red', None)] = \
  103. b'0\x000xff0000'
  104. self.app.expected_calls[
  105. ('dom0', 'admin.label.Index', 'red', None)] = \
  106. b'0\x001'
  107. self.app.expected_calls[
  108. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  109. 'rpc-clipboard', None)] = \
  110. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  111. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  112. kde_mock:
  113. kde_mock.return_value = []
  114. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  115. self.assertEqual(args, [
  116. '/usr/bin/qubes-guid', '-N', 'test-vm',
  117. '-c', '0xff0000',
  118. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  119. '-l', '1', '-v', '-v'])
  120. self.assertAllCalled()
  121. def test_012_common_args_rpc_clipboard(self):
  122. self.app.expected_calls[
  123. ('dom0', 'admin.vm.List', None, None)] = \
  124. b'0\x00test-vm class=AppVM state=Running\n'
  125. self.app.expected_calls[
  126. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  127. b'0\x00default=False type=label red'
  128. self.app.expected_calls[
  129. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  130. b'0\x00default=False type=bool False'
  131. self.app.expected_calls[
  132. ('dom0', 'admin.label.Get', 'red', None)] = \
  133. b'0\x000xff0000'
  134. self.app.expected_calls[
  135. ('dom0', 'admin.label.Index', 'red', None)] = \
  136. b'0\x001'
  137. self.app.expected_calls[
  138. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  139. 'rpc-clipboard', None)] = \
  140. b'0\x001'
  141. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  142. kde_mock:
  143. kde_mock.return_value = []
  144. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  145. self.assertEqual(args, [
  146. '/usr/bin/qubes-guid', '-N', 'test-vm',
  147. '-c', '0xff0000',
  148. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  149. '-l', '1', '-q', '-Q'])
  150. self.assertAllCalled()
  151. @unittest.mock.patch('asyncio.create_subprocess_exec')
  152. def test_020_start_gui_for_vm(self, proc_mock):
  153. loop = asyncio.new_event_loop()
  154. asyncio.set_event_loop(loop)
  155. self.addCleanup(loop.close)
  156. self.app.expected_calls[
  157. ('dom0', 'admin.vm.List', None, None)] = \
  158. b'0\x00test-vm class=AppVM state=Running\n'
  159. self.app.expected_calls[
  160. ('test-vm', 'admin.vm.List', None, None)] = \
  161. b'0\x00test-vm class=AppVM state=Running\n'
  162. self.app.expected_calls[
  163. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  164. b'0\x00default=False type=int 3000'
  165. self.app.expected_calls[
  166. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  167. b'0\x00default=False type=str pv'
  168. self.app.expected_calls[
  169. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  170. 'no-monitor-layout', None)] = \
  171. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  172. with unittest.mock.patch.object(self.launcher,
  173. 'common_guid_args', lambda vm: []):
  174. loop.run_until_complete(self.launcher.start_gui_for_vm(
  175. self.app.domains['test-vm']))
  176. # common arguments dropped for simplicity
  177. proc_mock.assert_called_once_with('-d', '3000')
  178. self.assertAllCalled()
  179. @unittest.mock.patch('asyncio.create_subprocess_exec')
  180. def test_021_start_gui_for_vm_hvm(self, proc_mock):
  181. loop = asyncio.new_event_loop()
  182. asyncio.set_event_loop(loop)
  183. self.addCleanup(loop.close)
  184. self.app.expected_calls[
  185. ('dom0', 'admin.vm.List', None, None)] = \
  186. b'0\x00test-vm class=AppVM state=Running\n'
  187. self.app.expected_calls[
  188. ('test-vm', 'admin.vm.List', None, None)] = \
  189. b'0\x00test-vm class=AppVM state=Running\n'
  190. self.app.expected_calls[
  191. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  192. b'0\x00default=False type=int 3000'
  193. self.app.expected_calls[
  194. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  195. b'0\x00default=False type=int 3001'
  196. self.app.expected_calls[
  197. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  198. b'0\x00default=False type=str hvm'
  199. self.app.expected_calls[
  200. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  201. b'0\x00default=False type=bool False'
  202. self.app.expected_calls[
  203. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  204. 'no-monitor-layout', None)] = \
  205. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  206. with unittest.mock.patch.object(self.launcher,
  207. 'common_guid_args', lambda vm: []):
  208. loop.run_until_complete(self.launcher.start_gui_for_vm(
  209. self.app.domains['test-vm']))
  210. # common arguments dropped for simplicity
  211. proc_mock.assert_called_once_with('-d', '3000', '-n')
  212. self.assertAllCalled()
  213. def test_022_start_gui_for_vm_hvm_stubdom(self):
  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.List', None, None)] = \
  222. b'0\x00test-vm class=AppVM state=Running\n'
  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. pidfile = tempfile.NamedTemporaryFile()
  240. pidfile.write(b'1234\n')
  241. pidfile.flush()
  242. self.addCleanup(pidfile.close)
  243. patch_proc = unittest.mock.patch('asyncio.create_subprocess_exec')
  244. patch_args = unittest.mock.patch.object(self.launcher,
  245. 'common_guid_args', lambda vm: [])
  246. patch_pidfile = unittest.mock.patch.object(self.launcher,
  247. 'guid_pidfile', lambda vm: pidfile.name)
  248. try:
  249. mock_proc = patch_proc.start()
  250. patch_args.start()
  251. patch_pidfile.start()
  252. loop.run_until_complete(self.launcher.start_gui_for_vm(
  253. self.app.domains['test-vm']))
  254. # common arguments dropped for simplicity
  255. mock_proc.assert_called_once_with(
  256. '-d', '3000', '-n', '-K', '1234')
  257. finally:
  258. unittest.mock.patch.stopall()
  259. self.assertAllCalled()
  260. def test_030_start_gui_for_stubdomain(self):
  261. loop = asyncio.new_event_loop()
  262. asyncio.set_event_loop(loop)
  263. self.addCleanup(loop.close)
  264. self.app.expected_calls[
  265. ('dom0', 'admin.vm.List', None, None)] = \
  266. b'0\x00test-vm class=AppVM state=Running\n'
  267. self.app.expected_calls[
  268. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  269. b'0\x00default=False type=int 3000'
  270. self.app.expected_calls[
  271. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  272. b'0\x00default=False type=int 3001'
  273. self.app.expected_calls[
  274. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  275. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  276. self.app.expected_calls[
  277. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  278. None)] = \
  279. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  280. proc_mock = unittest.mock.Mock()
  281. with unittest.mock.patch('asyncio.create_subprocess_exec',
  282. lambda *args: self.mock_coroutine(proc_mock, *args)):
  283. with unittest.mock.patch.object(self.launcher,
  284. 'common_guid_args', lambda vm: []):
  285. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  286. self.app.domains['test-vm']))
  287. # common arguments dropped for simplicity
  288. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  289. self.assertAllCalled()
  290. def test_031_start_gui_for_stubdomain_forced(self):
  291. loop = asyncio.new_event_loop()
  292. asyncio.set_event_loop(loop)
  293. self.addCleanup(loop.close)
  294. self.app.expected_calls[
  295. ('dom0', 'admin.vm.List', None, None)] = \
  296. b'0\x00test-vm class=AppVM state=Running\n'
  297. self.app.expected_calls[
  298. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  299. b'0\x00default=False type=int 3000'
  300. self.app.expected_calls[
  301. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  302. b'0\x00default=False type=int 3001'
  303. # self.app.expected_calls[
  304. # ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  305. # b'0\x00'
  306. self.app.expected_calls[
  307. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated',
  308. None)] = \
  309. b'0\x001'
  310. proc_mock = unittest.mock.Mock()
  311. with unittest.mock.patch('asyncio.create_subprocess_exec',
  312. lambda *args: self.mock_coroutine(proc_mock, *args)):
  313. with unittest.mock.patch.object(self.launcher,
  314. 'common_guid_args', lambda vm: []):
  315. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  316. self.app.domains['test-vm']))
  317. # common arguments dropped for simplicity
  318. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  319. self.assertAllCalled()
  320. @asyncio.coroutine
  321. def mock_coroutine(self, mock, *args, **kwargs):
  322. mock(*args, **kwargs)
  323. def test_040_start_gui(self):
  324. loop = asyncio.new_event_loop()
  325. asyncio.set_event_loop(loop)
  326. self.addCleanup(loop.close)
  327. self.app.expected_calls[
  328. ('dom0', 'admin.vm.List', None, None)] = \
  329. b'0\x00test-vm class=AppVM state=Running\n'
  330. self.app.expected_calls[
  331. ('test-vm', 'admin.vm.List', None, None)] = \
  332. b'0\x00test-vm class=AppVM state=Running\n'
  333. self.app.expected_calls[
  334. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  335. b'0\x00True'
  336. self.app.expected_calls[
  337. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  338. 'no-monitor-layout', None)] = \
  339. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  340. self.app.expected_calls[
  341. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  342. b'0\x00default=False type=str hvm'
  343. self.app.expected_calls[
  344. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  345. b'0\x00default=False type=int 3000'
  346. self.app.expected_calls[
  347. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  348. b'0\x00default=False type=int 3001'
  349. vm = self.app.domains['test-vm']
  350. mock_start_vm = unittest.mock.Mock()
  351. mock_start_stubdomain = unittest.mock.Mock()
  352. patch_start_vm = unittest.mock.patch.object(
  353. self.launcher, 'start_gui_for_vm', functools.partial(
  354. self.mock_coroutine, mock_start_vm))
  355. patch_start_stubdomain = unittest.mock.patch.object(
  356. self.launcher, 'start_gui_for_stubdomain', lambda vm_, force:
  357. self.mock_coroutine(mock_start_stubdomain, vm_))
  358. try:
  359. patch_start_vm.start()
  360. patch_start_stubdomain.start()
  361. loop.run_until_complete(self.launcher.start_gui(vm))
  362. mock_start_vm.assert_called_once_with(vm, monitor_layout=None)
  363. mock_start_stubdomain.assert_called_once_with(vm)
  364. finally:
  365. unittest.mock.patch.stopall()
  366. def test_041_start_gui_running(self):
  367. # simulate existing pidfiles, should not start processes
  368. self.skipTest('todo')
  369. def test_042_start_gui_pvh(self):
  370. # PVH - no stubdomain
  371. self.skipTest('todo')
  372. @unittest.mock.patch('subprocess.Popen')
  373. def test_050_get_monitor_layout1(self, proc_mock):
  374. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  375. HDMI1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
  376. 1920x1200 59.95*+
  377. 1920x1080 60.00 50.00 59.94
  378. 1920x1080i 60.00 50.00 59.94
  379. 1600x1200 60.00
  380. 1680x1050 59.88
  381. 1280x1024 60.02
  382. 1440x900 59.90
  383. 1280x960 60.00
  384. 1280x720 60.00 50.00 59.94
  385. 1024x768 60.00
  386. 800x600 60.32
  387. 720x576 50.00
  388. 720x480 60.00 59.94
  389. 720x480i 60.00 59.94
  390. 640x480 60.00 59.94
  391. HDMI2 disconnected (normal left inverted right x axis y axis)
  392. VGA1 disconnected (normal left inverted right x axis y axis)
  393. VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  394. '''.splitlines()
  395. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  396. ['1920 1200 0 0\n'])
  397. @unittest.mock.patch('subprocess.Popen')
  398. def test_051_get_monitor_layout_multiple(self, proc_mock):
  399. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 2880 x 1024, maximum 32767 x 32767
  400. LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis)
  401. VGA1 connected 1280x1024+1600+0 (normal left inverted right x axis y axis)
  402. '''.splitlines()
  403. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  404. ['1600 900 0 0\n', '1280 1024 1600 0\n'])
  405. @unittest.mock.patch('subprocess.Popen')
  406. def test_052_get_monitor_layout_hidpi1(self, proc_mock):
  407. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  408. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x 208mm
  409. 1920x1200 60.00*+
  410. '''.splitlines()
  411. dpi = 150
  412. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  413. ['2560 1920 0 0 {} {}\n'.format(
  414. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  415. @unittest.mock.patch('subprocess.Popen')
  416. def test_052_get_monitor_layout_hidpi2(self, proc_mock):
  417. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  418. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x 174mm
  419. 1920x1200 60.00*+
  420. '''.splitlines()
  421. dpi = 200
  422. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  423. ['2560 1920 0 0 {} {}\n'.format(
  424. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  425. @unittest.mock.patch('subprocess.Popen')
  426. def test_052_get_monitor_layout_hidpi3(self, proc_mock):
  427. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  428. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x 116mm
  429. 1920x1200 60.00*+
  430. '''.splitlines()
  431. dpi = 300
  432. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  433. ['2560 1920 0 0 {} {}\n'.format(
  434. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  435. def test_060_send_monitor_layout(self):
  436. loop = asyncio.new_event_loop()
  437. asyncio.set_event_loop(loop)
  438. self.addCleanup(loop.close)
  439. self.app.expected_calls[
  440. ('dom0', 'admin.vm.List', None, None)] = \
  441. b'0\x00test-vm class=AppVM state=Running\n'
  442. self.app.expected_calls[
  443. ('test-vm', 'admin.vm.List', None, None)] = \
  444. b'0\x00test-vm class=AppVM state=Running\n'
  445. self.app.expected_calls[
  446. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  447. 'no-monitor-layout', None)] = \
  448. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  449. vm = self.app.domains['test-vm']
  450. mock_run_service = unittest.mock.Mock(spec={})
  451. patch_run_service = unittest.mock.patch.object(
  452. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  453. mock_run_service)
  454. patch_run_service.start()
  455. self.addCleanup(patch_run_service.stop)
  456. monitor_layout = ['1920 1080 0 0\n']
  457. loop.run_until_complete(self.launcher.send_monitor_layout(
  458. vm, layout=monitor_layout, startup=True))
  459. mock_run_service.assert_called_once_with(
  460. 'qubes.SetMonitorLayout', b'1920 1080 0 0\n')
  461. self.assertAllCalled()
  462. def test_061_send_monitor_layout_exclude(self):
  463. loop = asyncio.new_event_loop()
  464. asyncio.set_event_loop(loop)
  465. self.addCleanup(loop.close)
  466. self.app.expected_calls[
  467. ('dom0', 'admin.vm.List', None, None)] = \
  468. b'0\x00test-vm class=AppVM state=Running\n'
  469. self.app.expected_calls[
  470. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  471. 'no-monitor-layout', None)] = \
  472. b'0\x00True'
  473. vm = self.app.domains['test-vm']
  474. mock_run_service = unittest.mock.Mock()
  475. patch_run_service = unittest.mock.patch.object(
  476. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  477. mock_run_service)
  478. patch_run_service.start()
  479. self.addCleanup(patch_run_service.stop)
  480. monitor_layout = ['1920 1080 0 0\n']
  481. loop.run_until_complete(self.launcher.send_monitor_layout(
  482. vm, layout=monitor_layout, startup=True))
  483. self.assertFalse(mock_run_service.called)
  484. self.assertAllCalled()
  485. def test_062_send_monitor_layout_not_running(self):
  486. loop = asyncio.new_event_loop()
  487. asyncio.set_event_loop(loop)
  488. self.addCleanup(loop.close)
  489. self.app.expected_calls[
  490. ('dom0', 'admin.vm.List', None, None)] = \
  491. b'0\x00test-vm class=AppVM state=Halted\n'
  492. self.app.expected_calls[
  493. ('test-vm', 'admin.vm.List', None, None)] = \
  494. b'0\x00test-vm class=AppVM state=Halted\n'
  495. self.app.expected_calls[
  496. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  497. 'no-monitor-layout', None)] = \
  498. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  499. vm = self.app.domains['test-vm']
  500. mock_run_service = unittest.mock.Mock()
  501. patch_run_service = unittest.mock.patch.object(
  502. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  503. mock_run_service)
  504. patch_run_service.start()
  505. self.addCleanup(patch_run_service.stop)
  506. monitor_layout = ['1920 1080 0 0\n']
  507. loop.run_until_complete(self.launcher.send_monitor_layout(
  508. vm, layout=monitor_layout, startup=True))
  509. self.assertFalse(mock_run_service.called)
  510. self.assertAllCalled()
  511. def test_063_send_monitor_layout_signal_existing(self):
  512. loop = asyncio.new_event_loop()
  513. asyncio.set_event_loop(loop)
  514. self.addCleanup(loop.close)
  515. self.app.expected_calls[
  516. ('dom0', 'admin.vm.List', None, None)] = \
  517. b'0\x00test-vm class=AppVM state=Running\n'
  518. self.app.expected_calls[
  519. ('test-vm', 'admin.vm.List', None, None)] = \
  520. b'0\x00test-vm class=AppVM state=Running\n'
  521. self.app.expected_calls[
  522. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  523. b'0\x00default=False type=int 123'
  524. self.app.expected_calls[
  525. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  526. b'0\x00default=False type=int 124'
  527. self.app.expected_calls[
  528. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  529. 'no-monitor-layout', None)] = \
  530. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  531. vm = self.app.domains['test-vm']
  532. self.addCleanup(unittest.mock.patch.stopall)
  533. with tempfile.NamedTemporaryFile() as pidfile:
  534. pidfile.write(b'1234\n')
  535. pidfile.flush()
  536. patch_guid_pidfile = unittest.mock.patch.object(
  537. self.launcher, 'guid_pidfile')
  538. mock_guid_pidfile = patch_guid_pidfile.start()
  539. mock_guid_pidfile.return_value = pidfile.name
  540. mock_kill = unittest.mock.patch('os.kill').start()
  541. monitor_layout = ['1920 1080 0 0\n']
  542. loop.run_until_complete(self.launcher.send_monitor_layout(
  543. vm, layout=monitor_layout, startup=False))
  544. self.assertEqual(mock_guid_pidfile.mock_calls,
  545. [unittest.mock.call(123),
  546. unittest.mock.call(124)])
  547. self.assertEqual(mock_kill.mock_calls,
  548. [unittest.mock.call(1234, signal.SIGHUP),
  549. unittest.mock.call(1234, signal.SIGHUP)])
  550. self.assertAllCalled()
  551. def test_070_send_monitor_layout_all(self):
  552. loop = asyncio.new_event_loop()
  553. asyncio.set_event_loop(loop)
  554. self.addCleanup(loop.close)
  555. self.app.expected_calls[
  556. ('dom0', 'admin.vm.List', None, None)] = \
  557. b'0\x00test-vm class=AppVM state=Running\n' \
  558. b'test-vm2 class=AppVM state=Running\n' \
  559. b'test-vm3 class=AppVM state=Runnig\n' \
  560. b'test-vm4 class=AppVM state=Halted\n'
  561. self.app.expected_calls[
  562. ('test-vm', 'admin.vm.List', None, None)] = \
  563. b'0\x00test-vm class=AppVM state=Running\n'
  564. self.app.expected_calls[
  565. ('test-vm2', 'admin.vm.List', None, None)] = \
  566. b'0\x00test-vm2 class=AppVM state=Running\n'
  567. self.app.expected_calls[
  568. ('test-vm3', 'admin.vm.List', None, None)] = \
  569. b'0\x00test-vm3 class=AppVM state=Running\n'
  570. self.app.expected_calls[
  571. ('test-vm4', 'admin.vm.List', None, None)] = \
  572. b'0\x00test-vm4 class=AppVM state=Halted\n'
  573. self.app.expected_calls[
  574. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  575. 'gui', None)] = \
  576. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  577. self.app.expected_calls[
  578. ('test-vm2', 'admin.vm.feature.CheckWithTemplate',
  579. 'gui', None)] = \
  580. b'0\x00True'
  581. self.app.expected_calls[
  582. ('test-vm3', 'admin.vm.feature.CheckWithTemplate',
  583. 'gui', None)] = \
  584. b'0\x00'
  585. vm = self.app.domains['test-vm']
  586. vm2 = self.app.domains['test-vm2']
  587. self.addCleanup(unittest.mock.patch.stopall)
  588. mock_send_monitor_layout = unittest.mock.Mock()
  589. patch_send_monitor_layout = unittest.mock.patch.object(
  590. self.launcher, 'send_monitor_layout',
  591. functools.partial(self.mock_coroutine, mock_send_monitor_layout))
  592. patch_send_monitor_layout.start()
  593. monitor_layout = ['1920 1080 0 0\n']
  594. mock_get_monior_layout = unittest.mock.patch(
  595. 'qubesadmin.tools.qvm_start_gui.get_monitor_layout').start()
  596. mock_get_monior_layout.return_value = monitor_layout
  597. self.launcher.send_monitor_layout_all()
  598. loop.stop()
  599. loop.run_forever()
  600. # test-vm3 not called b/c feature 'gui' set to false
  601. # test-vm4 not called b/c not running
  602. self.assertCountEqual(mock_send_monitor_layout.mock_calls,
  603. [unittest.mock.call(vm, monitor_layout),
  604. unittest.mock.call(vm2, monitor_layout)])
  605. mock_get_monior_layout.assert_called_once_with()
  606. self.assertAllCalled()