api_misc.py 11 KB

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