api_misc.py 12 KB

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