qvm_start_daemon.py 30 KB

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