api_misc.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. import asyncio
  21. from unittest import mock
  22. import qubes.tests
  23. import qubes.api.misc
  24. class TC_00_API_Misc(qubes.tests.QubesTestCase):
  25. def setUp(self):
  26. super(TC_00_API_Misc, self).setUp()
  27. self.tpl = mock.NonCallableMagicMock(name='template')
  28. del self.tpl.template
  29. self.src = mock.NonCallableMagicMock(name='appvm',
  30. template=self.tpl)
  31. self.app = mock.NonCallableMock()
  32. self.dest = mock.NonCallableMock()
  33. self.dest.name = 'dom0'
  34. self.app.configure_mock(domains={
  35. 'dom0': self.dest,
  36. 'test-vm': self.src,
  37. })
  38. def configure_qdb(self, entries):
  39. self.src.configure_mock(**{
  40. 'untrusted_qdb.read.side_effect': (
  41. lambda path: entries.get(path, None)),
  42. 'untrusted_qdb.list.side_effect': (
  43. lambda path: sorted(entries.keys())),
  44. })
  45. def call_mgmt_func(self, method, arg=b'', payload=b''):
  46. mgmt_obj = qubes.api.misc.QubesMiscAPI(self.app,
  47. b'test-vm', method, b'dom0', arg)
  48. loop = asyncio.get_event_loop()
  49. response = loop.run_until_complete(
  50. mgmt_obj.execute(untrusted_payload=payload))
  51. return response
  52. def test_000_features_request(self):
  53. qdb_entries = {
  54. '/features-request/feature1': b'1',
  55. '/features-request/feature2': b'',
  56. '/features-request/feature3': b'other',
  57. }
  58. self.configure_qdb(qdb_entries)
  59. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  60. self.assertIsNone(response)
  61. self.assertEqual(self.app.mock_calls, [
  62. mock.call.save()
  63. ])
  64. self.assertEqual(self.src.mock_calls, [
  65. mock.call.untrusted_qdb.list('/features-request/'),
  66. mock.call.untrusted_qdb.read('/features-request/feature1'),
  67. mock.call.untrusted_qdb.read('/features-request/feature2'),
  68. mock.call.untrusted_qdb.read('/features-request/feature3'),
  69. mock.call.fire_event_async('features-request', untrusted_features={
  70. 'feature1': '1', 'feature2': '', 'feature3': 'other'}),
  71. ('fire_event_async().__iter__', (), {}),
  72. ])
  73. def test_001_features_request_empty(self):
  74. self.configure_qdb({})
  75. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  76. self.assertIsNone(response)
  77. self.assertEqual(self.app.mock_calls, [
  78. mock.call.save()
  79. ])
  80. self.assertEqual(self.src.mock_calls, [
  81. mock.call.untrusted_qdb.list('/features-request/'),
  82. mock.call.fire_event_async('features-request',
  83. untrusted_features={}),
  84. ('fire_event_async().__iter__', (), {}),
  85. ])
  86. def test_002_features_request_invalid1(self):
  87. qdb_entries = {
  88. '/features-request/feature1': b'test spaces',
  89. }
  90. self.configure_qdb(qdb_entries)
  91. with self.assertRaises(qubes.api.PermissionDenied):
  92. self.call_mgmt_func(b'qubes.FeaturesRequest')
  93. self.assertEqual(self.app.mock_calls, [])
  94. self.assertEqual(self.src.mock_calls, [
  95. mock.call.untrusted_qdb.list('/features-request/'),
  96. mock.call.untrusted_qdb.read('/features-request/feature1'),
  97. ])
  98. def test_003_features_request_invalid2(self):
  99. qdb_entries = {
  100. '/features-request/feature1': b'\xfe\x01',
  101. }
  102. self.configure_qdb(qdb_entries)
  103. with self.assertRaises(UnicodeDecodeError):
  104. self.call_mgmt_func(b'qubes.FeaturesRequest')
  105. self.assertEqual(self.app.mock_calls, [])
  106. self.assertEqual(self.src.mock_calls, [
  107. mock.call.untrusted_qdb.list('/features-request/'),
  108. mock.call.untrusted_qdb.read('/features-request/feature1'),
  109. ])
  110. def test_010_notify_tools(self):
  111. qdb_entries = {
  112. '/qubes-tools/version': b'1',
  113. '/qubes-tools/qrexec': b'1',
  114. '/qubes-tools/gui': b'1',
  115. '/qubes-tools/os': b'Linux',
  116. '/qubes-tools/default-user': b'user',
  117. }
  118. self.configure_qdb(qdb_entries)
  119. response = self.call_mgmt_func(b'qubes.NotifyTools')
  120. self.assertIsNone(response)
  121. self.assertEqual(self.app.mock_calls, [
  122. mock.call.save()
  123. ])
  124. self.assertEqual(self.src.mock_calls, [
  125. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  126. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  127. mock.call.untrusted_qdb.read('/qubes-tools/gui-emulated'),
  128. mock.call.untrusted_qdb.read('/qubes-tools/default-user'),
  129. mock.call.untrusted_qdb.read('/qubes-tools/os'),
  130. mock.call.fire_event_async('features-request', untrusted_features={
  131. 'gui': '1',
  132. 'default-user': 'user',
  133. 'qrexec': '1',
  134. 'os': 'Linux'}),
  135. ('fire_event_async().__iter__', (), {}),
  136. ])
  137. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  138. def test_013_notify_tools_no_version(self):
  139. qdb_entries = {
  140. '/qubes-tools/qrexec': b'1',
  141. '/qubes-tools/gui': b'1',
  142. '/qubes-tools/os': b'Linux',
  143. '/qubes-tools/default-user': b'user',
  144. }
  145. self.configure_qdb(qdb_entries)
  146. response = self.call_mgmt_func(b'qubes.NotifyTools')
  147. self.assertIsNone(response)
  148. self.assertEqual(self.src.mock_calls, [
  149. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  150. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  151. mock.call.untrusted_qdb.read('/qubes-tools/gui-emulated'),
  152. mock.call.untrusted_qdb.read('/qubes-tools/default-user'),
  153. mock.call.untrusted_qdb.read('/qubes-tools/os'),
  154. mock.call.fire_event_async('features-request', untrusted_features={
  155. 'gui': '1',
  156. 'default-user': 'user',
  157. 'qrexec': '1',
  158. 'os': 'Linux'}),
  159. ('fire_event_async().__iter__', (), {}),
  160. ])
  161. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  162. def test_015_notify_tools_invalid_value_qrexec(self):
  163. qdb_entries = {
  164. '/qubes-tools/version': b'1',
  165. '/qubes-tools/qrexec': b'invalid value',
  166. '/qubes-tools/gui': b'0',
  167. '/qubes-tools/os': b'Linux',
  168. '/qubes-tools/default-user': b'user',
  169. }
  170. self.configure_qdb(qdb_entries)
  171. with self.assertRaises(qubes.api.PermissionDenied):
  172. self.call_mgmt_func(b'qubes.NotifyTools')
  173. self.assertEqual(self.app.mock_calls, [])
  174. self.assertEqual(self.src.mock_calls, [
  175. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  176. ])
  177. def test_016_notify_tools_invalid_value_gui(self):
  178. qdb_entries = {
  179. '/qubes-tools/version': b'1',
  180. '/qubes-tools/qrexec': b'1',
  181. '/qubes-tools/gui': b'invalid value',
  182. '/qubes-tools/os': b'Linux',
  183. '/qubes-tools/default-user': b'user',
  184. }
  185. self.configure_qdb(qdb_entries)
  186. with self.assertRaises(qubes.api.PermissionDenied):
  187. self.call_mgmt_func(b'qubes.NotifyTools')
  188. self.assertEqual(self.app.mock_calls, [])
  189. self.assertEqual(self.src.mock_calls, [
  190. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  191. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  192. ])
  193. def test_020_notify_updates_standalone(self):
  194. del self.src.template
  195. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  196. self.assertIsNone(response)
  197. self.assertEqual(self.src.mock_calls, [
  198. mock.call.updateable.__bool__(),
  199. mock.call.features.__setitem__('updates-available', True),
  200. ])
  201. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  202. def test_021_notify_updates_standalone2(self):
  203. del self.src.template
  204. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  205. self.assertIsNone(response)
  206. self.assertEqual(self.src.mock_calls, [
  207. mock.call.updateable.__bool__(),
  208. mock.call.features.__setitem__('updates-available', False),
  209. ])
  210. self.assertEqual(self.app.mock_calls, [
  211. mock.call.save()
  212. ])
  213. def test_022_notify_updates_invalid(self):
  214. del self.src.template
  215. with self.assertRaises(qubes.api.PermissionDenied):
  216. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'')
  217. self.assertEqual(self.src.mock_calls, [])
  218. self.assertEqual(self.tpl.mock_calls, [])
  219. self.assertEqual(self.app.mock_calls, [])
  220. def test_023_notify_updates_invalid2(self):
  221. del self.src.template
  222. with self.assertRaises(qubes.api.PermissionDenied):
  223. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'no updates')
  224. self.assertEqual(self.src.mock_calls, [])
  225. self.assertEqual(self.tpl.mock_calls, [])
  226. self.assertEqual(self.app.mock_calls, [])
  227. def test_024_notify_updates_template_based_no_updates(self):
  228. '''No updates on template-based VM, should not reset state'''
  229. self.src.updateable = False
  230. self.src.template.is_running.return_value = False
  231. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  232. self.assertIsNone(response)
  233. self.assertEqual(self.tpl.mock_calls, [
  234. mock.call.updateable.__bool__(),
  235. mock.call.is_running(),
  236. ])
  237. self.assertEqual(self.app.mock_calls, [])
  238. def test_025_notify_updates_template_based(self):
  239. '''Some updates on template-based VM, should save flag'''
  240. self.src.updateable = False
  241. self.tpl.is_running.return_value = False
  242. self.src.storage.outdated_volumes = []
  243. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  244. self.assertIsNone(response)
  245. self.assertEqual(self.src.mock_calls, [])
  246. self.assertEqual(self.tpl.mock_calls, [
  247. mock.call.updateable.__bool__(),
  248. mock.call.is_running(),
  249. mock.call.features.__setitem__('updates-available', True),
  250. ])
  251. self.assertEqual(self.app.mock_calls, [
  252. mock.call.save()
  253. ])
  254. def test_026_notify_updates_template_based_outdated(self):
  255. self.src.updateable = False
  256. self.src.template.is_running.return_value = False
  257. self.src.storage.outdated_volumes = ['root']
  258. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  259. self.assertIsNone(response)
  260. self.assertEqual(self.src.mock_calls, [])
  261. self.assertEqual(self.tpl.mock_calls, [
  262. mock.call.updateable.__bool__(),
  263. mock.call.is_running(),
  264. ])
  265. self.assertIsInstance(self.tpl.updates_available, mock.Mock)
  266. self.assertEqual(self.app.mock_calls, [])
  267. def test_027_notify_updates_template_based_template_running(self):
  268. self.src.updateable = False
  269. self.src.template.is_running.return_value = True
  270. self.src.storage.outdated_volumes = []
  271. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  272. self.assertIsNone(response)
  273. self.assertEqual(self.src.mock_calls, [])
  274. self.assertEqual(self.tpl.mock_calls, [
  275. mock.call.updateable.__bool__(),
  276. mock.call.is_running(),
  277. ])
  278. self.assertIsInstance(self.src.updates_available, mock.Mock)
  279. self.assertEqual(self.app.mock_calls, [])
  280. def test_028_notify_updates_template_based_dispvm(self):
  281. self.dvm = self.src
  282. self.dvm.updateable = False
  283. self.srv = mock.NonCallableMagicMock(template=self.dvm)
  284. self.src.updateable = False
  285. self.src.template.is_running.return_value = False
  286. self.src.storage.outdated_volumes = []
  287. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  288. self.assertIsNone(response)
  289. self.assertEqual(self.src.mock_calls, [])
  290. self.assertEqual(self.dvm.mock_calls, [])
  291. self.assertEqual(self.tpl.mock_calls, [
  292. mock.call.updateable.__bool__(),
  293. mock.call.is_running(),
  294. mock.call.features.__setitem__('updates-available', True),
  295. ])
  296. self.assertIsInstance(self.src.updates_available, mock.Mock)
  297. self.assertEqual(self.app.mock_calls, [
  298. mock.call.save()
  299. ])