api_misc.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.src = mock.NonCallableMagicMock()
  28. self.app = mock.NonCallableMock()
  29. self.dest = mock.NonCallableMock()
  30. self.dest.name = 'dom0'
  31. self.app.configure_mock(domains={
  32. 'dom0': self.dest,
  33. 'test-vm': self.src,
  34. })
  35. def configure_qdb(self, entries):
  36. self.src.configure_mock(**{
  37. 'untrusted_qdb.read.side_effect': (
  38. lambda path: entries.get(path, None)),
  39. 'untrusted_qdb.list.side_effect': (
  40. lambda path: sorted(entries.keys())),
  41. })
  42. def call_mgmt_func(self, method, arg=b'', payload=b''):
  43. mgmt_obj = qubes.api.misc.QubesMiscAPI(self.app,
  44. b'test-vm', method, b'dom0', arg)
  45. loop = asyncio.get_event_loop()
  46. response = loop.run_until_complete(
  47. mgmt_obj.execute(untrusted_payload=payload))
  48. return response
  49. def test_000_features_request(self):
  50. qdb_entries = {
  51. '/features-request/feature1': b'1',
  52. '/features-request/feature2': b'',
  53. '/features-request/feature3': b'other',
  54. }
  55. self.configure_qdb(qdb_entries)
  56. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  57. self.assertIsNone(response)
  58. self.assertEqual(self.app.mock_calls, [
  59. mock.call.save()
  60. ])
  61. self.assertEqual(self.src.mock_calls, [
  62. mock.call.untrusted_qdb.list('/features-request/'),
  63. mock.call.untrusted_qdb.read('/features-request/feature1'),
  64. mock.call.untrusted_qdb.read('/features-request/feature2'),
  65. mock.call.untrusted_qdb.read('/features-request/feature3'),
  66. mock.call.fire_event_async('features-request', untrusted_features={
  67. 'feature1': '1', 'feature2': '', 'feature3': 'other'}),
  68. ('fire_event_async().__iter__', (), {}),
  69. ])
  70. def test_001_features_request_empty(self):
  71. self.configure_qdb({})
  72. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  73. self.assertIsNone(response)
  74. self.assertEqual(self.app.mock_calls, [
  75. mock.call.save()
  76. ])
  77. self.assertEqual(self.src.mock_calls, [
  78. mock.call.untrusted_qdb.list('/features-request/'),
  79. mock.call.fire_event_async('features-request',
  80. untrusted_features={}),
  81. ('fire_event_async().__iter__', (), {}),
  82. ])
  83. def test_002_features_request_invalid1(self):
  84. qdb_entries = {
  85. '/features-request/feature1': b'test spaces',
  86. }
  87. self.configure_qdb(qdb_entries)
  88. with self.assertRaises(AssertionError):
  89. self.call_mgmt_func(b'qubes.FeaturesRequest')
  90. self.assertEqual(self.app.mock_calls, [])
  91. self.assertEqual(self.src.mock_calls, [
  92. mock.call.untrusted_qdb.list('/features-request/'),
  93. mock.call.untrusted_qdb.read('/features-request/feature1'),
  94. ])
  95. def test_003_features_request_invalid2(self):
  96. qdb_entries = {
  97. '/features-request/feature1': b'\xfe\x01',
  98. }
  99. self.configure_qdb(qdb_entries)
  100. with self.assertRaises(UnicodeDecodeError):
  101. self.call_mgmt_func(b'qubes.FeaturesRequest')
  102. self.assertEqual(self.app.mock_calls, [])
  103. self.assertEqual(self.src.mock_calls, [
  104. mock.call.untrusted_qdb.list('/features-request/'),
  105. mock.call.untrusted_qdb.read('/features-request/feature1'),
  106. ])
  107. def test_010_notify_tools(self):
  108. qdb_entries = {
  109. '/qubes-tools/version': b'1',
  110. '/qubes-tools/qrexec': b'1',
  111. '/qubes-tools/gui': b'1',
  112. '/qubes-tools/os': b'Linux',
  113. '/qubes-tools/default-user': b'user',
  114. }
  115. self.configure_qdb(qdb_entries)
  116. response = self.call_mgmt_func(b'qubes.NotifyTools')
  117. self.assertIsNone(response)
  118. self.assertEqual(self.app.mock_calls, [
  119. mock.call.save()
  120. ])
  121. self.assertEqual(self.src.mock_calls, [
  122. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  123. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  124. mock.call.untrusted_qdb.read('/qubes-tools/gui-emulated'),
  125. mock.call.untrusted_qdb.read('/qubes-tools/default-user'),
  126. mock.call.untrusted_qdb.read('/qubes-tools/os'),
  127. mock.call.fire_event_async('features-request', untrusted_features={
  128. 'gui': '1',
  129. 'default-user': 'user',
  130. 'qrexec': '1',
  131. 'os': 'Linux'}),
  132. ('fire_event_async().__iter__', (), {}),
  133. ])
  134. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  135. def test_013_notify_tools_no_version(self):
  136. qdb_entries = {
  137. '/qubes-tools/qrexec': b'1',
  138. '/qubes-tools/gui': b'1',
  139. '/qubes-tools/os': b'Linux',
  140. '/qubes-tools/default-user': b'user',
  141. }
  142. self.configure_qdb(qdb_entries)
  143. response = self.call_mgmt_func(b'qubes.NotifyTools')
  144. self.assertIsNone(response)
  145. self.assertEqual(self.src.mock_calls, [
  146. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  147. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  148. mock.call.untrusted_qdb.read('/qubes-tools/gui-emulated'),
  149. mock.call.untrusted_qdb.read('/qubes-tools/default-user'),
  150. mock.call.untrusted_qdb.read('/qubes-tools/os'),
  151. mock.call.fire_event_async('features-request', untrusted_features={
  152. 'gui': '1',
  153. 'default-user': 'user',
  154. 'qrexec': '1',
  155. 'os': 'Linux'}),
  156. ('fire_event_async().__iter__', (), {}),
  157. ])
  158. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  159. def test_015_notify_tools_invalid_value_qrexec(self):
  160. qdb_entries = {
  161. '/qubes-tools/version': b'1',
  162. '/qubes-tools/qrexec': b'invalid value',
  163. '/qubes-tools/gui': b'0',
  164. '/qubes-tools/os': b'Linux',
  165. '/qubes-tools/default-user': b'user',
  166. }
  167. self.configure_qdb(qdb_entries)
  168. with self.assertRaises(AssertionError):
  169. self.call_mgmt_func(b'qubes.NotifyTools')
  170. self.assertEqual(self.app.mock_calls, [])
  171. self.assertEqual(self.src.mock_calls, [
  172. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  173. ])
  174. def test_016_notify_tools_invalid_value_gui(self):
  175. qdb_entries = {
  176. '/qubes-tools/version': b'1',
  177. '/qubes-tools/qrexec': b'1',
  178. '/qubes-tools/gui': b'invalid value',
  179. '/qubes-tools/os': b'Linux',
  180. '/qubes-tools/default-user': b'user',
  181. }
  182. self.configure_qdb(qdb_entries)
  183. with self.assertRaises(AssertionError):
  184. self.call_mgmt_func(b'qubes.NotifyTools')
  185. self.assertEqual(self.app.mock_calls, [])
  186. self.assertEqual(self.src.mock_calls, [
  187. mock.call.untrusted_qdb.read('/qubes-tools/qrexec'),
  188. mock.call.untrusted_qdb.read('/qubes-tools/gui'),
  189. ])
  190. def test_020_notify_updates_standalone(self):
  191. del self.src.template
  192. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  193. self.assertIsNone(response)
  194. self.assertEqual(self.src.mock_calls, [
  195. mock.call.updateable.__bool__(),
  196. mock.call.features.__setitem__('updates-available', True),
  197. ])
  198. self.assertEqual(self.app.mock_calls, [mock.call.save()])
  199. def test_021_notify_updates_standalone2(self):
  200. del self.src.template
  201. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  202. self.assertIsNone(response)
  203. self.assertEqual(self.src.mock_calls, [
  204. mock.call.updateable.__bool__(),
  205. mock.call.features.__setitem__('updates-available', False),
  206. ])
  207. self.assertEqual(self.app.mock_calls, [
  208. mock.call.save()
  209. ])
  210. def test_022_notify_updates_invalid(self):
  211. del self.src.template
  212. with self.assertRaises(AssertionError):
  213. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'')
  214. self.assertEqual(self.src.mock_calls, [])
  215. self.assertEqual(self.app.mock_calls, [])
  216. def test_023_notify_updates_invalid2(self):
  217. del self.src.template
  218. with self.assertRaises(AssertionError):
  219. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'no updates')
  220. self.assertEqual(self.src.mock_calls, [])
  221. self.assertEqual(self.app.mock_calls, [])
  222. def test_024_notify_updates_template_based_no_updates(self):
  223. '''No updates on template-based VM, should not reset state'''
  224. self.src.updateable = False
  225. self.src.template.is_running.return_value = False
  226. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  227. self.assertIsNone(response)
  228. self.assertEqual(self.src.mock_calls, [
  229. mock.call.template.is_running(),
  230. ])
  231. self.assertEqual(self.app.mock_calls, [])
  232. def test_025_notify_updates_template_based(self):
  233. '''Some updates on template-based VM, should save flag'''
  234. self.src.updateable = False
  235. self.src.template.is_running.return_value = False
  236. self.src.storage.outdated_volumes = []
  237. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  238. self.assertIsNone(response)
  239. self.assertEqual(self.src.mock_calls, [
  240. mock.call.template.is_running(),
  241. mock.call.template.features.__setitem__('updates-available', True),
  242. ])
  243. self.assertEqual(self.app.mock_calls, [
  244. mock.call.save()
  245. ])
  246. def test_026_notify_updates_template_based_outdated(self):
  247. self.src.updateable = False
  248. self.src.template.is_running.return_value = False
  249. self.src.storage.outdated_volumes = ['root']
  250. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  251. self.assertIsNone(response)
  252. self.assertEqual(self.src.mock_calls, [
  253. mock.call.template.is_running(),
  254. ])
  255. self.assertIsInstance(self.src.template.updates_available, mock.Mock)
  256. self.assertEqual(self.app.mock_calls, [])
  257. def test_027_notify_updates_template_based_template_running(self):
  258. self.src.updateable = False
  259. self.src.template.is_running.return_value = True
  260. self.src.storage.outdated_volumes = []
  261. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  262. self.assertIsNone(response)
  263. self.assertEqual(self.src.mock_calls, [
  264. mock.call.template.is_running(),
  265. ])
  266. self.assertIsInstance(self.src.updates_available, mock.Mock)
  267. self.assertEqual(self.app.mock_calls, [])