api_internal.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2019 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 General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import asyncio
  21. import subprocess
  22. import qubes.api.internal
  23. import qubes.tests
  24. import qubes.vm.adminvm
  25. from unittest import mock
  26. from mock import call
  27. def mock_coro(f):
  28. @asyncio.coroutine
  29. def coro_f(*args, **kwargs):
  30. return f(*args, **kwargs)
  31. return coro_f
  32. class TC_00_API_Misc(qubes.tests.QubesTestCase):
  33. def setUp(self):
  34. super(TC_00_API_Misc, self).setUp()
  35. self.tpl = mock.NonCallableMagicMock(name='template')
  36. del self.tpl.template
  37. self.src = mock.NonCallableMagicMock(name='appvm',
  38. template=self.tpl)
  39. self.app = mock.NonCallableMock()
  40. self.dest = mock.NonCallableMock()
  41. self.dest.name = 'dom0'
  42. self.app.configure_mock(domains={
  43. 'dom0': self.dest,
  44. 'test-vm': self.src,
  45. })
  46. def configure_qdb(self, entries):
  47. self.src.configure_mock(**{
  48. 'untrusted_qdb.read.side_effect': (
  49. lambda path: entries.get(path, None)),
  50. 'untrusted_qdb.list.side_effect': (
  51. lambda path: sorted(entries.keys())),
  52. })
  53. def create_mockvm(self, features=None):
  54. if features is None:
  55. features = {}
  56. vm = mock.Mock()
  57. vm.features.check_with_template.side_effect = features.get
  58. vm.run_service.return_value.wait = mock_coro(
  59. vm.run_service.return_value.wait)
  60. vm.run_service = mock_coro(vm.run_service)
  61. vm.suspend = mock_coro(vm.suspend)
  62. vm.resume = mock_coro(vm.resume)
  63. return vm
  64. def call_mgmt_func(self, method, arg=b'', payload=b''):
  65. mgmt_obj = qubes.api.internal.QubesInternalAPI(self.app,
  66. b'dom0', method, b'dom0', arg)
  67. loop = asyncio.get_event_loop()
  68. response = loop.run_until_complete(
  69. mgmt_obj.execute(untrusted_payload=payload))
  70. return response
  71. def test_000_suspend_pre(self):
  72. dom0 = mock.NonCallableMock(spec=qubes.vm.adminvm.AdminVM)
  73. running_vm = self.create_mockvm(features={'qrexec': True})
  74. running_vm.is_running.return_value = True
  75. not_running_vm = self.create_mockvm(features={'qrexec': True})
  76. not_running_vm.is_running.return_value = False
  77. no_qrexec_vm = self.create_mockvm()
  78. no_qrexec_vm.is_running.return_value = True
  79. domains_dict = {
  80. 'dom0': dom0,
  81. 'running': running_vm,
  82. 'not-running': not_running_vm,
  83. 'no-qrexec': no_qrexec_vm,
  84. }
  85. self.addCleanup(domains_dict.clear)
  86. self.app.domains = mock.MagicMock(**{
  87. '__iter__': lambda _: iter(domains_dict.values()),
  88. '__getitem__': domains_dict.get,
  89. })
  90. ret = self.call_mgmt_func(b'internal.SuspendPre')
  91. self.assertIsNone(ret)
  92. self.assertFalse(dom0.called)
  93. self.assertNotIn(('run_service', ('qubes.SuspendPreAll',), mock.ANY),
  94. not_running_vm.mock_calls)
  95. self.assertNotIn(('suspend', (), {}),
  96. not_running_vm.mock_calls)
  97. self.assertIn(('run_service', ('qubes.SuspendPreAll',), mock.ANY),
  98. running_vm.mock_calls)
  99. self.assertIn(('suspend', (), {}),
  100. running_vm.mock_calls)
  101. self.assertNotIn(('run_service', ('qubes.SuspendPreAll',), mock.ANY),
  102. no_qrexec_vm.mock_calls)
  103. self.assertIn(('suspend', (), {}),
  104. no_qrexec_vm.mock_calls)
  105. def test_001_suspend_post(self):
  106. dom0 = mock.NonCallableMock(spec=qubes.vm.adminvm.AdminVM)
  107. running_vm = self.create_mockvm(features={'qrexec': True})
  108. running_vm.is_running.return_value = True
  109. running_vm.get_power_state.return_value = 'Suspended'
  110. not_running_vm = self.create_mockvm(features={'qrexec': True})
  111. not_running_vm.is_running.return_value = False
  112. not_running_vm.get_power_state.return_value = 'Halted'
  113. no_qrexec_vm = self.create_mockvm()
  114. no_qrexec_vm.is_running.return_value = True
  115. no_qrexec_vm.get_power_state.return_value = 'Suspended'
  116. domains_dict = {
  117. 'dom0': dom0,
  118. 'running': running_vm,
  119. 'not-running': not_running_vm,
  120. 'no-qrexec': no_qrexec_vm,
  121. }
  122. self.addCleanup(domains_dict.clear)
  123. self.app.domains = mock.MagicMock(**{
  124. '__iter__': lambda _: iter(domains_dict.values()),
  125. '__getitem__': domains_dict.get,
  126. })
  127. ret = self.call_mgmt_func(b'internal.SuspendPost')
  128. self.assertIsNone(ret)
  129. self.assertFalse(dom0.called)
  130. self.assertNotIn(('run_service', ('qubes.SuspendPostAll',), mock.ANY),
  131. not_running_vm.mock_calls)
  132. self.assertNotIn(('resume', (), {}),
  133. not_running_vm.mock_calls)
  134. self.assertIn(('run_service', ('qubes.SuspendPostAll',), mock.ANY),
  135. running_vm.mock_calls)
  136. self.assertIn(('resume', (), {}),
  137. running_vm.mock_calls)
  138. self.assertNotIn(('run_service', ('qubes.SuspendPostAll',), mock.ANY),
  139. no_qrexec_vm.mock_calls)
  140. self.assertIn(('resume', (), {}),
  141. no_qrexec_vm.mock_calls)